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
|
|
|
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2017-07-20 05:25:31 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-03 05:08:54 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2017-07-20 05:25:31 +00:00
|
|
|
#include "Core/Host.h"
|
2011-01-25 16:43:08 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
2017-07-20 05:25:31 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
|
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2017-07-20 05:25:31 +00:00
|
|
|
#include "VideoCommon/UberShaderVertex.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2011-01-31 01:28:32 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
2014-03-09 20:14:26 +00:00
|
|
|
static ID3D11VertexShader* SimpleVertexShader = nullptr;
|
|
|
|
static ID3D11VertexShader* ClearVertexShader = nullptr;
|
|
|
|
static ID3D11InputLayout* SimpleLayout = nullptr;
|
|
|
|
static ID3D11InputLayout* ClearLayout = nullptr;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11VertexShader* VertexShaderCache::GetSimpleVertexShader()
|
|
|
|
{
|
|
|
|
return SimpleVertexShader;
|
|
|
|
}
|
|
|
|
ID3D11VertexShader* VertexShaderCache::GetClearVertexShader()
|
|
|
|
{
|
|
|
|
return ClearVertexShader;
|
|
|
|
}
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetSimpleInputLayout()
|
|
|
|
{
|
|
|
|
return SimpleLayout;
|
|
|
|
}
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetClearInputLayout()
|
|
|
|
{
|
|
|
|
return ClearLayout;
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
ID3D11Buffer* vscbuf = nullptr;
|
2011-01-25 15:08:30 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11Buffer*& VertexShaderCache::GetConstantBuffer()
|
2011-01-25 15:08:30 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: divide the global variables of the generated shaders into about 5 constant buffers to
|
|
|
|
// speed this up
|
|
|
|
if (VertexShaderManager::dirty)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
D3D::context->Map(vscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
memcpy(map.pData, &VertexShaderManager::constants, sizeof(VertexShaderConstants));
|
|
|
|
D3D::context->Unmap(vscbuf, 0);
|
|
|
|
VertexShaderManager::dirty = false;
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(VertexShaderConstants));
|
|
|
|
}
|
|
|
|
return vscbuf;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this class will load the precompiled shaders into our cache
|
2017-07-20 05:25:31 +00:00
|
|
|
template <typename UidType>
|
|
|
|
class VertexShaderCacheInserter : public LinearDiskCacheReader<UidType, u8>
|
2010-11-15 05:22:03 +00:00
|
|
|
{
|
2010-06-13 19:50:06 +00:00
|
|
|
public:
|
2017-07-20 05:25:31 +00:00
|
|
|
void Read(const UidType& key, const u8* value, u32 value_size)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
D3DBlob* blob = new D3DBlob(value_size, value);
|
|
|
|
VertexShaderCache::InsertByteCode(key, blob);
|
|
|
|
blob->Release();
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char simple_shader_code[] = {
|
2016-06-24 08:43:46 +00:00
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
"float4 vPosition : POSITION;\n"
|
|
|
|
"float3 vTexCoord : TEXCOORD0;\n"
|
|
|
|
"};\n"
|
2018-05-02 11:58:56 +00:00
|
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float3 inTEX0 : TEXCOORD0)\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
"{\n"
|
|
|
|
"VSOUTPUT OUT;\n"
|
|
|
|
"OUT.vPosition = inPosition;\n"
|
2018-05-02 11:58:56 +00:00
|
|
|
"OUT.vTexCoord = inTEX0;\n"
|
2016-06-24 08:43:46 +00:00
|
|
|
"return OUT;\n"
|
|
|
|
"}\n"};
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
const char clear_shader_code[] = {
|
2016-06-24 08:43:46 +00:00
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
"float4 vPosition : POSITION;\n"
|
|
|
|
"float4 vColor0 : COLOR0;\n"
|
|
|
|
"};\n"
|
|
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float4 inColor0: COLOR0)\n"
|
|
|
|
"{\n"
|
|
|
|
"VSOUTPUT OUT;\n"
|
|
|
|
"OUT.vPosition = inPosition;\n"
|
|
|
|
"OUT.vColor0 = inColor0;\n"
|
|
|
|
"return OUT;\n"
|
|
|
|
"}\n"};
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
void VertexShaderCache::Init()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const D3D11_INPUT_ELEMENT_DESC simpleelems[2] = {
|
|
|
|
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
|
|
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
|
|
|
|
|
|
};
|
|
|
|
const D3D11_INPUT_ELEMENT_DESC clearelems[2] = {
|
|
|
|
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
|
|
{"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
|
|
};
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
unsigned int cbsize = Common::AlignUp(static_cast<unsigned int>(sizeof(VertexShaderConstants)),
|
|
|
|
16); // must be a multiple of 16
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER,
|
|
|
|
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
|
|
|
HRESULT hr = D3D::device->CreateBuffer(&cbdesc, nullptr, &vscbuf);
|
|
|
|
CHECK(hr == S_OK, "Create vertex shader constant buffer (size=%u)", cbsize);
|
2017-09-02 18:22:18 +00:00
|
|
|
D3D::SetDebugObjectName(vscbuf, "vertex shader constant buffer used to emulate the GX pipeline");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
D3DBlob* blob;
|
|
|
|
D3D::CompileVertexShader(simple_shader_code, &blob);
|
|
|
|
D3D::device->CreateInputLayout(simpleelems, 2, blob->Data(), blob->Size(), &SimpleLayout);
|
|
|
|
SimpleVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
|
|
if (SimpleLayout == nullptr || SimpleVertexShader == nullptr)
|
|
|
|
PanicAlert("Failed to create simple vertex shader or input layout at %s %d\n", __FILE__,
|
|
|
|
__LINE__);
|
|
|
|
blob->Release();
|
2017-09-02 18:22:18 +00:00
|
|
|
D3D::SetDebugObjectName(SimpleVertexShader, "simple vertex shader");
|
|
|
|
D3D::SetDebugObjectName(SimpleLayout, "simple input layout");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
D3D::CompileVertexShader(clear_shader_code, &blob);
|
|
|
|
D3D::device->CreateInputLayout(clearelems, 2, blob->Data(), blob->Size(), &ClearLayout);
|
|
|
|
ClearVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
|
|
if (ClearLayout == nullptr || ClearVertexShader == nullptr)
|
|
|
|
PanicAlert("Failed to create clear vertex shader or input layout at %s %d\n", __FILE__,
|
|
|
|
__LINE__);
|
|
|
|
blob->Release();
|
2017-09-02 18:22:18 +00:00
|
|
|
D3D::SetDebugObjectName(ClearVertexShader, "clear vertex shader");
|
|
|
|
D3D::SetDebugObjectName(ClearLayout, "clear input layout");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
SETSTAT(stats.numVertexShadersCreated, 0);
|
|
|
|
SETSTAT(stats.numVertexShadersAlive, 0);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Shutdown()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(vscbuf);
|
2011-01-25 15:08:30 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(SimpleVertexShader);
|
|
|
|
SAFE_RELEASE(ClearVertexShader);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(SimpleLayout);
|
|
|
|
SAFE_RELEASE(ClearLayout);
|
2017-07-20 05:25:31 +00:00
|
|
|
}
|
2011-01-31 01:28:32 +00:00
|
|
|
} // namespace DX11
|