summaryrefslogtreecommitdiff
path: root/tools/gfx/persistent-shader-cache.h
diff options
context:
space:
mode:
authorlucy96chen <47800040+lucy96chen@users.noreply.github.com>2022-10-17 17:38:59 -0700
committerGitHub <noreply@github.com>2022-10-17 17:38:59 -0700
commit8add41a6e37994577d928bc312801ddfa1c33173 (patch)
tree6ca5ef639a22c4e37c7287df1877cb5bf7ce691c /tools/gfx/persistent-shader-cache.h
parent09408e32d7c0ccebf38fe31b5d2ddf4b1cd128e4 (diff)
Shader cache index implementation (#2452)
Diffstat (limited to 'tools/gfx/persistent-shader-cache.h')
-rw-r--r--tools/gfx/persistent-shader-cache.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/tools/gfx/persistent-shader-cache.h b/tools/gfx/persistent-shader-cache.h
new file mode 100644
index 000000000..55738a9af
--- /dev/null
+++ b/tools/gfx/persistent-shader-cache.h
@@ -0,0 +1,79 @@
+// slang-shader-cache-index.h
+#pragma once
+#include "../../slang.h"
+#include "../../slang-com-ptr.h"
+
+#include "../../source/core/slang-string.h"
+#include "../../source/core/slang-dictionary.h"
+#include "../../source/core/slang-linked-list.h"
+
+namespace Slang
+{
+
+struct ShaderCacheEntry
+{
+ slang::Digest dependencyBasedDigest;
+ slang::Digest astBasedDigest;
+};
+
+class PersistentShaderCache
+{
+public:
+ // TODO: Remove in integration PR in favor of new ShaderCacheDesc in slang-gfx.h
+ struct Desc
+ {
+ String cacheFilename;
+ String shaderCachePath;
+ SlangInt entryCountLimit = 1000;
+ ISlangFileSystem* shaderCacheFileSystem = nullptr;
+ };
+
+ PersistentShaderCache(const Desc& inDesc);
+
+ // 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);
+
+ // 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
+ // delete the least recently used entry.
+ void addEntry(const slang::Digest& dependencyDigest, const slang::Digest& astDigest, ISlangBlob* compiledCode);
+
+ // 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& astDigest,
+ 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();
+
+ // The shader cache's description.
+ Desc 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;
+};
+
+}