From 5a2d119bda4fda240731dc45286aeff9fb68918f Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Sun, 29 Jan 2023 23:58:32 +1300 Subject: Implement AbstractGfx for Dx11 --- Source/Core/DolphinLib.props | 4 +- Source/Core/VideoBackends/D3D/CMakeLists.txt | 4 +- Source/Core/VideoBackends/D3D/D3DGfx.cpp | 295 +++++++++++++++++++++ Source/Core/VideoBackends/D3D/D3DGfx.h | 82 ++++++ Source/Core/VideoBackends/D3D/D3DMain.cpp | 26 +- .../VideoBackends/D3D/D3DNativeVertexFormat.cpp | 4 +- Source/Core/VideoBackends/D3D/D3DRender.cpp | 294 -------------------- Source/Core/VideoBackends/D3D/D3DRender.h | 82 ------ Source/Core/VideoBackends/D3D/D3DVertexManager.cpp | 2 +- Source/Core/VideoBackends/D3D/DXPipeline.cpp | 4 +- 10 files changed, 395 insertions(+), 402 deletions(-) create mode 100644 Source/Core/VideoBackends/D3D/D3DGfx.cpp create mode 100644 Source/Core/VideoBackends/D3D/D3DGfx.h delete mode 100644 Source/Core/VideoBackends/D3D/D3DRender.cpp delete mode 100644 Source/Core/VideoBackends/D3D/D3DRender.h (limited to 'Source') diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index b244c00e98..4df8e0cd84 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -534,7 +534,7 @@ - + @@ -1148,7 +1148,7 @@ - + diff --git a/Source/Core/VideoBackends/D3D/CMakeLists.txt b/Source/Core/VideoBackends/D3D/CMakeLists.txt index f296040526..ed75b29b77 100644 --- a/Source/Core/VideoBackends/D3D/CMakeLists.txt +++ b/Source/Core/VideoBackends/D3D/CMakeLists.txt @@ -7,8 +7,8 @@ add_library(videod3d D3DNativeVertexFormat.cpp D3DPerfQuery.cpp D3DPerfQuery.h - D3DRender.cpp - D3DRender.h + D3DGfx.cpp + D3DGfx.h D3DState.cpp D3DState.h D3DSwapChain.cpp diff --git a/Source/Core/VideoBackends/D3D/D3DGfx.cpp b/Source/Core/VideoBackends/D3D/D3DGfx.cpp new file mode 100644 index 0000000000..dc391f08b3 --- /dev/null +++ b/Source/Core/VideoBackends/D3D/D3DGfx.cpp @@ -0,0 +1,295 @@ +// Copyright 2010 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "VideoBackends/D3D/D3DGfx.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Common/Assert.h" +#include "Common/CommonTypes.h" +#include "Common/Logging/Log.h" +#include "Common/MathUtil.h" + +#include "Core/Core.h" + +#include "VideoBackends/D3D/D3DBase.h" +#include "VideoBackends/D3D/D3DBoundingBox.h" +#include "VideoBackends/D3D/D3DState.h" +#include "VideoBackends/D3D/D3DSwapChain.h" +#include "VideoBackends/D3D/DXPipeline.h" +#include "VideoBackends/D3D/DXShader.h" +#include "VideoBackends/D3D/DXTexture.h" + +#include "VideoCommon/BPFunctions.h" +#include "VideoCommon/FramebufferManager.h" +#include "VideoCommon/PostProcessing.h" +#include "VideoCommon/Present.h" +#include "VideoCommon/RenderState.h" +#include "VideoCommon/VideoConfig.h" +#include "VideoCommon/XFMemory.h" + +namespace DX11 +{ +Gfx::Gfx(std::unique_ptr swap_chain, float backbuffer_scale) + : m_backbuffer_scale(backbuffer_scale), m_swap_chain(std::move(swap_chain)) +{ +} + +Gfx::~Gfx() = default; + +bool Gfx::IsHeadless() const +{ + return !m_swap_chain; +} + +std::unique_ptr Gfx::CreateTexture(const TextureConfig& config, + std::string_view name) +{ + return DXTexture::Create(config, name); +} + +std::unique_ptr Gfx::CreateStagingTexture(StagingTextureType type, + const TextureConfig& config) +{ + return DXStagingTexture::Create(type, config); +} + +std::unique_ptr Gfx::CreateFramebuffer(AbstractTexture* color_attachment, + AbstractTexture* depth_attachment) +{ + return DXFramebuffer::Create(static_cast(color_attachment), + static_cast(depth_attachment)); +} + +std::unique_ptr +Gfx::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name) +{ + auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source); + if (!bytecode) + return nullptr; + + return DXShader::CreateFromBytecode(stage, std::move(*bytecode), name); +} + +std::unique_ptr Gfx::CreateShaderFromBinary(ShaderStage stage, + const void* data, size_t length, + std::string_view name) +{ + return DXShader::CreateFromBytecode(stage, DXShader::CreateByteCode(data, length), name); +} + +std::unique_ptr Gfx::CreatePipeline(const AbstractPipelineConfig& config, + const void* cache_data, + size_t cache_data_length) +{ + return DXPipeline::Create(config); +} + +void Gfx::SetPipeline(const AbstractPipeline* pipeline) +{ + const DXPipeline* dx_pipeline = static_cast(pipeline); + if (m_current_pipeline == dx_pipeline) + return; + + if (dx_pipeline) + { + D3D::stateman->SetRasterizerState(dx_pipeline->GetRasterizerState()); + D3D::stateman->SetDepthState(dx_pipeline->GetDepthState()); + D3D::stateman->SetBlendState(dx_pipeline->GetBlendState()); + D3D::stateman->SetPrimitiveTopology(dx_pipeline->GetPrimitiveTopology()); + D3D::stateman->SetInputLayout(dx_pipeline->GetInputLayout()); + D3D::stateman->SetVertexShader(dx_pipeline->GetVertexShader()); + D3D::stateman->SetGeometryShader(dx_pipeline->GetGeometryShader()); + D3D::stateman->SetPixelShader(dx_pipeline->GetPixelShader()); + D3D::stateman->SetIntegerRTV(dx_pipeline->UseLogicOp()); + } + else + { + // These will be destroyed at pipeline destruction. + D3D::stateman->SetInputLayout(nullptr); + D3D::stateman->SetVertexShader(nullptr); + D3D::stateman->SetGeometryShader(nullptr); + D3D::stateman->SetPixelShader(nullptr); + } +} + +void Gfx::SetScissorRect(const MathUtil::Rectangle& rc) +{ + // TODO: Move to stateman + const CD3D11_RECT rect(rc.left, rc.top, std::max(rc.right, rc.left + 1), + std::max(rc.bottom, rc.top + 1)); + D3D::context->RSSetScissorRects(1, &rect); +} + +void Gfx::SetViewport(float x, float y, float width, float height, float near_depth, + float far_depth) +{ + // TODO: Move to stateman + const CD3D11_VIEWPORT vp(x, y, width, height, near_depth, far_depth); + D3D::context->RSSetViewports(1, &vp); +} + +void Gfx::Draw(u32 base_vertex, u32 num_vertices) +{ + D3D::stateman->Apply(); + D3D::context->Draw(num_vertices, base_vertex); +} + +void Gfx::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) +{ + D3D::stateman->Apply(); + D3D::context->DrawIndexed(num_indices, base_index, base_vertex); +} + +void Gfx::DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, + u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) +{ + D3D::stateman->SetComputeShader(static_cast(shader)->GetD3DComputeShader()); + D3D::stateman->SyncComputeBindings(); + D3D::context->Dispatch(groups_x, groups_y, groups_z); +} + +void Gfx::BindBackbuffer(const ClearColor& clear_color) +{ + CheckForSwapChainChanges(); + SetAndClearFramebuffer(m_swap_chain->GetFramebuffer(), clear_color); +} + +void Gfx::PresentBackbuffer() +{ + m_swap_chain->Present(); +} + +void Gfx::OnConfigChanged(u32 bits) +{ + // Quad-buffer changes require swap chain recreation. + if (bits & CONFIG_CHANGE_BIT_STEREO_MODE && m_swap_chain) + m_swap_chain->SetStereo(SwapChain::WantsStereo()); +} + +void Gfx::CheckForSwapChainChanges() +{ + const bool surface_changed = g_presenter->SurfaceChangedTestAndClear(); + const bool surface_resized = + g_presenter->SurfaceResizedTestAndClear() || m_swap_chain->CheckForFullscreenChange(); + if (!surface_changed && !surface_resized) + return; + + if (surface_changed) + { + m_swap_chain->ChangeSurface(g_presenter->GetNewSurfaceHandle()); + } + else + { + m_swap_chain->ResizeSwapChain(); + } + + g_presenter->SetBackbuffer(m_swap_chain->GetWidth(), m_swap_chain->GetHeight()); +} + +void Gfx::SetFramebuffer(AbstractFramebuffer* framebuffer) +{ + if (m_current_framebuffer == framebuffer) + return; + + // We can't leave the framebuffer bound as a texture and a render target. + DXFramebuffer* fb = static_cast(framebuffer); + if ((fb->GetColorAttachment() && + D3D::stateman->UnsetTexture( + static_cast(fb->GetColorAttachment())->GetD3DSRV()) != 0) || + (fb->GetDepthAttachment() && + D3D::stateman->UnsetTexture( + static_cast(fb->GetDepthAttachment())->GetD3DSRV()) != 0)) + { + D3D::stateman->ApplyTextures(); + } + + D3D::stateman->SetFramebuffer(fb); + m_current_framebuffer = fb; +} + +void Gfx::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) +{ + SetFramebuffer(framebuffer); +} + +void Gfx::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, + const ClearColor& color_value, float depth_value) +{ + SetFramebuffer(framebuffer); + D3D::stateman->Apply(); + + if (framebuffer->GetColorFormat() != AbstractTextureFormat::Undefined) + { + D3D::context->ClearRenderTargetView( + static_cast(framebuffer)->GetRTVArray()[0], color_value.data()); + } + if (framebuffer->GetDepthFormat() != AbstractTextureFormat::Undefined) + { + D3D::context->ClearDepthStencilView(static_cast(framebuffer)->GetDSV(), + D3D11_CLEAR_DEPTH, depth_value, 0); + } +} + +void Gfx::SetTexture(u32 index, const AbstractTexture* texture) +{ + D3D::stateman->SetTexture(index, texture ? static_cast(texture)->GetD3DSRV() : + nullptr); +} + +void Gfx::SetSamplerState(u32 index, const SamplerState& state) +{ + D3D::stateman->SetSampler(index, m_state_cache.Get(state)); +} + +void Gfx::SetComputeImageTexture(AbstractTexture* texture, bool read, bool write) +{ + D3D::stateman->SetComputeUAV(texture ? static_cast(texture)->GetD3DUAV() : nullptr); +} + +void Gfx::UnbindTexture(const AbstractTexture* texture) +{ + if (D3D::stateman->UnsetTexture(static_cast(texture)->GetD3DSRV()) != 0) + D3D::stateman->ApplyTextures(); +} + +void Gfx::Flush() +{ + D3D::context->Flush(); +} + +void Gfx::WaitForGPUIdle() +{ + // There is no glFinish() equivalent in D3D. + D3D::context->Flush(); +} + +void Gfx::SetFullscreen(bool enable_fullscreen) +{ + if (m_swap_chain) + m_swap_chain->SetFullscreen(enable_fullscreen); +} + +bool Gfx::IsFullscreen() const +{ + return m_swap_chain && m_swap_chain->GetFullscreen(); +} + +SurfaceInfo Gfx::GetSurfaceInfo() const +{ + return { + m_swap_chain ? static_cast(m_swap_chain->GetWidth()) : 0, + m_swap_chain ? static_cast(m_swap_chain->GetHeight()) : 0, + m_backbuffer_scale, + m_swap_chain ? m_swap_chain->GetFormat() : AbstractTextureFormat::Undefined + }; +} + +} // namespace DX11 diff --git a/Source/Core/VideoBackends/D3D/D3DGfx.h b/Source/Core/VideoBackends/D3D/D3DGfx.h new file mode 100644 index 0000000000..8bbf998e36 --- /dev/null +++ b/Source/Core/VideoBackends/D3D/D3DGfx.h @@ -0,0 +1,82 @@ +// Copyright 2010 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include "VideoBackends/D3D/D3DState.h" +#include "VideoCommon/AbstractGfx.h" + +class BoundingBox; + +namespace DX11 +{ +class SwapChain; +class DXTexture; +class DXFramebuffer; + +class Gfx : public ::AbstractGfx +{ +public: + Gfx(std::unique_ptr swap_chain, float backbuffer_scale); + ~Gfx() override; + + StateCache& GetStateCache() { return m_state_cache; } + + bool IsHeadless() const override; + + std::unique_ptr CreateTexture(const TextureConfig& config, + std::string_view name) override; + std::unique_ptr + CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override; + std::unique_ptr CreateShaderFromSource(ShaderStage stage, std::string_view source, + std::string_view name) override; + std::unique_ptr CreateShaderFromBinary(ShaderStage stage, const void* data, + size_t length, + std::string_view name) override; + std::unique_ptr + CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; + std::unique_ptr CreatePipeline(const AbstractPipelineConfig& config, + const void* cache_data = nullptr, + size_t cache_data_length = 0) override; + std::unique_ptr + CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment) override; + + void SetPipeline(const AbstractPipeline* pipeline) override; + void SetFramebuffer(AbstractFramebuffer* framebuffer) override; + void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override; + void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {}, + float depth_value = 0.0f) override; + void SetScissorRect(const MathUtil::Rectangle& rc) override; + void SetTexture(u32 index, const AbstractTexture* texture) override; + void SetSamplerState(u32 index, const SamplerState& state) override; + void SetComputeImageTexture(AbstractTexture* texture, bool read, bool write) override; + void UnbindTexture(const AbstractTexture* texture) override; + void SetViewport(float x, float y, float width, float height, float near_depth, + float far_depth) override; + void Draw(u32 base_vertex, u32 num_vertices) override; + void DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) override; + void DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, + u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) override; + void BindBackbuffer(const ClearColor& clear_color = {}) override; + void PresentBackbuffer() override; + void SetFullscreen(bool enable_fullscreen) override; + bool IsFullscreen() const override; + + void Flush() override; + void WaitForGPUIdle() override; + + void OnConfigChanged(u32 bits) override; + + SurfaceInfo GetSurfaceInfo() const override; + +private: + void CheckForSwapChainChanges(); + + StateCache m_state_cache; + + float m_backbuffer_scale; + std::unique_ptr m_swap_chain; +}; +} // namespace DX11 diff --git a/Source/Core/VideoBackends/D3D/D3DMain.cpp b/Source/Core/VideoBackends/D3D/D3DMain.cpp index 1748a0b3fc..ae2e0cd959 100644 --- a/Source/Core/VideoBackends/D3D/D3DMain.cpp +++ b/Source/Core/VideoBackends/D3D/D3DMain.cpp @@ -14,11 +14,12 @@ #include "VideoBackends/D3D/D3DBase.h" #include "VideoBackends/D3D/D3DBoundingBox.h" #include "VideoBackends/D3D/D3DPerfQuery.h" -#include "VideoBackends/D3D/D3DRender.h" +#include "VideoBackends/D3D/D3DGfx.h" #include "VideoBackends/D3D/D3DSwapChain.h" #include "VideoBackends/D3D/D3DVertexManager.h" #include "VideoBackends/D3DCommon/D3DCommon.h" +#include "VideoCommon/AbstractGfx.h" #include "VideoCommon/FramebufferManager.h" #include "VideoCommon/ShaderCache.h" #include "VideoCommon/TextureCacheBase.h" @@ -143,7 +144,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) return false; FillBackendInfo(); - InitializeShared(); + UpdateActiveConfig(); std::unique_ptr swap_chain; if (wsi.render_surface && !(swap_chain = SwapChain::Create(wsi))) @@ -154,22 +155,13 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) return false; } - g_renderer = std::make_unique(std::move(swap_chain), wsi.render_surface_scale); - g_vertex_manager = std::make_unique(); - g_shader_cache = std::make_unique(); - g_framebuffer_manager = std::make_unique(); - g_texture_cache = std::make_unique(); - g_perf_query = std::make_unique(); - if (!g_vertex_manager->Initialize() || !g_shader_cache->Initialize() || - !g_renderer->Initialize() || !g_framebuffer_manager->Initialize() || - !g_texture_cache->Initialize()) - { - Shutdown(); - return false; - } + auto gfx = std::make_unique(std::move(swap_chain), wsi.render_surface_scale); + auto vertex_manager = std::make_unique(); + auto perf_query = std::make_unique(); + auto bounding_box = std::make_unique(); - g_shader_cache->InitializeShaderCache(); - return true; + return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + std::move(bounding_box)); } void VideoBackend::Shutdown() diff --git a/Source/Core/VideoBackends/D3D/D3DNativeVertexFormat.cpp b/Source/Core/VideoBackends/D3D/D3DNativeVertexFormat.cpp index aca5a177fa..54c1ae8220 100644 --- a/Source/Core/VideoBackends/D3D/D3DNativeVertexFormat.cpp +++ b/Source/Core/VideoBackends/D3D/D3DNativeVertexFormat.cpp @@ -7,7 +7,7 @@ #include "Common/EnumMap.h" #include "VideoBackends/D3D/D3DBase.h" -#include "VideoBackends/D3D/D3DRender.h" +#include "VideoBackends/D3D/D3DGfx.h" #include "VideoBackends/D3D/D3DState.h" #include "VideoBackends/D3D/D3DVertexManager.h" #include "VideoBackends/D3D/DXShader.h" @@ -18,7 +18,7 @@ namespace DX11 std::mutex s_input_layout_lock; std::unique_ptr -Renderer::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) +Gfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) { return std::make_unique(vtx_decl); } diff --git a/Source/Core/VideoBackends/D3D/D3DRender.cpp b/Source/Core/VideoBackends/D3D/D3DRender.cpp deleted file mode 100644 index 6e045979cb..0000000000 --- a/Source/Core/VideoBackends/D3D/D3DRender.cpp +++ /dev/null @@ -1,294 +0,0 @@ -// Copyright 2010 Dolphin Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "VideoBackends/D3D/D3DRender.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "Common/Assert.h" -#include "Common/CommonTypes.h" -#include "Common/Logging/Log.h" -#include "Common/MathUtil.h" - -#include "Core/Core.h" - -#include "VideoBackends/D3D/D3DBase.h" -#include "VideoBackends/D3D/D3DBoundingBox.h" -#include "VideoBackends/D3D/D3DState.h" -#include "VideoBackends/D3D/D3DSwapChain.h" -#include "VideoBackends/D3D/DXPipeline.h" -#include "VideoBackends/D3D/DXShader.h" -#include "VideoBackends/D3D/DXTexture.h" - -#include "VideoCommon/BPFunctions.h" -#include "VideoCommon/FramebufferManager.h" -#include "VideoCommon/PostProcessing.h" -#include "VideoCommon/Present.h" -#include "VideoCommon/RenderState.h" -#include "VideoCommon/VideoConfig.h" -#include "VideoCommon/XFMemory.h" - -namespace DX11 -{ -Renderer::Renderer(std::unique_ptr swap_chain, float backbuffer_scale) - : ::Renderer(swap_chain ? swap_chain->GetWidth() : 0, swap_chain ? swap_chain->GetHeight() : 0, - backbuffer_scale, - swap_chain ? swap_chain->GetFormat() : AbstractTextureFormat::Undefined), - m_swap_chain(std::move(swap_chain)) -{ -} - -Renderer::~Renderer() = default; - -bool Renderer::IsHeadless() const -{ - return !m_swap_chain; -} - -std::unique_ptr Renderer::CreateTexture(const TextureConfig& config, - std::string_view name) -{ - return DXTexture::Create(config, name); -} - -std::unique_ptr Renderer::CreateStagingTexture(StagingTextureType type, - const TextureConfig& config) -{ - return DXStagingTexture::Create(type, config); -} - -std::unique_ptr Renderer::CreateFramebuffer(AbstractTexture* color_attachment, - AbstractTexture* depth_attachment) -{ - return DXFramebuffer::Create(static_cast(color_attachment), - static_cast(depth_attachment)); -} - -std::unique_ptr -Renderer::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name) -{ - auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source); - if (!bytecode) - return nullptr; - - return DXShader::CreateFromBytecode(stage, std::move(*bytecode), name); -} - -std::unique_ptr Renderer::CreateShaderFromBinary(ShaderStage stage, - const void* data, size_t length, - std::string_view name) -{ - return DXShader::CreateFromBytecode(stage, DXShader::CreateByteCode(data, length), name); -} - -std::unique_ptr Renderer::CreatePipeline(const AbstractPipelineConfig& config, - const void* cache_data, - size_t cache_data_length) -{ - return DXPipeline::Create(config); -} - -void Renderer::SetPipeline(const AbstractPipeline* pipeline) -{ - const DXPipeline* dx_pipeline = static_cast(pipeline); - if (m_current_pipeline == dx_pipeline) - return; - - if (dx_pipeline) - { - D3D::stateman->SetRasterizerState(dx_pipeline->GetRasterizerState()); - D3D::stateman->SetDepthState(dx_pipeline->GetDepthState()); - D3D::stateman->SetBlendState(dx_pipeline->GetBlendState()); - D3D::stateman->SetPrimitiveTopology(dx_pipeline->GetPrimitiveTopology()); - D3D::stateman->SetInputLayout(dx_pipeline->GetInputLayout()); - D3D::stateman->SetVertexShader(dx_pipeline->GetVertexShader()); - D3D::stateman->SetGeometryShader(dx_pipeline->GetGeometryShader()); - D3D::stateman->SetPixelShader(dx_pipeline->GetPixelShader()); - D3D::stateman->SetIntegerRTV(dx_pipeline->UseLogicOp()); - } - else - { - // These will be destroyed at pipeline destruction. - D3D::stateman->SetInputLayout(nullptr); - D3D::stateman->SetVertexShader(nullptr); - D3D::stateman->SetGeometryShader(nullptr); - D3D::stateman->SetPixelShader(nullptr); - } -} - -void Renderer::SetScissorRect(const MathUtil::Rectangle& rc) -{ - // TODO: Move to stateman - const CD3D11_RECT rect(rc.left, rc.top, std::max(rc.right, rc.left + 1), - std::max(rc.bottom, rc.top + 1)); - D3D::context->RSSetScissorRects(1, &rect); -} - -void Renderer::SetViewport(float x, float y, float width, float height, float near_depth, - float far_depth) -{ - // TODO: Move to stateman - const CD3D11_VIEWPORT vp(x, y, width, height, near_depth, far_depth); - D3D::context->RSSetViewports(1, &vp); -} - -void Renderer::Draw(u32 base_vertex, u32 num_vertices) -{ - D3D::stateman->Apply(); - D3D::context->Draw(num_vertices, base_vertex); -} - -void Renderer::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) -{ - D3D::stateman->Apply(); - D3D::context->DrawIndexed(num_indices, base_index, base_vertex); -} - -void Renderer::DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, - u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) -{ - D3D::stateman->SetComputeShader(static_cast(shader)->GetD3DComputeShader()); - D3D::stateman->SyncComputeBindings(); - D3D::context->Dispatch(groups_x, groups_y, groups_z); -} - -void Renderer::BindBackbuffer(const ClearColor& clear_color) -{ - CheckForSwapChainChanges(); - SetAndClearFramebuffer(m_swap_chain->GetFramebuffer(), clear_color); -} - -void Renderer::PresentBackbuffer() -{ - m_swap_chain->Present(); -} - -void Renderer::OnConfigChanged(u32 bits) -{ - // Quad-buffer changes require swap chain recreation. - if (bits & CONFIG_CHANGE_BIT_STEREO_MODE && m_swap_chain) - m_swap_chain->SetStereo(SwapChain::WantsStereo()); -} - -void Renderer::CheckForSwapChainChanges() -{ - const bool surface_changed = g_presenter->SurfaceChangedTestAndClear(); - const bool surface_resized = - g_presenter->SurfaceResizedTestAndClear() || m_swap_chain->CheckForFullscreenChange(); - if (!surface_changed && !surface_resized) - return; - - if (surface_changed) - { - m_swap_chain->ChangeSurface(g_presenter->GetNewSurfaceHandle()); - } - else - { - m_swap_chain->ResizeSwapChain(); - } - - m_backbuffer_width = m_swap_chain->GetWidth(); - m_backbuffer_height = m_swap_chain->GetHeight(); -} - -void Renderer::SetFramebuffer(AbstractFramebuffer* framebuffer) -{ - if (m_current_framebuffer == framebuffer) - return; - - // We can't leave the framebuffer bound as a texture and a render target. - DXFramebuffer* fb = static_cast(framebuffer); - if ((fb->GetColorAttachment() && - D3D::stateman->UnsetTexture( - static_cast(fb->GetColorAttachment())->GetD3DSRV()) != 0) || - (fb->GetDepthAttachment() && - D3D::stateman->UnsetTexture( - static_cast(fb->GetDepthAttachment())->GetD3DSRV()) != 0)) - { - D3D::stateman->ApplyTextures(); - } - - D3D::stateman->SetFramebuffer(fb); - m_current_framebuffer = fb; -} - -void Renderer::SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) -{ - SetFramebuffer(framebuffer); -} - -void Renderer::SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, - const ClearColor& color_value, float depth_value) -{ - SetFramebuffer(framebuffer); - D3D::stateman->Apply(); - - if (framebuffer->GetColorFormat() != AbstractTextureFormat::Undefined) - { - D3D::context->ClearRenderTargetView( - static_cast(framebuffer)->GetRTVArray()[0], color_value.data()); - } - if (framebuffer->GetDepthFormat() != AbstractTextureFormat::Undefined) - { - D3D::context->ClearDepthStencilView(static_cast(framebuffer)->GetDSV(), - D3D11_CLEAR_DEPTH, depth_value, 0); - } -} - -void Renderer::SetTexture(u32 index, const AbstractTexture* texture) -{ - D3D::stateman->SetTexture(index, texture ? static_cast(texture)->GetD3DSRV() : - nullptr); -} - -void Renderer::SetSamplerState(u32 index, const SamplerState& state) -{ - D3D::stateman->SetSampler(index, m_state_cache.Get(state)); -} - -void Renderer::SetComputeImageTexture(AbstractTexture* texture, bool read, bool write) -{ - D3D::stateman->SetComputeUAV(texture ? static_cast(texture)->GetD3DUAV() : nullptr); -} - -void Renderer::UnbindTexture(const AbstractTexture* texture) -{ - if (D3D::stateman->UnsetTexture(static_cast(texture)->GetD3DSRV()) != 0) - D3D::stateman->ApplyTextures(); -} - -std::unique_ptr Renderer::CreateBoundingBox() const -{ - return std::make_unique(); -} - -void Renderer::Flush() -{ - D3D::context->Flush(); -} - -void Renderer::WaitForGPUIdle() -{ - // There is no glFinish() equivalent in D3D. - D3D::context->Flush(); -} - -void Renderer::SetFullscreen(bool enable_fullscreen) -{ - if (m_swap_chain) - m_swap_chain->SetFullscreen(enable_fullscreen); -} - -bool Renderer::IsFullscreen() const -{ - return m_swap_chain && m_swap_chain->GetFullscreen(); -} - -} // namespace DX11 diff --git a/Source/Core/VideoBackends/D3D/D3DRender.h b/Source/Core/VideoBackends/D3D/D3DRender.h deleted file mode 100644 index a0d25bc270..0000000000 --- a/Source/Core/VideoBackends/D3D/D3DRender.h +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2010 Dolphin Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include -#include -#include "VideoBackends/D3D/D3DState.h" -#include "VideoCommon/RenderBase.h" - -class BoundingBox; - -namespace DX11 -{ -class SwapChain; -class DXTexture; -class DXFramebuffer; - -class Renderer : public ::Renderer -{ -public: - Renderer(std::unique_ptr swap_chain, float backbuffer_scale); - ~Renderer() override; - - StateCache& GetStateCache() { return m_state_cache; } - - bool IsHeadless() const override; - - std::unique_ptr CreateTexture(const TextureConfig& config, - std::string_view name) override; - std::unique_ptr - CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override; - std::unique_ptr CreateShaderFromSource(ShaderStage stage, std::string_view source, - std::string_view name) override; - std::unique_ptr CreateShaderFromBinary(ShaderStage stage, const void* data, - size_t length, - std::string_view name) override; - std::unique_ptr - CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override; - std::unique_ptr CreatePipeline(const AbstractPipelineConfig& config, - const void* cache_data = nullptr, - size_t cache_data_length = 0) override; - std::unique_ptr - CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment) override; - - void SetPipeline(const AbstractPipeline* pipeline) override; - void SetFramebuffer(AbstractFramebuffer* framebuffer) override; - void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override; - void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {}, - float depth_value = 0.0f) override; - void SetScissorRect(const MathUtil::Rectangle& rc) override; - void SetTexture(u32 index, const AbstractTexture* texture) override; - void SetSamplerState(u32 index, const SamplerState& state) override; - void SetComputeImageTexture(AbstractTexture* texture, bool read, bool write) override; - void UnbindTexture(const AbstractTexture* texture) override; - void SetViewport(float x, float y, float width, float height, float near_depth, - float far_depth) override; - void Draw(u32 base_vertex, u32 num_vertices) override; - void DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) override; - void DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y, - u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) override; - void BindBackbuffer(const ClearColor& clear_color = {}) override; - void PresentBackbuffer() override; - void SetFullscreen(bool enable_fullscreen) override; - bool IsFullscreen() const override; - - void Flush() override; - void WaitForGPUIdle() override; - - void OnConfigChanged(u32 bits) override; - -protected: - std::unique_ptr CreateBoundingBox() const override; - -private: - void CheckForSwapChainChanges(); - - StateCache m_state_cache; - - std::unique_ptr m_swap_chain; -}; -} // namespace DX11 diff --git a/Source/Core/VideoBackends/D3D/D3DVertexManager.cpp b/Source/Core/VideoBackends/D3D/D3DVertexManager.cpp index ce41e90e31..4aa1c40266 100644 --- a/Source/Core/VideoBackends/D3D/D3DVertexManager.cpp +++ b/Source/Core/VideoBackends/D3D/D3DVertexManager.cpp @@ -13,7 +13,7 @@ #include "VideoBackends/D3D/D3DBase.h" #include "VideoBackends/D3D/D3DBoundingBox.h" -#include "VideoBackends/D3D/D3DRender.h" +#include "VideoBackends/D3D/D3DGfx.h" #include "VideoBackends/D3D/D3DState.h" #include "VideoBackends/D3DCommon/D3DCommon.h" diff --git a/Source/Core/VideoBackends/D3D/DXPipeline.cpp b/Source/Core/VideoBackends/D3D/DXPipeline.cpp index 0bdfeb0012..3553e8d512 100644 --- a/Source/Core/VideoBackends/D3D/DXPipeline.cpp +++ b/Source/Core/VideoBackends/D3D/DXPipeline.cpp @@ -7,7 +7,7 @@ #include "Common/Logging/Log.h" #include "VideoBackends/D3D/D3DBase.h" -#include "VideoBackends/D3D/D3DRender.h" +#include "VideoBackends/D3D/D3DGfx.h" #include "VideoBackends/D3D/D3DState.h" #include "VideoBackends/D3D/D3DVertexManager.h" #include "VideoBackends/D3D/DXShader.h" @@ -32,7 +32,7 @@ DXPipeline::~DXPipeline() = default; std::unique_ptr DXPipeline::Create(const AbstractPipelineConfig& config) { - StateCache& state_cache = static_cast(g_renderer.get())->GetStateCache(); + StateCache& state_cache = static_cast(g_gfx.get())->GetStateCache(); ID3D11RasterizerState* rasterizer_state = state_cache.Get(config.rasterization_state); ID3D11DepthStencilState* depth_state = state_cache.Get(config.depth_state); ID3D11BlendState* blend_state = state_cache.Get(config.blending_state); -- cgit v1.2.3