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 <jiawei.shao@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2020-09-28 12:28:13 +00:00 committed by Commit Bot service account
parent ed2b465f86
commit 145f115c54
20 changed files with 121 additions and 81 deletions

View File

@ -20,54 +20,55 @@
#include <map> #include <map>
#include <vector> #include <vector>
template <typename T> template <typename Serial, typename Value>
class SerialMap; class SerialMap;
template <typename T> template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialMap<T>> { struct SerialStorageTraits<SerialMap<SerialT, ValueT>> {
using Value = T; using Serial = SerialT;
using Storage = std::map<Serial, std::vector<T>>; using Value = ValueT;
using Storage = std::map<Serial, std::vector<Value>>;
using StorageIterator = typename Storage::iterator; using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_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 // Unlike SerialQueue, items may be enqueued with Serials in any
// arbitrary order. SerialMap provides useful iterators for iterating // arbitrary order. SerialMap provides useful iterators for iterating
// through T items in order of increasing Serial. // through Value items in order of increasing Serial.
template <typename T> template <typename Serial, typename Value>
class SerialMap : public SerialStorage<SerialMap<T>> { class SerialMap : public SerialStorage<SerialMap<Serial, Value>> {
public: public:
void Enqueue(const T& value, Serial serial); void Enqueue(const Value& value, Serial serial);
void Enqueue(T&& value, Serial serial); void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial); void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial); void Enqueue(std::vector<Value>&& values, Serial serial);
}; };
// SerialMap // SerialMap
template <typename T> template <typename Serial, typename Value>
void SerialMap<T>::Enqueue(const T& value, Serial serial) { void SerialMap<Serial, Value>::Enqueue(const Value& value, Serial serial) {
this->mStorage[serial].emplace_back(value); this->mStorage[serial].emplace_back(value);
} }
template <typename T> template <typename Serial, typename Value>
void SerialMap<T>::Enqueue(T&& value, Serial serial) { void SerialMap<Serial, Value>::Enqueue(Value&& value, Serial serial) {
this->mStorage[serial].emplace_back(value); this->mStorage[serial].emplace_back(value);
} }
template <typename T> template <typename Serial, typename Value>
void SerialMap<T>::Enqueue(const std::vector<T>& values, Serial serial) { void SerialMap<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(values.size() > 0);
for (const T& value : values) { for (const Value& value : values) {
Enqueue(value, serial); Enqueue(value, serial);
} }
} }
template <typename T> template <typename Serial, typename Value>
void SerialMap<T>::Enqueue(std::vector<T>&& values, Serial serial) { void SerialMap<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(values.size() > 0);
for (const T& value : values) { for (const Value& value : values) {
Enqueue(value, serial); Enqueue(value, serial);
} }
} }

View File

