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