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 <vector>
template <typename T>
template <typename Serial, typename Value>
class SerialMap;
template <typename T>
struct SerialStorageTraits<SerialMap<T>> {
using Value = T;
using Storage = std::map<Serial, std::vector<T>>;
template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialMap<SerialT, ValueT>> {
using Serial = SerialT;
using Value = ValueT;
using Storage = std::map<Serial, std::vector<Value>>;
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 <typename T>
class SerialMap : public SerialStorage<SerialMap<T>> {
// through Value items in order of increasing Serial.
template <typename Serial, typename Value>
class SerialMap : public SerialStorage<SerialMap<Serial, Value>> {
public:
void Enqueue(const T& value, Serial serial);
void Enqueue(T&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial);
void Enqueue(const Value& value, Serial serial);
void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<Value>&& values, Serial serial);
};
// SerialMap
template <typename T>
void SerialMap<T>::Enqueue(const T& value, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const Value& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(T&& value, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(Value&& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(const std::vector<T>& values, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
for (const Value& value : values) {
Enqueue(value, serial);
}
}
template <typename T>
void SerialMap<T>::Enqueue(std::vector<T>&& values, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
for (const Value& value : values) {
Enqueue(value, serial);
}
}

View File

@ -19,65 +19,65 @@
#include <vector>
template <typename T>
template <typename Serial, typename Value>
class SerialQueue;
template <typename T>
struct SerialStorageTraits<SerialQueue<T>> {
using Value = T;
using SerialPair = std::pair<Serial, std::vector<T>>;
template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialQueue<SerialT, ValueT>> {
using Serial = SerialT;
using Value = ValueT;
using SerialPair = std::pair<Serial, std::vector<Value>>;
using Storage = std::vector<SerialPair>;
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 <typename T>
class SerialQueue : public SerialStorage<SerialQueue<T>> {
template <typename Serial, typename Value>
class SerialQueue : public SerialStorage<SerialQueue<Serial, Value>> {
public:
using SerialPair = typename SerialStorageTraits<SerialQueue<T>>::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<T>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial);
void Enqueue(const Value& value, Serial serial);
void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<Value>&& values, Serial serial);
};
// SerialQueue
template <typename T>
void SerialQueue<T>::Enqueue(const T& value, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::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<T>{});
this->mStorage.emplace_back(serial, std::vector<Value>{});
}
this->mStorage.back().second.push_back(value);
}
template <typename T>
void SerialQueue<T>::Enqueue(T&& value, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::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<T>{});
this->mStorage.emplace_back(serial, std::vector<Value>{});
}
this->mStorage.back().second.push_back(std::move(value));
}
template <typename T>
void SerialQueue<T>::Enqueue(const std::vector<T>& values, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values);
}
template <typename T>
void SerialQueue<T>::Enqueue(std::vector<T>&& values, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -44,8 +44,8 @@ namespace dawn_native {
uint64_t size;
};
SerialQueue<Request> mInflightRequests; // Queue of the recorded sub-alloc requests (e.g.
// frame of resources).
SerialQueue<Serial, Request> 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).

View File

@ -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"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -161,7 +161,7 @@ namespace dawn_native { namespace vulkan {
VkQueue mQueue = VK_NULL_HANDLE;
uint32_t mComputeSubgroupSize = 0;
SerialQueue<Ref<BindGroupLayout>> mBindGroupLayoutsPendingDeallocation;
SerialQueue<Serial, Ref<BindGroupLayout>> mBindGroupLayoutsPendingDeallocation;
std::unique_ptr<FencedDeleter> mDeleter;
std::unique_ptr<ResourceMemoryAllocator> mResourceMemoryAllocator;
std::unique_ptr<RenderPassCache> mRenderPassCache;
@ -187,7 +187,7 @@ namespace dawn_native { namespace vulkan {
VkCommandPool pool = 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.
std::vector<CommandPoolAndBuffer> mUnusedCommands;
// There is always a valid recording context stored in mRecordingContext

View File

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

View File

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

View File

@ -15,8 +15,9 @@
#include <gtest/gtest.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
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<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 "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
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<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());
}