2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <fstream>
|
2010-06-13 13:50:06 -06:00
|
|
|
#include <string>
|
|
|
|
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2011-01-29 13:16:51 -07:00
|
|
|
namespace DX11
|
|
|
|
{
|
2010-06-13 13:50:06 -06:00
|
|
|
namespace D3D
|
|
|
|
{
|
|
|
|
// bytecode->shader
|
2017-07-19 23:25:31 -06:00
|
|
|
ID3D11VertexShader* CreateVertexShaderFromByteCode(const void* bytecode, size_t len)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11VertexShader* v_shader;
|
|
|
|
HRESULT hr = D3D::device->CreateVertexShader(bytecode, len, nullptr, &v_shader);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
2011-07-29 16:18:11 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return v_shader;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
// code->bytecode
|
2014-07-07 17:28:12 -06:00
|
|
|
bool CompileVertexShader(const std::string& code, D3DBlob** blob)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D10Blob* shaderBuffer = nullptr;
|
|
|
|
ID3D10Blob* errorBuffer = nullptr;
|
2011-06-11 13:37:21 -06:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG;
|
2011-06-11 13:37:21 -06:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_OPTIMIZATION_LEVEL3 |
|
|
|
|
D3D10_SHADER_SKIP_VALIDATION;
|
2011-06-11 13:37:21 -06:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
HRESULT hr = PD3DCompile(code.c_str(), code.length(), nullptr, nullptr, nullptr, "main",
|
|
|
|
D3D::VertexShaderVersionString(), flags, 0, &shaderBuffer, &errorBuffer);
|
|
|
|
if (errorBuffer)
|
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
INFO_LOG(VIDEO, "Vertex shader compiler messages:\n%s",
|
2016-06-24 02:43:46 -06:00
|
|
|
(const char*)errorBuffer->GetBufferPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
static int num_failures = 0;
|
|
|
|
std::string filename = StringFromFormat("%sbad_vs_%04i.txt",
|
|
|
|
File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
|
|
|
|
std::ofstream file;
|
2017-01-15 14:46:43 -07:00
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
2016-06-24 02:43:46 -06:00
|
|
|
file << code;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
PanicAlert("Failed to compile vertex shader: %s\nDebug info (%s):\n%s", filename.c_str(),
|
|
|
|
D3D::VertexShaderVersionString(), (const char*)errorBuffer->GetBufferPointer());
|
|
|
|
|
|
|
|
*blob = nullptr;
|
|
|
|
errorBuffer->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*blob = new D3DBlob(shaderBuffer);
|
|
|
|
shaderBuffer->Release();
|
|
|
|
}
|
|
|
|
return SUCCEEDED(hr);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2011-03-14 03:38:29 -06:00
|
|
|
// bytecode->shader
|
2017-07-19 23:25:31 -06:00
|
|
|
ID3D11GeometryShader* CreateGeometryShaderFromByteCode(const void* bytecode, size_t len)
|
2011-03-14 03:38:29 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11GeometryShader* g_shader;
|
|
|
|
HRESULT hr = D3D::device->CreateGeometryShader(bytecode, len, nullptr, &g_shader);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return nullptr;
|
2011-07-29 16:18:11 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return g_shader;
|
2011-03-14 03:38:29 -06:00
|
|
|
}
|
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
// code->bytecode
|
2016-06-24 02:43:46 -06:00
|
|
|
bool CompileGeometryShader(const std::string& code, D3DBlob** blob,
|
|
|
|
const D3D_SHADER_MACRO* pDefines)
|
2011-03-14 03:38:29 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D10Blob* shaderBuffer = nullptr;
|
|
|
|
ID3D10Blob* errorBuffer = nullptr;
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG;
|
2011-03-14 03:38:29 -06:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_OPTIMIZATION_LEVEL3 |
|
|
|
|
D3D10_SHADER_SKIP_VALIDATION;
|
2011-03-14 03:38:29 -06:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
HRESULT hr =
|
|
|
|
PD3DCompile(code.c_str(), code.length(), nullptr, pDefines, nullptr, "main",
|
|
|
|
D3D::GeometryShaderVersionString(), flags, 0, &shaderBuffer, &errorBuffer);
|
|
|
|
|
|
|
|
if (errorBuffer)
|
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
INFO_LOG(VIDEO, "Geometry shader compiler messages:\n%s",
|
2016-06-24 02:43:46 -06:00
|
|
|
(const char*)errorBuffer->GetBufferPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
static int num_failures = 0;
|
|
|
|
std::string filename = StringFromFormat("%sbad_gs_%04i.txt",
|
|
|
|
File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
|
|
|
|
std::ofstream file;
|
2017-01-15 14:46:43 -07:00
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
2016-06-24 02:43:46 -06:00
|
|
|
file << code;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
PanicAlert("Failed to compile geometry shader: %s\nDebug info (%s):\n%s", filename.c_str(),
|
|
|
|
D3D::GeometryShaderVersionString(), (const char*)errorBuffer->GetBufferPointer());
|
|
|
|
|
|
|
|
*blob = nullptr;
|
|
|
|
errorBuffer->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*blob = new D3DBlob(shaderBuffer);
|
|
|
|
shaderBuffer->Release();
|
|
|
|
}
|
|
|
|
return SUCCEEDED(hr);
|
2011-03-14 03:38:29 -06:00
|
|
|
}
|
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
// bytecode->shader
|
2017-07-19 23:25:31 -06:00
|
|
|
ID3D11PixelShader* CreatePixelShaderFromByteCode(const void* bytecode, size_t len)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11PixelShader* p_shader;
|
|
|
|
HRESULT hr = D3D::device->CreatePixelShader(bytecode, len, nullptr, &p_shader);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
PanicAlert("CreatePixelShaderFromByteCode failed at %s %d\n", __FILE__, __LINE__);
|
|
|
|
p_shader = nullptr;
|
|
|
|
}
|
|
|
|
return p_shader;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// code->bytecode
|
2014-07-07 17:28:12 -06:00
|
|
|
bool CompilePixelShader(const std::string& code, D3DBlob** blob, const D3D_SHADER_MACRO* pDefines)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D10Blob* shaderBuffer = nullptr;
|
|
|
|
ID3D10Blob* errorBuffer = nullptr;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_DEBUG;
|
2011-06-11 13:37:21 -06:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
UINT flags = D3D10_SHADER_OPTIMIZATION_LEVEL3;
|
2011-06-11 13:37:21 -06:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
HRESULT hr = PD3DCompile(code.c_str(), code.length(), nullptr, pDefines, nullptr, "main",
|
|
|
|
D3D::PixelShaderVersionString(), flags, 0, &shaderBuffer, &errorBuffer);
|
|
|
|
|
|
|
|
if (errorBuffer)
|
|
|
|
{
|
|
|
|
INFO_LOG(VIDEO, "Pixel shader compiler messages:\n%s",
|
|
|
|
(const char*)errorBuffer->GetBufferPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
static int num_failures = 0;
|
|
|
|
std::string filename = StringFromFormat("%sbad_ps_%04i.txt",
|
|
|
|
File::GetUserPath(D_DUMP_IDX).c_str(), num_failures++);
|
|
|
|
std::ofstream file;
|
2017-01-15 14:46:43 -07:00
|
|
|
File::OpenFStream(file, filename, std::ios_base::out);
|
2016-06-24 02:43:46 -06:00
|
|
|
file << code;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
PanicAlert("Failed to compile pixel shader: %s\nDebug info (%s):\n%s", filename.c_str(),
|
|
|
|
D3D::PixelShaderVersionString(), (const char*)errorBuffer->GetBufferPointer());
|
|
|
|
|
|
|
|
*blob = nullptr;
|
|
|
|
errorBuffer->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*blob = new D3DBlob(shaderBuffer);
|
|
|
|
shaderBuffer->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCEEDED(hr);
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2014-07-07 17:28:12 -06:00
|
|
|
ID3D11VertexShader* CompileAndCreateVertexShader(const std::string& code)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
D3DBlob* blob = nullptr;
|
|
|
|
if (CompileVertexShader(code, &blob))
|
|
|
|
{
|
|
|
|
ID3D11VertexShader* v_shader = CreateVertexShaderFromByteCode(blob);
|
|
|
|
blob->Release();
|
|
|
|
return v_shader;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2011-06-11 13:37:21 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11GeometryShader* CompileAndCreateGeometryShader(const std::string& code,
|
|
|
|
const D3D_SHADER_MACRO* pDefines)
|
2011-06-11 13:37:21 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
D3DBlob* blob = nullptr;
|
|
|
|
if (CompileGeometryShader(code, &blob, pDefines))
|
|
|
|
{
|
|
|
|
ID3D11GeometryShader* g_shader = CreateGeometryShaderFromByteCode(blob);
|
|
|
|
blob->Release();
|
|
|
|
return g_shader;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2011-03-14 03:38:29 -06:00
|
|
|
}
|
|
|
|
|
2014-07-07 17:28:12 -06:00
|
|
|
ID3D11PixelShader* CompileAndCreatePixelShader(const std::string& code)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
D3DBlob* blob = nullptr;
|
|
|
|
CompilePixelShader(code, &blob);
|
|
|
|
if (blob)
|
|
|
|
{
|
|
|
|
ID3D11PixelShader* p_shader = CreatePixelShaderFromByteCode(blob);
|
|
|
|
blob->Release();
|
|
|
|
return p_shader;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2011-01-29 13:16:51 -07:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
} // namespace DX11
|