2017-09-08 09:42:56 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
#include "VideoBackends/D3D/DXShader.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
DXShader::DXShader(ShaderStage stage, BinaryData bytecode, ID3D11DeviceChild* shader)
|
2019-03-09 13:31:36 +00:00
|
|
|
: D3DCommon::Shader(stage, std::move(bytecode)), m_shader(shader)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
DXShader::~DXShader() = default;
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
ID3D11VertexShader* DXShader::GetD3DVertexShader() const
|
|
|
|
{
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(m_stage == ShaderStage::Vertex);
|
2019-03-09 13:31:37 +00:00
|
|
|
return static_cast<ID3D11VertexShader*>(m_shader.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11GeometryShader* DXShader::GetD3DGeometryShader() const
|
|
|
|
{
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(m_stage == ShaderStage::Geometry);
|
2019-03-09 13:31:37 +00:00
|
|
|
return static_cast<ID3D11GeometryShader*>(m_shader.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11PixelShader* DXShader::GetD3DPixelShader() const
|
|
|
|
{
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(m_stage == ShaderStage::Pixel);
|
2019-03-09 13:31:37 +00:00
|
|
|
return static_cast<ID3D11PixelShader*>(m_shader.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11ComputeShader* DXShader::GetD3DComputeShader() const
|
|
|
|
{
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(m_stage == ShaderStage::Compute);
|
2019-03-09 13:31:37 +00:00
|
|
|
return static_cast<ID3D11ComputeShader*>(m_shader.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, BinaryData bytecode)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::Vertex:
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
ComPtr<ID3D11VertexShader> vs;
|
2019-02-15 01:59:50 +00:00
|
|
|
HRESULT hr = D3D::device->CreateVertexShader(bytecode.data(), bytecode.size(), nullptr, &vs);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create vertex shader");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
return std::make_unique<DXShader>(ShaderStage::Vertex, std::move(bytecode), vs.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderStage::Geometry:
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
ComPtr<ID3D11GeometryShader> gs;
|
2019-02-15 01:59:50 +00:00
|
|
|
HRESULT hr = D3D::device->CreateGeometryShader(bytecode.data(), bytecode.size(), nullptr, &gs);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create geometry shader");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
return std::make_unique<DXShader>(ShaderStage::Geometry, std::move(bytecode), gs.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ShaderStage::Pixel:
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
ComPtr<ID3D11PixelShader> ps;
|
2019-02-15 01:59:50 +00:00
|
|
|
HRESULT hr = D3D::device->CreatePixelShader(bytecode.data(), bytecode.size(), nullptr, &ps);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create pixel shader");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
return std::make_unique<DXShader>(ShaderStage::Pixel, std::move(bytecode), ps.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ShaderStage::Compute:
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
ComPtr<ID3D11ComputeShader> cs;
|
2019-02-15 01:59:50 +00:00
|
|
|
HRESULT hr = D3D::device->CreateComputeShader(bytecode.data(), bytecode.size(), nullptr, &cs);
|
|
|
|
CHECK(SUCCEEDED(hr), "Create compute shader");
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-03-09 13:31:37 +00:00
|
|
|
return std::make_unique<DXShader>(ShaderStage::Compute, std::move(bytecode), cs.Get());
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} // namespace DX11
|