dx caching result

This commit is contained in:
marauder2k7 2026-06-05 14:52:30 +01:00
parent 4cf780e7b8
commit dacc4db708
2 changed files with 65 additions and 19 deletions

View file

@ -27,24 +27,6 @@
//-----------------------------------------------------------------------------
// FileStream methods...
//-----------------------------------------------------------------------------
static void writeBufferToFile(
Torque::FS::FileRef& file,
U32 buffHead,
U32 buffTail,
const U8* buffer)
{
if (!file || buffHead == FileStream::BUFFER_INVALID)
return;
// match FileStream::flush logic
if (buffHead != file->getPosition())
file->setPosition(buffHead, Torque::FS::File::Begin);
U32 blockHead;
FileStream::calcBlockHead(buffHead, &blockHead);
file->write((char*)buffer + (buffHead - blockHead), buffTail - buffHead + 1);
}
struct FileCloseWorkItem : public ThreadPool::WorkItem
{

View file

@ -681,6 +681,24 @@ bool GFXD3D11Shader::_init()
return true;
}
static String buildMacroHash(const D3D_SHADER_MACRO* defines)
{
String combined;
if (!defines)
return "";
for (const D3D_SHADER_MACRO* m = defines; m->Name != nullptr; ++m)
{
combined += m->Name;
combined += "=";
combined += m->Definition ? m->Definition : "";
combined += ";";
}
return Torque::getStringHash64(combined);
}
bool GFXD3D11Shader::_compileShader( const Torque::Path &filePath,
GFXShaderStage shaderStage,
const D3D_SHADER_MACRO *defines)
@ -706,8 +724,40 @@ bool GFXD3D11Shader::_compileShader( const Torque::Path &filePath,
Con::printf( "Compiling Shader: '%s'", filePath.getFullPath().c_str() );
#endif
String macroHash = buildMacroHash(defines);
Torque::Path cachePath = filePath;
cachePath.setExtension("tso");
cachePath.setFileName(cachePath.getFileName() + "_" + macroHash);
if (Torque::FS::IsFile(cachePath))
{
Torque::FS::FileNodeRef rawFile = Torque::FS::GetFileNode(filePath);
Torque::FS::FileNodeRef cachedFile = Torque::FS::GetFileNode(cachePath);
if (rawFile != NULL && cachedFile != NULL)
{
if (cachedFile->getModifiedTime() >= rawFile->getModifiedTime())
{
FileStream fs;
if (fs.open(cachePath, Torque::FS::File::Read))
{
U32 size = fs.getStreamSize();
D3DCreateBlob(size, &code);
fs.read(size, code->GetBufferPointer());
res = 1;
}
}
}
}
bool loadedFromCache = (code != NULL);
// Is it an HLSL shader?
if(filePath.getExtension().equal("hlsl", String::NoCase))
if(filePath.getExtension().equal("hlsl", String::NoCase) && !loadedFromCache)
{
// Set this so that the D3DInclude::Open will have this
// information for relative paths.
@ -788,6 +838,20 @@ bool GFXD3D11Shader::_compileShader( const Torque::Path &filePath,
AssertISV(SUCCEEDED(res), "Unable to compile shader!");
// succeeded write out a cache
if (!loadedFromCache)
{
if (SUCCEEDED(res) && code)
{
// Save cache
FileStream out(FileStream::AsyncMode::Background);
if (out.open(cachePath, Torque::FS::File::Write))
{
out.write(code->GetBufferSize(), code->GetBufferPointer());
}
}
}
if(code != NULL)
{
switch (shaderStage)