diff --git a/src/dawn/native/CacheRequest.h b/src/dawn/native/CacheRequest.h index cdaa117494..b861419ce8 100644 --- a/src/dawn/native/CacheRequest.h +++ b/src/dawn/native/CacheRequest.h @@ -129,11 +129,7 @@ class CacheRequestImpl { using ReturnType = ResultOrError; 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 19c5df1700..61f7162789 100644 --- a/src/dawn/native/Device.cpp +++ b/src/dawn/native/Device.cpp @@ -618,27 +618,18 @@ BlobCache* DeviceBase::GetBlobCache() { // 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 4e734ca4f3..075edec877 100644 --- a/src/dawn/native/Instance.cpp +++ b/src/dawn/native/Instance.cpp @@ -456,8 +456,11 @@ dawn::platform::Platform* InstanceBase::GetPlatform() { 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 0589061069..ac3a11048b 100644 --- a/src/dawn/native/Instance.h +++ b/src/dawn/native/Instance.h @@ -93,7 +93,7 @@ class InstanceBase final : public RefCountedWithExternalCount { 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 @@ class InstanceBase final : public RefCountedWithExternalCount { dawn::platform::Platform* mPlatform = nullptr; std::unique_ptr mDefaultPlatform; std::unique_ptr mBlobCache; + BlobCache mPassthroughBlobCache; std::vector> mBackends; std::vector> mAdapters; diff --git a/src/dawn/native/PipelineCache.cpp b/src/dawn/native/PipelineCache.cpp index 762e08526d..e34116f58d 100644 --- a/src/dawn/native/PipelineCache.cpp +++ b/src/dawn/native/PipelineCache.cpp @@ -21,7 +21,7 @@ PipelineCacheBase::PipelineCacheBase(BlobCache* cache, const CacheKey& key) 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 @@ bool PipelineCacheBase::CacheHit() const { } 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 4aa4d3c8d1..68a6f72a9e 100644 --- a/src/dawn/native/d3d12/ShaderModuleD3D12.cpp +++ b/src/dawn/native/d3d12/ShaderModuleD3D12.cpp @@ -648,9 +648,7 @@ ResultOrError ShaderModule::Compile( 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 80627da845..318d3734ff 100644 --- a/src/dawn/native/metal/ShaderModuleMTL.mm +++ b/src/dawn/native/metal/ShaderModuleMTL.mm @@ -366,9 +366,7 @@ MaybeError ShaderModule::CreateFunction(SingleShaderStage stage, 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 caaf8634a4..4cd29af9af 100644 --- a/src/dawn/native/opengl/ShaderModuleGL.cpp +++ b/src/dawn/native/opengl/ShaderModuleGL.cpp @@ -299,9 +299,7 @@ ResultOrError ShaderModule::CompileShader(const OpenGLFunctions& gl, } } - 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 4703954946..4966e921f1 100644 --- a/src/dawn/native/vulkan/ShaderModuleVk.cpp +++ b/src/dawn/native/vulkan/ShaderModuleVk.cpp @@ -375,9 +375,8 @@ ResultOrError ShaderModule::GetHandleAndSpirv( 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());