Skip to content

Commit

Permalink
[dxvk] Check if the whole file is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
K0bin committed Aug 25, 2018
1 parent 6465555 commit 83daf00
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions src/dxvk/dxvk_state_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ namespace dxvk {
fileStream.write(reinterpret_cast<const char*>(&hash), sizeof(hash));

key.write(fileStream);
Logger::debug("DxvkStateCache: Wrote pipeline to hash");
}
}
}
Expand Down Expand Up @@ -192,58 +193,70 @@ namespace dxvk {
Rc<DxvkGraphicsPipelineInstance> instance = new DxvkGraphicsPipelineInstance(
m_device->vkd(), curr->stateVector(), *renderPass, VK_NULL_HANDLE);

Logger::info("Compiling from state cache");
Logger::info("DxvkStateCache: Compiling from state cache");
m_pipeCompiler->queueCompilation(pipeline, instance);
}
}
}


bool DxvkStateCache::readCacheFile() {
std::ifstream fileStream(getFilePath(), std::ios_base::binary);

if (!fileStream)
return false;

DxvkStateCacheHeader header = { };
header.read(fileStream);

if (header.version != DxvkStateCacheHeader::CurrVersion
|| header.keySize != sizeof(DxvkGraphicsPipelineStateKey))
return false;

// Read as many entries from the file as
// possible and fill the data structures
while (fileStream) {
Sha1Hash expectedHash;
fileStream.read(reinterpret_cast<char*>(&expectedHash), sizeof(expectedHash));

DxvkGraphicsPipelineStateKey key;
key.read(fileStream);

Sha1Hash actualHash = Sha1Hash::compute(
reinterpret_cast<const uint8_t*>(&key),
sizeof(key));
uint32_t consecutiveCorruptPipelines = 0;
{
std::ifstream fileStream(getFilePath(), std::ios_base::binary);

if (!fileStream)
break;

if (!(actualHash == expectedHash)) {
Logger::debug("Skipping shader due to hash");
continue;
}
return false;

const DxvkGraphicsPipelineStateKey* ptr = this->insertEntry(key);
DxvkStateCacheHeader header = { };
header.read(fileStream);

if (ptr != nullptr) {
this->insertShaderEntry(key.vsKey(), ptr);
this->insertShaderEntry(key.tcsKey(), ptr);
this->insertShaderEntry(key.tesKey(), ptr);
this->insertShaderEntry(key.gsKey(), ptr);
this->insertShaderEntry(key.psKey(), ptr);
if (header.version != DxvkStateCacheHeader::CurrVersion
|| header.keySize != sizeof(DxvkGraphicsPipelineStateKey))
return false;


// Read as many entries from the file as
// possible and fill the data structures
while (fileStream) {
Sha1Hash expectedHash;
fileStream.read(reinterpret_cast<char*>(&expectedHash), sizeof(expectedHash));

DxvkGraphicsPipelineStateKey key;
key.read(fileStream);

Sha1Hash actualHash = Sha1Hash::compute(
reinterpret_cast<const uint8_t*>(&key),
sizeof(key));

if (!fileStream)
break;

if (!(actualHash == expectedHash)) {
Logger::debug("DxvkStateCache: Skipping pipeline due to hash");
consecutiveCorruptPipelines++;
continue;
}

consecutiveCorruptPipelines = 0;
const DxvkGraphicsPipelineStateKey* ptr = this->insertEntry(key);

if (ptr != nullptr) {
this->insertShaderEntry(key.vsKey(), ptr);
this->insertShaderEntry(key.tcsKey(), ptr);
this->insertShaderEntry(key.tesKey(), ptr);
this->insertShaderEntry(key.gsKey(), ptr);
this->insertShaderEntry(key.psKey(), ptr);
}
}
}

if (consecutiveCorruptPipelines > 10) {
Logger::warn("DxvkStateCache: State cache is corrupted, deleting file.");
// The state file is corrupted
std::remove(getFilePath().c_str());
}

Logger::info(str::format(
"DxvkStateCache: Read ", m_cacheEntries.size(), " entries"));
return true;
Expand Down

0 comments on commit 83daf00

Please sign in to comment.