OSDN Git Service

Rename constant cache to snapshot cache
authorNicolas Capens <capn@google.com>
Thu, 19 Mar 2020 17:31:41 +0000 (13:31 -0400)
committerNicolas Capens <nicolascapens@google.com>
Sat, 28 Mar 2020 11:18:26 +0000 (11:18 +0000)
This helps clarify that the cache is able to take snapshots of its
contents, and this snapshot can be queried without the overhead of
acquiring a mutex.

Bug: b/131246679
Change-Id: I4317628a22f70d37309908b24df62dc5ec5a946c
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/42568
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
src/Device/LRUCache.hpp
src/Device/Renderer.cpp
src/Pipeline/SpirvShaderSampling.cpp
src/Vulkan/VkDevice.cpp
src/Vulkan/VkDevice.hpp

index dedca45..848d421 100644 (file)
@@ -47,30 +47,32 @@ protected:
        Data *data;
 };
 
+// An LRU cache which can take a 'snapshot' of its current state. This is useful
+// for allowing concurrent read access without requiring a mutex to be locked.
 template<class Key, class Data, class Hasher = std::hash<Key>>
-class LRUConstCache : public LRUCache<Key, Data>
+class LRUSnapshotCache : public LRUCache<Key, Data>
 {
        using LRUBase = LRUCache<Key, Data>;
 
 public:
-       LRUConstCache(int n)
+       LRUSnapshotCache(int n)
            : LRUBase(n)
        {}
-       ~LRUConstCache() { clearConstCache(); }
+       ~LRUSnapshotCache() { clearSnapshot(); }
 
        Data add(const Key &key, const Data &data) override
        {
-               constCacheNeedsUpdate = true;
+               snapshotNeedsUpdate = true;
                return LRUBase::add(key, data);
        }
 
-       void updateConstCache();
-       const Data &queryConstCache(const Key &key) const;
+       void updateSnapshot();
+       const Data &querySnapshot(const Key &key) const;
 
 private:
-       void clearConstCache();
-       bool constCacheNeedsUpdate = false;
-       std::unordered_map<Key, Data, Hasher> constCache;
+       void clearSnapshot();
+       bool snapshotNeedsUpdate = false;
+       std::unordered_map<Key, Data, Hasher> snapshot;
 };
 
 }  // namespace sw
@@ -153,36 +155,36 @@ Data LRUCache<Key, Data>::add(const Key &key, const Data &data)
 }
 
 template<class Key, class Data, class Hasher>
-void LRUConstCache<Key, Data, Hasher>::clearConstCache()
+void LRUSnapshotCache<Key, Data, Hasher>::clearSnapshot()
 {
-       constCache.clear();
+       snapshot.clear();
 }
 
 template<class Key, class Data, class Hasher>
-void LRUConstCache<Key, Data, Hasher>::updateConstCache()
+void LRUSnapshotCache<Key, Data, Hasher>::updateSnapshot()
 {
-       if(constCacheNeedsUpdate)
+       if(snapshotNeedsUpdate)
        {
-               clearConstCache();
+               clearSnapshot();
 
                for(int i = 0; i < LRUBase::size; i++)
                {
                        if(LRUBase::data[i])
                        {
-                               constCache[*LRUBase::ref[i]] = LRUBase::data[i];
+                               snapshot[*LRUBase::ref[i]] = LRUBase::data[i];
                        }
                }
 
-               constCacheNeedsUpdate = false;
+               snapshotNeedsUpdate = false;
        }
 }
 
 template<class Key, class Data, class Hasher>
-const Data &LRUConstCache<Key, Data, Hasher>::queryConstCache(const Key &key) const
+const Data &LRUSnapshotCache<Key, Data, Hasher>::querySnapshot(const Key &key) const
 {
-       auto it = constCache.find(key);
+       auto it = snapshot.find(key);
        static Data null = {};
-       return (it != constCache.end()) ? it->second : null;
+       return (it != snapshot.end()) ? it->second : null;
 }
 
 }  // namespace sw
index 90db545..7667f96 100644 (file)
@@ -555,7 +555,7 @@ void Renderer::synchronize()
        MARL_SCOPED_EVENT("synchronize");
        auto ticket = drawTickets.take();
        ticket.wait();
-       device->updateSamplingRoutineConstCache();
+       device->updateSamplingRoutineSnapshotCache();
        ticket.done();
 }
 
index 5434645..adc465b 100644 (file)
@@ -40,7 +40,7 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl
 
        ASSERT(imageDescriptor->device);
 
-       if(auto routine = imageDescriptor->device->findInConstCache(key))
+       if(auto routine = imageDescriptor->device->querySnapshotCache(key))
        {
                return (ImageSampler *)(routine->getEntry());
        }
index 33c9731..492c676 100644 (file)
@@ -49,14 +49,14 @@ void Device::SamplingRoutineCache::add(const vk::Device::SamplingRoutineCache::K
        cache.add(key, routine);
 }
 
-rr::Routine *Device::SamplingRoutineCache::queryConst(const vk::Device::SamplingRoutineCache::Key &key) const
+rr::Routine *Device::SamplingRoutineCache::querySnapshot(const vk::Device::SamplingRoutineCache::Key &key) const
 {
-       return cache.queryConstCache(key).get();
+       return cache.querySnapshot(key).get();
 }
 
-void Device::SamplingRoutineCache::updateConstCache()
+void Device::SamplingRoutineCache::updateSnapshot()
 {
-       cache.updateConstCache();
+       cache.updateSnapshot();
 }
 
 Device::SamplerIndexer::~SamplerIndexer()
@@ -302,15 +302,15 @@ Device::SamplingRoutineCache *Device::getSamplingRoutineCache() const
        return samplingRoutineCache.get();
 }
 
-rr::Routine *Device::findInConstCache(const SamplingRoutineCache::Key &key) const
+rr::Routine *Device::querySnapshotCache(const SamplingRoutineCache::Key &key) const
 {
-       return samplingRoutineCache->queryConst(key);
+       return samplingRoutineCache->querySnapshot(key);
 }
 
-void Device::updateSamplingRoutineConstCache()
+void Device::updateSamplingRoutineSnapshotCache()
 {
        std::unique_lock<std::mutex> lock(samplingRoutineCacheMutex);
-       samplingRoutineCache->updateConstCache();
+       samplingRoutineCache->updateSnapshot();
 }
 
 std::mutex &Device::getSamplingRoutineCacheMutex()
index c2ba927..2a52473 100644 (file)
@@ -89,17 +89,17 @@ public:
                std::shared_ptr<rr::Routine> query(const Key &key) const;
                void add(const Key &key, const std::shared_ptr<rr::Routine> &routine);
 
-               rr::Routine *queryConst(const Key &key) const;
-               void updateConstCache();
+               rr::Routine *querySnapshot(const Key &key) const;
+               void updateSnapshot();
 
        private:
-               sw::LRUConstCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache;
+               sw::LRUSnapshotCache<Key, std::shared_ptr<rr::Routine>, Key::Hash> cache;
        };
 
        SamplingRoutineCache *getSamplingRoutineCache() const;
        std::mutex &getSamplingRoutineCacheMutex();
-       rr::Routine *findInConstCache(const SamplingRoutineCache::Key &key) const;
-       void updateSamplingRoutineConstCache();
+       rr::Routine *querySnapshotCache(const SamplingRoutineCache::Key &key) const;
+       void updateSamplingRoutineSnapshotCache();
 
        class SamplerIndexer
        {