mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
D3D11: Use D3DCommon where appropriate
This commit is contained in:
@ -2,22 +2,14 @@
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/FileUtil.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/MsgHandler.h"
|
||||
#include "Common/StringUtil.h"
|
||||
|
||||
#include "VideoBackends/D3D/D3DBase.h"
|
||||
#include "VideoBackends/D3D/DXShader.h"
|
||||
#include "VideoCommon/VideoConfig.h"
|
||||
#include "Common/Assert.h"
|
||||
#include "VideoBackends/D3D/D3DBase.h"
|
||||
|
||||
namespace DX11
|
||||
{
|
||||
DXShader::DXShader(ShaderStage stage, BinaryData bytecode, ID3D11DeviceChild* shader)
|
||||
: AbstractShader(stage), m_bytecode(bytecode), m_shader(shader)
|
||||
: D3DCommon::Shader(stage, std::move(bytecode)), m_shader(shader)
|
||||
{
|
||||
}
|
||||
|
||||
@ -50,16 +42,6 @@ ID3D11ComputeShader* DXShader::GetD3DComputeShader() const
|
||||
return static_cast<ID3D11ComputeShader*>(m_shader);
|
||||
}
|
||||
|
||||
bool DXShader::HasBinary() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
AbstractShader::BinaryData DXShader::GetBinary() const
|
||||
{
|
||||
return m_bytecode;
|
||||
}
|
||||
|
||||
std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, BinaryData bytecode)
|
||||
{
|
||||
switch (stage)
|
||||
@ -117,86 +99,4 @@ std::unique_ptr<DXShader> DXShader::CreateFromBytecode(ShaderStage stage, Binary
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static const char* GetCompileTarget(ShaderStage stage)
|
||||
{
|
||||
switch (stage)
|
||||
{
|
||||
case ShaderStage::Vertex:
|
||||
return D3D::VertexShaderVersionString();
|
||||
case ShaderStage::Geometry:
|
||||
return D3D::GeometryShaderVersionString();
|
||||
case ShaderStage::Pixel:
|
||||
return D3D::PixelShaderVersionString();
|
||||
case ShaderStage::Compute:
|
||||
return D3D::ComputeShaderVersionString();
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
bool DXShader::CompileShader(BinaryData* out_bytecode, ShaderStage stage, const char* source,
|
||||
size_t length)
|
||||
{
|
||||
static constexpr D3D_SHADER_MACRO macros[] = {{"API_D3D", "1"}, {nullptr, nullptr}};
|
||||
const UINT flags = g_ActiveConfig.bEnableValidationLayer ?
|
||||
(D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION) :
|
||||
(D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_SKIP_VALIDATION);
|
||||
const char* target = GetCompileTarget(stage);
|
||||
|
||||
ID3DBlob* code = nullptr;
|
||||
ID3DBlob* errors = nullptr;
|
||||
HRESULT hr = PD3DCompile(source, length, nullptr, macros, nullptr, "main", target, flags, 0,
|
||||
&code, &errors);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
static int num_failures = 0;
|
||||
std::string filename = StringFromFormat(
|
||||
"%sbad_%s_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), target, num_failures++);
|
||||
std::ofstream file;
|
||||
File::OpenFStream(file, filename, std::ios_base::out);
|
||||
file.write(source, length);
|
||||
file << "\n";
|
||||
file.write(static_cast<const char*>(errors->GetBufferPointer()), errors->GetBufferSize());
|
||||
file.close();
|
||||
|
||||
PanicAlert("Failed to compile %s:\nDebug info (%s):\n%s", filename.c_str(), target,
|
||||
static_cast<const char*>(errors->GetBufferPointer()));
|
||||
errors->Release();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (errors && errors->GetBufferSize() > 0)
|
||||
{
|
||||
WARN_LOG(VIDEO, "%s compilation succeeded with warnings:\n%s", target,
|
||||
static_cast<const char*>(errors->GetBufferPointer()));
|
||||
}
|
||||
SAFE_RELEASE(errors);
|
||||
|
||||
out_bytecode->resize(code->GetBufferSize());
|
||||
std::memcpy(out_bytecode->data(), code->GetBufferPointer(), code->GetBufferSize());
|
||||
code->Release();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<DXShader> DXShader::CreateFromSource(ShaderStage stage, const char* source,
|
||||
size_t length)
|
||||
{
|
||||
BinaryData bytecode;
|
||||
if (!CompileShader(&bytecode, stage, source, length))
|
||||
return nullptr;
|
||||
|
||||
return CreateFromBytecode(stage, std::move(bytecode));
|
||||
}
|
||||
|
||||
std::unique_ptr<DXShader> DXShader::CreateFromBinary(ShaderStage stage, const void* data,
|
||||
size_t length)
|
||||
{
|
||||
if (length == 0)
|
||||
return nullptr;
|
||||
|
||||
BinaryData bytecode(length);
|
||||
std::memcpy(bytecode.data(), data, length);
|
||||
return CreateFromBytecode(stage, std::move(bytecode));
|
||||
}
|
||||
} // namespace DX11
|
||||
|
Reference in New Issue
Block a user