@ -19,65 +19,65 @@
#include <vector> #include <vector>
template <typename T> template <typename Serial, typename Value>
class SerialQueue; class SerialQueue;
template <typename T> template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialQueue<T>> { struct SerialStorageTraits<SerialQueue<SerialT, ValueT>> {
using Value = T; using Serial = SerialT;
using SerialPair = std::pair<Serial, std::vector<T>>; using Value = ValueT;
using SerialPair = std::pair<Serial, std::vector<Value>>;
using Storage = std::vector<SerialPair>; using Storage = std::vector<SerialPair>;
using StorageIterator = typename Storage::iterator; using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_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. // It enforces that the Serials enqueued are strictly non-decreasing.
// This makes it very efficient iterate or clear all items added up // This makes it very efficient iterate or clear all items added up
// to some Serial value because they are stored contiguously in memory. // to some Serial value because they are stored contiguously in memory.
template <typename T> template <typename Serial, typename Value>
class SerialQueue : public SerialStorage<SerialQueue<T>> { class SerialQueue : public SerialStorage<SerialQueue<Serial, Value>> {
public: public:
using SerialPair = typename SerialStorageTraits<SerialQueue<T>>::SerialPair;
// The serial must be given in (not strictly) increasing order. // The serial must be given in (not strictly) increasing order.
void Enqueue(const T& value, Serial serial); void Enqueue(const Value& value, Serial serial);
void Enqueue(T&& value, Serial serial); void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial); void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial); void Enqueue(std::vector<Value>&& values, Serial serial);
}; };
// SerialQueue // SerialQueue
template <typename T> template <typename Serial, typename Value>
void SerialQueue<T>::Enqueue(const T& value, Serial serial) { void SerialQueue<Serial, Value>::Enqueue(const Value& value, Serial serial) {
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
if (this->Empty() || this->mStorage.back().first < serial) { if (this->Empty() || this->mStorage.back().first < serial) {
this->mStorage.emplace_back(serial, std::vector<T>{}); this->mStorage.emplace_back(serial, std::vector<Value>{});
} }
this->mStorage.back().second.push_back(value); this->mStorage.back().second.push_back(value);
} }
template <typename T> template <typename Serial, typename Value>
void SerialQueue<T>::Enqueue(T&& value, Serial serial) { void SerialQueue<Serial, Value>::Enqueue(Value&& value, Serial serial) {
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
if (this->Empty() || this->mStorage.back().first < serial) { if (this->Empty() || this->mStorage.back().first < serial) {
this->mStorage.emplace_back(serial, std::vector<T>{}); this->mStorage.emplace_back(serial, std::vector<Value>{});
} }
this->mStorage.back().second.push_back(std::move(value)); this->mStorage.back().second.push_back(std::move(value));
} }
template <typename T> template <typename Serial, typename Value>
void SerialQueue<T>::Enqueue(const std::vector<T>& values, Serial serial) { void SerialQueue<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values); this->mStorage.emplace_back(serial, values);
} }
template <typename T> template <typename Serial, typename Value>
void SerialQueue<T>::Enqueue(std::vector<T>&& values, Serial serial) { void SerialQueue<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0); DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial); DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values); this->mStorage.emplace_back(serial, values);

View File

@ -27,6 +27,7 @@ struct SerialStorageTraits {};
template <typename Derived> template <typename Derived>
class SerialStorage { class SerialStorage {
private: private:
using Serial = typename SerialStorageTraits<Derived>::Serial;
using Value = typename SerialStorageTraits<Derived>::Value; using Value = typename SerialStorageTraits<Derived>::Value;
using Storage = typename SerialStorageTraits<Derived>::Storage; using Storage = typename SerialStorageTraits<Derived>::Storage;
using StorageIterator = typename SerialStorageTraits<Derived>::StorageIterator; using StorageIterator = typename SerialStorageTraits<Derived>::StorageIterator;
@ -158,13 +159,13 @@ void SerialStorage<Derived>::ClearUpTo(Serial serial) {
} }
template <typename Derived> template <typename Derived>
Serial SerialStorage<Derived>::FirstSerial() const { typename SerialStorage<Derived>::Serial SerialStorage<Derived>::FirstSerial() const {
DAWN_ASSERT(!Empty()); DAWN_ASSERT(!Empty());
return mStorage.begin()->first; return mStorage.begin()->first;
} }
template <typename Derived> template <typename Derived>
Serial SerialStorage<Derived>::LastSerial() const { typename SerialStorage<Derived>::Serial SerialStorage<Derived>::LastSerial() const {
DAWN_ASSERT(!Empty()); DAWN_ASSERT(!Empty());
return mStorage.back().first; return mStorage.back().first;
} }

View File

@ -56,7 +56,7 @@ namespace dawn_native {
ResultOrError<UploadHandle> AllocateInternal(uint64_t allocationSize, Serial serial); ResultOrError<UploadHandle> AllocateInternal(uint64_t allocationSize, Serial serial);
std::vector<std::unique_ptr<RingBuffer>> mRingBuffers; std::vector<std::unique_ptr<RingBuffer>> mRingBuffers;
SerialQueue<std::unique_ptr<StagingBufferBase>> mReleasedStagingBuffers; SerialQueue<Serial, std::unique_ptr<StagingBufferBase>> mReleasedStagingBuffers;
DeviceBase* mDevice; DeviceBase* mDevice;
}; };
} // namespace dawn_native } // namespace dawn_native

View File

@ -34,7 +34,7 @@ namespace dawn_native {
protected: protected:
DeviceBase* mDevice; DeviceBase* mDevice;
SerialQueue<Ref<ErrorScope>> mScopesInFlight; SerialQueue<Serial, Ref<ErrorScope>> mScopesInFlight;
}; };
} // namespace dawn_native } // namespace dawn_native

