2019-03-28 10:35:46 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-28 10:35:46 +00:00
|
|
|
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/DX12Shader.h"
|
2021-08-28 05:30:05 +00:00
|
|
|
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
2019-03-28 10:35:46 +00:00
|
|
|
#include "VideoBackends/D3D12/Common.h"
|
2020-09-15 13:01:29 +00:00
|
|
|
#include "VideoBackends/D3D12/DX12Context.h"
|
2019-03-28 10:35:46 +00:00
|
|
|
|
|
|
|
namespace DX12
|
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
DXShader::DXShader(ShaderStage stage, BinaryData bytecode, std::string_view name)
|
|
|
|
: D3DCommon::Shader(stage, std::move(bytecode)), m_name(UTF8ToWString(name))
|
2019-03-28 10:35:46 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
if (!m_name.empty())
|
|
|
|
{
|
|
|
|
m_compute_pipeline->SetName(m_name.c_str());
|
|
|
|
}
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DXShader::~DXShader() = default;
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, BinaryData bytecode,
|
|
|
|
std::string_view name)
|
2019-03-28 10:35:46 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
std::unique_ptr<DXShader> shader(new DXShader(stage, std::move(bytecode), name));
|
2019-03-28 10:35:46 +00:00
|
|
|
if (stage == ShaderStage::Compute && !shader->CreateComputePipeline())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
std::unique_ptr<DXShader> DXShader::CreateFromSource(ShaderStage stage, std::string_view source,
|
|
|
|
std::string_view name)
|
2019-03-28 10:35:46 +00:00
|
|
|
{
|
2019-07-26 23:34:27 +00:00
|
|
|
auto bytecode = CompileShader(g_dx_context->GetFeatureLevel(), stage, source);
|
|
|
|
if (!bytecode)
|
2019-03-28 10:35:46 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
return CreateFromBytecode(stage, std::move(*bytecode), name);
|
2019-03-28 10:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
D3D12_SHADER_BYTECODE DXShader::GetD3DByteCode() const
|
|
|
|
{
|
|
|
|
return D3D12_SHADER_BYTECODE{m_bytecode.data(), m_bytecode.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DXShader::CreateComputePipeline()
|
|
|
|
{
|
|
|
|
D3D12_COMPUTE_PIPELINE_STATE_DESC desc = {};
|
|
|
|
desc.pRootSignature = g_dx_context->GetComputeRootSignature();
|
|
|
|
desc.CS = GetD3DByteCode();
|
|
|
|
desc.NodeMask = 1;
|
|
|
|
|
|
|
|
HRESULT hr = g_dx_context->GetDevice()->CreateComputePipelineState(
|
|
|
|
&desc, IID_PPV_ARGS(&m_compute_pipeline));
|
|
|
|
CHECK(SUCCEEDED(hr), "Creating compute pipeline failed");
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DX12
|