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
|
|
|
|
2014-06-02 23:08:54 -06:00
|
|
|
#include <string>
|
|
|
|
|
2016-11-27 03:56:22 -07:00
|
|
|
#include "Common/Align.h"
|
2016-01-02 13:01:12 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/LinearDiskCache.h"
|
2017-07-19 23:25:31 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2017-07-19 23:25:31 -06:00
|
|
|
#include "Core/Host.h"
|
2011-01-25 09:43:08 -07:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
2017-07-19 23:25:31 -06:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
|
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2017-07-19 23:25:31 -06:00
|
|
|
#include "VideoCommon/UberShaderVertex.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2011-01-30 18:28:32 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
namespace DX11
|
|
|
|
{
|
2010-06-13 13:50:06 -06:00
|
|
|
VertexShaderCache::VSCache VertexShaderCache::vshaders;
|
2017-07-19 23:25:31 -06:00
|
|
|
VertexShaderCache::UberVSCache VertexShaderCache::ubervshaders;
|
2016-06-24 02:43:46 -06:00
|
|
|
const VertexShaderCache::VSCacheEntry* VertexShaderCache::last_entry;
|
2017-07-19 23:25:31 -06:00
|
|
|
const VertexShaderCache::VSCacheEntry* VertexShaderCache::last_uber_entry;
|
2013-03-26 16:35:14 -06:00
|
|
|
VertexShaderUid VertexShaderCache::last_uid;
|
2017-07-19 23:25:31 -06:00
|
|
|
UberShader::VertexShaderUid VertexShaderCache::last_uber_uid;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
static ID3D11VertexShader* SimpleVertexShader = nullptr;
|
|
|
|
static ID3D11VertexShader* ClearVertexShader = nullptr;
|
|
|
|
static ID3D11InputLayout* SimpleLayout = nullptr;
|
|
|
|
static ID3D11InputLayout* ClearLayout = nullptr;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2013-03-26 16:35:14 -06:00
|
|
|
LinearDiskCache<VertexShaderUid, u8> g_vs_disk_cache;
|
2017-07-19 23:25:31 -06:00
|
|
|
LinearDiskCache<UberShader::VertexShaderUid, u8> g_uber_vs_disk_cache;
|
|
|
|
std::unique_ptr<VideoCommon::AsyncShaderCompiler> g_async_compiler;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11VertexShader* VertexShaderCache::GetSimpleVertexShader()
|
|
|
|
{
|
|
|
|
return SimpleVertexShader;
|
|
|
|
}
|
|
|
|
ID3D11VertexShader* VertexShaderCache::GetClearVertexShader()
|
|
|
|
{
|
|
|
|
return ClearVertexShader;
|
|
|
|
}
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetSimpleInputLayout()
|
|
|
|
{
|
|
|
|
return SimpleLayout;
|
|
|
|
}
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetClearInputLayout()
|
|
|
|
{
|
|
|
|
return ClearLayout;
|
|
|
|
}
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2014-03-09 14:14:26 -06:00
|
|
|
ID3D11Buffer* vscbuf = nullptr;
|
2011-01-25 08:08:30 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ID3D11Buffer*& VertexShaderCache::GetConstantBuffer()
|
2011-01-25 08:08:30 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06: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 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// this class will load the precompiled shaders into our cache
|
2017-07-19 23:25:31 -06:00
|
|
|
template <typename UidType>
|
|
|
|
class VertexShaderCacheInserter : public LinearDiskCacheReader<UidType, u8>
|
2010-11-14 22:22:03 -07:00
|
|
|
{
|
2010-06-13 13:50:06 -06:00
|
|
|
public:
|
2017-07-19 23:25:31 -06:00
|
|
|
void Read(const UidType& key, const u8* value, u32 value_size)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
D3DBlob* blob = new D3DBlob(value_size, value);
|
|
|
|
VertexShaderCache::InsertByteCode(key, blob);
|
|
|
|
blob->Release();
|
|
|
|
}
|
2010-06-13 13:50:06 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
const char simple_shader_code[] = {
|
2016-06-24 02:43:46 -06:00
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
"float4 vPosition : POSITION;\n"
|
|
|
|
"float3 vTexCoord : TEXCOORD0;\n"
|
|
|
|
"float vTexCoord1 : TEXCOORD1;\n"
|
|
|
|
"};\n"
|
|
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float4 inTEX0 : TEXCOORD0)\n"
|
|
|
|
"{\n"
|
|
|
|
"VSOUTPUT OUT;\n"
|
|
|
|
"OUT.vPosition = inPosition;\n"
|
|
|
|
"OUT.vTexCoord = inTEX0.xyz;\n"
|
|
|
|
"OUT.vTexCoord1 = inTEX0.w;\n"
|
|
|
|
"return OUT;\n"
|
|
|
|
"}\n"};
|
2010-06-13 13:50:06 -06:00
|
|
|
|
|
|
|
const char clear_shader_code[] = {
|
2016-06-24 02:43:46 -06: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 13:50:06 -06:00
|
|
|
|
|
|
|
void VertexShaderCache::Init()
|
|
|
|
{
|
2016-06-24 02:43:46 -06: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 03:56:22 -07:00
|
|
|
unsigned int cbsize = Common::AlignUp(static_cast<unsigned int>(sizeof(VertexShaderConstants)),
|
|
|
|
16); // must be a multiple of 16
|
2016-06-24 02:43:46 -06: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);
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)vscbuf,
|
|
|
|
"vertex shader constant buffer used to emulate the GX pipeline");
|
|
|
|
|
|
|
|
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();
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleVertexShader, "simple vertex shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleLayout, "simple input layout");
|
|
|
|
|
|
|
|
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();
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearVertexShader, "clear vertex shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearLayout, "clear input layout");
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
SETSTAT(stats.numVertexShadersCreated, 0);
|
|
|
|
SETSTAT(stats.numVertexShadersAlive, 0);
|
|
|
|
|
2017-02-19 04:02:47 -07:00
|
|
|
if (g_ActiveConfig.bShaderCache)
|
2017-06-24 03:29:03 -06:00
|
|
|
LoadShaderCache();
|
2017-07-19 23:25:31 -06:00
|
|
|
|
|
|
|
g_async_compiler = std::make_unique<VideoCommon::AsyncShaderCompiler>();
|
2017-07-26 21:15:38 -06:00
|
|
|
g_async_compiler->ResizeWorkerThreads(g_ActiveConfig.CanPrecompileUberShaders() ?
|
|
|
|
g_ActiveConfig.GetShaderPrecompilerThreads() :
|
|
|
|
g_ActiveConfig.GetShaderCompilerThreads());
|
2017-07-19 23:25:31 -06:00
|
|
|
|
|
|
|
if (g_ActiveConfig.CanPrecompileUberShaders())
|
2017-07-26 21:15:38 -06:00
|
|
|
QueueUberShaderCompiles();
|
2017-06-24 03:29:03 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-24 03:29:03 -06:00
|
|
|
void VertexShaderCache::LoadShaderCache()
|
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
VertexShaderCacheInserter<VertexShaderUid> inserter;
|
2017-07-20 01:10:02 -06:00
|
|
|
g_vs_disk_cache.OpenAndRead(GetDiskShaderCacheFileName(APIType::D3D, "VS", true, true), inserter);
|
2017-07-19 23:25:31 -06:00
|
|
|
|
|
|
|
VertexShaderCacheInserter<UberShader::VertexShaderUid> uber_inserter;
|
|
|
|
g_uber_vs_disk_cache.OpenAndRead(GetDiskShaderCacheFileName(APIType::D3D, "UberVS", false, true),
|
|
|
|
uber_inserter);
|
2017-06-24 03:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Reload()
|
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
g_async_compiler->WaitUntilCompletion();
|
|
|
|
g_async_compiler->RetrieveWorkItems();
|
|
|
|
|
2017-06-24 03:29:03 -06:00
|
|
|
g_vs_disk_cache.Sync();
|
|
|
|
g_vs_disk_cache.Close();
|
2017-07-19 23:25:31 -06:00
|
|
|
g_uber_vs_disk_cache.Sync();
|
|
|
|
g_uber_vs_disk_cache.Close();
|
2017-06-24 03:29:03 -06:00
|
|
|
Clear();
|
|
|
|
|
|
|
|
if (g_ActiveConfig.bShaderCache)
|
|
|
|
LoadShaderCache();
|
2017-07-19 23:25:31 -06:00
|
|
|
|
|
|
|
if (g_ActiveConfig.CanPrecompileUberShaders())
|
2017-07-26 21:15:38 -06:00
|
|
|
QueueUberShaderCompiles();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Clear()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& iter : vshaders)
|
|
|
|
iter.second.Destroy();
|
2017-07-19 23:25:31 -06:00
|
|
|
for (auto& iter : ubervshaders)
|
|
|
|
iter.second.Destroy();
|
2016-06-24 02:43:46 -06:00
|
|
|
vshaders.clear();
|
2017-07-19 23:25:31 -06:00
|
|
|
ubervshaders.clear();
|
2011-09-29 14:54:52 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
last_uid = {};
|
|
|
|
last_uber_uid = {};
|
2016-06-24 02:43:46 -06:00
|
|
|
last_entry = nullptr;
|
2017-07-19 23:25:31 -06:00
|
|
|
last_uber_entry = nullptr;
|
2017-06-24 03:29:03 -06:00
|
|
|
last_uid = {};
|
2017-07-19 23:25:31 -06:00
|
|
|
last_uber_uid = {};
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Shutdown()
|
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
g_async_compiler->StopWorkerThreads();
|
|
|
|
g_async_compiler->RetrieveWorkItems();
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
SAFE_RELEASE(vscbuf);
|
2011-01-25 08:08:30 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
SAFE_RELEASE(SimpleVertexShader);
|
|
|
|
SAFE_RELEASE(ClearVertexShader);
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
SAFE_RELEASE(SimpleLayout);
|
|
|
|
SAFE_RELEASE(ClearLayout);
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
Clear();
|
|
|
|
g_vs_disk_cache.Sync();
|
|
|
|
g_vs_disk_cache.Close();
|
2017-07-19 23:25:31 -06:00
|
|
|
g_uber_vs_disk_cache.Sync();
|
|
|
|
g_uber_vs_disk_cache.Close();
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
bool VertexShaderCache::SetShader(D3DVertexFormat* vertex_format)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-07-31 07:21:38 -06:00
|
|
|
if (g_ActiveConfig.bDisableSpecializedShaders)
|
2017-07-19 23:25:31 -06:00
|
|
|
return SetUberShader(vertex_format);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
VertexShaderUid uid = GetVertexShaderUid();
|
|
|
|
if (last_entry && uid == last_uid)
|
|
|
|
{
|
|
|
|
if (last_entry->pending)
|
|
|
|
return SetUberShader(vertex_format);
|
|
|
|
|
|
|
|
if (!last_entry->shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vertex_format->SetInputLayout(last_entry->bytecode);
|
|
|
|
D3D::stateman->SetVertexShader(last_entry->shader);
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
auto iter = vshaders.find(uid);
|
2016-06-24 02:43:46 -06:00
|
|
|
if (iter != vshaders.end())
|
|
|
|
{
|
|
|
|
const VSCacheEntry& entry = iter->second;
|
2017-07-19 23:25:31 -06:00
|
|
|
if (entry.pending)
|
|
|
|
return SetUberShader(vertex_format);
|
|
|
|
|
|
|
|
last_uid = uid;
|
2016-06-24 02:43:46 -06:00
|
|
|
last_entry = &entry;
|
|
|
|
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true);
|
2017-07-19 23:25:31 -06:00
|
|
|
if (!last_entry->shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vertex_format->SetInputLayout(last_entry->bytecode);
|
|
|
|
D3D::stateman->SetVertexShader(last_entry->shader);
|
|
|
|
return true;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
// Background compiling?
|
|
|
|
if (g_ActiveConfig.CanBackgroundCompileShaders())
|
|
|
|
{
|
|
|
|
// Create a pending entry
|
|
|
|
VSCacheEntry entry;
|
|
|
|
entry.pending = true;
|
|
|
|
vshaders[uid] = entry;
|
|
|
|
|
|
|
|
// Queue normal shader compiling and use ubershader
|
|
|
|
g_async_compiler->QueueWorkItem(
|
|
|
|
g_async_compiler->CreateWorkItem<VertexShaderCompilerWorkItem>(uid));
|
|
|
|
return SetUberShader(vertex_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to compile a new shader
|
|
|
|
D3DBlob* bytecode = nullptr;
|
2017-07-20 01:10:02 -06:00
|
|
|
ShaderCode code =
|
|
|
|
GenerateVertexShaderCode(APIType::D3D, ShaderHostConfig::GetCurrent(), uid.GetUidData());
|
2017-07-19 23:25:31 -06:00
|
|
|
D3D::CompileVertexShader(code.GetBuffer(), &bytecode);
|
|
|
|
if (!InsertByteCode(uid, bytecode))
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(bytecode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_vs_disk_cache.Append(uid, bytecode->Data(), bytecode->Size());
|
|
|
|
bytecode->Release();
|
|
|
|
return SetShader(vertex_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::SetUberShader(D3DVertexFormat* vertex_format)
|
|
|
|
{
|
|
|
|
D3DVertexFormat* uber_vertex_format = static_cast<D3DVertexFormat*>(
|
|
|
|
VertexLoaderManager::GetUberVertexFormat(vertex_format->GetVertexDeclaration()));
|
|
|
|
UberShader::VertexShaderUid uid = UberShader::GetVertexShaderUid();
|
|
|
|
if (last_uber_entry && last_uber_uid == uid)
|
|
|
|
{
|
|
|
|
if (!last_uber_entry->shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uber_vertex_format->SetInputLayout(last_uber_entry->bytecode);
|
|
|
|
D3D::stateman->SetVertexShader(last_uber_entry->shader);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = ubervshaders.find(uid);
|
|
|
|
if (iter != ubervshaders.end())
|
|
|
|
{
|
|
|
|
const VSCacheEntry& entry = iter->second;
|
|
|
|
last_uber_uid = uid;
|
|
|
|
last_uber_entry = &entry;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true);
|
|
|
|
if (!last_uber_entry->shader)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uber_vertex_format->SetInputLayout(last_uber_entry->bytecode);
|
|
|
|
D3D::stateman->SetVertexShader(last_uber_entry->shader);
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
// Need to compile a new shader
|
|
|
|
D3DBlob* bytecode = nullptr;
|
|
|
|
ShaderCode code =
|
|
|
|
UberShader::GenVertexShader(APIType::D3D, ShaderHostConfig::GetCurrent(), uid.GetUidData());
|
|
|
|
D3D::CompileVertexShader(code.GetBuffer(), &bytecode);
|
|
|
|
if (!InsertByteCode(uid, bytecode))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
SAFE_RELEASE(bytecode);
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
g_uber_vs_disk_cache.Append(uid, bytecode->Data(), bytecode->Size());
|
|
|
|
bytecode->Release();
|
|
|
|
return SetUberShader(vertex_format);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
bool VertexShaderCache::InsertByteCode(const VertexShaderUid& uid, D3DBlob* blob)
|
|
|
|
{
|
|
|
|
ID3D11VertexShader* shader =
|
|
|
|
blob ? D3D::CreateVertexShaderFromByteCode(blob->Data(), blob->Size()) : nullptr;
|
|
|
|
bool result = InsertShader(uid, shader, blob);
|
|
|
|
SAFE_RELEASE(shader);
|
|
|
|
return result;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
bool VertexShaderCache::InsertByteCode(const UberShader::VertexShaderUid& uid, D3DBlob* blob)
|
2010-06-13 13:50:06 -06:00
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
ID3D11VertexShader* shader =
|
|
|
|
blob ? D3D::CreateVertexShaderFromByteCode(blob->Data(), blob->Size()) : nullptr;
|
|
|
|
bool result = InsertShader(uid, shader, blob);
|
|
|
|
SAFE_RELEASE(shader);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::InsertShader(const VertexShaderUid& uid, ID3D11VertexShader* shader,
|
|
|
|
D3DBlob* blob)
|
|
|
|
{
|
|
|
|
auto iter = vshaders.find(uid);
|
|
|
|
if (iter != vshaders.end() && !iter->second.pending)
|
2016-06-24 02:43:46 -06:00
|
|
|
return false;
|
2011-09-08 14:59:34 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
VSCacheEntry& newentry = vshaders[uid];
|
|
|
|
newentry.pending = false;
|
|
|
|
if (!shader || !blob)
|
|
|
|
return false;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
shader->AddRef();
|
|
|
|
newentry.SetByteCode(blob);
|
|
|
|
newentry.shader = shader;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
INCSTAT(stats.numPixelShadersCreated);
|
|
|
|
SETSTAT(stats.numPixelShadersAlive, static_cast<int>(vshaders.size()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::InsertShader(const UberShader::VertexShaderUid& uid,
|
|
|
|
ID3D11VertexShader* shader, D3DBlob* blob)
|
|
|
|
{
|
|
|
|
auto iter = ubervshaders.find(uid);
|
|
|
|
if (iter != ubervshaders.end() && !iter->second.pending)
|
|
|
|
return false;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
VSCacheEntry& newentry = ubervshaders[uid];
|
|
|
|
newentry.pending = false;
|
|
|
|
if (!shader || !blob)
|
|
|
|
return false;
|
2010-06-13 13:50:06 -06:00
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
shader->AddRef();
|
|
|
|
newentry.SetByteCode(blob);
|
|
|
|
newentry.shader = shader;
|
2016-06-24 02:43:46 -06:00
|
|
|
return true;
|
2010-06-13 13:50:06 -06:00
|
|
|
}
|
|
|
|
|
2017-07-19 23:25:31 -06:00
|
|
|
void VertexShaderCache::RetreiveAsyncShaders()
|
|
|
|
{
|
|
|
|
g_async_compiler->RetrieveWorkItems();
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:15:38 -06:00
|
|
|
void VertexShaderCache::QueueUberShaderCompiles()
|
2017-07-19 23:25:31 -06:00
|
|
|
{
|
|
|
|
UberShader::EnumerateVertexShaderUids([&](const UberShader::VertexShaderUid& uid) {
|
|
|
|
if (ubervshaders.find(uid) != ubervshaders.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_async_compiler->QueueWorkItem(
|
|
|
|
g_async_compiler->CreateWorkItem<UberVertexShaderCompilerWorkItem>(uid));
|
|
|
|
});
|
2017-07-26 21:15:38 -06:00
|
|
|
}
|
2017-07-19 23:25:31 -06:00
|
|
|
|
2017-07-26 21:15:38 -06:00
|
|
|
void VertexShaderCache::WaitForBackgroundCompilesToComplete()
|
|
|
|
{
|
2017-07-19 23:25:31 -06:00
|
|
|
g_async_compiler->WaitUntilCompletion([](size_t completed, size_t total) {
|
|
|
|
Host_UpdateProgressDialog(GetStringT("Compiling shaders...").c_str(),
|
|
|
|
static_cast<int>(completed), static_cast<int>(total));
|
|
|
|
});
|
|
|
|
g_async_compiler->RetrieveWorkItems();
|
|
|
|
Host_UpdateProgressDialog("", -1, -1);
|
2017-07-26 21:15:38 -06:00
|
|
|
|
|
|
|
// Switch from precompile -> runtime compiler threads.
|
|
|
|
g_async_compiler->ResizeWorkerThreads(g_ActiveConfig.GetShaderCompilerThreads());
|
2017-07-19 23:25:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexShaderCache::VertexShaderCompilerWorkItem::VertexShaderCompilerWorkItem(
|
|
|
|
const VertexShaderUid& uid)
|
|
|
|
{
|
|
|
|
std::memcpy(&m_uid, &uid, sizeof(uid));
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexShaderCache::VertexShaderCompilerWorkItem::~VertexShaderCompilerWorkItem()
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(m_bytecode);
|
|
|
|
SAFE_RELEASE(m_vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::VertexShaderCompilerWorkItem::Compile()
|
|
|
|
{
|
|
|
|
ShaderCode code =
|
|
|
|
GenerateVertexShaderCode(APIType::D3D, ShaderHostConfig::GetCurrent(), m_uid.GetUidData());
|
|
|
|
|
|
|
|
if (D3D::CompileVertexShader(code.GetBuffer(), &m_bytecode))
|
|
|
|
m_vs = D3D::CreateVertexShaderFromByteCode(m_bytecode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::VertexShaderCompilerWorkItem::Retrieve()
|
|
|
|
{
|
|
|
|
if (InsertShader(m_uid, m_vs, m_bytecode))
|
|
|
|
g_vs_disk_cache.Append(m_uid, m_bytecode->Data(), m_bytecode->Size());
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexShaderCache::UberVertexShaderCompilerWorkItem::UberVertexShaderCompilerWorkItem(
|
|
|
|
const UberShader::VertexShaderUid& uid)
|
|
|
|
{
|
|
|
|
std::memcpy(&m_uid, &uid, sizeof(uid));
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexShaderCache::UberVertexShaderCompilerWorkItem::~UberVertexShaderCompilerWorkItem()
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(m_bytecode);
|
|
|
|
SAFE_RELEASE(m_vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::UberVertexShaderCompilerWorkItem::Compile()
|
|
|
|
{
|
|
|
|
ShaderCode code =
|
|
|
|
UberShader::GenVertexShader(APIType::D3D, ShaderHostConfig::GetCurrent(), m_uid.GetUidData());
|
|
|
|
|
|
|
|
if (D3D::CompileVertexShader(code.GetBuffer(), &m_bytecode))
|
|
|
|
m_vs = D3D::CreateVertexShaderFromByteCode(m_bytecode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::UberVertexShaderCompilerWorkItem::Retrieve()
|
|
|
|
{
|
|
|
|
if (InsertShader(m_uid, m_vs, m_bytecode))
|
|
|
|
g_uber_vs_disk_cache.Append(m_uid, m_bytecode->Data(), m_bytecode->Size());
|
|
|
|
}
|
|
|
|
|
2011-01-30 18:28:32 -07:00
|
|
|
} // namespace DX11
|