2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-12-03 21:17:56 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2016-11-27 10:56:22 +00:00
|
|
|
#include "Common/Align.h"
|
2014-12-03 21:17:56 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/LinearDiskCache.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
2014-12-21 14:37:05 +00:00
|
|
|
#include "VideoBackends/D3D/FramebufferManager.h"
|
2014-12-03 21:17:56 +00:00
|
|
|
#include "VideoBackends/D3D/GeometryShaderCache.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/Debugger.h"
|
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
2014-12-14 20:23:13 +00:00
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-12-03 21:17:56 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
GeometryShaderCache::GSCache GeometryShaderCache::GeometryShaders;
|
|
|
|
const GeometryShaderCache::GSCacheEntry* GeometryShaderCache::last_entry;
|
|
|
|
GeometryShaderUid GeometryShaderCache::last_uid;
|
2014-12-16 16:28:35 +00:00
|
|
|
const GeometryShaderCache::GSCacheEntry GeometryShaderCache::pass_entry;
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2014-11-23 01:13:25 +00:00
|
|
|
ID3D11GeometryShader* ClearGeometryShader = nullptr;
|
2014-11-29 20:31:30 +00:00
|
|
|
ID3D11GeometryShader* CopyGeometryShader = nullptr;
|
2014-11-23 01:13:25 +00:00
|
|
|
|
2014-12-03 21:17:56 +00:00
|
|
|
LinearDiskCache<GeometryShaderUid, u8> g_gs_disk_cache;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11GeometryShader* GeometryShaderCache::GetClearGeometryShader()
|
|
|
|
{
|
|
|
|
return (g_ActiveConfig.iStereoMode > 0) ? ClearGeometryShader : nullptr;
|
|
|
|
}
|
|
|
|
ID3D11GeometryShader* GeometryShaderCache::GetCopyGeometryShader()
|
|
|
|
{
|
|
|
|
return (g_ActiveConfig.iStereoMode > 0) ? CopyGeometryShader : nullptr;
|
|
|
|
}
|
2014-11-23 01:13:25 +00:00
|
|
|
|
2014-12-03 21:17:56 +00:00
|
|
|
ID3D11Buffer* gscbuf = nullptr;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11Buffer*& GeometryShaderCache::GetConstantBuffer()
|
2014-12-14 20:23:13 +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 (GeometryShaderManager::dirty)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
D3D::context->Map(gscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
memcpy(map.pData, &GeometryShaderManager::constants, sizeof(GeometryShaderConstants));
|
|
|
|
D3D::context->Unmap(gscbuf, 0);
|
|
|
|
GeometryShaderManager::dirty = false;
|
|
|
|
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(GeometryShaderConstants));
|
|
|
|
}
|
|
|
|
return gscbuf;
|
2014-12-14 20:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 21:17:56 +00:00
|
|
|
// this class will load the precompiled shaders into our cache
|
|
|
|
class GeometryShaderCacheInserter : public LinearDiskCacheReader<GeometryShaderUid, u8>
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
void Read(const GeometryShaderUid& key, const u8* value, u32 value_size)
|
|
|
|
{
|
|
|
|
GeometryShaderCache::InsertByteCode(key, value, value_size);
|
|
|
|
}
|
2014-12-03 21:17:56 +00:00
|
|
|
};
|
|
|
|
|
2014-11-23 01:13:25 +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"
|
|
|
|
"struct GSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 vPosition : POSITION;\n"
|
|
|
|
" float4 vColor0 : COLOR0;\n"
|
|
|
|
" uint slice : SV_RenderTargetArrayIndex;\n"
|
|
|
|
"};\n"
|
|
|
|
"[maxvertexcount(6)]\n"
|
|
|
|
"void main(triangle VSOUTPUT o[3], inout TriangleStream<GSOUTPUT> Output)\n"
|
|
|
|
"{\n"
|
|
|
|
"for(int slice = 0; slice < 2; slice++)\n"
|
|
|
|
"{\n"
|
|
|
|
" for(int i = 0; i < 3; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" GSOUTPUT OUT;\n"
|
|
|
|
" OUT.vPosition = o[i].vPosition;\n"
|
|
|
|
" OUT.vColor0 = o[i].vColor0;\n"
|
|
|
|
" OUT.slice = slice;\n"
|
|
|
|
" Output.Append(OUT);\n"
|
|
|
|
" }\n"
|
|
|
|
" Output.RestartStrip();\n"
|
|
|
|
"}\n"
|
|
|
|
"}\n"};
|
2014-11-23 01:13:25 +00:00
|
|
|
|
2014-11-29 20:31:30 +00:00
|
|
|
const char copy_shader_code[] = {
|
2016-06-24 08:43:46 +00:00
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 vPosition : POSITION;\n"
|
|
|
|
" float3 vTexCoord : TEXCOORD0;\n"
|
|
|
|
" float vTexCoord1 : TEXCOORD1;\n"
|
|
|
|
"};\n"
|
|
|
|
"struct GSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
" float4 vPosition : POSITION;\n"
|
|
|
|
" float3 vTexCoord : TEXCOORD0;\n"
|
|
|
|
" float vTexCoord1 : TEXCOORD1;\n"
|
|
|
|
" uint slice : SV_RenderTargetArrayIndex;\n"
|
|
|
|
"};\n"
|
|
|
|
"[maxvertexcount(6)]\n"
|
|
|
|
"void main(triangle VSOUTPUT o[3], inout TriangleStream<GSOUTPUT> Output)\n"
|
|
|
|
"{\n"
|
|
|
|
"for(int slice = 0; slice < 2; slice++)\n"
|
|
|
|
"{\n"
|
|
|
|
" for(int i = 0; i < 3; i++)\n"
|
|
|
|
" {\n"
|
|
|
|
" GSOUTPUT OUT;\n"
|
|
|
|
" OUT.vPosition = o[i].vPosition;\n"
|
|
|
|
" OUT.vTexCoord = o[i].vTexCoord;\n"
|
|
|
|
" OUT.vTexCoord.z = slice;\n"
|
|
|
|
" OUT.vTexCoord1 = o[i].vTexCoord1;\n"
|
|
|
|
" OUT.slice = slice;\n"
|
|
|
|
" Output.Append(OUT);\n"
|
|
|
|
" }\n"
|
|
|
|
" Output.RestartStrip();\n"
|
|
|
|
"}\n"
|
|
|
|
"}\n"};
|
2014-11-29 20:31:30 +00:00
|
|
|
|
2014-12-03 21:17:56 +00:00
|
|
|
void GeometryShaderCache::Init()
|
|
|
|
{
|
2016-11-27 10:56:22 +00:00
|
|
|
unsigned int gbsize = Common::AlignUp(static_cast<unsigned int>(sizeof(GeometryShaderConstants)),
|
|
|
|
16); // must be a multiple of 16
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D11_BUFFER_DESC gbdesc = CD3D11_BUFFER_DESC(gbsize, D3D11_BIND_CONSTANT_BUFFER,
|
|
|
|
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
|
|
|
HRESULT hr = D3D::device->CreateBuffer(&gbdesc, nullptr, &gscbuf);
|
|
|
|
CHECK(hr == S_OK, "Create geometry shader constant buffer (size=%u)", gbsize);
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)gscbuf,
|
|
|
|
"geometry shader constant buffer used to emulate the GX pipeline");
|
|
|
|
|
|
|
|
// used when drawing clear quads
|
|
|
|
ClearGeometryShader = D3D::CompileAndCreateGeometryShader(clear_shader_code);
|
|
|
|
CHECK(ClearGeometryShader != nullptr, "Create clear geometry shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearGeometryShader, "clear geometry shader");
|
|
|
|
|
|
|
|
// used for buffer copy
|
|
|
|
CopyGeometryShader = D3D::CompileAndCreateGeometryShader(copy_shader_code);
|
|
|
|
CHECK(CopyGeometryShader != nullptr, "Create copy geometry shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)CopyGeometryShader, "copy geometry shader");
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
2017-02-19 11:02:47 +00:00
|
|
|
if (g_ActiveConfig.bShaderCache)
|
2017-06-24 09:29:03 +00:00
|
|
|
LoadShaderCache();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-24 09:29:03 +00:00
|
|
|
void GeometryShaderCache::LoadShaderCache()
|
|
|
|
{
|
|
|
|
GeometryShaderCacheInserter inserter;
|
2017-07-19 10:35:17 +00:00
|
|
|
g_gs_disk_cache.OpenAndRead(g_ActiveConfig.GetDiskCacheFileName(APIType::D3D, "GS", true, true),
|
|
|
|
inserter);
|
2017-06-24 09:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeometryShaderCache::Reload()
|
|
|
|
{
|
|
|
|
g_gs_disk_cache.Sync();
|
|
|
|
g_gs_disk_cache.Close();
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
if (g_ActiveConfig.bShaderCache)
|
|
|
|
LoadShaderCache();
|
2014-12-03 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ONLY to be used during shutdown.
|
|
|
|
void GeometryShaderCache::Clear()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (auto& iter : GeometryShaders)
|
|
|
|
iter.second.Destroy();
|
|
|
|
GeometryShaders.clear();
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
last_entry = nullptr;
|
2017-06-24 09:29:03 +00:00
|
|
|
last_uid = {};
|
2014-12-03 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GeometryShaderCache::Shutdown()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(gscbuf);
|
2014-12-14 20:23:13 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(ClearGeometryShader);
|
|
|
|
SAFE_RELEASE(CopyGeometryShader);
|
2014-11-23 01:13:25 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Clear();
|
|
|
|
g_gs_disk_cache.Sync();
|
|
|
|
g_gs_disk_cache.Close();
|
2014-12-03 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
2014-12-14 18:41:16 +00:00
|
|
|
bool GeometryShaderCache::SetShader(u32 primitive_type)
|
2014-12-03 21:17:56 +00:00
|
|
|
{
|
2016-01-16 10:49:39 +00:00
|
|
|
GeometryShaderUid uid = GetGeometryShaderUid(primitive_type);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Check if the shader is already set
|
|
|
|
if (last_entry)
|
|
|
|
{
|
|
|
|
if (uid == last_uid)
|
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_uid = uid;
|
|
|
|
|
|
|
|
// Check if the shader is a pass-through shader
|
|
|
|
if (uid.GetUidData()->IsPassthrough())
|
|
|
|
{
|
|
|
|
// Return the default pass-through shader
|
|
|
|
last_entry = &pass_entry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the shader is already in the cache
|
|
|
|
GSCache::iterator iter;
|
|
|
|
iter = GeometryShaders.find(uid);
|
|
|
|
if (iter != GeometryShaders.end())
|
|
|
|
{
|
|
|
|
const GSCacheEntry& entry = iter->second;
|
|
|
|
last_entry = &entry;
|
|
|
|
|
|
|
|
return (entry.shader != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to compile a new shader
|
2016-07-21 23:04:57 +00:00
|
|
|
ShaderCode code = GenerateGeometryShaderCode(APIType::D3D, uid.GetUidData());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
D3DBlob* pbytecode;
|
|
|
|
if (!D3D::CompileGeometryShader(code.GetBuffer(), &pbytecode))
|
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the bytecode into the caches
|
|
|
|
g_gs_disk_cache.Append(uid, pbytecode->Data(), pbytecode->Size());
|
|
|
|
|
|
|
|
bool success = InsertByteCode(uid, pbytecode->Data(), pbytecode->Size());
|
|
|
|
pbytecode->Release();
|
|
|
|
|
|
|
|
return success;
|
2014-12-03 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
bool GeometryShaderCache::InsertByteCode(const GeometryShaderUid& uid, const void* bytecode,
|
|
|
|
unsigned int bytecodelen)
|
2014-12-03 21:17:56 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
ID3D11GeometryShader* shader = D3D::CreateGeometryShaderFromByteCode(bytecode, bytecodelen);
|
|
|
|
if (shader == nullptr)
|
|
|
|
return false;
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: Somehow make the debug name a bit more specific
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)shader, "a pixel shader of GeometryShaderCache");
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Make an entry in the table
|
|
|
|
GSCacheEntry newentry;
|
|
|
|
newentry.shader = shader;
|
|
|
|
GeometryShaders[uid] = newentry;
|
|
|
|
last_entry = &GeometryShaders[uid];
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!shader)
|
|
|
|
return false;
|
2014-12-03 21:17:56 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return true;
|
2014-12-03 21:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // DX11
|