| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3D12/D3D12Gfx.h" |
| 5 | |
| 6 | #include "Common/Logging/Log.h" |
| 7 | |
| 8 | #include "VideoBackends/D3D12/Common.h" |
| 9 | #include "VideoBackends/D3D12/D3D12BoundingBox.h" |
| 10 | #include "VideoBackends/D3D12/D3D12PerfQuery.h" |
| 11 | #include "VideoBackends/D3D12/D3D12SwapChain.h" |
| 12 | #include "VideoBackends/D3D12/DX12Context.h" |
| 13 | #include "VideoBackends/D3D12/DX12Pipeline.h" |
| 14 | #include "VideoBackends/D3D12/DX12Shader.h" |
| 15 | #include "VideoBackends/D3D12/DX12Texture.h" |
| 16 | #include "VideoBackends/D3D12/DX12VertexFormat.h" |
| 17 | #include "VideoBackends/D3D12/DescriptorHeapManager.h" |
| 18 | #include "VideoCommon/Present.h" |
| 19 | #include "VideoCommon/VideoConfig.h" |
| 20 | |
| 21 | namespace DX12 |
| 22 | { |
| 23 | static bool UsesDynamicVertexLoader(const AbstractPipeline* pipeline) |
| 24 | { |
| 25 | const AbstractPipelineUsage usage = static_cast<const DXPipeline*>(pipeline)->GetUsage(); |
| 26 | return (g_backend_info.bSupportsDynamicVertexLoader && usage == AbstractPipelineUsage::GXUber) || |
| 27 | (g_ActiveConfig.UseVSForLinePointExpand() && usage != AbstractPipelineUsage::Utility); |
| 28 | } |
| 29 | |
| 30 | Gfx::Gfx(std::unique_ptr<SwapChain> swap_chain, float backbuffer_scale) |
| 31 | : m_backbuffer_scale(backbuffer_scale), m_swap_chain(std::move(swap_chain)) |
| 32 | { |
| 33 | m_state.root_signature = g_dx_context->GetGXRootSignature(); |
| 34 | |
| 35 | for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) |
| 36 | { |
| 37 | m_state.textures[i].ptr = g_dx_context->GetNullSRVDescriptor().cpu_handle.ptr; |
| 38 | m_state.samplers.states[i] = RenderState::GetPointSamplerState(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Gfx::~Gfx() = default; |
| 43 | |
| 44 | bool Gfx::IsHeadless() const |
| 45 | { |
| 46 | return !m_swap_chain; |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<AbstractTexture> Gfx::CreateTexture(const TextureConfig& config, |
| 50 | std::string_view name) |
| 51 | { |
| 52 | return DXTexture::Create(config, name); |
| 53 | } |
| 54 | |
| 55 | std::unique_ptr<AbstractStagingTexture> Gfx::CreateStagingTexture(StagingTextureType type, |
| 56 | const TextureConfig& config) |
| 57 | { |
| 58 | return DXStagingTexture::Create(type, config); |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<AbstractFramebuffer> |
| 62 | Gfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment, |
| 63 | std::vector<AbstractTexture*> additional_color_attachments) |
| 64 | { |
| 65 | return DXFramebuffer::Create(static_cast<DXTexture*>(color_attachment), |
| 66 | static_cast<DXTexture*>(depth_attachment), |
| 67 | std::move(additional_color_attachments)); |
| 68 | } |
| 69 | |
| 70 | std::unique_ptr<AbstractShader> |
| 71 | Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source, |
| 72 | VideoCommon::ShaderIncluder* shader_includer, std::string_view name) |
| 73 | { |
| 74 | return DXShader::CreateFromSource(stage, source, shader_includer, name); |
| 75 | } |
| 76 | |
| 77 | std::unique_ptr<AbstractShader> Gfx::CreateShaderFromBinary(ShaderStage stage, const void* data, |
| 78 | size_t length, std::string_view name) |
| 79 | { |
| 80 | return DXShader::CreateFromBytecode(stage, DXShader::CreateByteCode(data, length), name); |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<NativeVertexFormat> |
| 84 | Gfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) |
| 85 | { |
| 86 | return std::make_unique<DXVertexFormat>(vtx_decl); |
| 87 | } |
| 88 | |
| 89 | std::unique_ptr<AbstractPipeline> Gfx::CreatePipeline(const AbstractPipelineConfig& config, |
| 90 | const void* cache_data, |
| 91 | size_t cache_data_length) |
| 92 | { |
| 93 | return DXPipeline::Create(config, cache_data, cache_data_length); |
| 94 | } |
| 95 | |
| 96 | void Gfx::Flush() |
| 97 | { |
| 98 | ExecuteCommandList(false); |
| 99 | } |
| 100 | |
| 101 | void Gfx::WaitForGPUIdle() |
| 102 | { |
| 103 | ExecuteCommandList(true); |
| 104 | } |
| 105 | |
| 106 | void Gfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool color_enable, |
| 107 | bool alpha_enable, bool z_enable, u32 color, u32 z) |
| 108 | { |
| 109 | // Use a fast path without the shader if both color/alpha are enabled. |
| 110 | const bool fast_color_clear = color_enable && alpha_enable; |
| 111 | if (fast_color_clear || z_enable) |
| 112 | { |
| 113 | const D3D12_RECT d3d_clear_rc{target_rc.left, target_rc.top, target_rc.right, target_rc.bottom}; |
| 114 | auto* d3d_frame_buffer = static_cast<const DXFramebuffer*>(m_current_framebuffer); |
| 115 | |
| 116 | if (fast_color_clear) |
| 117 | { |
| 118 | d3d_frame_buffer->TransitionRenderTargets(); |
| 119 | |
| 120 | const std::array<float, 4> clear_color = { |
| 121 | {static_cast<float>((color >> 16) & 0xFF) / 255.0f, |
| 122 | static_cast<float>((color >> 8) & 0xFF) / 255.0f, |
| 123 | static_cast<float>((color >> 0) & 0xFF) / 255.0f, |
| 124 | static_cast<float>((color >> 24) & 0xFF) / 255.0f}}; |
| 125 | d3d_frame_buffer->ClearRenderTargets(clear_color, &d3d_clear_rc); |
| 126 | color_enable = false; |
| 127 | alpha_enable = false; |
| 128 | } |
| 129 | |
| 130 | if (z_enable) |
| 131 | { |
| 132 | static_cast<DXTexture*>(m_current_framebuffer->GetDepthAttachment()) |
| 133 | ->TransitionToState(D3D12_RESOURCE_STATE_DEPTH_WRITE); |
| 134 | |
| 135 | // D3D does not support reversed depth ranges. |
| 136 | const float clear_depth = 1.0f - static_cast<float>(z & 0xFFFFFF) / 16777216.0f; |
| 137 | d3d_frame_buffer->ClearDepth(clear_depth, &d3d_clear_rc); |
| 138 | z_enable = false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Anything left over, fall back to clear triangle. |
| 143 | if (color_enable || alpha_enable || z_enable) |
| 144 | ::AbstractGfx::ClearRegion(target_rc, color_enable, alpha_enable, z_enable, color, z); |
| 145 | } |
| 146 | |
| 147 | void Gfx::SetPipeline(const AbstractPipeline* pipeline) |
| 148 | { |
| 149 | const DXPipeline* dx_pipeline = static_cast<const DXPipeline*>(pipeline); |
| 150 | if (m_current_pipeline == dx_pipeline) |
| 151 | return; |
| 152 | |
| 153 | m_current_pipeline = dx_pipeline; |
| 154 | m_dirty_bits |= DirtyState_Pipeline; |
| 155 | |
| 156 | if (dx_pipeline) |
| 157 | { |
| 158 | if (dx_pipeline->GetRootSignature() != m_state.root_signature) |
| 159 | { |
| 160 | m_state.root_signature = dx_pipeline->GetRootSignature(); |
| 161 | m_dirty_bits |= DirtyState_RootSignature | DirtyState_PS_CBV | DirtyState_VS_CBV | |
| 162 | DirtyState_GS_CBV | DirtyState_SRV_Descriptor | |
| 163 | DirtyState_Sampler_Descriptor | DirtyState_UAV_Descriptor | |
| 164 | DirtyState_VS_SRV_Descriptor | DirtyState_CUS_CBV; |
| 165 | } |
| 166 | if (dx_pipeline->UseIntegerRTV() != m_state.using_integer_rtv) |
| 167 | { |
| 168 | m_state.using_integer_rtv = dx_pipeline->UseIntegerRTV(); |
| 169 | m_dirty_bits |= DirtyState_Framebuffer; |
| 170 | } |
| 171 | if (dx_pipeline->GetPrimitiveTopology() != m_state.primitive_topology) |
| 172 | { |
| 173 | m_state.primitive_topology = dx_pipeline->GetPrimitiveTopology(); |
| 174 | m_dirty_bits |= DirtyState_PrimitiveTopology; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void Gfx::BindFramebuffer(DXFramebuffer* fb) |
| 180 | { |
| 181 | fb->TransitionRenderTargets(); |
| 182 | if (fb->HasDepthBuffer()) |
| 183 | { |
| 184 | static_cast<DXTexture*>(fb->GetDepthAttachment()) |
| 185 | ->TransitionToState(D3D12_RESOURCE_STATE_DEPTH_WRITE); |
| 186 | } |
| 187 | |
| 188 | g_dx_context->GetCommandList()->OMSetRenderTargets( |
| 189 | fb->GetRTVDescriptorCount(), |
| 190 | m_state.using_integer_rtv ? fb->GetIntRTVDescriptorArray() : fb->GetRTVDescriptorArray(), |
| 191 | FALSE, fb->GetDSVDescriptorArray()); |
| 192 | m_current_framebuffer = fb; |
| 193 | m_dirty_bits &= ~DirtyState_Framebuffer; |
| 194 | } |
| 195 | |
| 196 | void Gfx::SetFramebuffer(AbstractFramebuffer* framebuffer) |
| 197 | { |
| 198 | if (m_current_framebuffer == framebuffer) |
| 199 | return; |
| 200 | |
| 201 | m_current_framebuffer = framebuffer; |
| 202 | m_dirty_bits |= DirtyState_Framebuffer; |
| 203 | } |
| 204 | |
| 205 | void Gfx::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) |
| 206 | { |
| 207 | SetFramebuffer(framebuffer); |
| 208 | |
| 209 | DXFramebuffer* d3d_frame_buffer = static_cast<DXFramebuffer*>(framebuffer); |
| 210 | d3d_frame_buffer->TransitionRenderTargets(); |
| 211 | if (d3d_frame_buffer->HasDepthBuffer()) |
| 212 | { |
| 213 | static_cast<DXTexture*>(d3d_frame_buffer->GetDepthAttachment()) |
| 214 | ->TransitionToState(D3D12_RESOURCE_STATE_DEPTH_WRITE); |
| 215 | } |
| 216 | d3d_frame_buffer->Unbind(); |
| 217 | } |
| 218 | |
| 219 | void Gfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value, |
| 220 | float depth_value) |
| 221 | { |
| 222 | DXFramebuffer* dxfb = static_cast<DXFramebuffer*>(framebuffer); |
| 223 | BindFramebuffer(dxfb); |
| 224 | |
| 225 | dxfb->ClearRenderTargets(color_value, nullptr); |
| 226 | dxfb->ClearDepth(depth_value, nullptr); |
| 227 | } |
| 228 | |
| 229 | void Gfx::SetScissorRect(const MathUtil::Rectangle<int>& rc) |
| 230 | { |
| 231 | if (m_state.scissor.left == rc.left && m_state.scissor.right == rc.right && |
| 232 | m_state.scissor.top == rc.top && m_state.scissor.bottom == rc.bottom) |
| 233 | { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | m_state.scissor.left = rc.left; |
| 238 | m_state.scissor.right = rc.right; |
| 239 | m_state.scissor.top = rc.top; |
| 240 | m_state.scissor.bottom = rc.bottom; |
| 241 | m_dirty_bits |= DirtyState_ScissorRect; |
| 242 | } |
| 243 | |
| 244 | void Gfx::SetTexture(u32 index, const AbstractTexture* texture) |
| 245 | { |
| 246 | const DXTexture* dxtex = static_cast<const DXTexture*>(texture); |
| 247 | if (m_state.textures[index].ptr == dxtex->GetSRVDescriptor().cpu_handle.ptr) |
| 248 | return; |
| 249 | |
| 250 | m_state.textures[index].ptr = dxtex->GetSRVDescriptor().cpu_handle.ptr; |
| 251 | if (dxtex) |
| 252 | dxtex->TransitionToState(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); |
| 253 | |
| 254 | m_dirty_bits |= DirtyState_Textures; |
| 255 | } |
| 256 | |
| 257 | void Gfx::SetSamplerState(u32 index, const SamplerState& state) |
| 258 | { |
| 259 | if (m_state.samplers.states[index] == state) |
| 260 | return; |
| 261 | |
| 262 | m_state.samplers.states[index] = state; |
| 263 | m_dirty_bits |= DirtyState_Samplers; |
| 264 | } |
| 265 | |
| 266 | void Gfx::SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write) |
| 267 | { |
| 268 | const DXTexture* dxtex = static_cast<const DXTexture*>(texture); |
| 269 | if (m_state.compute_image_texture == dxtex) |
| 270 | return; |
| 271 | |
| 272 | m_state.compute_image_texture = dxtex; |
| 273 | if (dxtex) |
| 274 | dxtex->TransitionToState(D3D12_RESOURCE_STATE_UNORDERED_ACCESS); |
| 275 | |
| 276 | m_dirty_bits |= DirtyState_ComputeImageTexture; |
| 277 | } |
| 278 | |
| 279 | void Gfx::UnbindTexture(const AbstractTexture* texture) |
| 280 | { |
| 281 | const auto srv_shadow_descriptor = |
| 282 | static_cast<const DXTexture*>(texture)->GetSRVDescriptor().cpu_handle; |
| 283 | for (u32 i = 0; i < VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; i++) |
| 284 | { |
| 285 | if (m_state.textures[i].ptr == srv_shadow_descriptor.ptr) |
| 286 | { |
| 287 | m_state.textures[i].ptr = g_dx_context->GetNullSRVDescriptor().cpu_handle.ptr; |
| 288 | m_dirty_bits |= DirtyState_Textures; |
| 289 | } |
| 290 | } |
| 291 | if (m_state.compute_image_texture == texture) |
| 292 | { |
| 293 | m_state.compute_image_texture = nullptr; |
| 294 | m_dirty_bits |= DirtyState_ComputeImageTexture; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void Gfx::SetViewport(float x, float y, float width, float height, float near_depth, |
| 299 | float far_depth) |
| 300 | { |
| 301 | if (m_state.viewport.TopLeftX == x && m_state.viewport.TopLeftY == y && |
| 302 | m_state.viewport.Width == width && m_state.viewport.Height == height && |
| 303 | near_depth == m_state.viewport.MinDepth && far_depth == m_state.viewport.MaxDepth) |
| 304 | { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | m_state.viewport.TopLeftX = x; |
| 309 | m_state.viewport.TopLeftY = y; |
| 310 | m_state.viewport.Width = width; |
| 311 | m_state.viewport.Height = height; |
| 312 | m_state.viewport.MinDepth = near_depth; |
| 313 | m_state.viewport.MaxDepth = far_depth; |
| 314 | m_dirty_bits |= DirtyState_Viewport; |
| 315 | } |
| 316 | |
| 317 | void Gfx::Draw(u32 base_vertex, u32 num_vertices) |
| 318 | { |
| 319 | if (!ApplyState()) |
| 320 | return; |
| 321 | |
| 322 | g_dx_context->GetCommandList()->DrawInstanced(num_vertices, 1, base_vertex, 0); |
| 323 | } |
| 324 | |
| 325 | void Gfx::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) |
| 326 | { |
| 327 | if (!ApplyState()) |
| 328 | return; |
| 329 | |
| 330 | // DX12 is great and doesn't include the base vertex in SV_VertexID |
| 331 | if (UsesDynamicVertexLoader(m_current_pipeline)) |
| 332 | g_dx_context->GetCommandList()->SetGraphicsRoot32BitConstant( |
| 333 | ROOT_PARAMETER_BASE_VERTEX_CONSTANT, base_vertex, 0); |
| 334 | g_dx_context->GetCommandList()->DrawIndexedInstanced(num_indices, 1, base_index, base_vertex, 0); |
| 335 | } |
| 336 | |
| 337 | void Gfx::DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, |
| 338 | u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) |
| 339 | { |
| 340 | SetRootSignatures(); |
| 341 | SetDescriptorHeaps(); |
| 342 | UpdateDescriptorTables(); |
| 343 | |
| 344 | if (m_dirty_bits & DirtyState_ComputeImageTexture && !UpdateComputeUAVDescriptorTable()) |
| 345 | { |
| 346 | ExecuteCommandList(false); |
| 347 | SetRootSignatures(); |
| 348 | SetDescriptorHeaps(); |
| 349 | UpdateDescriptorTables(); |
| 350 | UpdateComputeUAVDescriptorTable(); |
| 351 | } |
| 352 | |
| 353 | // Share graphics and compute state. No need to track now since dispatches are infrequent. |
| 354 | auto* const cmdlist = g_dx_context->GetCommandList(); |
| 355 | cmdlist->SetPipelineState(static_cast<const DXShader*>(shader)->GetComputePipeline()); |
| 356 | cmdlist->SetComputeRootConstantBufferView(CS_ROOT_PARAMETER_CBV, m_state.constant_buffers[0]); |
| 357 | cmdlist->SetComputeRootDescriptorTable(CS_ROOT_PARAMETER_SRV, m_state.srv_descriptor_base); |
| 358 | cmdlist->SetComputeRootDescriptorTable(CS_ROOT_PARAMETER_SAMPLERS, |
| 359 | m_state.sampler_descriptor_base); |
| 360 | cmdlist->SetComputeRootDescriptorTable(CS_ROOT_PARAMETER_UAV, |
| 361 | m_state.compute_uav_descriptor_base); |
| 362 | cmdlist->Dispatch(groups_x, groups_y, groups_z); |
| 363 | |
| 364 | // Compute and graphics state share the same pipeline object? :( |
| 365 | m_dirty_bits |= DirtyState_Pipeline; |
| 366 | } |
| 367 | |
| 368 | bool Gfx::BindBackbuffer(const ClearColor& clear_color) |
| 369 | { |
| 370 | CheckForSwapChainChanges(); |
| 371 | SetAndClearFramebuffer(m_swap_chain->GetCurrentFramebuffer(), clear_color); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | void Gfx::CheckForSwapChainChanges() |
| 376 | { |
| 377 | const bool surface_changed = g_presenter->SurfaceChangedTestAndClear(); |
| 378 | const bool surface_resized = |
| 379 | g_presenter->SurfaceResizedTestAndClear() || m_swap_chain->CheckForFullscreenChange(); |
| 380 | if (!surface_changed && !surface_resized) |
| 381 | return; |
| 382 | |
| 383 | // The swap chain could be in use from a previous frame. |
| 384 | WaitForGPUIdle(); |
| 385 | if (surface_changed) |
| 386 | { |
| 387 | m_swap_chain->ChangeSurface(g_presenter->GetNewSurfaceHandle()); |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | m_swap_chain->ResizeSwapChain(); |
| 392 | } |
| 393 | |
| 394 | g_presenter->SetBackbuffer(m_swap_chain->GetWidth(), m_swap_chain->GetHeight()); |
| 395 | } |
| 396 | |
| 397 | void Gfx::PresentBackbuffer() |
| 398 | { |
| 399 | m_current_framebuffer = nullptr; |
| 400 | |
| 401 | m_swap_chain->GetCurrentTexture()->TransitionToState(D3D12_RESOURCE_STATE_PRESENT); |
| 402 | ExecuteCommandList(false); |
| 403 | |
| 404 | m_swap_chain->Present(); |
| 405 | } |
| 406 | |
| 407 | SurfaceInfo Gfx::GetSurfaceInfo() const |
| 408 | { |
| 409 | return {m_swap_chain ? static_cast<u32>(m_swap_chain->GetWidth()) : 0, |
| 410 | m_swap_chain ? static_cast<u32>(m_swap_chain->GetHeight()) : 0, m_backbuffer_scale, |
| 411 | m_swap_chain ? m_swap_chain->GetFormat() : AbstractTextureFormat::Undefined}; |
| 412 | } |
| 413 | |
| 414 | void Gfx::OnConfigChanged(u32 bits) |
| 415 | { |
| 416 | AbstractGfx::OnConfigChanged(bits); |
| 417 | |
| 418 | // For quad-buffered stereo we need to change the layer count, so recreate the swap chain. |
| 419 | if (m_swap_chain && bits & CONFIG_CHANGE_BIT_STEREO_MODE) |
| 420 | { |
| 421 | ExecuteCommandList(true); |
| 422 | m_swap_chain->SetStereo(SwapChain::WantsStereo()); |
| 423 | } |
| 424 | |
| 425 | if (m_swap_chain && bits & CONFIG_CHANGE_BIT_HDR) |
| 426 | { |
| 427 | ExecuteCommandList(true); |
| 428 | m_swap_chain->SetHDR(SwapChain::WantsHDR()); |
| 429 | } |
| 430 | |
| 431 | // Wipe sampler cache if force texture filtering or anisotropy changes. |
| 432 | if (bits & (CONFIG_CHANGE_BIT_ANISOTROPY | CONFIG_CHANGE_BIT_FORCE_TEXTURE_FILTERING)) |
| 433 | { |
| 434 | ExecuteCommandList(true); |
| 435 | g_dx_context->GetSamplerHeapManager().Clear(); |
| 436 | g_dx_context->ResetSamplerAllocators(); |
| 437 | } |
| 438 | |
| 439 | // If the host config changed (e.g. bbox/per-pixel-shading), recreate the root signature. |
| 440 | if (bits & CONFIG_CHANGE_BIT_HOST_CONFIG) |
| 441 | g_dx_context->RecreateGXRootSignature(); |
| 442 | } |
| 443 | |
| 444 | void Gfx::ExecuteCommandList(bool wait_for_completion) |
| 445 | { |
| 446 | PerfQuery::GetInstance()->ResolveQueries(); |
| 447 | g_dx_context->ExecuteCommandList(wait_for_completion); |
| 448 | m_dirty_bits = DirtyState_All; |
| 449 | } |
| 450 | |
| 451 | void Gfx::SetConstantBuffer(u32 index, D3D12_GPU_VIRTUAL_ADDRESS address) |
| 452 | { |
| 453 | if (m_state.constant_buffers[index] == address) |
| 454 | return; |
| 455 | |
| 456 | m_state.constant_buffers[index] = address; |
| 457 | m_dirty_bits |= DirtyState_PS_CBV << index; |
| 458 | } |
| 459 | |
| 460 | void Gfx::SetTextureDescriptor(u32 index, D3D12_CPU_DESCRIPTOR_HANDLE handle) |
| 461 | { |
| 462 | if (m_state.textures[index].ptr == handle.ptr) |
| 463 | return; |
| 464 | |
| 465 | m_state.textures[index].ptr = handle.ptr; |
| 466 | m_dirty_bits |= DirtyState_Textures; |
| 467 | } |
| 468 | |
| 469 | void Gfx::SetPixelShaderUAV(D3D12_CPU_DESCRIPTOR_HANDLE handle) |
| 470 | { |
| 471 | if (m_state.ps_uav.ptr == handle.ptr) |
| 472 | return; |
| 473 | |
| 474 | m_state.ps_uav = handle; |
| 475 | m_dirty_bits |= DirtyState_PS_UAV; |
| 476 | } |
| 477 | |
| 478 | void Gfx::SetVertexBuffer(D3D12_GPU_VIRTUAL_ADDRESS address, D3D12_CPU_DESCRIPTOR_HANDLE srv, |
| 479 | u32 stride, u32 size) |
| 480 | { |
| 481 | if (m_state.vertex_buffer.BufferLocation != address || |
| 482 | m_state.vertex_buffer.StrideInBytes != stride || m_state.vertex_buffer.SizeInBytes != size) |
| 483 | { |
| 484 | m_state.vertex_buffer.BufferLocation = address; |
| 485 | m_state.vertex_buffer.StrideInBytes = stride; |
| 486 | m_state.vertex_buffer.SizeInBytes = size; |
| 487 | m_dirty_bits |= DirtyState_VertexBuffer; |
| 488 | } |
| 489 | if (m_state.vs_srv.ptr != srv.ptr) |
| 490 | { |
| 491 | m_state.vs_srv = srv; |
| 492 | m_dirty_bits |= DirtyState_VS_SRV; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void Gfx::SetIndexBuffer(D3D12_GPU_VIRTUAL_ADDRESS address, u32 size, DXGI_FORMAT format) |
| 497 | { |
| 498 | if (m_state.index_buffer.BufferLocation == address && m_state.index_buffer.SizeInBytes == size && |
| 499 | m_state.index_buffer.Format == format) |
| 500 | { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | m_state.index_buffer.BufferLocation = address; |
| 505 | m_state.index_buffer.SizeInBytes = size; |
| 506 | m_state.index_buffer.Format = format; |
| 507 | m_dirty_bits |= DirtyState_IndexBuffer; |
| 508 | } |
| 509 | |
| 510 | bool Gfx::ApplyState() |
| 511 | { |
| 512 | if (!m_current_framebuffer || !m_current_pipeline) |
| 513 | return false; |
| 514 | |
| 515 | // Updating the descriptor tables can cause command list execution if no descriptors remain. |
| 516 | SetRootSignatures(); |
| 517 | SetDescriptorHeaps(); |
| 518 | UpdateDescriptorTables(); |
| 519 | |
| 520 | // Clear bits before actually changing state. Some state (e.g. cbuffers) can't be set |
| 521 | // if utility pipelines are bound. |
| 522 | const u32 dirty_bits = m_dirty_bits; |
| 523 | m_dirty_bits &= |
| 524 | ~(DirtyState_Framebuffer | DirtyState_Pipeline | DirtyState_Viewport | |
| 525 | DirtyState_ScissorRect | DirtyState_PS_UAV | DirtyState_PS_CBV | DirtyState_VS_CBV | |
| 526 | DirtyState_GS_CBV | DirtyState_SRV_Descriptor | DirtyState_Sampler_Descriptor | |
| 527 | DirtyState_UAV_Descriptor | DirtyState_VertexBuffer | DirtyState_IndexBuffer | |
| 528 | DirtyState_PrimitiveTopology | DirtyState_VS_SRV_Descriptor | DirtyState_CUS_CBV); |
| 529 | |
| 530 | auto* const cmdlist = g_dx_context->GetCommandList(); |
| 531 | auto* const pipeline = static_cast<const DXPipeline*>(m_current_pipeline); |
| 532 | if (dirty_bits & DirtyState_Pipeline) |
| 533 | cmdlist->SetPipelineState(pipeline->GetPipeline()); |
| 534 | |
| 535 | if (dirty_bits & DirtyState_Framebuffer) |
| 536 | BindFramebuffer(static_cast<DXFramebuffer*>(m_current_framebuffer)); |
| 537 | |
| 538 | if (dirty_bits & DirtyState_Viewport) |
| 539 | cmdlist->RSSetViewports(1, &m_state.viewport); |
| 540 | |
| 541 | if (dirty_bits & DirtyState_ScissorRect) |
| 542 | cmdlist->RSSetScissorRects(1, &m_state.scissor); |
| 543 | |
| 544 | if (dirty_bits & DirtyState_VertexBuffer) |
| 545 | cmdlist->IASetVertexBuffers(0, 1, &m_state.vertex_buffer); |
| 546 | |
| 547 | if (dirty_bits & DirtyState_IndexBuffer) |
| 548 | cmdlist->IASetIndexBuffer(&m_state.index_buffer); |
| 549 | |
| 550 | if (dirty_bits & DirtyState_PrimitiveTopology) |
| 551 | cmdlist->IASetPrimitiveTopology(m_state.primitive_topology); |
| 552 | |
| 553 | if (dirty_bits & DirtyState_SRV_Descriptor) |
| 554 | cmdlist->SetGraphicsRootDescriptorTable(ROOT_PARAMETER_PS_SRV, m_state.srv_descriptor_base); |
| 555 | |
| 556 | if (dirty_bits & DirtyState_Sampler_Descriptor) |
| 557 | { |
| 558 | cmdlist->SetGraphicsRootDescriptorTable(ROOT_PARAMETER_PS_SAMPLERS, |
| 559 | m_state.sampler_descriptor_base); |
| 560 | } |
| 561 | |
| 562 | if (pipeline->GetUsage() != AbstractPipelineUsage::Utility) |
| 563 | { |
| 564 | if (dirty_bits & DirtyState_VS_CBV) |
| 565 | { |
| 566 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_VS_CBV, |
| 567 | m_state.constant_buffers[1]); |
| 568 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_VS_CBV2, |
| 569 | m_state.constant_buffers[1]); |
| 570 | |
| 571 | if (g_ActiveConfig.bEnablePixelLighting) |
| 572 | { |
| 573 | cmdlist->SetGraphicsRootConstantBufferView( |
| 574 | g_ActiveConfig.bBBoxEnable ? ROOT_PARAMETER_PS_CBV2 : ROOT_PARAMETER_PS_UAV_OR_CBV2, |
| 575 | m_state.constant_buffers[1]); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (dirty_bits & DirtyState_CUS_CBV) |
| 580 | { |
| 581 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_VS_CUS_CBV, |
| 582 | m_state.constant_buffers[2]); |
| 583 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_PS_CUS_CBV, |
| 584 | m_state.constant_buffers[2]); |
| 585 | } |
| 586 | |
| 587 | if (dirty_bits & DirtyState_VS_SRV_Descriptor && UsesDynamicVertexLoader(pipeline)) |
| 588 | { |
| 589 | cmdlist->SetGraphicsRootDescriptorTable(ROOT_PARAMETER_VS_SRV, |
| 590 | m_state.vertex_srv_descriptor_base); |
| 591 | } |
| 592 | |
| 593 | if (dirty_bits & DirtyState_GS_CBV) |
| 594 | { |
| 595 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_GS_CBV, |
| 596 | m_state.constant_buffers[3]); |
| 597 | } |
| 598 | |
| 599 | if (dirty_bits & DirtyState_UAV_Descriptor && g_ActiveConfig.bBBoxEnable) |
| 600 | { |
| 601 | cmdlist->SetGraphicsRootDescriptorTable(ROOT_PARAMETER_PS_UAV_OR_CBV2, |
| 602 | m_state.uav_descriptor_base); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (dirty_bits & DirtyState_PS_CBV) |
| 607 | { |
| 608 | cmdlist->SetGraphicsRootConstantBufferView(ROOT_PARAMETER_PS_CBV, m_state.constant_buffers[0]); |
| 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | void Gfx::SetRootSignatures() |
| 615 | { |
| 616 | const u32 dirty_bits = m_dirty_bits; |
| 617 | if (dirty_bits & DirtyState_RootSignature) |
| 618 | g_dx_context->GetCommandList()->SetGraphicsRootSignature(m_state.root_signature); |
| 619 | if (dirty_bits & DirtyState_ComputeRootSignature) |
| 620 | { |
| 621 | g_dx_context->GetCommandList()->SetComputeRootSignature( |
| 622 | g_dx_context->GetComputeRootSignature()); |
| 623 | } |
| 624 | m_dirty_bits &= ~(DirtyState_RootSignature | DirtyState_ComputeRootSignature); |
| 625 | } |
| 626 | |
| 627 | void Gfx::SetDescriptorHeaps() |
| 628 | { |
| 629 | if (m_dirty_bits & DirtyState_DescriptorHeaps) |
| 630 | { |
| 631 | g_dx_context->GetCommandList()->SetDescriptorHeaps(g_dx_context->GetGPUDescriptorHeapCount(), |
| 632 | g_dx_context->GetGPUDescriptorHeaps()); |
| 633 | m_dirty_bits &= ~DirtyState_DescriptorHeaps; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void Gfx::UpdateDescriptorTables() |
| 638 | { |
| 639 | // Samplers force a full sync because any of the samplers could be in use. |
| 640 | const bool texture_update_failed = |
| 641 | (m_dirty_bits & DirtyState_Textures) && !UpdateSRVDescriptorTable(); |
| 642 | const bool sampler_update_failed = |
| 643 | (m_dirty_bits & DirtyState_Samplers) && !UpdateSamplerDescriptorTable(); |
| 644 | const bool uav_update_failed = (m_dirty_bits & DirtyState_PS_UAV) && !UpdateUAVDescriptorTable(); |
| 645 | const bool srv_update_failed = |
| 646 | (m_dirty_bits & DirtyState_VS_SRV) && !UpdateVSSRVDescriptorTable(); |
| 647 | if (texture_update_failed || sampler_update_failed || uav_update_failed || srv_update_failed) |
| 648 | { |
| 649 | WARN_LOG_FMT(VIDEO, "Executing command list while waiting for temporary {}", |
| 650 | texture_update_failed ? "descriptors" : "samplers"); |
| 651 | ExecuteCommandList(false); |
| 652 | SetRootSignatures(); |
| 653 | SetDescriptorHeaps(); |
| 654 | UpdateSRVDescriptorTable(); |
| 655 | UpdateSamplerDescriptorTable(); |
| 656 | UpdateUAVDescriptorTable(); |
| 657 | UpdateVSSRVDescriptorTable(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | bool Gfx::UpdateSRVDescriptorTable() |
| 662 | { |
| 663 | static constexpr std::array<UINT, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> src_sizes = { |
| 664 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 665 | DescriptorHandle dst_base_handle; |
| 666 | const UINT dst_handle_sizes = VideoCommon::MAX_PIXEL_SHADER_SAMPLERS; |
| 667 | if (!g_dx_context->GetDescriptorAllocator()->Allocate(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, |
| 668 | &dst_base_handle)) |
| 669 | return false; |
| 670 | |
| 671 | g_dx_context->GetDevice()->CopyDescriptors( |
| 672 | 1, &dst_base_handle.cpu_handle, &dst_handle_sizes, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS, |
| 673 | m_state.textures.data(), src_sizes.data(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); |
| 674 | m_state.srv_descriptor_base = dst_base_handle.gpu_handle; |
| 675 | m_dirty_bits = (m_dirty_bits & ~DirtyState_Textures) | DirtyState_SRV_Descriptor; |
| 676 | return true; |
| 677 | } |
| 678 | |
| 679 | bool Gfx::UpdateSamplerDescriptorTable() |
| 680 | { |
| 681 | if (!g_dx_context->GetSamplerAllocator()->GetGroupHandle(m_state.samplers, |
| 682 | &m_state.sampler_descriptor_base)) |
| 683 | { |
| 684 | g_dx_context->ResetSamplerAllocators(); |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | m_dirty_bits = (m_dirty_bits & ~DirtyState_Samplers) | DirtyState_Sampler_Descriptor; |
| 689 | return true; |
| 690 | } |
| 691 | |
| 692 | bool Gfx::UpdateUAVDescriptorTable() |
| 693 | { |
| 694 | // We can skip writing the UAV descriptor if bbox isn't enabled, since it's not used otherwise. |
| 695 | if (!g_ActiveConfig.bBBoxEnable) |
| 696 | return true; |
| 697 | |
| 698 | DescriptorHandle handle; |
| 699 | if (!g_dx_context->GetDescriptorAllocator()->Allocate(1, &handle)) |
| 700 | return false; |
| 701 | |
| 702 | g_dx_context->GetDevice()->CopyDescriptorsSimple(1, handle.cpu_handle, m_state.ps_uav, |
| 703 | D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); |
| 704 | m_state.uav_descriptor_base = handle.gpu_handle; |
| 705 | m_dirty_bits = (m_dirty_bits & ~DirtyState_PS_UAV) | DirtyState_UAV_Descriptor; |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | bool Gfx::UpdateVSSRVDescriptorTable() |
| 710 | { |
| 711 | if (!UsesDynamicVertexLoader(m_current_pipeline)) |
| 712 | { |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | DescriptorHandle handle; |
| 717 | if (!g_dx_context->GetDescriptorAllocator()->Allocate(1, &handle)) |
| 718 | return false; |
| 719 | |
| 720 | g_dx_context->GetDevice()->CopyDescriptorsSimple(1, handle.cpu_handle, m_state.vs_srv, |
| 721 | D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); |
| 722 | m_state.vertex_srv_descriptor_base = handle.gpu_handle; |
| 723 | m_dirty_bits = (m_dirty_bits & ~DirtyState_VS_SRV) | DirtyState_VS_SRV_Descriptor; |
| 724 | return true; |
| 725 | } |
| 726 | |
| 727 | bool Gfx::UpdateComputeUAVDescriptorTable() |
| 728 | { |
| 729 | DescriptorHandle handle; |
| 730 | if (!g_dx_context->GetDescriptorAllocator()->Allocate(1, &handle)) |
| 731 | return false; |
| 732 | |
| 733 | if (m_state.compute_image_texture) |
| 734 | { |
| 735 | g_dx_context->GetDevice()->CopyDescriptorsSimple( |
| 736 | 1, handle.cpu_handle, m_state.compute_image_texture->GetUAVDescriptor().cpu_handle, |
| 737 | D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); |
| 738 | } |
| 739 | else |
| 740 | { |
| 741 | constexpr D3D12_UNORDERED_ACCESS_VIEW_DESC null_uav_desc = {}; |
| 742 | g_dx_context->GetDevice()->CreateUnorderedAccessView(nullptr, nullptr, &null_uav_desc, |
| 743 | handle.cpu_handle); |
| 744 | } |
| 745 | |
| 746 | m_dirty_bits &= ~DirtyState_ComputeImageTexture; |
| 747 | m_state.compute_uav_descriptor_base = handle.gpu_handle; |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | } // namespace DX12 |