[dxvk] Only create state cache file on demand

If no pipelines are ever written to it, we should not create
the cache file in the first place.
This commit is contained in:
Philip Rebohle 2023-01-09 18:29:15 +01:00
parent 6a4fe06ac6
commit 06fb93daf0
2 changed files with 62 additions and 29 deletions

View File

@ -242,26 +242,7 @@ namespace dxvk {
bool newFile = (useStateCache == "reset") || (!readCacheFile());
if (newFile) {
Logger::warn("DXVK: Creating new state cache file");
// Start with an empty file
std::ofstream file(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::trunc);
if (!file && env::createDirectory(getCacheDir())) {
file = std::ofstream(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::trunc);
}
// Write header with the current version number
DxvkStateCacheHeader header;
auto data = reinterpret_cast<const char*>(&header);
auto size = sizeof(header);
file.write(data, size);
auto file = openCacheFileForWrite(true);
// Write all valid entries to the cache file in
// case we're recovering a corrupted cache file
@ -454,12 +435,13 @@ namespace dxvk {
bool DxvkStateCache::readCacheFile() {
// Open state file and just fail if it doesn't exist
std::ifstream ifile(getCacheFileName().c_str(), std::ios_base::binary);
// Return success if the file was not found.
// This way we will only create it on demand.
std::ifstream ifile = openCacheFileForRead();
if (!ifile) {
Logger::warn("DXVK: No state cache file found");
return false;
return true;
}
// The header stores the state cache version,
@ -771,11 +753,8 @@ namespace dxvk {
m_writerQueue.pop();
}
if (!file.is_open()) {
file.open(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::app);
}
if (!file.is_open())
file = openCacheFileForWrite(false);
writeCacheEntry(file, entry);
}
@ -806,6 +785,55 @@ namespace dxvk {
}
std::ifstream DxvkStateCache::openCacheFileForRead() const {
return std::ifstream(getCacheFileName().c_str(), std::ios_base::binary);
}
std::ofstream DxvkStateCache::openCacheFileForWrite(bool recreate) const {
std::ofstream file;
if (!recreate) {
// Apparently there's no other way to check whether
// the file is empty after creating an ofstream
recreate = !openCacheFileForRead();
}
if (recreate) {
file = std::ofstream(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::trunc);
if (!file && env::createDirectory(getCacheDir())) {
file = std::ofstream(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::trunc);
}
} else {
file = std::ofstream(getCacheFileName().c_str(),
std::ios_base::binary |
std::ios_base::app);
}
if (!file)
return file;
if (recreate) {
Logger::warn("DXVK: Creating new state cache file");
// Write header with the current version number
DxvkStateCacheHeader header;
auto data = reinterpret_cast<const char*>(&header);
auto size = sizeof(header);
file.write(data, size);
}
return file;
}
std::string DxvkStateCache::getCacheDir() const {
return env::getEnvVar("DXVK_STATE_CACHE_PATH");
}

View File

@ -160,7 +160,12 @@ namespace dxvk {
void createWriter();
str::path_string getCacheFileName() const;
std::ifstream openCacheFileForRead() const;
std::ofstream openCacheFileForWrite(
bool recreate) const;
std::string getCacheDir() const;
};