D3D12: Remove `mSamplerAllocator` in `BindGroupLayout`

This patch removes `mSamplerAllocator` in `BindGroupLayout` as it
can be easily got from `mDevice` without much computation.

This patch also removes `mSamplerAllocator` in `SamplerHeapCacheEntry`.

Bug: chromium:42241306
Change-Id: I5ea52a5aa4a1d401c1a69fd1caa78a5cffd4d7ce
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/217096
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
diff --git a/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp b/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp
index 2ea4c8e..10c346d 100644
--- a/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp
+++ b/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp
@@ -209,8 +209,6 @@
 
         descriptorRanges.push_back(range);
     }
-
-    mSamplerAllocator = device->GetSamplerStagingDescriptorAllocator(GetSamplerDescriptorCount());
 }
 
 ResultOrError<Ref<BindGroup>> BindGroupLayout::AllocateBindGroup(
@@ -233,10 +231,9 @@
         mBindGroupAllocator->Allocate(device, descriptor, viewSizeIncrement, viewAllocation));
 
     if (GetSamplerDescriptorCount() > 0) {
-        DAWN_ASSERT(mSamplerAllocator != nullptr);
         Ref<SamplerHeapCacheEntry> samplerHeapCacheEntry;
-        DAWN_TRY_ASSIGN(samplerHeapCacheEntry, device->GetSamplerHeapCache()->GetOrCreate(
-                                                   bindGroup.Get(), *mSamplerAllocator));
+        DAWN_TRY_ASSIGN(samplerHeapCacheEntry,
+                        device->GetSamplerHeapCache()->GetOrCreate(bindGroup.Get()));
         bindGroup->SetSamplerAllocationEntry(std::move(samplerHeapCacheEntry));
     }
 
diff --git a/src/dawn/native/d3d12/BindGroupLayoutD3D12.h b/src/dawn/native/d3d12/BindGroupLayoutD3D12.h
index 36e1e0f..4029472 100644
--- a/src/dawn/native/d3d12/BindGroupLayoutD3D12.h
+++ b/src/dawn/native/d3d12/BindGroupLayoutD3D12.h
@@ -99,10 +99,6 @@
     std::vector<D3D12_STATIC_SAMPLER_DESC> mStaticSamplers;
 
     MutexProtected<SlabAllocator<BindGroup>> mBindGroupAllocator;
-
-    // TODO(https://crbug.com/dawn/2361): Rewrite this member with raw_ptr<T>.
-    // This is currently failing with MSVC cl.exe compiler.
-    RAW_PTR_EXCLUSION MutexProtected<StagingDescriptorAllocator>* mSamplerAllocator = nullptr;
 };
 
 }  // namespace dawn::native::d3d12
diff --git a/src/dawn/native/d3d12/DeviceD3D12.cpp b/src/dawn/native/d3d12/DeviceD3D12.cpp
index 4d167ce..e71cf26 100644
--- a/src/dawn/native/d3d12/DeviceD3D12.cpp
+++ b/src/dawn/native/d3d12/DeviceD3D12.cpp
@@ -753,8 +753,9 @@
 MutexProtected<StagingDescriptorAllocator>* Device::GetSamplerStagingDescriptorAllocator(
     uint32_t descriptorCount) const {
     DAWN_ASSERT(descriptorCount <= kMaxSamplerDescriptorsPerBindGroup);
+    DAWN_ASSERT(descriptorCount > 0);
     // This is Log2 of the next power of two, plus 1.
-    uint32_t allocatorIndex = descriptorCount == 0 ? 0 : Log2Ceil(descriptorCount) + 1;
+    uint32_t allocatorIndex = Log2Ceil(descriptorCount) + 1;
     return mSamplerAllocators[allocatorIndex].get();
 }
 
diff --git a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
index ebf8e15..8925dd9 100644
--- a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
+++ b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
@@ -42,17 +42,14 @@
 
 namespace dawn::native::d3d12 {
 
-SamplerHeapCacheEntry::SamplerHeapCacheEntry(MutexProtected<StagingDescriptorAllocator>& allocator,
-                                             std::vector<Sampler*> samplers)
-    : mSamplers(std::move(samplers)), mAllocator(allocator) {}
+SamplerHeapCacheEntry::SamplerHeapCacheEntry(std::vector<Sampler*> samplers)
+    : mSamplers(std::move(samplers)) {}
 
 SamplerHeapCacheEntry::SamplerHeapCacheEntry(SamplerHeapCache* cache,
-                                             MutexProtected<StagingDescriptorAllocator>& allocator,
                                              std::vector<Sampler*> samplers,
                                              CPUDescriptorHeapAllocation allocation)
     : mCPUAllocation(std::move(allocation)),
       mSamplers(std::move(samplers)),
-      mAllocator(allocator),
       mCache(cache) {
     DAWN_ASSERT(mCache != nullptr);
     DAWN_ASSERT(mCPUAllocation.IsValid());
@@ -60,6 +57,8 @@
 }
 
 std::vector<Sampler*>&& SamplerHeapCacheEntry::AcquireSamplers() {
+    // This function should only be called when SamplerHeapCacheEntry is created for blueprint.
+    DAWN_ASSERT(!mCPUAllocation.IsValid());
     return std::move(mSamplers);
 }
 
@@ -67,7 +66,11 @@
     // If this is a blueprint then the CPU allocation cannot exist and has no entry to remove.
     if (mCPUAllocation.IsValid()) {
         mCache->RemoveCacheEntry(this);
-        mAllocator->Deallocate(&mCPUAllocation);
+        DAWN_ASSERT(!mSamplers.empty());
+        auto* allocator = mCache->GetDevice()->GetSamplerStagingDescriptorAllocator(
+            static_cast<uint32_t>(mSamplers.size()));
+        DAWN_ASSERT(allocator != nullptr);
+        (*allocator)->Deallocate(&mCPUAllocation);
     }
 
     DAWN_ASSERT(!mCPUAllocation.IsValid());
@@ -106,17 +109,16 @@
     return mGPUAllocation.GetBaseDescriptor();
 }
 
