2016-03-04 23:02:44 +00:00
|
|
|
#include "hecl/Runtime.hpp"
|
|
|
|
#include <athena/FileReader.hpp>
|
|
|
|
#include <athena/FileWriter.hpp>
|
2015-11-13 02:12:09 +00:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <algorithm>
|
2015-11-14 06:39:27 +00:00
|
|
|
#include <ctime>
|
2015-11-13 02:12:09 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
#include "hecl/Backend/GLSL.hpp"
|
|
|
|
#include "hecl/Backend/Metal.hpp"
|
2015-11-16 04:30:06 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
namespace hecl
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
|
|
|
namespace Runtime
|
|
|
|
{
|
2017-12-06 03:22:31 +00:00
|
|
|
#if BOO_HAS_GL
|
2016-03-30 19:15:08 +00:00
|
|
|
IShaderBackendFactory* _NewGLSLBackendFactory();
|
2017-12-06 03:22:31 +00:00
|
|
|
#endif
|
2015-11-18 23:56:45 +00:00
|
|
|
#if _WIN32
|
2016-03-30 19:15:08 +00:00
|
|
|
IShaderBackendFactory* _NewHLSLBackendFactory();
|
2016-02-23 02:33:29 +00:00
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
2016-03-30 19:15:08 +00:00
|
|
|
IShaderBackendFactory* _NewMetalBackendFactory();
|
2015-11-18 23:56:45 +00:00
|
|
|
#endif
|
2016-02-23 02:33:29 +00:00
|
|
|
#if BOO_HAS_VULKAN
|
2016-03-30 19:15:08 +00:00
|
|
|
IShaderBackendFactory* _NewSPIRVBackendFactory();
|
2016-02-23 02:33:29 +00:00
|
|
|
#endif
|
2015-11-16 04:30:06 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
static logvisor::Module Log("ShaderCacheManager");
|
2015-11-16 22:04:13 +00:00
|
|
|
static uint64_t IDX_MAGIC = SBig(uint64_t(0xDEADFEEDC001D00D));
|
|
|
|
static uint64_t DAT_MAGIC = SBig(uint64_t(0xC001D00DDEADBABE));
|
2015-11-13 02:12:09 +00:00
|
|
|
static uint64_t ZERO64 = 0;
|
|
|
|
|
2015-11-14 06:39:27 +00:00
|
|
|
static uint64_t timeHash()
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
2015-11-14 06:39:27 +00:00
|
|
|
char buf[80];
|
|
|
|
time_t now;
|
|
|
|
struct tm* timeinfo;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
timeinfo = localtime(&now);
|
|
|
|
strftime(buf, 80, "%Y-%m-%dT%H:%M:%S+%H:%M", timeinfo);
|
|
|
|
Hash tmp(buf, 80);
|
|
|
|
return tmp.val64();
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
static void UpdateFunctionHash(XXH64_state_t* st, const ShaderCacheExtensions::Function& func)
|
|
|
|
{
|
|
|
|
if (func.m_source)
|
|
|
|
XXH64_update(st, func.m_source, strlen(func.m_source));
|
|
|
|
if (func.m_entry)
|
|
|
|
XXH64_update(st, func.m_entry, strlen(func.m_entry));
|
|
|
|
}
|
2017-03-17 23:32:18 +00:00
|
|
|
template<typename T>
|
|
|
|
static void UpdateFieldHash(XXH64_state_t* st, T field)
|
|
|
|
{
|
|
|
|
XXH64_update(st, &field, sizeof(field));
|
|
|
|
}
|
2015-11-16 04:30:06 +00:00
|
|
|
uint64_t ShaderCacheExtensions::hashExtensions() const
|
|
|
|
{
|
|
|
|
XXH64_state_t st;
|
|
|
|
XXH64_reset(&st, 0);
|
|
|
|
for (const ExtensionSlot& slot : m_extensionSlots)
|
|
|
|
{
|
|
|
|
UpdateFunctionHash(&st, slot.lighting);
|
|
|
|
UpdateFunctionHash(&st, slot.post);
|
2017-03-17 23:32:18 +00:00
|
|
|
UpdateFieldHash(&st, slot.srcFactor);
|
|
|
|
UpdateFieldHash(&st, slot.dstFactor);
|
|
|
|
UpdateFieldHash(&st, slot.depthTest);
|
2017-12-10 05:29:23 +00:00
|
|
|
UpdateFieldHash(&st, slot.cullMode);
|
2017-03-17 23:32:18 +00:00
|
|
|
UpdateFieldHash(&st, slot.noDepthWrite);
|
|
|
|
UpdateFieldHash(&st, slot.noColorWrite);
|
|
|
|
UpdateFieldHash(&st, slot.noAlphaWrite);
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
return XXH64_digest(&st);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderCacheManager::bootstrapIndex()
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
2015-11-14 06:39:27 +00:00
|
|
|
m_timeHash = timeHash();
|
2015-11-13 02:12:09 +00:00
|
|
|
m_idxFr.close();
|
|
|
|
m_datFr.close();
|
|
|
|
|
2016-02-18 12:55:39 +00:00
|
|
|
#if _WIN32
|
2016-02-18 20:52:36 +00:00
|
|
|
SystemString idxFilename = m_idxFr.wfilename();
|
2016-02-18 12:55:39 +00:00
|
|
|
#else
|
2016-02-18 20:52:36 +00:00
|
|
|
SystemString idxFilename = m_idxFr.filename();
|
2016-02-18 12:55:39 +00:00
|
|
|
#endif
|
2015-11-14 06:39:27 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
FILE* idxFp = hecl::Fopen(idxFilename.c_str(), _S("wb"));
|
2015-11-13 02:12:09 +00:00
|
|
|
if (!idxFp)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, _S("unable to write shader cache index at %s"),
|
2015-11-14 06:39:27 +00:00
|
|
|
idxFilename.c_str());
|
2015-11-13 02:12:09 +00:00
|
|
|
fwrite(&IDX_MAGIC, 1, 8, idxFp);
|
2015-11-14 06:39:27 +00:00
|
|
|
fwrite(&m_timeHash, 1, 8, idxFp);
|
2015-11-16 04:30:06 +00:00
|
|
|
fwrite(&m_extensionsHash, 1, 8, idxFp);
|
2015-11-13 02:12:09 +00:00
|
|
|
fwrite(&ZERO64, 1, 8, idxFp);
|
|
|
|
fclose(idxFp);
|
2016-02-18 12:55:39 +00:00
|
|
|
#if _WIN32
|
2016-02-18 20:52:36 +00:00
|
|
|
SystemString datFilename = m_datFr.wfilename();
|
2016-02-18 12:55:39 +00:00
|
|
|
#else
|
2016-02-18 20:52:36 +00:00
|
|
|
SystemString datFilename = m_datFr.filename();
|
2016-02-18 12:55:39 +00:00
|
|
|
#endif
|
2015-11-14 08:48:31 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
FILE* datFp = hecl::Fopen(datFilename.c_str(), _S("wb"));
|
2015-11-13 02:12:09 +00:00
|
|
|
if (!datFp)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, _S("unable to write shader cache data at %s"),
|
2016-02-18 20:52:36 +00:00
|
|
|
datFilename.c_str());
|
2015-11-13 02:12:09 +00:00
|
|
|
fwrite(&DAT_MAGIC, 1, 8, datFp);
|
2015-11-14 06:39:27 +00:00
|
|
|
fwrite(&m_timeHash, 1, 8, datFp);
|
2015-11-13 02:12:09 +00:00
|
|
|
fclose(datFp);
|
|
|
|
|
|
|
|
m_idxFr.open();
|
|
|
|
m_datFr.open();
|
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCacheManager::ShaderCacheManager(const FileStoreManager& storeMgr,
|
|
|
|
boo::IGraphicsDataFactory* gfxFactory,
|
|
|
|
ShaderCacheExtensions&& extension)
|
|
|
|
: m_storeMgr(storeMgr),
|
|
|
|
m_extensions(std::move(extension)),
|
2017-11-13 06:13:53 +00:00
|
|
|
m_idxFr(SystemString(storeMgr.getStoreRoot()) + _S("/shadercache") +
|
|
|
|
gfxFactory->platformName() + _S(".idx"), 32*1024, false),
|
|
|
|
m_datFr(SystemString(storeMgr.getStoreRoot()) + _S("/shadercache") +
|
|
|
|
gfxFactory->platformName() + _S(".dat"), 32*1024, false)
|
2016-02-18 20:52:36 +00:00
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
boo::IGraphicsDataFactory::Platform plat = gfxFactory->platform();
|
|
|
|
if (m_extensions && m_extensions.m_plat != plat)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "ShaderCacheExtension backend mismatch (should be %s)",
|
2015-11-16 04:30:06 +00:00
|
|
|
gfxFactory->platformName());
|
|
|
|
m_extensionsHash = m_extensions.hashExtensions();
|
|
|
|
|
|
|
|
switch (plat)
|
|
|
|
{
|
2017-12-06 03:22:31 +00:00
|
|
|
#if BOO_HAS_GL
|
2016-08-24 04:34:26 +00:00
|
|
|
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
2016-03-30 19:15:08 +00:00
|
|
|
m_factory.reset(_NewGLSLBackendFactory());
|
2015-11-16 04:30:06 +00:00
|
|
|
break;
|
2017-12-06 03:22:31 +00:00
|
|
|
#endif
|
2015-11-18 23:56:45 +00:00
|
|
|
#if _WIN32
|
2015-11-21 02:59:43 +00:00
|
|
|
case boo::IGraphicsDataFactory::Platform::D3D11:
|
|
|
|
case boo::IGraphicsDataFactory::Platform::D3D12:
|
2016-03-30 19:15:08 +00:00
|
|
|
m_factory.reset(_NewHLSLBackendFactory());
|
2015-11-18 06:16:08 +00:00
|
|
|
break;
|
2016-02-23 02:33:29 +00:00
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
2015-11-21 02:16:54 +00:00
|
|
|
case boo::IGraphicsDataFactory::Platform::Metal:
|
2016-03-30 19:15:08 +00:00
|
|
|
m_factory.reset(_NewMetalBackendFactory());
|
2015-11-18 23:56:45 +00:00
|
|
|
break;
|
2016-02-23 02:33:29 +00:00
|
|
|
#endif
|
|
|
|
#if BOO_HAS_VULKAN
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
2016-03-30 19:15:08 +00:00
|
|
|
m_factory.reset(_NewSPIRVBackendFactory());
|
2016-02-23 02:33:29 +00:00
|
|
|
break;
|
2015-11-18 23:56:45 +00:00
|
|
|
#endif
|
2015-11-16 04:30:06 +00:00
|
|
|
default:
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, _S("unsupported backend %s"), gfxFactory->platformName());
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reload();
|
|
|
|
}
|
|
|
|
|
2015-11-13 02:12:09 +00:00
|
|
|
void ShaderCacheManager::reload()
|
|
|
|
{
|
|
|
|
m_entries.clear();
|
|
|
|
m_entryLookup.clear();
|
2015-11-14 06:39:27 +00:00
|
|
|
m_timeHash = 0;
|
2015-11-13 02:12:09 +00:00
|
|
|
|
|
|
|
/* Attempt to open existing index */
|
2016-03-04 23:02:44 +00:00
|
|
|
m_idxFr.seek(0, athena::Begin);
|
|
|
|
m_datFr.seek(0, athena::Begin);
|
2015-11-13 02:12:09 +00:00
|
|
|
if (m_idxFr.hasError() || m_datFr.hasError())
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
bootstrapIndex();
|
2015-11-13 02:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint64_t idxMagic;
|
|
|
|
size_t rb = m_idxFr.readUBytesToBuf(&idxMagic, 8);
|
|
|
|
if (rb != 8 || idxMagic != IDX_MAGIC)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
bootstrapIndex();
|
2015-11-13 02:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t datMagic;
|
|
|
|
rb = m_datFr.readUBytesToBuf(&datMagic, 8);
|
|
|
|
if (rb != 8 || datMagic != DAT_MAGIC)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
bootstrapIndex();
|
2015-11-13 02:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t idxRand, datRand;
|
|
|
|
rb = m_idxFr.readUBytesToBuf(&idxRand, 8);
|
|
|
|
size_t rb2 = m_datFr.readUBytesToBuf(&datRand, 8);
|
|
|
|
if (rb != 8 || rb2 != 8 || idxRand != datRand)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
bootstrapIndex();
|
2015-11-13 02:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-14 06:39:27 +00:00
|
|
|
m_timeHash = idxRand;
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
atUint64 extensionsHash;
|
|
|
|
size_t rb = m_idxFr.readUBytesToBuf(&extensionsHash, 8);
|
|
|
|
if (rb != 8 || extensionsHash != m_extensionsHash)
|
|
|
|
{
|
|
|
|
bootstrapIndex();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-13 02:12:09 +00:00
|
|
|
atUint64 idxCount = m_idxFr.readUint64Big();
|
2015-11-16 04:30:06 +00:00
|
|
|
if (m_idxFr.position() != 32)
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
bootstrapIndex();
|
2015-11-13 02:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
/* Read existing entries */
|
|
|
|
if (idxCount)
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
m_entries.reserve(idxCount);
|
|
|
|
m_entryLookup.reserve(idxCount);
|
|
|
|
for (atUint64 i=0 ; i<idxCount ; ++i)
|
|
|
|
{
|
|
|
|
m_entries.emplace_back();
|
|
|
|
IndexEntry& ent = m_entries.back();
|
|
|
|
ent.read(m_idxFr);
|
|
|
|
m_entryLookup[ent.m_hash] = m_entries.size() - 1;
|
|
|
|
}
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData ShaderCacheManager::lookupData(const Hash& hash)
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
|
|
|
auto search = m_entryLookup.find(hash);
|
|
|
|
if (search == m_entryLookup.cend())
|
2016-07-27 03:38:25 +00:00
|
|
|
return {};
|
2015-11-13 02:12:09 +00:00
|
|
|
|
|
|
|
const IndexEntry& ent = m_entries[search->second];
|
|
|
|
if (ent.m_compOffset + ent.m_compSize > m_datFr.length())
|
|
|
|
{
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Warning, "shader cache not long enough to read entry, might be corrupt");
|
2016-07-27 03:38:25 +00:00
|
|
|
return {};
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File-streamed decompression */
|
2016-03-04 23:02:44 +00:00
|
|
|
m_datFr.seek(ent.m_compOffset, athena::Begin);
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData ret(ShaderTag(ent.m_hash, ent.m_meta), ent.m_decompSize);
|
2015-11-13 02:12:09 +00:00
|
|
|
uint8_t compDat[2048];
|
|
|
|
z_stream z = {};
|
|
|
|
inflateInit(&z);
|
|
|
|
z.avail_out = ent.m_decompSize;
|
|
|
|
z.next_out = ret.m_data.get();
|
|
|
|
while (z.avail_out)
|
|
|
|
{
|
|
|
|
z.avail_in = std::min(size_t(2048), size_t(ent.m_compSize - z.total_in));
|
|
|
|
m_datFr.readUBytesToBuf(compDat, z.avail_in);
|
|
|
|
z.next_in = compDat;
|
2016-07-27 03:38:25 +00:00
|
|
|
int ret = inflate(&z, Z_NO_FLUSH);
|
2017-02-03 06:22:54 +00:00
|
|
|
if (ret == Z_STREAM_END)
|
|
|
|
break;
|
2016-07-27 03:38:25 +00:00
|
|
|
if (ret != Z_OK)
|
|
|
|
{
|
|
|
|
inflateEnd(&z);
|
|
|
|
return {};
|
|
|
|
}
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
inflateEnd(&z);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-16 04:30:06 +00:00
|
|
|
bool ShaderCacheManager::addData(const ShaderCachedData& data)
|
2015-11-13 02:12:09 +00:00
|
|
|
{
|
|
|
|
m_idxFr.close();
|
|
|
|
m_datFr.close();
|
|
|
|
|
|
|
|
/* Perform one-shot buffer compression */
|
2015-11-16 04:30:06 +00:00
|
|
|
uLong cBound = compressBound(data.m_sz);
|
2015-11-13 02:12:09 +00:00
|
|
|
void* compBuf = malloc(cBound);
|
2015-11-16 04:30:06 +00:00
|
|
|
if (compress((Bytef*)compBuf, &cBound, (Bytef*)data.m_data.get(), data.m_sz) != Z_OK)
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to deflate data");
|
2015-11-13 02:12:09 +00:00
|
|
|
|
|
|
|
/* Open index for writing (non overwriting) */
|
2016-03-04 23:02:44 +00:00
|
|
|
athena::io::FileWriter idxFw(m_idxFr.filename(), false);
|
2015-11-13 02:12:09 +00:00
|
|
|
if (idxFw.hasError())
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, _S("unable to append shader cache index at %s"),
|
2015-11-13 02:12:09 +00:00
|
|
|
m_idxFr.filename().c_str());
|
|
|
|
|
|
|
|
/* Open data for writing (non overwriting) */
|
2016-03-04 23:02:44 +00:00
|
|
|
athena::io::FileWriter datFw(m_datFr.filename(), false);
|
2015-11-13 02:12:09 +00:00
|
|
|
if (datFw.hasError())
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, _S("unable to append shader cache data at %s"),
|
2015-11-13 02:12:09 +00:00
|
|
|
m_datFr.filename().c_str());
|
|
|
|
|
|
|
|
size_t targetOffset = 0;
|
2015-11-16 04:30:06 +00:00
|
|
|
auto search = m_entryLookup.find(data.m_tag);
|
2015-11-13 02:12:09 +00:00
|
|
|
if (search != m_entryLookup.cend())
|
|
|
|
{
|
|
|
|
/* Hash already present, attempt to replace data */
|
|
|
|
IndexEntry& ent = m_entries[search->second];
|
|
|
|
if (search->second == m_entries.size() - 1)
|
|
|
|
{
|
|
|
|
/* Replacing final entry; simply write-over */
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_meta = data.m_tag.getMetaData();
|
2015-11-13 02:12:09 +00:00
|
|
|
ent.m_compSize = cBound;
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_decompSize = data.m_sz;
|
2015-11-13 02:12:09 +00:00
|
|
|
targetOffset = ent.m_compOffset;
|
|
|
|
idxFw.seek(search->second * 32 + 32);
|
|
|
|
ent.write(idxFw);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Replacing non-final entry; write into available space */
|
|
|
|
IndexEntry& nent = m_entries[search->second+1];
|
|
|
|
size_t space = nent.m_compOffset - ent.m_compOffset;
|
|
|
|
if (cBound <= space)
|
|
|
|
{
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_meta = data.m_tag.getMetaData();
|
2015-11-13 02:12:09 +00:00
|
|
|
ent.m_compSize = cBound;
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_decompSize = data.m_sz;
|
2015-11-13 02:12:09 +00:00
|
|
|
targetOffset = ent.m_compOffset;
|
|
|
|
idxFw.seek(search->second * 32 + 32);
|
|
|
|
ent.write(idxFw);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not enough space; null-entry and add to end */
|
|
|
|
ent.m_hash = 0;
|
|
|
|
ent.m_meta = 0;
|
|
|
|
ent.m_compOffset = 0;
|
|
|
|
ent.m_compSize = 0;
|
|
|
|
ent.m_decompSize = 0;
|
|
|
|
idxFw.seek(search->second * 32 + 32);
|
|
|
|
ent.write(idxFw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!targetOffset)
|
|
|
|
{
|
|
|
|
/* New index entry at end */
|
2016-03-04 23:02:44 +00:00
|
|
|
idxFw.seek(24, athena::Begin);
|
2015-11-13 02:12:09 +00:00
|
|
|
idxFw.writeUint64Big(m_entries.size() + 1);
|
2016-03-04 23:02:44 +00:00
|
|
|
idxFw.seek(m_entries.size() * 32 + 32, athena::Begin);
|
|
|
|
datFw.seek(0, athena::End);
|
2015-11-16 04:30:06 +00:00
|
|
|
m_entryLookup[data.m_tag] = m_entries.size();
|
2015-11-13 02:12:09 +00:00
|
|
|
m_entries.emplace_back();
|
|
|
|
|
|
|
|
IndexEntry& ent = m_entries.back();
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_hash = data.m_tag.val64();
|
|
|
|
ent.m_meta = data.m_tag.getMetaData();
|
2015-11-13 02:12:09 +00:00
|
|
|
ent.m_compOffset = datFw.position();
|
|
|
|
ent.m_compSize = cBound;
|
2015-11-16 04:30:06 +00:00
|
|
|
ent.m_decompSize = data.m_sz;
|
2015-11-13 02:12:09 +00:00
|
|
|
ent.write(idxFw);
|
|
|
|
|
|
|
|
datFw.writeUBytes((atUint8*)compBuf, cBound);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Reusing index entry and data space */
|
2016-03-04 23:02:44 +00:00
|
|
|
datFw.seek(targetOffset, athena::Begin);
|
2015-11-13 02:12:09 +00:00
|
|
|
datFw.writeUBytes((atUint8*)compBuf, cBound);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(compBuf);
|
|
|
|
|
|
|
|
idxFw.close();
|
|
|
|
datFw.close();
|
|
|
|
|
|
|
|
m_idxFr.open();
|
|
|
|
m_datFr.open();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline>
|
2016-03-30 19:15:08 +00:00
|
|
|
ShaderCacheManager::buildFromCache(const ShaderCachedData& foundData,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2016-03-30 19:15:08 +00:00
|
|
|
return m_factory->buildShaderFromCache(foundData, ctx);
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 04:52:55 +00:00
|
|
|
std::shared_ptr<ShaderPipelines>
|
2017-11-13 06:13:53 +00:00
|
|
|
ShaderCacheManager::buildShader(const ShaderTag& tag, std::string_view source,
|
|
|
|
std::string_view diagName,
|
2016-09-12 04:52:55 +00:00
|
|
|
boo::IGraphicsDataFactory& factory)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2016-09-12 04:52:55 +00:00
|
|
|
auto search = m_pipelineLookup.find(tag);
|
|
|
|
if (search != m_pipelineLookup.cend())
|
2017-10-22 06:10:59 +00:00
|
|
|
return search->second;
|
2016-09-12 04:52:55 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ShaderPipelines> ret = std::make_shared<ShaderPipelines>();
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData foundData = lookupData(tag);
|
|
|
|
if (foundData)
|
2016-08-08 18:22:32 +00:00
|
|
|
{
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building cached shader '%s' %016llX", diagName.data(), tag.val64());
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> build = buildFromCache(foundData, ctx);
|
2016-09-12 04:52:55 +00:00
|
|
|
if (build)
|
|
|
|
{
|
|
|
|
ret->m_pipelines.push_back(build);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
if (ret->m_pipelines.size())
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
m_pipelineLookup[tag] = ret;
|
2016-08-08 18:22:32 +00:00
|
|
|
return ret;
|
2016-09-12 04:52:55 +00:00
|
|
|
}
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Warning, "invalid cache read, rebuilding shader '%s'", diagName.data());
|
2016-08-08 18:22:32 +00:00
|
|
|
}
|
2016-09-12 04:52:55 +00:00
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
hecl::Frontend::IR ir = FE.compileSource(source, diagName);
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
FE.getDiagnostics().reset(diagName);
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> build;
|
2016-09-12 04:52:55 +00:00
|
|
|
addData(m_factory->buildShaderFromIR(tag, ir, FE.getDiagnostics(), ctx, build));
|
|
|
|
ret->m_pipelines.push_back(build);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
m_pipelineLookup[tag] = ret;
|
|
|
|
return ret;
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 04:52:55 +00:00
|
|
|
std::shared_ptr<ShaderPipelines>
|
2016-03-04 23:02:44 +00:00
|
|
|
ShaderCacheManager::buildShader(const ShaderTag& tag, const hecl::Frontend::IR& ir,
|
2017-11-13 06:13:53 +00:00
|
|
|
std::string_view diagName,
|
2016-09-12 04:52:55 +00:00
|
|
|
boo::IGraphicsDataFactory& factory)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2016-09-12 04:52:55 +00:00
|
|
|
auto search = m_pipelineLookup.find(tag);
|
|
|
|
if (search != m_pipelineLookup.cend())
|
2017-10-22 06:10:59 +00:00
|
|
|
return search->second;
|
2016-09-12 04:52:55 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ShaderPipelines> ret = std::make_shared<ShaderPipelines>();
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData foundData = lookupData(tag);
|
|
|
|
if (foundData)
|
2016-08-08 18:22:32 +00:00
|
|
|
{
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building cached shader '%s' %016llX", diagName.data(), tag.val64());
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> build = buildFromCache(foundData, ctx);
|
2016-09-12 04:52:55 +00:00
|
|
|
if (build)
|
|
|
|
{
|
|
|
|
ret->m_pipelines.push_back(build);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
if (ret->m_pipelines.size())
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
m_pipelineLookup[tag] = ret;
|
2016-08-08 18:22:32 +00:00
|
|
|
return ret;
|
2016-09-12 04:52:55 +00:00
|
|
|
}
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Warning, "invalid cache read, rebuilding shader '%s'", diagName.data());
|
2016-08-08 18:22:32 +00:00
|
|
|
}
|
2016-09-12 04:52:55 +00:00
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
FE.getDiagnostics().reset(diagName);
|
2017-11-05 06:15:03 +00:00
|
|
|
boo::ObjToken<boo::IShaderPipeline> build;
|
2016-09-12 04:52:55 +00:00
|
|
|
addData(m_factory->buildShaderFromIR(tag, ir, FE.getDiagnostics(), ctx, build));
|
|
|
|
ret->m_pipelines.push_back(build);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
m_pipelineLookup[tag] = ret;
|
2015-11-16 04:30:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
std::vector<boo::ObjToken<boo::IShaderPipeline>>
|
2016-03-30 19:15:08 +00:00
|
|
|
ShaderCacheManager::buildExtendedFromCache(const ShaderCachedData& foundData,
|
|
|
|
boo::IGraphicsDataFactory::Context& ctx)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2017-11-05 06:15:03 +00:00
|
|
|
std::vector<boo::ObjToken<boo::IShaderPipeline>> shaders;
|
2015-11-16 04:30:06 +00:00
|
|
|
shaders.reserve(m_extensions.m_extensionSlots.size());
|
2016-08-08 18:22:32 +00:00
|
|
|
if (!m_factory->buildExtendedShaderFromCache(foundData, m_extensions.m_extensionSlots, ctx,
|
2017-11-05 06:15:03 +00:00
|
|
|
[&](const boo::ObjToken<boo::IShaderPipeline>& shader){shaders.push_back(shader);}))
|
2016-08-08 18:22:32 +00:00
|
|
|
return {};
|
2015-11-16 04:30:06 +00:00
|
|
|
if (shaders.size() != m_extensions.m_extensionSlots.size())
|
2016-03-04 23:02:44 +00:00
|
|
|
Log.report(logvisor::Fatal, "buildShaderFromCache returned %" PRISize " times, expected %" PRISize,
|
2015-11-16 04:30:06 +00:00
|
|
|
shaders.size(), m_extensions.m_extensionSlots.size());
|
|
|
|
return shaders;
|
|
|
|
}
|
|
|
|
|
2016-09-12 04:52:55 +00:00
|
|
|
std::shared_ptr<ShaderPipelines>
|
2017-11-13 06:13:53 +00:00
|
|
|
ShaderCacheManager::buildExtendedShader(const ShaderTag& tag, std::string_view source,
|
|
|
|
std::string_view diagName,
|
2016-09-12 04:52:55 +00:00
|
|
|
boo::IGraphicsDataFactory& factory)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2016-09-12 04:52:55 +00:00
|
|
|
auto search = m_pipelineLookup.find(tag);
|
|
|
|
if (search != m_pipelineLookup.cend())
|
2017-10-22 06:10:59 +00:00
|
|
|
return search->second;
|
2016-09-12 04:52:55 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ShaderPipelines> ret = std::make_shared<ShaderPipelines>();
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData foundData = lookupData(tag);
|
|
|
|
if (foundData)
|
2016-08-08 18:22:32 +00:00
|
|
|
{
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building cached shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
ret->m_pipelines = buildExtendedFromCache(foundData, ctx);
|
2017-11-05 06:15:03 +00:00
|
|
|
return ret->m_pipelines.size() != 0;
|
2016-09-12 04:52:55 +00:00
|
|
|
});
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
if (ret->m_pipelines.size())
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
m_pipelineLookup[tag] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Warning, "invalid cache read, rebuilding shader '%s'", diagName.data());
|
2016-08-08 18:22:32 +00:00
|
|
|
}
|
2016-09-12 04:52:55 +00:00
|
|
|
|
2016-03-04 23:02:44 +00:00
|
|
|
hecl::Frontend::IR ir = FE.compileSource(source, diagName);
|
2016-09-12 04:52:55 +00:00
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
ret->m_pipelines.reserve(m_extensions.m_extensionSlots.size());
|
|
|
|
FE.getDiagnostics().reset(diagName);
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
ShaderCachedData data =
|
|
|
|
m_factory->buildExtendedShaderFromIR(tag, ir, FE.getDiagnostics(), m_extensions.m_extensionSlots, ctx,
|
2017-11-05 06:15:03 +00:00
|
|
|
[&](const boo::ObjToken<boo::IShaderPipeline>& shader){ret->m_pipelines.push_back(shader);});
|
2016-09-12 04:52:55 +00:00
|
|
|
if (ret->m_pipelines.size() != m_extensions.m_extensionSlots.size())
|
|
|
|
Log.report(logvisor::Fatal, "buildShaderFromIR returned %" PRISize " times, expected %" PRISize,
|
|
|
|
ret->m_pipelines.size(), m_extensions.m_extensionSlots.size());
|
|
|
|
addData(data);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
m_pipelineLookup[tag] = ret;
|
|
|
|
return ret;
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 04:52:55 +00:00
|
|
|
std::shared_ptr<ShaderPipelines>
|
2016-03-04 23:02:44 +00:00
|
|
|
ShaderCacheManager::buildExtendedShader(const ShaderTag& tag, const hecl::Frontend::IR& ir,
|
2017-11-13 06:13:53 +00:00
|
|
|
std::string_view diagName,
|
2016-09-12 04:52:55 +00:00
|
|
|
boo::IGraphicsDataFactory& factory)
|
2015-11-16 04:30:06 +00:00
|
|
|
{
|
2016-09-12 04:52:55 +00:00
|
|
|
auto search = m_pipelineLookup.find(tag);
|
|
|
|
if (search != m_pipelineLookup.cend())
|
2017-10-22 06:10:59 +00:00
|
|
|
return search->second;
|
2016-09-12 04:52:55 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ShaderPipelines> ret = std::make_shared<ShaderPipelines>();
|
2015-11-16 04:30:06 +00:00
|
|
|
ShaderCachedData foundData = lookupData(tag);
|
2017-11-21 10:22:36 +00:00
|
|
|
if (foundData)
|
2016-08-08 18:22:32 +00:00
|
|
|
{
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building cached shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
ret->m_pipelines = buildExtendedFromCache(foundData, ctx);
|
2017-11-05 06:15:03 +00:00
|
|
|
return ret->m_pipelines.size() != 0;
|
2016-09-12 04:52:55 +00:00
|
|
|
});
|
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
if (ret->m_pipelines.size() != 0)
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
m_pipelineLookup[tag] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Warning, "invalid cache read, rebuilding shader '%s'", diagName.data());
|
2016-08-08 18:22:32 +00:00
|
|
|
}
|
2016-09-12 04:52:55 +00:00
|
|
|
|
2017-11-05 06:15:03 +00:00
|
|
|
factory.commitTransaction([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
|
2016-09-12 04:52:55 +00:00
|
|
|
{
|
|
|
|
ret->m_pipelines.reserve(m_extensions.m_extensionSlots.size());
|
|
|
|
FE.getDiagnostics().reset(diagName);
|
2017-11-13 06:13:53 +00:00
|
|
|
Log.report(logvisor::Info, "building shader '%s' %016llX", diagName.data(), tag.val64());
|
2016-09-12 04:52:55 +00:00
|
|
|
ShaderCachedData data =
|
|
|
|
m_factory->buildExtendedShaderFromIR(tag, ir, FE.getDiagnostics(), m_extensions.m_extensionSlots, ctx,
|
2017-11-05 06:15:03 +00:00
|
|
|
[&](const boo::ObjToken<boo::IShaderPipeline>& shader){ret->m_pipelines.push_back(shader);});
|
2016-09-12 04:52:55 +00:00
|
|
|
if (ret->m_pipelines.size() != m_extensions.m_extensionSlots.size())
|
|
|
|
Log.report(logvisor::Fatal, "buildShaderFromIR returned %" PRISize " times, expected %" PRISize,
|
|
|
|
ret->m_pipelines.size(), m_extensions.m_extensionSlots.size());
|
|
|
|
addData(data);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
m_pipelineLookup[tag] = ret;
|
|
|
|
return ret;
|
2015-11-16 04:30:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 02:12:09 +00:00
|
|
|
}
|
|
|
|
}
|