Ensures blob cache is always available, even if it is just a placeholder

Bug: dawn:549
Change-Id: I7efbaa58d93691648107fc6b94d76596a77f6516
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/111140
Reviewed-by: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Loko Kung <lokokung@google.com>
diff --git a/src/dawn/native/CacheRequest.h b/src/dawn/native/CacheRequest.h
index cdaa117..b861419 100644
--- a/src/dawn/native/CacheRequest.h
+++ b/src/dawn/native/CacheRequest.h
@@ -129,11 +129,7 @@
         using ReturnType = ResultOrError<CacheResultType>;
 
         CacheKey key = r.CreateCacheKey(device);
-        BlobCache* cache = device->GetBlobCache();
-        Blob blob;
-        if (cache != nullptr) {
-            blob = cache->Load(key);
-        }
+        Blob blob = device->GetBlobCache()->Load(key);
 
         if (!blob.Empty()) {
             // Cache hit. Handle the cached blob.
diff --git a/src/dawn/native/Device.cpp b/src/dawn/native/Device.cpp
index 19c5df1..61f7162 100644
--- a/src/dawn/native/Device.cpp
+++ b/src/dawn/native/Device.cpp
@@ -618,27 +618,18 @@
     // TODO(crbug.com/dawn/1481): Shader caching currently has a dependency on the WGSL writer to
     // generate cache keys. We can lift the dependency once we also cache frontend parsing,
     // transformations, and reflection.
-    if (!IsToggleEnabled(Toggle::DisableBlobCache)) {
-        return mAdapter->GetInstance()->GetBlobCache();
-    }
+    return mAdapter->GetInstance()->GetBlobCache(!IsToggleEnabled(Toggle::DisableBlobCache));
 #endif
-    return nullptr;
+    return mAdapter->GetInstance()->GetBlobCache(false);
 }
 
 Blob DeviceBase::LoadCachedBlob(const CacheKey& key) {
-    BlobCache* blobCache = GetBlobCache();
-    if (!blobCache) {
-        return Blob();
-    }
-    return blobCache->Load(key);
+    return GetBlobCache()->Load(key);
 }
 
 void DeviceBase::StoreCachedBlob(const CacheKey& key, const Blob& blob) {
     if (!blob.Empty()) {
-        BlobCache* blobCache = GetBlobCache();
-        if (blobCache) {
-            blobCache->Store(key, blob);
-        }
+        GetBlobCache()->Store(key, blob);
     }
 }
 
diff --git a/src/dawn/native/Instance.cpp b/src/dawn/native/Instance.cpp
index 4e734ca..075edec 100644
--- a/src/dawn/native/Instance.cpp
+++ b/src/dawn/native/Instance.cpp
@@ -456,8 +456,11 @@
     return mPlatform;
 }
 