View File

@ -61,7 +61,7 @@ namespace dawn_native {
uint64_t mSignalValue; uint64_t mSignalValue;
uint64_t mCompletedValue; uint64_t mCompletedValue;
Ref<QueueBase> mQueue; Ref<QueueBase> mQueue;
SerialMap<OnCompletionData> mRequests; SerialMap<Serial, OnCompletionData> mRequests;
}; };
} // namespace dawn_native } // namespace dawn_native

View File

@ -39,7 +39,7 @@ namespace dawn_native {
private: private:
DeviceBase* mDevice; DeviceBase* mDevice;
SerialQueue<FenceInFlight> mFencesInFlight; SerialQueue<Serial, FenceInFlight> mFencesInFlight;
}; };
} // namespace dawn_native } // namespace dawn_native

View File

@ -35,7 +35,7 @@ namespace dawn_native {
Ref<BufferBase> buffer; Ref<BufferBase> buffer;
uint32_t mapSerial; uint32_t mapSerial;
}; };
SerialQueue<Request> mInflightRequests; SerialQueue<Serial, Request> mInflightRequests;
}; };
} // namespace dawn_native } // namespace dawn_native

View File

@ -44,8 +44,8 @@ namespace dawn_native {
uint64_t size; uint64_t size;
}; };
SerialQueue<Request> mInflightRequests; // Queue of the recorded sub-alloc requests (e.g. SerialQueue<Serial, Request> mInflightRequests; // Queue of the recorded sub-alloc requests
// frame of resources). // (e.g. frame of resources).
uint64_t mUsedEndOffset = 0; // Tail of used sub-alloc requests (in bytes). uint64_t mUsedEndOffset = 0; // Tail of used sub-alloc requests (in bytes).
uint64_t mUsedStartOffset = 0; // Head of used sub-alloc requests (in bytes). uint64_t mUsedStartOffset = 0; // Head of used sub-alloc requests (in bytes).

View File

@ -15,9 +15,9 @@
#ifndef DAWNNATIVE_D3D12_BUFFERD3D12_H_ #ifndef DAWNNATIVE_D3D12_BUFFERD3D12_H_
#define DAWNNATIVE_D3D12_BUFFERD3D12_H_ #define DAWNNATIVE_D3D12_BUFFERD3D12_H_
#include "common/SerialQueue.h"
#include "dawn_native/Buffer.h" #include "dawn_native/Buffer.h"
#include "common/Serial.h"
#include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h" #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
#include "dawn_native/d3d12/d3d12_platform.h" #include "dawn_native/d3d12/d3d12_platform.h"

View File

@ -49,7 +49,7 @@ namespace dawn_native { namespace d3d12 {
ComPtr<ID3D12CommandAllocator> mCommandAllocators[kMaxCommandAllocators]; ComPtr<ID3D12CommandAllocator> mCommandAllocators[kMaxCommandAllocators];
std::bitset<kMaxCommandAllocators> mFreeAllocators; std::bitset<kMaxCommandAllocators> mFreeAllocators;
SerialQueue<IndexedCommandAllocator> mInFlightCommandAllocators; SerialQueue<Serial, IndexedCommandAllocator> mInFlightCommandAllocators;
}; };
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -196,7 +196,7 @@ namespace dawn_native { namespace d3d12 {
CommandRecordingContext mPendingCommands; CommandRecordingContext mPendingCommands;
SerialQueue<ComPtr<IUnknown>> mUsedComObjectRefs; SerialQueue<Serial, ComPtr<IUnknown>> mUsedComObjectRefs;
std::unique_ptr<CommandAllocatorManager> mCommandAllocatorManager; std::unique_ptr<CommandAllocatorManager> mCommandAllocatorManager;
std::unique_ptr<ResourceAllocatorManager> mResourceAllocatorManager; std::unique_ptr<ResourceAllocatorManager> mResourceAllocatorManager;

View File

@ -98,7 +98,7 @@ namespace dawn_native { namespace d3d12 {
std::array<std::unique_ptr<PooledResourceMemoryAllocator>, ResourceHeapKind::EnumCount> std::array<std::unique_ptr<PooledResourceMemoryAllocator>, ResourceHeapKind::EnumCount>
mPooledHeapAllocators; mPooledHeapAllocators;
SerialQueue<ResourceHeapAllocation> mAllocationsToDelete; SerialQueue<Serial, ResourceHeapAllocation> mAllocationsToDelete;
}; };
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -77,7 +77,7 @@ namespace dawn_native { namespace d3d12 {
D3D12_DESCRIPTOR_HEAP_TYPE mHeapType; D3D12_DESCRIPTOR_HEAP_TYPE mHeapType;
SerialQueue<CPUDescriptorHeapAllocation> mAllocationsToDelete; SerialQueue<Serial, CPUDescriptorHeapAllocation> mAllocationsToDelete;
}; };
}} // namespace dawn_native::d3d12 }} // namespace dawn_native::d3d12

