2019-03-09 13:31:35 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <wrl/client.h>
|
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2020-01-24 08:29:38 +00:00
|
|
|
#include "Common/Version.h"
|
2019-03-09 13:31:35 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/D3DCommon/Shader.h"
|
2020-01-24 08:29:38 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2019-03-09 13:31:35 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
|
|
|
namespace D3DCommon
|
|
|
|
{
|
|
|
|
Shader::Shader(ShaderStage stage, BinaryData bytecode)
|
|
|
|
: AbstractShader(stage), m_bytecode(std::move(bytecode))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Shader::~Shader() = default;
|
|
|
|
|
|
|
|
AbstractShader::BinaryData Shader::GetBinary() const
|
|
|
|
{
|
|
|
|
return m_bytecode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* GetCompileTarget(D3D_FEATURE_LEVEL feature_level, ShaderStage stage)
|
|
|
|
{
|
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::Vertex:
|
|
|
|
{
|
|
|
|
switch (feature_level)
|
|
|
|
{
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
return "vs_4_0";
|
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
return "vs_4_1";
|
|
|
|
default:
|
|
|
|
return "vs_5_0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderStage::Geometry:
|
|
|
|
{
|
|
|
|
switch (feature_level)
|
|
|
|
{
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
return "gs_4_0";
|
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
return "gs_4_1";
|
|
|
|
default:
|
|
|
|
return "gs_5_0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderStage::Pixel:
|
|
|
|
{
|
|
|
|
switch (feature_level)
|
|
|
|
{
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
return "ps_4_0";
|
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
return "ps_4_1";
|
|
|
|
default:
|
|
|
|
return "ps_5_0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderStage::Compute:
|
|
|
|
{
|
|
|
|
switch (feature_level)
|
|
|
|
{
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
return "";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "cs_5_0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:34:27 +00:00
|
|
|
std::optional<Shader::BinaryData> Shader::CompileShader(D3D_FEATURE_LEVEL feature_level,
|
|
|
|
ShaderStage stage, std::string_view source)
|
2019-03-09 13:31:35 +00:00
|
|
|
{
|
|
|
|
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(feature_level, stage);
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID3DBlob> code;
|
|
|
|
Microsoft::WRL::ComPtr<ID3DBlob> errors;
|
2019-05-30 07:07:40 +00:00
|
|
|
HRESULT hr = d3d_compile(source.data(), source.size(), nullptr, macros, nullptr, "main", target,
|
|
|
|
flags, 0, &code, &errors);
|
2019-03-09 13:31:35 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
static int num_failures = 0;
|
2020-01-24 08:29:38 +00:00
|
|
|
std::string filename = VideoBackendBase::BadShaderFilename(target, num_failures++);
|
2019-03-09 13:31:35 +00:00
|
|
|
std::ofstream file;
|
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
2019-05-30 07:07:40 +00:00
|
|
|
file.write(source.data(), source.size());
|
2019-03-09 13:31:35 +00:00
|
|
|
file << "\n";
|
|
|
|
file.write(static_cast<const char*>(errors->GetBufferPointer()), errors->GetBufferSize());
|
2020-01-24 08:29:38 +00:00
|
|
|
file << "\n";
|
|
|
|
file << "Dolphin Version: " + Common::scm_rev_str + "\n";
|
|
|
|
file << "Video Backend: " + g_video_backend->GetDisplayName();
|
2019-03-09 13:31:35 +00:00
|
|
|
file.close();
|
|
|
|
|
|
|
|
PanicAlert("Failed to compile %s:\nDebug info (%s):\n%s", filename.c_str(), target,
|
|
|
|
static_cast<const char*>(errors->GetBufferPointer()));
|
2019-07-26 23:34:27 +00:00
|
|
|
return std::nullopt;
|
2019-03-09 13:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (errors && errors->GetBufferSize() > 0)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "%s compilation succeeded with warnings:\n%s", target,
|
|
|
|
static_cast<const char*>(errors->GetBufferPointer()));
|
|
|
|
}
|
|
|
|
|
2019-07-26 23:34:27 +00:00
|
|
|
return CreateByteCode(code->GetBufferPointer(), code->GetBufferSize());
|
2019-03-09 13:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AbstractShader::BinaryData Shader::CreateByteCode(const void* data, size_t length)
|
|
|
|
{
|
2019-07-26 23:27:55 +00:00
|
|
|
const auto* const begin = static_cast<const u8*>(data);
|
|
|
|
const auto* const end = begin + length;
|
|
|
|
|
|
|
|
return {begin, end};
|
2019-03-09 13:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace D3DCommon
|