-ResultOrError<Ref<SamplerHeapCacheEntry>> SamplerHeapCache::GetOrCreate(
-    const BindGroup* group,
-    MutexProtected<StagingDescriptorAllocator>& samplerAllocator) {
+ResultOrError<Ref<SamplerHeapCacheEntry>> SamplerHeapCache::GetOrCreate(const BindGroup* group) {
     const BindGroupLayout* bgl = ToBackend(group->GetLayout());
 
     // If a previously created bindgroup used the same samplers, the backing sampler heap
     // allocation can be reused. The packed list of samplers acts as the key to lookup the
     // allocation in a cache.
     // TODO(dawn:155): Avoid re-allocating the vector each lookup.
+    uint32_t samplerCount = bgl->GetSamplerDescriptorCount();
     std::vector<Sampler*> samplers;
-    samplers.reserve(bgl->GetSamplerDescriptorCount());
+    samplers.reserve(samplerCount);
 
     for (BindingIndex bindingIndex = bgl->GetDynamicBufferCount();
          bindingIndex < bgl->GetBindingCount(); ++bindingIndex) {
@@ -128,7 +130,7 @@
 
     // Check the cache if there exists a sampler heap allocation that corresponds to the
     // samplers.
-    SamplerHeapCacheEntry blueprint(samplerAllocator, std::move(samplers));
+    SamplerHeapCacheEntry blueprint(std::move(samplers));
     auto iter = mCache.find(&blueprint);
     if (iter != mCache.end()) {
         return Ref<SamplerHeapCacheEntry>(*iter);
@@ -140,7 +142,9 @@
 
     uint32_t samplerSizeIncrement = 0;
     CPUDescriptorHeapAllocation allocation;
-    DAWN_TRY(samplerAllocator.Use([&](auto allocator) -> MaybeError {
+    Device* device = ToBackend(group->GetDevice());
+    auto* samplerAllocator = device->GetSamplerStagingDescriptorAllocator(samplerCount);
+    DAWN_TRY(samplerAllocator->Use([&](auto allocator) -> MaybeError {
         DAWN_TRY_ASSIGN(allocation, allocator->AllocateCPUDescriptors());
         samplerSizeIncrement = allocator->GetSizeIncrement();
         return {};
@@ -153,8 +157,8 @@
         d3d12Device->CreateSampler(&samplerDesc, allocation.OffsetFrom(samplerSizeIncrement, i));
     }
 
-    Ref<SamplerHeapCacheEntry> entry = AcquireRef(new SamplerHeapCacheEntry(
-        this, samplerAllocator, std::move(samplers), std::move(allocation)));
+    Ref<SamplerHeapCacheEntry> entry =
+        AcquireRef(new SamplerHeapCacheEntry(this, std::move(samplers), std::move(allocation)));
     mCache.insert(entry.Get());
     return std::move(entry);
 }
@@ -165,6 +169,10 @@
     DAWN_ASSERT(mCache.empty());
 }
 
+Device* SamplerHeapCache::GetDevice() const {
+    return mDevice;
+}
+
 void SamplerHeapCache::RemoveCacheEntry(SamplerHeapCacheEntry* entry) {
     DAWN_ASSERT(entry->GetRefCountForTesting() == 0);
     size_t removedCount = mCache.erase(entry);
diff --git a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
index a76d17e..70a4a1c 100644
--- a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
+++ b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
@@ -62,10 +62,8 @@
 // Wraps sampler descriptor heap allocations in a cache.
 class SamplerHeapCacheEntry : public RefCounted {
   public:
-    explicit SamplerHeapCacheEntry(MutexProtected<StagingDescriptorAllocator>& allocator,
-                                   std::vector<Sampler*> samplers);
+    explicit SamplerHeapCacheEntry(std::vector<Sampler*> samplers);
     SamplerHeapCacheEntry(SamplerHeapCache* cache,
-                          MutexProtected<StagingDescriptorAllocator>& allocator,
                           std::vector<Sampler*> samplers,
                           CPUDescriptorHeapAllocation allocation);
     ~SamplerHeapCacheEntry() override;
@@ -93,9 +91,6 @@
     // by the device and will already be unique.
     std::vector<Sampler*> mSamplers;
 
-    // TODO(https://crbug.com/dawn/2361): Rewrite this member with raw_ref<T>.
-    // This is currently failing with MSVC cl.exe compiler.
-    RAW_PTR_EXCLUSION MutexProtected<StagingDescriptorAllocator>& mAllocator;
     raw_ptr<SamplerHeapCache> mCache = nullptr;
 };
 
@@ -106,12 +101,12 @@
     explicit SamplerHeapCache(Device* device);
     ~SamplerHeapCache();
 
-    ResultOrError<Ref<SamplerHeapCacheEntry>> GetOrCreate(
-        const BindGroup* group,
-        MutexProtected<StagingDescriptorAllocator>& samplerAllocator);
+    ResultOrError<Ref<SamplerHeapCacheEntry>> GetOrCreate(const BindGroup* group);
 
     void RemoveCacheEntry(SamplerHeapCacheEntry* entry);
 
+    Device* GetDevice() const;
+
   private:
     raw_ptr<Device> mDevice;