-BlobCache* InstanceBase::GetBlobCache() {
-    return mBlobCache.get();
+BlobCache* InstanceBase::GetBlobCache(bool enabled) {
+    if (enabled) {
+        return mBlobCache.get();
+    }
+    return &mPassthroughBlobCache;
 }
 
 uint64_t InstanceBase::GetDeviceCountForTesting() const {
diff --git a/src/dawn/native/Instance.h b/src/dawn/native/Instance.h
index 0589061..ac3a110 100644
--- a/src/dawn/native/Instance.h
+++ b/src/dawn/native/Instance.h
@@ -93,7 +93,7 @@
     void SetPlatform(dawn::platform::Platform* platform);
     void SetPlatformForTesting(dawn::platform::Platform* platform);
     dawn::platform::Platform* GetPlatform();
-    BlobCache* GetBlobCache();
+    BlobCache* GetBlobCache(bool enabled = true);
 
     uint64_t GetDeviceCountForTesting() const;
     void IncrementDeviceCountForTesting();
@@ -139,6 +139,7 @@
     dawn::platform::Platform* mPlatform = nullptr;
     std::unique_ptr<dawn::platform::Platform> mDefaultPlatform;
     std::unique_ptr<BlobCache> mBlobCache;
+    BlobCache mPassthroughBlobCache;
 
     std::vector<std::unique_ptr<BackendConnection>> mBackends;
     std::vector<Ref<AdapterBase>> mAdapters;
diff --git a/src/dawn/native/PipelineCache.cpp b/src/dawn/native/PipelineCache.cpp
index 762e085..e34116f 100644
--- a/src/dawn/native/PipelineCache.cpp
+++ b/src/dawn/native/PipelineCache.cpp
@@ -21,7 +21,7 @@
 
 Blob PipelineCacheBase::Initialize() {
     ASSERT(!mInitialized);
-    Blob blob = mCache != nullptr ? mCache->Load(mKey) : Blob();
+    Blob blob = mCache->Load(mKey);
     mCacheHit = !blob.Empty();
     mInitialized = true;
     return blob;
@@ -33,9 +33,6 @@
 }
 
 MaybeError PipelineCacheBase::Flush() {
-    if (mCache == nullptr) {
-        return {};
-    }
     // Try to write the data out to the persistent cache.
     Blob blob;
     DAWN_TRY(SerializeToBlobImpl(&blob));
diff --git a/src/dawn/native/d3d12/ShaderModuleD3D12.cpp b/src/dawn/native/d3d12/ShaderModuleD3D12.cpp
index 4aa4d3c..68a6f72 100644
--- a/src/dawn/native/d3d12/ShaderModuleD3D12.cpp
+++ b/src/dawn/native/d3d12/ShaderModuleD3D12.cpp
@@ -648,9 +648,7 @@
         device->EmitLog(WGPULoggingType_Info, dumpedMsg.str().c_str());
     }
 
-    if (BlobCache* cache = device->GetBlobCache()) {
-        cache->EnsureStored(compiledShader);
-    }
+    device->GetBlobCache()->EnsureStored(compiledShader);
 
     // Clear the hlslSource. It is only used for logging and should not be used
     // outside of the compilation.
diff --git a/src/dawn/native/metal/ShaderModuleMTL.mm b/src/dawn/native/metal/ShaderModuleMTL.mm
index 80627da..318d373 100644
--- a/src/dawn/native/metal/ShaderModuleMTL.mm
+++ b/src/dawn/native/metal/ShaderModuleMTL.mm
@@ -366,9 +366,7 @@
         out->function = AcquireNSPRef([*library newFunctionWithName:name.Get()]);
     }
 
-    if (BlobCache* cache = GetDevice()->GetBlobCache()) {
-        cache->EnsureStored(mslCompilation);
-    }
+    GetDevice()->GetBlobCache()->EnsureStored(mslCompilation);
 
     if (GetDevice()->IsToggleEnabled(Toggle::MetalEnableVertexPulling) &&
         GetEntryPoint(entryPointName).usedVertexInputs.any()) {
diff --git a/src/dawn/native/opengl/ShaderModuleGL.cpp b/src/dawn/native/opengl/ShaderModuleGL.cpp
index caaf863..4cd29af 100644
--- a/src/dawn/native/opengl/ShaderModuleGL.cpp
+++ b/src/dawn/native/opengl/ShaderModuleGL.cpp
@@ -299,9 +299,7 @@
         }
     }
 
-    if (BlobCache* cache = GetDevice()->GetBlobCache()) {
-        cache->EnsureStored(compilationResult);
-    }
+    GetDevice()->GetBlobCache()->EnsureStored(compilationResult);
     *needsPlaceholderSampler = compilationResult->needsPlaceholderSampler;
     *combinedSamplers = std::move(compilationResult->combinedSamplerInfo);
     return shader;
diff --git a/src/dawn/native/vulkan/ShaderModuleVk.cpp b/src/dawn/native/vulkan/ShaderModuleVk.cpp
index 4703954..4966e92 100644
--- a/src/dawn/native/vulkan/ShaderModuleVk.cpp
+++ b/src/dawn/native/vulkan/ShaderModuleVk.cpp
@@ -375,9 +375,8 @@
 
     ModuleAndSpirv moduleAndSpirv;
     if (newHandle != VK_NULL_HANDLE) {
-        if (BlobCache* cache = device->GetBlobCache()) {
-            cache->EnsureStored(compilation);
-        }
+        device->GetBlobCache()->EnsureStored(compilation);
+
         // Set the label on `newHandle` now, and not on `moduleAndSpirv.module` later
         // since `moduleAndSpirv.module` may be in use by multiple threads.
         SetDebugName(ToBackend(GetDevice()), newHandle, "Dawn_ShaderModule", GetLabel());