From 145f115c5496a0c4ab17d13a20410143b0ce03d4 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Mon, 28 Sep 2020 12:28:13 +0000 Subject: [PATCH] SerialQueue/Map: Take the serial type as type paramater. This is in preparation for follow-up CLs that will use typed integers for the various serial types. Bug: dawn:442 Change-Id: I5296546e96acd6ac9f7a0bfc46dc7eba40cb3cf5 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/28921 Reviewed-by: Jiawei Shao Reviewed-by: Austin Eng Commit-Queue: Corentin Wallez --- src/common/SerialMap.h | 47 ++++++++++--------- src/common/SerialQueue.h | 46 +++++++++--------- src/common/SerialStorage.h | 5 +- src/dawn_native/DynamicUploader.h | 2 +- src/dawn_native/ErrorScopeTracker.h | 2 +- src/dawn_native/Fence.h | 2 +- src/dawn_native/FenceSignalTracker.h | 2 +- src/dawn_native/MapRequestTracker.h | 2 +- src/dawn_native/RingBufferAllocator.h | 4 +- src/dawn_native/d3d12/BufferD3D12.h | 2 +- .../d3d12/CommandAllocatorManager.h | 2 +- src/dawn_native/d3d12/DeviceD3D12.h | 2 +- .../d3d12/ResourceAllocatorManagerD3D12.h | 2 +- .../d3d12/StagingDescriptorAllocatorD3D12.h | 2 +- .../vulkan/DescriptorSetAllocator.h | 2 +- src/dawn_native/vulkan/DeviceVk.h | 4 +- src/dawn_native/vulkan/FencedDeleter.h | 30 ++++++------ .../vulkan/ResourceMemoryAllocatorVk.h | 2 +- src/tests/unittests/SerialMapTests.cpp | 21 ++++++++- src/tests/unittests/SerialQueueTests.cpp | 21 ++++++++- 20 files changed, 121 insertions(+), 81 deletions(-) diff --git a/src/common/SerialMap.h b/src/common/SerialMap.h index 93b0019789..fe3ca18891 100644 --- a/src/common/SerialMap.h +++ b/src/common/SerialMap.h @@ -20,54 +20,55 @@ #include #include -template +template class SerialMap; -template -struct SerialStorageTraits> { - using Value = T; - using Storage = std::map>; +template +struct SerialStorageTraits> { + using Serial = SerialT; + using Value = ValueT; + using Storage = std::map>; using StorageIterator = typename Storage::iterator; using ConstStorageIterator = typename Storage::const_iterator; }; -// SerialMap stores a map from Serial to T. +// SerialMap stores a map from Serial to Value. // Unlike SerialQueue, items may be enqueued with Serials in any // arbitrary order. SerialMap provides useful iterators for iterating -// through T items in order of increasing Serial. -template -class SerialMap : public SerialStorage> { +// through Value items in order of increasing Serial. +template +class SerialMap : public SerialStorage> { public: - void Enqueue(const T& value, Serial serial); - void Enqueue(T&& value, Serial serial); - void Enqueue(const std::vector& values, Serial serial); - void Enqueue(std::vector&& values, Serial serial); + void Enqueue(const Value& value, Serial serial); + void Enqueue(Value&& value, Serial serial); + void Enqueue(const std::vector& values, Serial serial); + void Enqueue(std::vector&& values, Serial serial); }; // SerialMap -template -void SerialMap::Enqueue(const T& value, Serial serial) { +template +void SerialMap::Enqueue(const Value& value, Serial serial) { this->mStorage[serial].emplace_back(value); } -template -void SerialMap::Enqueue(T&& value, Serial serial) { +template +void SerialMap::Enqueue(Value&& value, Serial serial) { this->mStorage[serial].emplace_back(value); } -template -void SerialMap::Enqueue(const std::vector& values, Serial serial) { +template +void SerialMap::Enqueue(const std::vector& values, Serial serial) { DAWN_ASSERT(values.size() > 0); - for (const T& value : values) { + for (const Value& value : values) { Enqueue(value, serial); } } -template -void SerialMap::Enqueue(std::vector&& values, Serial serial) { +template +void SerialMap::Enqueue(std::vector&& values, Serial serial) { DAWN_ASSERT(values.size() > 0); - for (const T& value : values) { + for (const Value& value : values) { Enqueue(value, serial); } } diff --git a/src/common/SerialQueue.h b/src/common/SerialQueue.h index 24303c7f60..5f2494786b 100644 --- a/src/common/SerialQueue.h +++ b/src/common/SerialQueue.h @@ -19,65 +19,65 @@ #include -template +template class SerialQueue; -template -struct SerialStorageTraits> { - using Value = T; - using SerialPair = std::pair>; +template +struct SerialStorageTraits> { + using Serial = SerialT; + using Value = ValueT; + using SerialPair = std::pair>; using Storage = std::vector; using StorageIterator = typename Storage::iterator; using ConstStorageIterator = typename Storage::const_iterator; }; -// SerialQueue stores an associative list mapping a Serial to T. +// SerialQueue stores an associative list mapping a Serial to Value. // It enforces that the Serials enqueued are strictly non-decreasing. // This makes it very efficient iterate or clear all items added up // to some Serial value because they are stored contiguously in memory. -template -class SerialQueue : public SerialStorage> { +template +class SerialQueue : public SerialStorage> { public: - using SerialPair = typename SerialStorageTraits>::SerialPair; // The serial must be given in (not strictly) increasing order. - void Enqueue(const T& value, Serial serial); - void Enqueue(T&& value, Serial serial); - void Enqueue(const std::vector& values, Serial serial); - void Enqueue(std::vector&& values, Serial serial); + void Enqueue(const Value& value, Serial serial); + void Enqueue(Value&& value, Serial serial); + void Enqueue(const std::vector& values, Serial serial); + void Enqueue(std::vector&& values, Serial serial); }; // SerialQueue -template -void SerialQueue::Enqueue(const T& value, Serial serial) { +template +void SerialQueue::Enqueue(const Value& value, Serial serial) { DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); if (this->Empty() || this->mStorage.back().first < serial) { - this->mStorage.emplace_back(serial, std::vector{}); + this->mStorage.emplace_back(serial, std::vector{}); } this->mStorage.back().second.push_back(value); } -template -void SerialQueue::Enqueue(T&& value, Serial serial) { +template +void SerialQueue::Enqueue(Value&& value, Serial serial) { DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); if (this->Empty() || this->mStorage.back().first < serial) { - this->mStorage.emplace_back(serial, std::vector{}); + this->mStorage.emplace_back(serial, std::vector{}); } this->mStorage.back().second.push_back(std::move(value)); } -template -void SerialQueue::Enqueue(const std::vector& values, Serial serial) { +template +void SerialQueue::Enqueue(const std::vector& values, Serial serial) { DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); this->mStorage.emplace_back(serial, values); } -template -void SerialQueue::Enqueue(std::vector&& values, Serial serial) { +template +void SerialQueue::Enqueue(std::vector&& values, Serial serial) { DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); this->mStorage.emplace_back(serial, values); diff --git a/src/common/SerialStorage.h b/src/common/SerialStorage.h index d71c7dd07d..4ff18f2f09 100644 --- a/src/common/SerialStorage.h +++ b/src/common/SerialStorage.h @@ -27,6 +27,7 @@ struct SerialStorageTraits {}; template class SerialStorage { private: + using Serial = typename SerialStorageTraits::Serial; using Value = typename SerialStorageTraits::Value; using Storage = typename SerialStorageTraits::Storage; using StorageIterator = typename SerialStorageTraits::StorageIterator; @@ -158,13 +159,13 @@ void SerialStorage::ClearUpTo(Serial serial) { } template -Serial SerialStorage::FirstSerial() const { +typename SerialStorage::Serial SerialStorage::FirstSerial() const { DAWN_ASSERT(!Empty()); return mStorage.begin()->first; } template -Serial SerialStorage::LastSerial() const { +typename SerialStorage::Serial SerialStorage::LastSerial() const { DAWN_ASSERT(!Empty()); return mStorage.back().first; } diff --git a/src/dawn_native/DynamicUploader.h b/src/dawn_native/DynamicUploader.h index a652bd8853..d58e425a10 100644 --- a/src/dawn_native/DynamicUploader.h +++ b/src/dawn_native/DynamicUploader.h @@ -56,7 +56,7 @@ namespace dawn_native { ResultOrError AllocateInternal(uint64_t allocationSize, Serial serial); std::vector> mRingBuffers; - SerialQueue> mReleasedStagingBuffers; + SerialQueue> mReleasedStagingBuffers; DeviceBase* mDevice; }; } // namespace dawn_native diff --git a/src/dawn_native/ErrorScopeTracker.h b/src/dawn_native/ErrorScopeTracker.h index 2ca651198f..ddedbcdf8b 100644 --- a/src/dawn_native/ErrorScopeTracker.h +++ b/src/dawn_native/ErrorScopeTracker.h @@ -34,7 +34,7 @@ namespace dawn_native { protected: DeviceBase* mDevice; - SerialQueue> mScopesInFlight; + SerialQueue> mScopesInFlight; }; } // namespace dawn_native diff --git a/src/dawn_native/Fence.h b/src/dawn_native/Fence.h index f5596599fe..b4fb821412 100644 --- a/src/dawn_native/Fence.h +++ b/src/dawn_native/Fence.h @@ -61,7 +61,7 @@ namespace dawn_native { uint64_t mSignalValue; uint64_t mCompletedValue; Ref mQueue; - SerialMap mRequests; + SerialMap mRequests; }; } // namespace dawn_native diff --git a/src/dawn_native/FenceSignalTracker.h b/src/dawn_native/FenceSignalTracker.h index 10026a44ef..623140f316 100644 --- a/src/dawn_native/FenceSignalTracker.h +++ b/src/dawn_native/FenceSignalTracker.h @@ -39,7 +39,7 @@ namespace dawn_native { private: DeviceBase* mDevice; - SerialQueue mFencesInFlight; + SerialQueue mFencesInFlight; }; } // namespace dawn_native diff --git a/src/dawn_native/MapRequestTracker.h b/src/dawn_native/MapRequestTracker.h index 78744e05e9..06eae3dbad 100644 --- a/src/dawn_native/MapRequestTracker.h +++ b/src/dawn_native/MapRequestTracker.h @@ -35,7 +35,7 @@ namespace dawn_native { Ref buffer; uint32_t mapSerial; }; - SerialQueue mInflightRequests; + SerialQueue mInflightRequests; }; } // namespace dawn_native diff --git a/src/dawn_native/RingBufferAllocator.h b/src/dawn_native/RingBufferAllocator.h index 50b0f15d00..0b9682a180 100644 --- a/src/dawn_native/RingBufferAllocator.h +++ b/src/dawn_native/RingBufferAllocator.h @@ -44,8 +44,8 @@ namespace dawn_native { uint64_t size; }; - SerialQueue mInflightRequests; // Queue of the recorded sub-alloc requests (e.g. - // frame of resources). + SerialQueue mInflightRequests; // Queue of the recorded sub-alloc requests + // (e.g. frame of resources). uint64_t mUsedEndOffset = 0; // Tail of used sub-alloc requests (in bytes). uint64_t mUsedStartOffset = 0; // Head of used sub-alloc requests (in bytes). diff --git a/src/dawn_native/d3d12/BufferD3D12.h b/src/dawn_native/d3d12/BufferD3D12.h index d355e321f8..6bba440f82 100644 --- a/src/dawn_native/d3d12/BufferD3D12.h +++ b/src/dawn_native/d3d12/BufferD3D12.h @@ -15,9 +15,9 @@ #ifndef DAWNNATIVE_D3D12_BUFFERD3D12_H_ #define DAWNNATIVE_D3D12_BUFFERD3D12_H_ -#include "common/SerialQueue.h" #include "dawn_native/Buffer.h" +#include "common/Serial.h" #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h" #include "dawn_native/d3d12/d3d12_platform.h" diff --git a/src/dawn_native/d3d12/CommandAllocatorManager.h b/src/dawn_native/d3d12/CommandAllocatorManager.h index 654d3be467..135ad208d0 100644 --- a/src/dawn_native/d3d12/CommandAllocatorManager.h +++ b/src/dawn_native/d3d12/CommandAllocatorManager.h @@ -49,7 +49,7 @@ namespace dawn_native { namespace d3d12 { ComPtr mCommandAllocators[kMaxCommandAllocators]; std::bitset mFreeAllocators; - SerialQueue mInFlightCommandAllocators; + SerialQueue mInFlightCommandAllocators; }; }} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/d3d12/DeviceD3D12.h b/src/dawn_native/d3d12/DeviceD3D12.h index 9e4399a6d8..a82dbfe75e 100644 --- a/src/dawn_native/d3d12/DeviceD3D12.h +++ b/src/dawn_native/d3d12/DeviceD3D12.h @@ -196,7 +196,7 @@ namespace dawn_native { namespace d3d12 { CommandRecordingContext mPendingCommands; - SerialQueue> mUsedComObjectRefs; + SerialQueue> mUsedComObjectRefs; std::unique_ptr mCommandAllocatorManager; std::unique_ptr mResourceAllocatorManager; diff --git a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h index 0bf2a02c61..b3900d35aa 100644 --- a/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h +++ b/src/dawn_native/d3d12/ResourceAllocatorManagerD3D12.h @@ -98,7 +98,7 @@ namespace dawn_native { namespace d3d12 { std::array, ResourceHeapKind::EnumCount> mPooledHeapAllocators; - SerialQueue mAllocationsToDelete; + SerialQueue mAllocationsToDelete; }; }} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/d3d12/StagingDescriptorAllocatorD3D12.h b/src/dawn_native/d3d12/StagingDescriptorAllocatorD3D12.h index 519920b465..24a76a446a 100644 --- a/src/dawn_native/d3d12/StagingDescriptorAllocatorD3D12.h +++ b/src/dawn_native/d3d12/StagingDescriptorAllocatorD3D12.h @@ -77,7 +77,7 @@ namespace dawn_native { namespace d3d12 { D3D12_DESCRIPTOR_HEAP_TYPE mHeapType; - SerialQueue mAllocationsToDelete; + SerialQueue mAllocationsToDelete; }; }} // namespace dawn_native::d3d12 diff --git a/src/dawn_native/vulkan/DescriptorSetAllocator.h b/src/dawn_native/vulkan/DescriptorSetAllocator.h index 44ea4a4325..1113edb788 100644 --- a/src/dawn_native/vulkan/DescriptorSetAllocator.h +++ b/src/dawn_native/vulkan/DescriptorSetAllocator.h @@ -61,7 +61,7 @@ namespace dawn_native { namespace vulkan { PoolIndex poolIndex; SetIndex setIndex; }; - SerialQueue mPendingDeallocations; + SerialQueue mPendingDeallocations; Serial mLastDeallocationSerial = 0; }; diff --git a/src/dawn_native/vulkan/DeviceVk.h b/src/dawn_native/vulkan/DeviceVk.h index 1b44bb7e3c..68c53733ab 100644 --- a/src/dawn_native/vulkan/DeviceVk.h +++ b/src/dawn_native/vulkan/DeviceVk.h @@ -161,7 +161,7 @@ namespace dawn_native { namespace vulkan { VkQueue mQueue = VK_NULL_HANDLE; uint32_t mComputeSubgroupSize = 0; - SerialQueue> mBindGroupLayoutsPendingDeallocation; + SerialQueue> mBindGroupLayoutsPendingDeallocation; std::unique_ptr mDeleter; std::unique_ptr mResourceMemoryAllocator; std::unique_ptr mRenderPassCache; @@ -187,7 +187,7 @@ namespace dawn_native { namespace vulkan { VkCommandPool pool = VK_NULL_HANDLE; VkCommandBuffer commandBuffer = VK_NULL_HANDLE; }; - SerialQueue mCommandsInFlight; + SerialQueue mCommandsInFlight; // Command pools in the unused list haven't been reset yet. std::vector mUnusedCommands; // There is always a valid recording context stored in mRecordingContext diff --git a/src/dawn_native/vulkan/FencedDeleter.h b/src/dawn_native/vulkan/FencedDeleter.h index 9e516b1006..5f897e40a1 100644 --- a/src/dawn_native/vulkan/FencedDeleter.h +++ b/src/dawn_native/vulkan/FencedDeleter.h @@ -47,21 +47,21 @@ namespace dawn_native { namespace vulkan { private: Device* mDevice = nullptr; - SerialQueue mBuffersToDelete; - SerialQueue mDescriptorPoolsToDelete; - SerialQueue mMemoriesToDelete; - SerialQueue mFramebuffersToDelete; - SerialQueue mImagesToDelete; - SerialQueue mImageViewsToDelete; - SerialQueue mPipelinesToDelete; - SerialQueue mPipelineLayoutsToDelete; - SerialQueue mQueryPoolsToDelete; - SerialQueue mRenderPassesToDelete; - SerialQueue mSamplersToDelete; - SerialQueue mSemaphoresToDelete; - SerialQueue mShaderModulesToDelete; - SerialQueue mSurfacesToDelete; - SerialQueue mSwapChainsToDelete; + SerialQueue mBuffersToDelete; + SerialQueue mDescriptorPoolsToDelete; + SerialQueue mMemoriesToDelete; + SerialQueue mFramebuffersToDelete; + SerialQueue mImagesToDelete; + SerialQueue mImageViewsToDelete; + SerialQueue mPipelinesToDelete; + SerialQueue mPipelineLayoutsToDelete; + SerialQueue mQueryPoolsToDelete; + SerialQueue mRenderPassesToDelete; + SerialQueue mSamplersToDelete; + SerialQueue mSemaphoresToDelete; + SerialQueue mShaderModulesToDelete; + SerialQueue mSurfacesToDelete; + SerialQueue mSwapChainsToDelete; }; }} // namespace dawn_native::vulkan diff --git a/src/dawn_native/vulkan/ResourceMemoryAllocatorVk.h b/src/dawn_native/vulkan/ResourceMemoryAllocatorVk.h index 04176a3300..54a3aa9acb 100644 --- a/src/dawn_native/vulkan/ResourceMemoryAllocatorVk.h +++ b/src/dawn_native/vulkan/ResourceMemoryAllocatorVk.h @@ -49,7 +49,7 @@ namespace dawn_native { namespace vulkan { class SingleTypeAllocator; std::vector> mAllocatorsPerType; - SerialQueue mSubAllocationsToDelete; + SerialQueue mSubAllocationsToDelete; }; }} // namespace dawn_native::vulkan diff --git a/src/tests/unittests/SerialMapTests.cpp b/src/tests/unittests/SerialMapTests.cpp index e5d23e9dba..d86b11c164 100644 --- a/src/tests/unittests/SerialMapTests.cpp +++ b/src/tests/unittests/SerialMapTests.cpp @@ -15,8 +15,9 @@ #include #include "common/SerialMap.h" +#include "common/TypedInteger.h" -using TestSerialMap = SerialMap; +using TestSerialMap = SerialMap; // A number of basic tests for SerialMap that are difficult to split from one another TEST(SerialMap, BasicTest) { @@ -162,3 +163,21 @@ TEST(SerialMap, FirstSerial) { map.Enqueue(vector1, 6); EXPECT_EQ(map.FirstSerial(), 6u); } + +// Test basic functionality with type integers +TEST(SerialMap, TypedInteger) { + using MySerial = TypedInteger; + using MySerialMap = SerialMap; + + MySerialMap map; + map.Enqueue(1, MySerial(0)); + map.Enqueue(2, MySerial(0)); + + std::vector expectedValues = {1, 2}; + for (int value : map.IterateAll()) { + EXPECT_EQ(expectedValues.front(), value); + ASSERT_FALSE(expectedValues.empty()); + expectedValues.erase(expectedValues.begin()); + } + ASSERT_TRUE(expectedValues.empty()); +} diff --git a/src/tests/unittests/SerialQueueTests.cpp b/src/tests/unittests/SerialQueueTests.cpp index 2545c51929..5161b81b71 100644 --- a/src/tests/unittests/SerialQueueTests.cpp +++ b/src/tests/unittests/SerialQueueTests.cpp @@ -15,8 +15,9 @@ #include #include "common/SerialQueue.h" +#include "common/TypedInteger.h" -using TestSerialQueue = SerialQueue; +using TestSerialQueue = SerialQueue; // A number of basic tests for SerialQueue that are difficult to split from one another TEST(SerialQueue, BasicTest) { @@ -154,3 +155,21 @@ TEST(SerialQueue, LastSerial) { queue.Enqueue({2}, 1); EXPECT_EQ(queue.LastSerial(), 1u); } + +// Test basic functionality with type integers +TEST(SerialQueue, TypedInteger) { + using MySerial = TypedInteger; + using MySerialQueue = SerialQueue; + + MySerialQueue queue; + queue.Enqueue(1, MySerial(0)); + queue.Enqueue(2, MySerial(0)); + + std::vector expectedValues = {1, 2}; + for (int value : queue.IterateAll()) { + EXPECT_EQ(expectedValues.front(), value); + ASSERT_FALSE(expectedValues.empty()); + expectedValues.erase(expectedValues.begin()); + } + ASSERT_TRUE(expectedValues.empty()); +}