summaryrefslogtreecommitdiff
path: root/tools/gfx/persistent-shader-cache.h
diff options
context:
space:
mode:
authorlucy96chen <47800040+lucy96chen@users.noreply.github.com>2022-11-29 12:35:54 -0800
committerGitHub <noreply@github.com>2022-11-29 12:35:54 -0800
commitd85c7b809d02e6dc0844aab07e66a6bac2462017 (patch)
tree5b0c094e705df8b1c0a98de26e3d28514b014d80 /tools/gfx/persistent-shader-cache.h
parentd60c925229cf911b0363bf9d5e25a6f73c6d5737 (diff)
FileStream-based implementation for updating cache index file (#2485)
* Draft FileStream-based implementation for updating cache file * File streams fully integrated into shader cache code paths; Tests will not run unless file system is on disk as file streams do not play nicely with in-memory * Brought old code back as fallback path, but tests need to ensure previous is freed first * Testing structure updated, beginning cleanup work * All tests working * Cleanup changes * Removed an extra tab at the end of a line * Cleanup change * Undo externals change * Removed redundant logic for OS vs memory file system handling of the shader cache; Removed extra helper function left over from old cache implementation * Reverted performance change to generate contents hashes when modules are being loaded as this code path is not always followed; Contents hashing now uses a combination of hashing and checking the last modified time for all file dependencies, only reading in and hashing the contents of all files if the last modified hash does not match * Added handling to Module::updateContentsBasedHash for file dependencies which are not from a physical source file on disk; Added test for above Co-authored-by: Lucy Chen <lucchen@nvidia.com> Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tools/gfx/persistent-shader-cache.h')
-rw-r--r--tools/gfx/persistent-shader-cache.h65
1 files changed, 44 insertions, 21 deletions
diff --git a/tools/gfx/persistent-shader-cache.h b/tools/gfx/persistent-shader-cache.h
index 8d4ded4b9..8e05eddf6 100644
--- a/tools/gfx/persistent-shader-cache.h
+++ b/tools/gfx/persistent-shader-cache.h
@@ -7,27 +7,42 @@
#include "../../source/core/slang-string.h"
#include "../../source/core/slang-dictionary.h"
#include "../../source/core/slang-linked-list.h"
+#include "../../source/core/slang-stream.h"
namespace gfx
{
- using namespace Slang;
+using namespace Slang;
struct ShaderCacheEntry
{
slang::Digest dependencyBasedDigest;
slang::Digest contentsBasedDigest;
+ double lastAccessedTime;
+
+ bool operator==(const ShaderCacheEntry& rhs)
+ {
+ return dependencyBasedDigest == rhs.dependencyBasedDigest
+ && contentsBasedDigest == rhs.contentsBasedDigest
+ && lastAccessedTime == rhs.lastAccessedTime;
+ }
+
+ uint32_t getHashCode()
+ {
+ return dependencyBasedDigest.getHashCode();
+ }
};
class PersistentShaderCache : public RefObject
{
public:
PersistentShaderCache(const IDevice::ShaderCacheDesc& inDesc);
+ ~PersistentShaderCache();
// Fetch the cache entry corresponding to the provided key. If found, move the entry to
// the front of entries and return the entry and the corresponding compiled code in
// outCompiledCode. Else, return nullptr.
- LinkedNode<ShaderCacheEntry>* findEntry(const slang::Digest& key, ISlangBlob** outCompiledCode);
+ ShaderCacheEntry* findEntry(const slang::Digest& key, ISlangBlob** outCompiledCode);
// Add an entry to the cache with the provided key and contents hashes. If
// adding an entry causes the cache to exceed size limitations, this will also
@@ -36,38 +51,46 @@ public:
// Update the contents hash for the specified entry in the cache and update the
// corresponding file on disk.
- void updateEntry(
- LinkedNode<ShaderCacheEntry>* entryNode,
- const slang::Digest& dependencyDigest,
- const slang::Digest& contentsDigest,
- ISlangBlob* updatedCode);
+ void updateEntry(const slang::Digest& dependencyDigest, const slang::Digest& contentsDigest, ISlangBlob* updatedCode);
private:
// Load a previous cache index saved to disk. If not found, create a new cache index
// and save it to disk as filename.
void loadCacheFromFile();
- // Update the cache index on disk. This should be called any time an entry changes.
- void saveCacheToFile();
-
// Delete the last entry (the least recently used) from entries, remove its key/value pair
- // from keyToEntry, and remove the corresponding file on disk. This should only be called
- // by addEntry() when the cache reaches maximum capacity.
- void deleteLRUEntry();
+ // from keyToEntry, and remove the corresponding file on disk. Returns the index in 'entries'
+ // of the removed entry so addEntry() can overwrite the corresponding entry in 'entries'
+ // with the new entry. This should only be called by addEntry() when the cache reaches maximum capacity.
+ Index deleteLRUEntry();
+
+ // Without access to a physical file path, in-memory file systems cannot leverage file streams and
+ // need to fall back on a different implementation for loading and saving the cache to memory.
+ void loadCacheFromMemory();
+ void saveCacheToMemory();
// The shader cache's description.
IDevice::ShaderCacheDesc desc;
- // Dictionary mapping each shader's key to its corresponding node (entry) in the list
- // of entries.
- Dictionary<slang::Digest, LinkedNode<ShaderCacheEntry>*> keyToEntry;
-
- // Linked list containing the entries stored in the shader cache in order
- // of most to least recently used.
- LinkedList<ShaderCacheEntry> entries;
-
// The underlying file system used for the shader cache.
ComPtr<ISlangMutableFileSystem> mutableShaderCacheFileSystem = nullptr;
+ bool isMemoryFileSystem = false;
+
+ // A file stream to the index file opened during cache load. This will only
+ // exist for a cache that exists on-disk.
+ FileStream indexStream;
+
+ // Dictionary mapping each shader's key to its corresponding node (entry) in the
+ // linked list 'orderedEntries'.
+ Dictionary<slang::Digest, LinkedNode<Index>*> keyToEntry;
+
+ // Linked list containing the corresponding indices in 'entries' for entries in the
+ // shader cache ordered from most to least recently used.
+ LinkedList<Index> orderedEntries;
+
+ // List of entries in the shader cache. This list is not guaranteed to be in order of recency
+ // as the main and fall back implementations handle outputting to the file differently.
+ List<ShaderCacheEntry> entries;
};
}