D3D12: Move resource into allocation handle.

Allows buffer/texture direct access to underlying resource rather than indirectly with a opaque memory type.

BUG=dawn:27

Change-Id: I2eb69f4e30c96c431dbc96094d671be1e0a29869
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/11800
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
diff --git a/BUILD.gn b/BUILD.gn
index acf0042..6da0aff 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -279,8 +279,8 @@
       "src/dawn_native/d3d12/ResourceAllocator.h",
       "src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp",
       "src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h",
-      "src/dawn_native/d3d12/ResourceHeapD3D12.cpp",
-      "src/dawn_native/d3d12/ResourceHeapD3D12.h",
+      "src/dawn_native/d3d12/ResourceHeapAllocationD3D12.cpp",
+      "src/dawn_native/d3d12/ResourceHeapAllocationD3D12.h",
       "src/dawn_native/d3d12/SamplerD3D12.cpp",
       "src/dawn_native/d3d12/SamplerD3D12.h",
       "src/dawn_native/d3d12/ShaderModuleD3D12.cpp",
diff --git a/src/dawn_native/d3d12/BufferD3D12.cpp b/src/dawn_native/d3d12/BufferD3D12.cpp
index 148a740..b8544bd 100644
--- a/src/dawn_native/d3d12/BufferD3D12.cpp
+++ b/src/dawn_native/d3d12/BufferD3D12.cpp
@@ -18,7 +18,6 @@
 #include "common/Constants.h"
 #include "common/Math.h"
 #include "dawn_native/d3d12/DeviceD3D12.h"
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
 
 namespace dawn_native { namespace d3d12 {
 
@@ -125,7 +124,7 @@
     }
 
     ComPtr<ID3D12Resource> Buffer::GetD3D12Resource() const {
-        return ToBackend(mResourceAllocation.GetResourceHeap())->GetD3D12Resource();
+        return mResourceAllocation.GetD3D12Resource();
     }
 
     // When true is returned, a D3D12_RESOURCE_BARRIER has been created and must be used in a
@@ -198,7 +197,7 @@
     }
 
     D3D12_GPU_VIRTUAL_ADDRESS Buffer::GetVA() const {
-        return ToBackend(mResourceAllocation.GetResourceHeap())->GetGPUPointer();
+        return mResourceAllocation.GetGPUPointer();
     }
 
     void Buffer::OnMapCommandSerialFinished(uint32_t mapSerial, void* data, bool isWrite) {
diff --git a/src/dawn_native/d3d12/BufferD3D12.h b/src/dawn_native/d3d12/BufferD3D12.h
index 7a9b433..f7ccd13 100644
--- a/src/dawn_native/d3d12/BufferD3D12.h
+++ b/src/dawn_native/d3d12/BufferD3D12.h
@@ -18,7 +18,7 @@
 #include "common/SerialQueue.h"
 #include "dawn_native/Buffer.h"
 
-#include "dawn_native/ResourceMemoryAllocation.h"
+#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 namespace dawn_native { namespace d3d12 {
@@ -51,7 +51,7 @@
         bool IsMapWritable() const override;
         virtual MaybeError MapAtCreationImpl(uint8_t** mappedPointer) override;
 
-        ResourceMemoryAllocation mResourceAllocation;
+        ResourceHeapAllocation mResourceAllocation;
         bool mFixedResourceState = false;
         dawn::BufferUsage mLastUsage = dawn::BufferUsage::None;
         Serial mLastUsedSerial = UINT64_MAX;
diff --git a/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.cpp b/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.cpp
index 115ade4..9a55e69 100644
--- a/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.cpp
+++ b/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.cpp
@@ -14,7 +14,6 @@
 
 #include "dawn_native/d3d12/CommittedResourceAllocatorD3D12.h"
 #include "dawn_native/d3d12/DeviceD3D12.h"
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
 
 namespace dawn_native { namespace d3d12 {
 
@@ -22,7 +21,7 @@
         : mDevice(device), mHeapType(heapType) {
     }
 
-    ResultOrError<ResourceMemoryAllocation> CommittedResourceAllocator::Allocate(
+    ResultOrError<ResourceHeapAllocation> CommittedResourceAllocator::Allocate(
         const D3D12_RESOURCE_DESC& resourceDescriptor,
         D3D12_RESOURCE_STATES initialUsage,
         D3D12_HEAP_FLAGS heapFlags) {
@@ -43,13 +42,11 @@
         AllocationInfo info;
         info.mMethod = AllocationMethod::kDirect;
 
-        return ResourceMemoryAllocation{info,
-                                        /*offset*/ 0,
-                                        new ResourceHeap(std::move(committedResource))};
+        return ResourceHeapAllocation{info,
+                                      /*offset*/ 0, std::move(committedResource)};
     }
 
-    void CommittedResourceAllocator::Deallocate(ResourceMemoryAllocation& allocation) {
-        std::unique_ptr<ResourceHeap> resourceHeap(ToBackend(allocation.GetResourceHeap()));
-        mDevice->ReferenceUntilUnused(resourceHeap->GetD3D12Resource());
+    void CommittedResourceAllocator::Deallocate(ResourceHeapAllocation& allocation) {
+        mDevice->ReferenceUntilUnused(allocation.GetD3D12Resource());
     }
 }}  // namespace dawn_native::d3d12
diff --git a/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.h b/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.h
index 419d1c6..7bfb9d8 100644
--- a/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.h
+++ b/src/dawn_native/d3d12/CommittedResourceAllocatorD3D12.h
@@ -17,7 +17,7 @@
 
 #include "common/SerialQueue.h"
 #include "dawn_native/Error.h"
-#include "dawn_native/ResourceMemoryAllocation.h"
+#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 namespace dawn_native { namespace d3d12 {
@@ -31,11 +31,11 @@
         CommittedResourceAllocator(Device* device, D3D12_HEAP_TYPE heapType);
         ~CommittedResourceAllocator() = default;
 
-        ResultOrError<ResourceMemoryAllocation> Allocate(
+        ResultOrError<ResourceHeapAllocation> Allocate(
             const D3D12_RESOURCE_DESC& resourceDescriptor,
             D3D12_RESOURCE_STATES initialUsage,
             D3D12_HEAP_FLAGS heapFlags);
-        void Deallocate(ResourceMemoryAllocation& allocation);
+        void Deallocate(ResourceHeapAllocation& allocation);
 
       private:
         Device* mDevice;
diff --git a/src/dawn_native/d3d12/DeviceD3D12.cpp b/src/dawn_native/d3d12/DeviceD3D12.cpp
index 651b366..b3c4c97 100644
--- a/src/dawn_native/d3d12/DeviceD3D12.cpp
+++ b/src/dawn_native/d3d12/DeviceD3D12.cpp
@@ -32,7 +32,6 @@
 #include "dawn_native/d3d12/RenderPipelineD3D12.h"
 #include "dawn_native/d3d12/ResourceAllocator.h"
 #include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h"
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
 #include "dawn_native/d3d12/SamplerD3D12.h"
 #include "dawn_native/d3d12/ShaderModuleD3D12.h"
 #include "dawn_native/d3d12/StagingBufferD3D12.h"
@@ -348,11 +347,11 @@
         return {};
     }
 
-    void Device::DeallocateMemory(ResourceMemoryAllocation& allocation) {
+    void Device::DeallocateMemory(ResourceHeapAllocation& allocation) {
         mResourceAllocatorManager->DeallocateMemory(allocation);
     }
 
-    ResultOrError<ResourceMemoryAllocation> Device::AllocateMemory(
+    ResultOrError<ResourceHeapAllocation> Device::AllocateMemory(
         D3D12_HEAP_TYPE heapType,
         const D3D12_RESOURCE_DESC& resourceDescriptor,
         D3D12_RESOURCE_STATES initialUsage,
diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h
index e1a2fe2..eb57543 100644
--- a/src/dawn_native/d3d12/DeviceD3D12.h
+++ b/src/dawn_native/d3d12/DeviceD3D12.h
@@ -19,8 +19,8 @@
 
 #include "common/SerialQueue.h"
 #include "dawn_native/Device.h"
-#include "dawn_native/ResourceMemoryAllocation.h"
 #include "dawn_native/d3d12/Forward.h"
+#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 #include <memory>
@@ -87,13 +87,13 @@
                                            uint64_t destinationOffset,
                                            uint64_t size) override;
 
-        ResultOrError<ResourceMemoryAllocation> AllocateMemory(
+        ResultOrError<ResourceHeapAllocation> AllocateMemory(
             D3D12_HEAP_TYPE heapType,
             const D3D12_RESOURCE_DESC& resourceDescriptor,
             D3D12_RESOURCE_STATES initialUsage,
             D3D12_HEAP_FLAGS heapFlags);
 
-        void DeallocateMemory(ResourceMemoryAllocation& allocation);
+        void DeallocateMemory(ResourceHeapAllocation& allocation);
 
         TextureBase* WrapSharedHandle(const TextureDescriptor* descriptor, HANDLE sharedHandle);
 
diff --git a/src/dawn_native/d3d12/Forward.h b/src/dawn_native/d3d12/Forward.h
index f42f824..ade12e3 100644
--- a/src/dawn_native/d3d12/Forward.h
+++ b/src/dawn_native/d3d12/Forward.h
@@ -29,7 +29,6 @@
     class PipelineLayout;
     class Queue;
     class RenderPipeline;
-    class ResourceHeap;
     class Sampler;
     class ShaderModule;
     class StagingBuffer;
@@ -48,7 +47,6 @@
         using PipelineLayoutType = PipelineLayout;
         using QueueType = Queue;
         using RenderPipelineType = RenderPipeline;
-        using ResourceHeapType = ResourceHeap;
         using SamplerType = Sampler;
         using ShaderModuleType = ShaderModule;
         using StagingBufferType = StagingBuffer;
diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp
index 9562892..b18c998 100644
--- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp
+++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.cpp
@@ -14,14 +14,13 @@
 
 #include "dawn_native/d3d12/ResourceAllocatorManagerD3D12.h"
 #include "dawn_native/d3d12/Forward.h"
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
 
 namespace dawn_native { namespace d3d12 {
 
     ResourceAllocatorManager::ResourceAllocatorManager(Device* device) : mDevice(device) {
     }
 
-    ResultOrError<ResourceMemoryAllocation> ResourceAllocatorManager::AllocateMemory(
+    ResultOrError<ResourceHeapAllocation> ResourceAllocatorManager::AllocateMemory(
         D3D12_HEAP_TYPE heapType,
         const D3D12_RESOURCE_DESC& resourceDescriptor,
         D3D12_RESOURCE_STATES initialUsage,
@@ -37,7 +36,7 @@
             allocator = mDirectResourceAllocators[heapTypeIndex].get();
         }
 
-        ResourceMemoryAllocation allocation;
+        ResourceHeapAllocation allocation;
         DAWN_TRY_ASSIGN(allocation,
                         allocator->Allocate(resourceDescriptor, initialUsage, heapFlags));
 
@@ -50,15 +49,13 @@
         return heapType - 1;
     }
 
-    void ResourceAllocatorManager::DeallocateMemory(ResourceMemoryAllocation& allocation) {
+    void ResourceAllocatorManager::DeallocateMemory(ResourceHeapAllocation& allocation) {
         if (allocation.GetInfo().mMethod == AllocationMethod::kInvalid) {
             return;
         }
         CommittedResourceAllocator* allocator = nullptr;
         D3D12_HEAP_PROPERTIES heapProp;
-        ToBackend(allocation.GetResourceHeap())
-            ->GetD3D12Resource()
-            ->GetHeapProperties(&heapProp, nullptr);
+        allocation.GetD3D12Resource()->GetHeapProperties(&heapProp, nullptr);
         const size_t heapTypeIndex = GetD3D12HeapTypeToIndex(heapProp.Type);
         ASSERT(heapTypeIndex < kNumHeapTypes);
         allocator = mDirectResourceAllocators[heapTypeIndex].get();
diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h
index 7de6c58..d8f1cdb 100644
--- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h
+++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h
@@ -29,13 +29,13 @@
       public:
         ResourceAllocatorManager(Device* device);
 
-        ResultOrError<ResourceMemoryAllocation> AllocateMemory(
+        ResultOrError<ResourceHeapAllocation> AllocateMemory(
             D3D12_HEAP_TYPE heapType,
             const D3D12_RESOURCE_DESC& resourceDescriptor,
             D3D12_RESOURCE_STATES initialUsage,
             D3D12_HEAP_FLAGS heapFlags);
 
-        void DeallocateMemory(ResourceMemoryAllocation& allocation);
+        void DeallocateMemory(ResourceHeapAllocation& allocation);
 
       private:
         size_t GetD3D12HeapTypeToIndex(D3D12_HEAP_TYPE heapType) const;
diff --git a/src/dawn_native/d3d12/ResourceHeapAllocationD3D12.cpp b/src/dawn_native/d3d12/ResourceHeapAllocationD3D12.cpp
new file mode 100644
index 0000000..a559834
--- /dev/null
+++ b/src/dawn_native/d3d12/ResourceHeapAllocationD3D12.cpp
@@ -0,0 +1,31 @@
+// Copyright 2019 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
+
+namespace dawn_native { namespace d3d12 {
+    ResourceHeapAllocation::ResourceHeapAllocation(const AllocationInfo& info,
+                                                   uint64_t offset,
+                                                   ComPtr<ID3D12Resource> resource)
+        : ResourceMemoryAllocation(info, offset, nullptr), mResource(std::move(resource)) {
+    }
+
+    ComPtr<ID3D12Resource> ResourceHeapAllocation::GetD3D12Resource() const {
+        return mResource;
+    }
+
+    D3D12_GPU_VIRTUAL_ADDRESS ResourceHeapAllocation::GetGPUPointer() const {
+        return mResource->GetGPUVirtualAddress();
+    }
+}}  // namespace dawn_native::d3d12
\ No newline at end of file
diff --git a/src/dawn_native/d3d12/ResourceHeapD3D12.h b/src/dawn_native/d3d12/ResourceHeapAllocationD3D12.h
similarity index 62%
rename from src/dawn_native/d3d12/ResourceHeapD3D12.h
rename to src/dawn_native/d3d12/ResourceHeapAllocationD3D12.h
index 18b342a..8230857 100644
--- a/src/dawn_native/d3d12/ResourceHeapD3D12.h
+++ b/src/dawn_native/d3d12/ResourceHeapAllocationD3D12.h
@@ -12,20 +12,21 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#ifndef DAWNNATIVE_D3D12_RESOURCEHEAPD3D12_H_
-#define DAWNNATIVE_D3D12_RESOURCEHEAPD3D12_H_
+#ifndef DAWNNATIVE_D3D12_RESOURCEHEAPALLOCATIOND3D12_H_
+#define DAWNNATIVE_D3D12_RESOURCEHEAPALLOCATIOND3D12_H_
 
-#include "dawn_native/ResourceHeap.h"
+#include "dawn_native/ResourceMemoryAllocation.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 namespace dawn_native { namespace d3d12 {
 
-    // Wrapper for physical memory used with or without a resource object.
-    class ResourceHeap : public ResourceHeapBase {
+    class ResourceHeapAllocation : public ResourceMemoryAllocation {
       public:
-        ResourceHeap(ComPtr<ID3D12Resource> resource);
-
-        ~ResourceHeap() = default;
+        ResourceHeapAllocation() = default;
+        ResourceHeapAllocation(const AllocationInfo& info,
+                               uint64_t offset,
+                               ComPtr<ID3D12Resource> resource);
+        ~ResourceHeapAllocation() = default;
 
         ComPtr<ID3D12Resource> GetD3D12Resource() const;
         D3D12_GPU_VIRTUAL_ADDRESS GetGPUPointer() const;
@@ -35,4 +36,4 @@
     };
 }}  // namespace dawn_native::d3d12
 
-#endif  // DAWNNATIVE_D3D12_RESOURCEHEAPD3D12_H_
\ No newline at end of file
+#endif  // DAWNNATIVE_D3D12_RESOURCEHEAPALLOCATIOND3D12_H_
\ No newline at end of file
diff --git a/src/dawn_native/d3d12/ResourceHeapD3D12.cpp b/src/dawn_native/d3d12/ResourceHeapD3D12.cpp
deleted file mode 100644
index 5aec4b3..0000000
--- a/src/dawn_native/d3d12/ResourceHeapD3D12.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2019 The Dawn Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
-#include "dawn_native/d3d12/DeviceD3D12.h"
-
-namespace dawn_native { namespace d3d12 {
-
-    ResourceHeap::ResourceHeap(ComPtr<ID3D12Resource> resource) : mResource(resource) {
-    }
-
-    ComPtr<ID3D12Resource> ResourceHeap::GetD3D12Resource() const {
-        return mResource;
-    }
-
-    D3D12_GPU_VIRTUAL_ADDRESS ResourceHeap::GetGPUPointer() const {
-        return mResource->GetGPUVirtualAddress();
-    }
-}}  // namespace dawn_native::d3d12
\ No newline at end of file
diff --git a/src/dawn_native/d3d12/StagingBufferD3D12.cpp b/src/dawn_native/d3d12/StagingBufferD3D12.cpp
index cab3a18..9e6c2bd 100644
--- a/src/dawn_native/d3d12/StagingBufferD3D12.cpp
+++ b/src/dawn_native/d3d12/StagingBufferD3D12.cpp
@@ -14,7 +14,6 @@
 
 #include "dawn_native/d3d12/StagingBufferD3D12.h"
 #include "dawn_native/d3d12/DeviceD3D12.h"
-#include "dawn_native/d3d12/ResourceHeapD3D12.h"
 
 namespace dawn_native { namespace d3d12 {
 
@@ -56,7 +55,7 @@
     }
 
     ID3D12Resource* StagingBuffer::GetResource() const {
-        return ToBackend(mUploadHeap.GetResourceHeap())->GetD3D12Resource().Get();
+        return mUploadHeap.GetD3D12Resource().Get();
     }
 
 }}  // namespace dawn_native::d3d12
diff --git a/src/dawn_native/d3d12/StagingBufferD3D12.h b/src/dawn_native/d3d12/StagingBufferD3D12.h
index 633be53..ebba0c6 100644
--- a/src/dawn_native/d3d12/StagingBufferD3D12.h
+++ b/src/dawn_native/d3d12/StagingBufferD3D12.h
@@ -15,8 +15,8 @@
 #ifndef DAWNNATIVE_STAGINGBUFFERD3D12_H_
 #define DAWNNATIVE_STAGINGBUFFERD3D12_H_
 
-#include "dawn_native/ResourceMemoryAllocation.h"
 #include "dawn_native/StagingBuffer.h"
+#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
 #include "dawn_native/d3d12/d3d12_platform.h"
 
 namespace dawn_native { namespace d3d12 {
@@ -34,7 +34,7 @@
 
       private:
         Device* mDevice;
-        ResourceMemoryAllocation mUploadHeap;
+        ResourceHeapAllocation mUploadHeap;
     };
 }}  // namespace dawn_native::d3d12