View File

@ -61,7 +61,7 @@ namespace dawn_native { namespace vulkan {
PoolIndex poolIndex; PoolIndex poolIndex;
SetIndex setIndex; SetIndex setIndex;
}; };
SerialQueue<Deallocation> mPendingDeallocations; SerialQueue<Serial, Deallocation> mPendingDeallocations;
Serial mLastDeallocationSerial = 0; Serial mLastDeallocationSerial = 0;
}; };

View File

@ -161,7 +161,7 @@ namespace dawn_native { namespace vulkan {
VkQueue mQueue = VK_NULL_HANDLE; VkQueue mQueue = VK_NULL_HANDLE;
uint32_t mComputeSubgroupSize = 0; uint32_t mComputeSubgroupSize = 0;
SerialQueue<Ref<BindGroupLayout>> mBindGroupLayoutsPendingDeallocation; SerialQueue<Serial, Ref<BindGroupLayout>> mBindGroupLayoutsPendingDeallocation;
std::unique_ptr<FencedDeleter> mDeleter; std::unique_ptr<FencedDeleter> mDeleter;
std::unique_ptr<ResourceMemoryAllocator> mResourceMemoryAllocator; std::unique_ptr<ResourceMemoryAllocator> mResourceMemoryAllocator;
std::unique_ptr<RenderPassCache> mRenderPassCache; std::unique_ptr<RenderPassCache> mRenderPassCache;
@ -187,7 +187,7 @@ namespace dawn_native { namespace vulkan {
VkCommandPool pool = VK_NULL_HANDLE; VkCommandPool pool = VK_NULL_HANDLE;
VkCommandBuffer commandBuffer = VK_NULL_HANDLE; VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
}; };
SerialQueue<CommandPoolAndBuffer> mCommandsInFlight; SerialQueue<Serial, CommandPoolAndBuffer> mCommandsInFlight;
// Command pools in the unused list haven't been reset yet. // Command pools in the unused list haven't been reset yet.
std::vector<CommandPoolAndBuffer> mUnusedCommands; std::vector<CommandPoolAndBuffer> mUnusedCommands;
// There is always a valid recording context stored in mRecordingContext // There is always a valid recording context stored in mRecordingContext

View File

@ -47,21 +47,21 @@ namespace dawn_native { namespace vulkan {
private: private:
Device* mDevice = nullptr; Device* mDevice = nullptr;
SerialQueue<VkBuffer> mBuffersToDelete; SerialQueue<Serial, VkBuffer> mBuffersToDelete;
SerialQueue<VkDescriptorPool> mDescriptorPoolsToDelete; SerialQueue<Serial, VkDescriptorPool> mDescriptorPoolsToDelete;
SerialQueue<VkDeviceMemory> mMemoriesToDelete; SerialQueue<Serial, VkDeviceMemory> mMemoriesToDelete;
SerialQueue<VkFramebuffer> mFramebuffersToDelete; SerialQueue<Serial, VkFramebuffer> mFramebuffersToDelete;
SerialQueue<VkImage> mImagesToDelete; SerialQueue<Serial, VkImage> mImagesToDelete;
SerialQueue<VkImageView> mImageViewsToDelete; SerialQueue<Serial, VkImageView> mImageViewsToDelete;
SerialQueue<VkPipeline> mPipelinesToDelete; SerialQueue<Serial, VkPipeline> mPipelinesToDelete;
SerialQueue<VkPipelineLayout> mPipelineLayoutsToDelete; SerialQueue<Serial, VkPipelineLayout> mPipelineLayoutsToDelete;
SerialQueue<VkQueryPool> mQueryPoolsToDelete; SerialQueue<Serial, VkQueryPool> mQueryPoolsToDelete;
SerialQueue<VkRenderPass> mRenderPassesToDelete; SerialQueue<Serial, VkRenderPass> mRenderPassesToDelete;
SerialQueue<VkSampler> mSamplersToDelete; SerialQueue<Serial, VkSampler> mSamplersToDelete;
SerialQueue<VkSemaphore> mSemaphoresToDelete; SerialQueue<Serial, VkSemaphore> mSemaphoresToDelete;
SerialQueue<VkShaderModule> mShaderModulesToDelete; SerialQueue<Serial, VkShaderModule> mShaderModulesToDelete;
SerialQueue<VkSurfaceKHR> mSurfacesToDelete; SerialQueue<Serial, VkSurfaceKHR> mSurfacesToDelete;
SerialQueue<VkSwapchainKHR> mSwapChainsToDelete; SerialQueue<Serial, VkSwapchainKHR> mSwapChainsToDelete;
}; };
}} // namespace dawn_native::vulkan }} // namespace dawn_native::vulkan

