summaryrefslogtreecommitdiff
path: root/tools/gfx/persistent-shader-cache.h
diff options
context:
space:
mode:
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;
};
}