D3D12: Remove mViewAllocator and mSamplerAllocator in BindGroupTracker

This patch removes `mViewAllocator` and `mSamplerAllocator` in
`BindGroupTracker` as they can be directly got from `mDevice` without
any extra overhead.

This patch also removes the `Device*` parameter in `PopulateSamplers()`
as `Device` can be queried from another parameter `samplerAllocator`.

Bug: chromium:42241306
Change-Id: I41e1920d6d68c86a06753ff87d4486c431ff8f81
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/217394
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Loko Kung <lokokung@google.com>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn/native/d3d12/BindGroupD3D12.cpp b/src/dawn/native/d3d12/BindGroupD3D12.cpp
index 6adf65e..493b6f2 100644
--- a/src/dawn/native/d3d12/BindGroupD3D12.cpp
+++ b/src/dawn/native/d3d12/BindGroupD3D12.cpp
@@ -264,12 +264,11 @@
 }
 
 bool BindGroup::PopulateSamplers(
-    Device* device,
     MutexProtected<ShaderVisibleDescriptorAllocator>& samplerAllocator) {
     if (mSamplerAllocationEntry == nullptr) {
         return true;
     }
-    return mSamplerAllocationEntry->Populate(device, samplerAllocator);
+    return mSamplerAllocationEntry->Populate(samplerAllocator);
 }
 
 void BindGroup::SetSamplerAllocationEntry(Ref<SamplerHeapCacheEntry> entry) {
diff --git a/src/dawn/native/d3d12/BindGroupD3D12.h b/src/dawn/native/d3d12/BindGroupD3D12.h
index 26955c1..96f96ce 100644
--- a/src/dawn/native/d3d12/BindGroupD3D12.h
+++ b/src/dawn/native/d3d12/BindGroupD3D12.h
@@ -54,8 +54,7 @@
 
     // Returns true if the BindGroup was successfully populated.
     bool PopulateViews(MutexProtected<ShaderVisibleDescriptorAllocator>& viewAllocator);
-    bool PopulateSamplers(Device* device,
-                          MutexProtected<ShaderVisibleDescriptorAllocator>& samplerAllocator);
+    bool PopulateSamplers(MutexProtected<ShaderVisibleDescriptorAllocator>& samplerAllocator);
 
     D3D12_GPU_DESCRIPTOR_HANDLE GetBaseViewDescriptor() const;
     D3D12_GPU_DESCRIPTOR_HANDLE GetBaseSamplerDescriptor() const;
diff --git a/src/dawn/native/d3d12/CommandBufferD3D12.cpp b/src/dawn/native/d3d12/CommandBufferD3D12.cpp
index b422f40..b5597b9 100644
--- a/src/dawn/native/d3d12/CommandBufferD3D12.cpp
+++ b/src/dawn/native/d3d12/CommandBufferD3D12.cpp
@@ -413,11 +413,7 @@
 
   public:
     BindGroupStateTracker(Device* device, DescriptorHeapState* heapState)
-        : BindGroupTrackerBase(),
-          mDevice(device),
-          mHeapState(heapState),
-          mViewAllocator(device->GetViewShaderVisibleDescriptorAllocator()),
-          mSamplerAllocator(device->GetSamplerShaderVisibleDescriptorAllocator()) {}
+        : BindGroupTrackerBase(), mDevice(device), mHeapState(heapState) {}
 
     MaybeError Apply(CommandRecordingContext* commandContext) {
         BeforeApply();
@@ -425,6 +421,9 @@
         ID3D12GraphicsCommandList* commandList = commandContext->GetCommandList();
         UpdateRootSignatureIfNecessary(commandList);
 
+        auto& viewAllocator = mDevice->GetViewShaderVisibleDescriptorAllocator();
+        auto& samplerAllocator = mDevice->GetSamplerShaderVisibleDescriptorAllocator();
+
         // Bindgroups are allocated in shader-visible descriptor heaps which are managed by a
         // ringbuffer. There can be a single shader-visible descriptor heap of each type bound
         // at any given time. This means that when we switch heaps, all other currently bound
@@ -436,8 +435,8 @@
         bool didCreateBindGroupSamplers = true;
         for (BindGroupIndex index : IterateBitSet(mDirtyBindGroups)) {
             BindGroup* group = ToBackend(mBindGroups[index]);
-            didCreateBindGroupViews = group->PopulateViews(mViewAllocator);
-            didCreateBindGroupSamplers = group->PopulateSamplers(mDevice, mSamplerAllocator);
+            didCreateBindGroupViews = group->PopulateViews(viewAllocator);
+            didCreateBindGroupSamplers = group->PopulateSamplers(samplerAllocator);
             if (!didCreateBindGroupViews && !didCreateBindGroupSamplers) {
                 break;
             }
@@ -445,11 +444,11 @@
 
         if (!didCreateBindGroupViews || !didCreateBindGroupSamplers) {
             if (!didCreateBindGroupViews) {
-                DAWN_TRY(mViewAllocator->AllocateAndSwitchShaderVisibleHeap());
+                DAWN_TRY(viewAllocator->AllocateAndSwitchShaderVisibleHeap());
             }
 
             if (!didCreateBindGroupSamplers) {
-                DAWN_TRY(mSamplerAllocator->AllocateAndSwitchShaderVisibleHeap());
+                DAWN_TRY(samplerAllocator->AllocateAndSwitchShaderVisibleHeap());
             }
 
             mDirtyBindGroupsObjectChangedOrIsDynamic |= mBindGroupLayoutsMask;
@@ -460,8 +459,8 @@
 
             for (BindGroupIndex index : IterateBitSet(mBindGroupLayoutsMask)) {
                 BindGroup* group = ToBackend(mBindGroups[index]);
-                didCreateBindGroupViews = group->PopulateViews(mViewAllocator);
-                didCreateBindGroupSamplers = group->PopulateSamplers(mDevice, mSamplerAllocator);
+                didCreateBindGroupViews = group->PopulateViews(viewAllocator);
+                didCreateBindGroupSamplers = group->PopulateSamplers(samplerAllocator);
                 DAWN_ASSERT(didCreateBindGroupViews);
                 DAWN_ASSERT(didCreateBindGroupSamplers);
             }
@@ -646,11 +645,6 @@
     raw_ptr<DescriptorHeapState> mHeapState;
 
     PerBindGroup<D3D12_GPU_DESCRIPTOR_HANDLE> mBoundRootSamplerTables = {};
-
-    // TODO(https://crbug.com/dawn/2361): Rewrite those members with raw_ref<T>.
-    // This is currently failing with MSVC cl.exe compiler.
-    RAW_PTR_EXCLUSION MutexProtected<ShaderVisibleDescriptorAllocator>& mViewAllocator;
-    RAW_PTR_EXCLUSION MutexProtected<ShaderVisibleDescriptorAllocator>& mSamplerAllocator;
 };
 
 class DescriptorHeapState {
diff --git a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
index 188714a..ebf8e15 100644
--- a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
+++ b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.cpp
@@ -73,14 +73,15 @@
     DAWN_ASSERT(!mCPUAllocation.IsValid());
 }
 
-bool SamplerHeapCacheEntry::Populate(Device* device,
-                                     MutexProtected<ShaderVisibleDescriptorAllocator>& allocator) {
+bool SamplerHeapCacheEntry::Populate(MutexProtected<ShaderVisibleDescriptorAllocator>& allocator) {
     if (allocator->IsAllocationStillValid(mGPUAllocation)) {
         return true;
     }
 
     DAWN_ASSERT(!mSamplers.empty());
 
+    Device* device = allocator->GetDevice();
+
     // Attempt to allocate descriptors for the currently bound shader-visible heaps.
     // If either failed, return early to re-allocate and switch the heaps.
     const uint32_t descriptorCount = mSamplers.size();
diff --git a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
index af9baf2..a76d17e 100644
--- a/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
+++ b/src/dawn/native/d3d12/SamplerHeapCacheD3D12.h
@@ -74,7 +74,7 @@
 
     std::vector<Sampler*>&& AcquireSamplers();
 
-    bool Populate(Device* device, MutexProtected<ShaderVisibleDescriptorAllocator>& allocator);
+    bool Populate(MutexProtected<ShaderVisibleDescriptorAllocator>& allocator);
 
     // Functors necessary for the unordered_map<SamplerHeapCacheEntry*>-based cache.
     struct HashFunc {
diff --git a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
index bae9a74..b52ca79 100644
--- a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
+++ b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.cpp
@@ -228,6 +228,10 @@
     return {};
 }
 
+Device* ShaderVisibleDescriptorAllocator::GetDevice() const {
+    return mDevice;
+}
+
 HeapVersionID ShaderVisibleDescriptorAllocator::GetShaderVisibleHeapSerialForTesting() const {
     return mHeapSerial;
 }
diff --git a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
index 8c993f6..aadf980 100644
--- a/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
+++ b/src/dawn/native/d3d12/ShaderVisibleDescriptorAllocatorD3D12.h
@@ -88,6 +88,8 @@
 
     bool IsAllocationStillValid(const GPUDescriptorHeapAllocation& allocation) const;
 
+    Device* GetDevice() const;
+
   private:
     struct SerialDescriptorHeap {
         ExecutionSerial heapSerial;