View File

@ -49,7 +49,7 @@ namespace dawn_native { namespace vulkan {
class SingleTypeAllocator; class SingleTypeAllocator;
std::vector<std::unique_ptr<SingleTypeAllocator>> mAllocatorsPerType; std::vector<std::unique_ptr<SingleTypeAllocator>> mAllocatorsPerType;
SerialQueue<ResourceMemoryAllocation> mSubAllocationsToDelete; SerialQueue<Serial, ResourceMemoryAllocation> mSubAllocationsToDelete;
}; };
}} // namespace dawn_native::vulkan }} // namespace dawn_native::vulkan

View File

@ -15,8 +15,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "common/SerialMap.h" #include "common/SerialMap.h"
#include "common/TypedInteger.h"
using TestSerialMap = SerialMap<int>; using TestSerialMap = SerialMap<uint64_t, int>;
// A number of basic tests for SerialMap that are difficult to split from one another // A number of basic tests for SerialMap that are difficult to split from one another
TEST(SerialMap, BasicTest) { TEST(SerialMap, BasicTest) {
@ -162,3 +163,21 @@ TEST(SerialMap, FirstSerial) {
map.Enqueue(vector1, 6); map.Enqueue(vector1, 6);
EXPECT_EQ(map.FirstSerial(), 6u); EXPECT_EQ(map.FirstSerial(), 6u);
} }
// Test basic functionality with type integers
TEST(SerialMap, TypedInteger) {
using MySerial = TypedInteger<struct MySerialT, uint64_t>;
using MySerialMap = SerialMap<MySerial, int>;
MySerialMap map;
map.Enqueue(1, MySerial(0));
map.Enqueue(2, MySerial(0));
std::vector<int> 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());
}

View File

@ -15,8 +15,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "common/SerialQueue.h" #include "common/SerialQueue.h"
#include "common/TypedInteger.h"
using TestSerialQueue = SerialQueue<int>; using TestSerialQueue = SerialQueue<uint64_t, int>;
// A number of basic tests for SerialQueue that are difficult to split from one another // A number of basic tests for SerialQueue that are difficult to split from one another
TEST(SerialQueue, BasicTest) { TEST(SerialQueue, BasicTest) {
@ -154,3 +155,21 @@ TEST(SerialQueue, LastSerial) {
queue.Enqueue({2}, 1); queue.Enqueue({2}, 1);
EXPECT_EQ(queue.LastSerial(), 1u); EXPECT_EQ(queue.LastSerial(), 1u);
} }
// Test basic functionality with type integers
TEST(SerialQueue, TypedInteger) {
using MySerial = TypedInteger<struct MySerialT, uint64_t>;
using MySerialQueue = SerialQueue<MySerial, int>;
MySerialQueue queue;
queue.Enqueue(1, MySerial(0));
queue.Enqueue(2, MySerial(0));
std::vector<int> 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());
}