dolphin/Source/Core/VideoBackends/D3D/D3DRender.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

295 lines
9.1 KiB
C++
Raw Normal View History

// Copyright 2010 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "VideoBackends/D3D/D3DRender.h"
#include <algorithm>
#include <array>
2013-10-26 09:55:41 +00:00
#include <cmath>
#include <cstring>
#include <memory>
#include <string>
#include <strsafe.h>
#include <tuple>
#include "Common/Assert.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
2015-05-06 22:37:58 +00:00
#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"
2017-05-29 22:02:09 +00:00
#include "VideoBackends/D3D/DXTexture.h"
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/FramebufferManager.h"
#include "VideoCommon/PostProcessing.h"
#include "VideoCommon/RenderState.h"
2017-09-03 02:30:34 +00:00
#include "VideoCommon/VideoConfig.h"
#include "VideoCommon/XFMemory.h"
namespace DX11
{
2019-03-09 13:31:36 +00:00
Renderer::Renderer(std::unique_ptr<SwapChain> 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
{
2019-03-09 13:31:36 +00:00
return !m_swap_chain;
}
std::unique_ptr<AbstractTexture> Renderer::CreateTexture(const TextureConfig& config,
std::string_view name)
{
return DXTexture::Create(config, name);
}
std::unique_ptr<AbstractStagingTexture> Renderer::CreateStagingTexture(StagingTextureType type,
const TextureConfig& config)
{
return DXStagingTexture::Create(type, config);
}
std::unique_ptr<AbstractFramebuffer> Renderer::CreateFramebuffer(AbstractTexture* color_attachment,
AbstractTexture* depth_attachment)
{
return DXFramebuffer::Create(static_cast<DXTexture*>(color_attachment),
static_cast<DXTexture*>(depth_attachment));
}
std::unique_ptr<AbstractShader>
Renderer::CreateShaderFromSource(ShaderStage stage, std::string_view source, std::string_view name)
{
auto bytecode = DXShader::CompileShader(D3D::feature_level, stage, source);
if (!bytecode)
2019-03-09 13:31:36 +00:00
return nullptr;
return DXShader::CreateFromBytecode(stage, std::move(*bytecode), name);
}
std::unique_ptr<AbstractShader> 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<AbstractPipeline> 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<const DXPipeline*>(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<int>& 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);
}
2018-11-27 07:16:53 +00:00
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);
}
2022-06-01 09:58:13 +00:00
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<const DXShader*>(shader)->GetD3DComputeShader());
D3D::stateman->SyncComputeBindings();
D3D::context->Dispatch(groups_x, groups_y, groups_z);
}
2018-11-28 04:30:47 +00:00
void Renderer::BindBackbuffer(const ClearColor& clear_color)
{
2019-03-09 13:31:36 +00:00
CheckForSwapChainChanges();
SetAndClearFramebuffer(m_swap_chain->GetFramebuffer(), clear_color);
2018-11-28 04:30:47 +00:00
}
2018-11-28 04:30:47 +00:00
void Renderer::PresentBackbuffer()
{
2019-03-09 13:31:36 +00:00
m_swap_chain->Present();
2018-11-28 04:30:47 +00:00
}
2018-11-28 04:30:47 +00:00
void Renderer::OnConfigChanged(u32 bits)
{
2019-03-09 13:31:36 +00:00
// Quad-buffer changes require swap chain recreation.
if (bits & CONFIG_CHANGE_BIT_STEREO_MODE && m_swap_chain)
m_swap_chain->SetStereo(SwapChain::WantsStereo());
}
2019-03-09 13:31:36 +00:00
void Renderer::CheckForSwapChainChanges()
{
2019-03-09 13:31:36 +00:00
const bool surface_changed = m_surface_changed.TestAndClear();
const bool surface_resized =
m_surface_resized.TestAndClear() || m_swap_chain->CheckForFullscreenChange();
if (!surface_changed && !surface_resized)
return;
2019-03-09 13:31:36 +00:00
if (surface_changed)
{
2019-03-09 13:31:36 +00:00
m_swap_chain->ChangeSurface(m_new_surface_handle);
m_new_surface_handle = nullptr;
}
else
{
2019-03-09 13:31:36 +00:00
m_swap_chain->ResizeSwapChain();
}
2019-03-09 13:31:36 +00:00
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<DXFramebuffer*>(framebuffer);
if ((fb->GetColorAttachment() &&
D3D::stateman->UnsetTexture(
static_cast<DXTexture*>(fb->GetColorAttachment())->GetD3DSRV()) != 0) ||
(fb->GetDepthAttachment() &&
D3D::stateman->UnsetTexture(
static_cast<DXTexture*>(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<const DXFramebuffer*>(framebuffer)->GetRTVArray()[0], color_value.data());
}
if (framebuffer->GetDepthFormat() != AbstractTextureFormat::Undefined)
{
D3D::context->ClearDepthStencilView(static_cast<const DXFramebuffer*>(framebuffer)->GetDSV(),
D3D11_CLEAR_DEPTH, depth_value, 0);
}
}
void Renderer::SetTexture(u32 index, const AbstractTexture* texture)
{
D3D::stateman->SetTexture(index, texture ? static_cast<const DXTexture*>(texture)->GetD3DSRV() :
nullptr);
}
void Renderer::SetSamplerState(u32 index, const SamplerState& state)
{
2018-02-24 15:15:35 +00:00
D3D::stateman->SetSampler(index, m_state_cache.Get(state));
}
void Renderer::SetComputeImageTexture(AbstractTexture* texture, bool read, bool write)
{
D3D::stateman->SetComputeUAV(texture ? static_cast<DXTexture*>(texture)->GetD3DUAV() : nullptr);
}
void Renderer::UnbindTexture(const AbstractTexture* texture)
{
if (D3D::stateman->UnsetTexture(static_cast<const DXTexture*>(texture)->GetD3DSRV()) != 0)
D3D::stateman->ApplyTextures();
}
std::unique_ptr<BoundingBox> Renderer::CreateBoundingBox() const
{
return std::make_unique<D3DBoundingBox>();
}
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)
{
2019-03-09 13:31:36 +00:00
if (m_swap_chain)
m_swap_chain->SetFullscreen(enable_fullscreen);
}
2016-11-13 21:16:29 +00:00
bool Renderer::IsFullscreen() const
{
2019-03-09 13:31:36 +00:00
return m_swap_chain && m_swap_chain->GetFullscreen();
}
} // namespace DX11