Implement Set, Refactor Map into Tree

This commit is contained in:
Dynamitos
2023-12-22 19:46:07 +01:00
parent ac1d11402e
commit c8f99bb64d
20 changed files with 674 additions and 603 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ void taskMain(
{ {
uint m = mesh.meshletOffset + i; uint m = mesh.meshletOffset + i;
MeshletDescription meshlet = pScene.meshletInfos[m]; MeshletDescription meshlet = pScene.meshletInfos[m];
if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum)) //if(meshlet.boundingBox.insideFrustum(localToClip, viewFrustum))
{ {
uint index; uint index;
InterlockedAdd(head, 1, index); InterlockedAdd(head, 1, index);
+2 -2
View File
@@ -20,8 +20,8 @@ struct MeshData
uint32_t pad0[3]; uint32_t pad0[3];
}; };
static const uint MAX_VERTICES = 256; static const uint MAX_VERTICES = 64;
static const uint MAX_PRIMITIVES = 256; static const uint MAX_PRIMITIVES = 126;
static const uint TASK_GROUP_SIZE = 128; static const uint TASK_GROUP_SIZE = 128;
static const uint MESH_GROUP_SIZE = 32; static const uint MESH_GROUP_SIZE = 32;
static const uint MAX_MESHLETS_PER_MESH = 512; static const uint MAX_MESHLETS_PER_MESH = 512;
+18 -7
View File
@@ -10,6 +10,11 @@ template<>
struct Dependencies<> struct Dependencies<>
{ {
int x; int x;
template<typename... Right>
Dependencies<Right...> operator|(const Dependencies<Right...>& other)
{
return Dependencies<Right...>();
}
}; };
template<typename This, typename... Rest> template<typename This, typename... Rest>
struct Dependencies<This, Rest...> : public Dependencies<Rest...> struct Dependencies<This, Rest...> : public Dependencies<Rest...>
@@ -24,25 +29,31 @@ struct Dependencies<This, Rest...> : public Dependencies<Rest...>
namespace Component namespace Component
{ {
template<typename Comp> template<typename Comp>
static int accessComponent(Comp& comp); Comp& getComponent();
} }
template<typename Comp> template<typename Comp>
concept has_dependencies = requires(Comp) { Comp::dependencies; }; concept has_dependencies = requires(Comp) { Comp::dependencies; };
#define REQUIRE_COMPONENT(x) \ #define REQUIRE_COMPONENT(x) \
private: \ private: \
x& get##x() { return *tl_##x; } \ x& get##x() { return getComponent<##x>(); } \
const x& get##x() const { return *tl_##x; }; \ const x& get##x() const { return getComponent<##x>(); } \
public: \ public: \
constexpr static Dependencies<x> dependencies = {}; constexpr static Dependencies<x> dependencies = {};
#define DECLARE_COMPONENT(x) \ #define DECLARE_COMPONENT(x) \
thread_local extern x* tl_##x; \ void accessComponent(x& val); \
template<> \ template<> \
int accessComponent<x>(x& val) { tl_##x = &val; return 0; } x& getComponent<x>();
#define DEFINE_COMPONENT(x) \ #define DEFINE_COMPONENT(x) \
thread_local x* Seele::Component::tl_##x; thread_local x* tl_##x = nullptr; \
void Seele::Component::accessComponent(x& ref) { tl_##x = &ref; } \
template<> \
x& Seele::Component::getComponent<x>() { return *tl_##x; }
} // namespace Seele } // namespace Seele
+1 -1
View File
@@ -2,7 +2,7 @@
using namespace Seele::Component; using namespace Seele::Component;
DEFINE_COMPONENT(Transform) DEFINE_COMPONENT(Transform);
void Transform::setPosition(Vector pos) void Transform::setPosition(Vector pos)
{ {
+6 -2
View File
@@ -3,7 +3,9 @@ target_sources(Engine
Array.h Array.h
List.h List.h
Map.h Map.h
Pair.h) Pair.h
Set.h
Tree.h)
target_sources(Engine target_sources(Engine
PUBLIC FILE_SET HEADERS PUBLIC FILE_SET HEADERS
@@ -11,4 +13,6 @@ target_sources(Engine
Array.h Array.h
List.h List.h
Map.h Map.h
Pair.h) Pair.h
Set.h
Tree.h)
+44 -543
View File
@@ -2,416 +2,101 @@
#include <utility> #include <utility>
#include "Array.h" #include "Array.h"
#include "Pair.h" #include "Pair.h"
#include "Tree.h"
#include "Serialization/Serialization.h" #include "Serialization/Serialization.h"
namespace Seele namespace Seele
{ {
template<typename KeyType, typename PairType>
struct _KeyFun
{
const KeyType& operator()(const PairType& pair) const
{
return pair.key;
}
};
template <typename K, template <typename K,
typename V, typename V,
typename Compare = std::less<K>, typename Compare = std::less<K>,
typename Allocator = std::pmr::polymorphic_allocator<Pair<const K,V>>> typename Allocator = std::pmr::polymorphic_allocator<Pair<const K, V>>>
struct Map struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>
{ {
private: using Super = Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>;
struct Node using key_type = Super::key_type;
{
Node* leftChild;
Node* rightChild;
Pair<K, V> pair;
Node()
: leftChild(nullptr)
, rightChild(nullptr)
, pair()
{
}
Node(const Node& other) = default;
Node(Node&& other) = default;
Node& operator=(const Node& other) = default;
Node& operator=(Node&& other) = default;
Node(K key)
: leftChild(nullptr)
, rightChild(nullptr)
, pair(std::move(key))
{
}
~Node()
{
}
};
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
public:
template<typename PairType>
class IteratorBase
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = PairType;
using difference_type = std::ptrdiff_t;
using reference = PairType&;
using pointer = PairType*;
constexpr IteratorBase(Node* x = nullptr, Array<Node*> &&beginIt = Array<Node*>())
: node(x), traversal(std::move(beginIt))
{
}
constexpr IteratorBase(const IteratorBase &i)
: node(i.node), traversal(i.traversal)
{
}
constexpr IteratorBase(IteratorBase&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
{
}
constexpr IteratorBase& operator=(const IteratorBase& other)
{
if(this != &other)
{
node = other.node;
traversal = other.traversal;
}
return *this;
}
constexpr IteratorBase& operator=(IteratorBase&& other)
{
if(this != &other)
{
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
constexpr reference operator*() const
{
return node->pair;
}
constexpr pointer operator->() const
{
return &(node->pair);
}
constexpr bool operator!=(const IteratorBase &other)
{
return node != other.node;
}
constexpr bool operator==(const IteratorBase &other)
{
return node == other.node;
}
constexpr IteratorBase &operator++()
{
node = node->rightChild;
while (node != nullptr
&& node->leftChild != nullptr)
{
traversal.add(node);
node = node->leftChild;
}
if (node == nullptr
&& traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase &operator--()
{
node = node->leftChild;
while (node != nullptr
&& node->rightChild != nullptr)
{
traversal.add(node);
node = node->rightchild;
}
if (node == nullptr
&& traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase operator--(int)
{
IteratorBase tmp(*this);
--*this;
return tmp;
}
constexpr IteratorBase operator++(int)
{
IteratorBase tmp(*this);
++*this;
return tmp;
}
private:
Node* node;
Array<Node*> traversal;
};
using Iterator = IteratorBase<Pair<K,V>>;
using ConstIterator = IteratorBase<const Pair<K,V>>;
using key_type = K;
using mapped_type = V; using mapped_type = V;
using value_type = Pair<K,V>; using value_type = Super::value_type;
using allocator_type = Allocator;
using size_type = std::size_t; using allocator_type = Super::allocator_type;
using difference_type = std::ptrdiff_t; using size_type = Super::size_type;
using reference = value_type&; using difference_type = Super::difference_type;
using const_reference = const value_type&; using reference = Super::reference;
using pointer = std::allocator_traits<Allocator>::pointer; using const_reference = Super::reference;
using const_pointer = std::allocator_traits<Allocator>::const_pointer; using pointer = Super::pointer;
using const_pointer = Super::const_pointer;
using iterator = Super::iterator;
using const_iterator = Super::const_iterator;
using reverse_iterator = Super::reverse_iterator;
using const_reverse_iterator = Super::const_reverse_iterator;
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr Map() noexcept constexpr Map() noexcept
: root(nullptr) : Super()
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(Compare())
{ {
} }
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator())) constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
: alloc(alloc) : Super(comp, alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(comp)
{ {
} }
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare())) constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
: alloc(alloc) : Super(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(Compare())
{ {
} }
constexpr Map(const Map& other)
: alloc(other.alloc)
, root(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(other.comp)
{
for(const auto& [key, val] : other)
{
this->operator[](key) = val;
}
}
constexpr Map(Map&& other) noexcept
: alloc(std::move(other.alloc))
, root(std::move(other.root))
, iteratorsDirty(true)
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
}
constexpr ~Map() noexcept
{
clear();
}
constexpr Map& operator=(const Map& other)
{
if(this != &other)
{
alloc = other.alloc;
comp = other.comp;
root = nullptr;
_size = 0;
for(const auto& [key, val] : other)
{
this->operator[](key) = val;
}
markIteratorsDirty();
}
return *this;
}
constexpr Map& operator=(Map&& other)
{
if(this != &other)
{
alloc = std::move(other.alloc);
root = std::move(other.root);
_size = std::move(other._size);
comp = std::move(other.comp);
markIteratorsDirty();
}
return *this;
}
constexpr mapped_type& operator[](const key_type& key) constexpr mapped_type& operator[](const key_type& key)
{ {
root = splay(root, key); auto [it, inserted] = Super::insert(Pair<K, V>(key, V()));
if (root == nullptr || !equal(root->pair.key, key)) return it->value;
{
root = insert(root, key);
_size++;
}
markIteratorsDirty();
return root->pair.value;
} }
constexpr const mapped_type& operator[](const key_type& key) const constexpr const mapped_type& operator[](const key_type& key) const
{ {
Node* it = root; return Super::find(key)->value;
while (!equal(it->pair.key, key))
{
if (comp(key, it->pair.key))
{
it = it->leftChild;
}
else
{
it = it->rightChild;
}
}
return it->pair.value;
} }
constexpr mapped_type& at(const key_type& key) constexpr mapped_type& at(const key_type& key)
{ {
root = splay(root, key); iterator elem = Super::find(key);
if (root == nullptr || !equal(root->pair.key, key)) if (elem == Super::end())
{ {
throw std::logic_error("Key not found"); throw std::logic_error("Key not found");
} }
markIteratorsDirty(); return elem->value;
return root->pair.value;
} }
constexpr mapped_type& at(key_type&& key)
{
root = splay(root, std::move(key));
if (root == nullptr || !equal(root->pair.key, key))
{
throw std::logic_error("Key not found");
}
markIteratorsDirty();
return root->pair.value;
}
constexpr const mapped_type& at(const key_type& key) const constexpr const mapped_type& at(const key_type& key) const
{ {
Node* it = root; return Super::find(key)->value;
while (!equal(it->pair.key, key))
{
if (comp(key, it->pair.key))
{
it = it->leftChild;
}
else
{
it = it->rightChild;
}
}
return it->pair.value;
} }
constexpr iterator find(const key_type& key) constexpr iterator find(const key_type& key)
{ {
root = splay(root, key); return Super::find(key);
refreshIterators();
if (root == nullptr || !equal(root->pair.key, key))
{
return endIt;
}
return iterator(root);
}
constexpr iterator find(key_type&& key)
{
root = splay(root, std::move(key));
refreshIterators();
if (root == nullptr || !equal(root->pair.key, key))
{
return endIt;
}
return iterator(root);
} }
constexpr iterator erase(const key_type& key) constexpr iterator erase(const key_type& key)
{ {
root = remove(root, key); return Super::remove(key);
refreshIterators();
assert(root != nullptr || _size == 0);
return iterator(root);
}
constexpr iterator erase(key_type&& key)
{
root = remove(root, std::move(key));
refreshIterators();
return iterator(root);
}
constexpr void clear()
{
while(_size > 0)
{
root = remove(root, root->pair.key);
}
root = nullptr;
markIteratorsDirty();
} }
constexpr bool exists(const key_type& key) constexpr bool exists(const key_type& key)
{ {
return find(key) != endIt; return Super::find(key) != Super::end();
}
constexpr bool exists(key_type&& key)
{
return find(std::move(key)) != endIt;
} }
constexpr bool contains(const key_type& key) constexpr bool contains(const key_type& key)
{ {
return exists(key); return exists(key);
} }
constexpr bool contains(key_type&& key) constexpr Pair<iterator, bool> insert(const value_type& val)
{ {
return exists(key); return Super::insert(val);
}
constexpr iterator begin()
{
if(iteratorsDirty)
{
refreshIterators();
}
return beginIt;
}
constexpr iterator end()
{
if(iteratorsDirty)
{
refreshIterators();
}
return endIt;
}
constexpr iterator begin() const
{
if(iteratorsDirty)
{
return calcBeginIterator();
}
return beginIt;
}
constexpr iterator end() const
{
if(iteratorsDirty)
{
return calcEndIterator();
}
return endIt;
}
constexpr bool empty() const
{
return _size == 0;
}
constexpr size_type size() const
{
return _size;
} }
void save(ArchiveBuffer& buffer) const void save(ArchiveBuffer& buffer) const
{ {
buffer.writeBytes(&_size, sizeof(uint64)); size_t s = Super::size();
buffer.writeBytes(&s, sizeof(uint64));
for(const auto& [k, v] : *this) for(const auto& [k, v] : *this)
{ {
Serialization::save(buffer, k); Serialization::save(buffer, k);
@@ -431,189 +116,5 @@ public:
this->operator[](std::move(k)) = std::move(v); this->operator[](std::move(k)) = std::move(v);
} }
} }
private:
void verifyTree()
{
size_t numElems = 0;
for (const auto& it : *this)
{
numElems++;
}
assert(numElems == _size);
}
void markIteratorsDirty()
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
constexpr Iterator calcBeginIterator() const
{
Node* begin = root;
Array<Node*> beginTraversal;
while (begin != nullptr)
{
beginTraversal.add(begin);
begin = begin->leftChild;
}
if(!beginTraversal.empty())
{
begin = beginTraversal.back();
beginTraversal.pop();
}
return Iterator(begin, std::move(beginTraversal));
}
constexpr Iterator calcEndIterator() const
{
Node* endIndex = root;
Array<Node*> endTraversal;
while (endIndex != nullptr)
{
endTraversal.add(endIndex);
endIndex = endIndex->rightChild;
}
return Iterator(endIndex, std::move(endTraversal));
}
NodeAlloc alloc;
Node* root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
size_type _size;
Compare comp;
Node* rotateLeft(Node* x)
{
Node* y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;
return y;
}
Node* rotateRight(Node* x)
{
Node* y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
}
template<class KeyType>
Node* insert(Node* r, KeyType&& key)
{
if (r == nullptr)
{
root = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, root, std::forward<KeyType>(key));
return root;
}
r = splay(r, key);
if (equal(r->pair.key, key))
return r;
Node *newNode = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, std::forward<KeyType>(key));
if (comp(newNode->pair.key, r->pair.key))
{
newNode->rightChild = r;
newNode->leftChild = r->leftChild;
r->leftChild = nullptr;
}
else
{
newNode->leftChild = r;
newNode->rightChild = r->rightChild;
r->rightChild = nullptr;
}
return newNode;
}
template<class KeyType>
Node* remove(Node* r, KeyType&& key)
{
Node* temp;
if (r == nullptr)
return nullptr;
r = splay(r, key);
if (!equal(r->pair.key, key))
return r;
if (r->leftChild == nullptr)
{
temp = r;
r = r->rightChild;
}
else
{
temp = r;
r = splay(r->leftChild, key);
r->rightChild = temp->rightChild;
}
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
alloc.deallocate(temp, 1);
_size--;
return r;
}
template<class KeyType>
Node* splay(Node* r, KeyType&& key)
{
if (r == nullptr || equal(r->pair.key, key))
{
return r;
}
if (comp(key, r->pair.key))
{
if (r->leftChild == nullptr)
return r;
if (comp(key, r->leftChild->pair.key))
{
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
r = rotateRight(r);
}
else if (comp(r->leftChild->pair.key, key))
{
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
if (r->leftChild->rightChild != nullptr)
{
r->leftChild = rotateLeft(r->leftChild);
}
}
return (r->leftChild == nullptr) ? r : rotateRight(r);
}
else
{
if (r->rightChild == nullptr)
return r;
if (comp(key, r->rightChild->pair.key))
{
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
if (r->rightChild->leftChild != nullptr)
{
r->rightChild = rotateRight(r->rightChild);
}
}
else if (comp(r->rightChild->pair.key, key))
{
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
}
return (r->rightChild == nullptr) ? r : rotateLeft(r);
}
}
bool equal(const key_type& a, const key_type& b) const
{
return !comp(a, b) && !comp(b, a);
}
}; };
} // namespace Seele } // namespace Seele
+10 -9
View File
@@ -6,20 +6,21 @@ template <typename K, typename V>
struct Pair struct Pair
{ {
public: public:
Pair() constexpr Pair()
: key(K()), value(V()) : key(K()), value(V())
{} {}
constexpr Pair(const K & key, const V & value)
: key(key), value(value)
{}
template<class U1 = K, class U2 = V>
constexpr Pair(U1&& x, U2&& y)
: key(std::forward<U1>(x))
, value(std::forward<U2>(y))
{
}
Pair(const Pair& other) = default; Pair(const Pair& other) = default;
Pair(Pair&& other) = default; Pair(Pair&& other) = default;
~Pair(){} ~Pair(){}
template<class KeyType>
explicit Pair(KeyType&& key)
: key(std::forward<KeyType>(key)), value(V())
{}
template<class KeyType, class ValueType>
explicit Pair(KeyType&& key, ValueType&& value)
: key(std::forward<KeyType>(key)), value(std::forward<ValueType>(value))
{}
Pair& operator=(const Pair& other) = default; Pair& operator=(const Pair& other) = default;
Pair& operator=(Pair&& other) = default; Pair& operator=(Pair&& other) = default;
constexpr friend bool operator<(const Pair& left, const Pair& right) constexpr friend bool operator<(const Pair& left, const Pair& right)
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <utility>
#include "Array.h"
#include "Tree.h"
namespace Seele
{
template<class Key, class Compare = std::less<Key>, class Allocator = std::pmr::polymorphic_allocator<Key>>
class Set : public Tree<Key, Key, std::identity, Compare, Allocator>
{
public:
using Super = Tree<Key, Key, std::identity, Compare, Allocator>;
using key_type = Key;
using value_type = Key;
using node_type = Super::Node;
using allocator_type = Super::allocator_type;
using size_type = Super::size_type;
using difference_type = Super::difference_type;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
using iterator = Super::iterator;
using const_iterator = Super::const_iterator;
using reverse_iterator = Super::reverse_iterator;
using const_reverse_iterator = Super::const_reverse_iterator;
Set()
: Set(Compare())
{
}
explicit Set(const Compare& comp, const Allocator alloc = Allocator())
: Super(comp, alloc)
{
}
explicit Set(const Allocator alloc)
: Super(alloc)
{
}
constexpr Pair<iterator, bool> insert(const value_type& value)
{
return Super::insert(value);
}
};
} // namespace Seele
+510
View File
@@ -0,0 +1,510 @@
#pragma once
#include <memory_resource>
#include "Pair.h"
namespace Seele
{
template <
typename KeyType,
typename NodeData,
typename KeyFun,
typename Compare,
typename Allocator>
struct Tree
{
protected:
struct Node
{
Node* leftChild;
Node* rightChild;
NodeData data;
};
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
public:
template<typename IterType>
class IteratorBase
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = IterType;
using difference_type = std::ptrdiff_t;
using reference = IterType&;
using pointer = IterType*;
constexpr IteratorBase(Node* x = nullptr, Array<Node*>&& beginIt = Array<Node*>())
: node(x), traversal(std::move(beginIt))
{
}
constexpr IteratorBase(const IteratorBase& i)
: node(i.node), traversal(i.traversal)
{
}
constexpr IteratorBase(IteratorBase&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
{
}
constexpr IteratorBase& operator=(const IteratorBase& other)
{
if (this != &other)
{
node = other.node;
traversal = other.traversal;
}
return *this;
}
constexpr IteratorBase& operator=(IteratorBase&& other)
{
if (this != &other)
{
node = std::move(other.node);
traversal = std::move(other.traversal);
}
return *this;
}
constexpr reference operator*() const
{
return node->data;
}
constexpr pointer operator->() const
{
return &(node->data);
}
constexpr bool operator!=(const IteratorBase& other)
{
return node != other.node;
}
constexpr bool operator==(const IteratorBase& other)
{
return node == other.node;
}
constexpr IteratorBase& operator++()
{
node = node->rightChild;
while (node != nullptr
&& node->leftChild != nullptr)
{
traversal.add(node);
node = node->leftChild;
}
if (node == nullptr
&& traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase& operator--()
{
node = node->leftChild;
while (node != nullptr
&& node->rightChild != nullptr)
{
traversal.add(node);
node = node->rightchild;
}
if (node == nullptr
&& traversal.size() > 0)
{
node = traversal.back();
traversal.pop();
}
return *this;
}
constexpr IteratorBase operator--(int)
{
IteratorBase tmp(*this);
--*this;
return tmp;
}
constexpr IteratorBase operator++(int)
{
IteratorBase tmp(*this);
++*this;
return tmp;
}
Node* getNode()
{
return node;
}
private:
Node* node;
Array<Node*> traversal;
};
using Iterator = IteratorBase<NodeData>;
using ConstIterator = IteratorBase<const NodeData>;
using key_type = KeyType;
using value_type = NodeData;
using allocator_type = Allocator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr Tree() noexcept
: root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(Compare())
{
}
constexpr explicit Tree(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
: alloc(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(comp)
{
}
constexpr explicit Tree(const Allocator& alloc) noexcept(noexcept(Compare()))
: alloc(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(true)
, _size(0)
, comp(Compare())
{
}
constexpr Tree(const Tree& other)
: alloc(other.alloc)
, root(nullptr)
, iteratorsDirty(true)
, _size()
, comp(other.comp)
{
std::copy(other.begin(), other.end(), begin());
}
constexpr Tree(Tree&& other) noexcept
: alloc(std::move(other.alloc))
, root(std::move(other.root))
, iteratorsDirty(true)
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
}
constexpr ~Tree() noexcept
{
clear();
}
constexpr Tree& operator=(const Tree& other)
{
if (this != &other)
{
clear();
alloc = other.alloc;
comp = other.comp;
root = nullptr;
_size = 0;
std::copy(other.begin(), other.end(), begin());
markIteratorsDirty();
}
return *this;
}
constexpr Tree& operator=(Tree&& other)
{
if (this != &other)
{
clear();
alloc = std::move(other.alloc);
root = std::move(other.root);
_size = std::move(other._size);
comp = std::move(other.comp);
markIteratorsDirty();
}
return *this;
}
constexpr void clear()
{
while (_size > 0)
{
root = _remove(root, keyFun(root->data));
}
root = nullptr;
markIteratorsDirty();
}
constexpr iterator begin()
{
if (iteratorsDirty)
{
refreshIterators();
}
return beginIt;
}
constexpr iterator end()
{
if (iteratorsDirty)
{
refreshIterators();
}
return endIt;
}
constexpr iterator begin() const
{
if (iteratorsDirty)
{
return calcBeginIterator();
}
return beginIt;
}
constexpr iterator end() const
{
if (iteratorsDirty)
{
return calcEndIterator();
}
return endIt;
}
constexpr bool empty() const
{
return _size == 0;
}
constexpr size_type size() const
{
return _size;
}
protected:
constexpr iterator find(const key_type& key)
{
root = _splay(root, key);
if (root == nullptr || !equal(root->data, key))
{
return end();
}
return iterator(root);
}
constexpr iterator find(const key_type& key) const
{
Node* it = root;
while (!equal(it->data, key))
{
if (comp(key, keyFun(it->data)))
{
it = it->leftChild;
}
else
{
it = it->rightChild;
}
}
return iterator(it);
}
constexpr Pair<iterator, bool> insert(const NodeData& data)
{
auto [r, inserted] = _insert(root, data);
root = r;
return Pair<iterator, bool>(iterator(root), inserted);
}
constexpr Pair<iterator, bool> insert(NodeData&& data)
{
auto [r, inserted] = _insert(root, std::move(data));
root = r;
return Pair<iterator, bool>(iterator(root), inserted);
}
constexpr iterator remove(const key_type& key)
{
root = _remove(root, key);
return iterator(root);
}
private:
void verifyTree()
{
size_t numElems = 0;
for (const auto& it : *this)
{
numElems++;
}
assert(numElems == _size);
}
void markIteratorsDirty()
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
constexpr Iterator calcBeginIterator() const
{
Node* begin = root;
Array<Node*> beginTraversal;
while (begin != nullptr)
{
beginTraversal.add(begin);
begin = begin->leftChild;
}
if (!beginTraversal.empty())
{
begin = beginTraversal.back();
beginTraversal.pop();
}
return Iterator(begin, std::move(beginTraversal));
}
constexpr Iterator calcEndIterator() const
{
Node* endIndex = root;
Array<Node*> endTraversal;
while (endIndex != nullptr)
{
endTraversal.add(endIndex);
endIndex = endIndex->rightChild;
}
return Iterator(endIndex, std::move(endTraversal));
}
NodeAlloc alloc;
Node* root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
size_type _size;
Compare comp;
KeyFun keyFun = KeyFun();
Node* rotateLeft(Node* x)
{
Node* y = x->rightChild;
x->rightChild = y->leftChild;
y->leftChild = x;
return y;
}
Node* rotateRight(Node* x)
{
Node* y = x->leftChild;
x->leftChild = y->rightChild;
y->rightChild = x;
return y;
}
template<class NodeDataType>
Pair<Node*, bool> _insert(Node* r, NodeDataType&& data)
{
if (r == nullptr)
{
root = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, root, nullptr, nullptr, std::forward<NodeDataType>(data));
_size++;
return Pair<Node*, bool>(root, true);
}
r = _splay(r, keyFun(data));
if (equal(r->data, keyFun(data)))
return Pair<Node*, bool>(r, false);
Node* newNode = alloc.allocate(1);
std::allocator_traits<NodeAlloc>::construct(alloc, newNode, nullptr, nullptr, std::forward<NodeDataType>(data));
if (comp(keyFun(r->data), keyFun(newNode->data)))
{
newNode->rightChild = r;
newNode->leftChild = r->leftChild;
r->leftChild = nullptr;
}
else
{
newNode->leftChild = r;
newNode->rightChild = r->rightChild;
r->rightChild = nullptr;
}
_size++;
return Pair<Node*, bool>(newNode, true);
}
template<class KeyType>
Node* _remove(Node* r, KeyType&& key)
{
Node* temp;
if (r == nullptr)
return nullptr;
r = _splay(r, key);
if (!equal(r->data, key))
return r;
if (r->leftChild == nullptr)
{
temp = r;
r = r->rightChild;
}
else
{
temp = r;
r = _splay(r->leftChild, key);
r->rightChild = temp->rightChild;
}
std::allocator_traits<NodeAlloc>::destroy(alloc, temp);
alloc.deallocate(temp, 1);
_size--;
return r;
}
template<class KeyType>
Node* _splay(Node* r, KeyType&& key)
{
if (r == nullptr || equal(r->data, key))
{
return r;
}
if (comp(keyFun(r->data), key))
{
if (r->leftChild == nullptr)
return r;
if (comp(keyFun(r->leftChild->data), key))
{
r->leftChild->leftChild = _splay(r->leftChild->leftChild, key);
r = rotateRight(r);
}
else if (comp(keyFun(r->leftChild->data), key))
{
r->leftChild->rightChild = _splay(r->leftChild->rightChild, key);
if (r->leftChild->rightChild != nullptr)
{
r->leftChild = rotateLeft(r->leftChild);
}
}
return (r->leftChild == nullptr) ? r : rotateRight(r);
}
else
{
if (r->rightChild == nullptr)
return r;
if (comp(keyFun(r->rightChild->data), key))
{
r->rightChild->leftChild = _splay(r->rightChild->leftChild, key);
if (r->rightChild->leftChild != nullptr)
{
r->rightChild = rotateRight(r->rightChild);
}
}
else if (comp(keyFun(r->rightChild->data), key))
{
r->rightChild->rightChild = _splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
}
return (r->rightChild == nullptr) ? r : rotateLeft(r);
}
}
template<typename KeyType>
bool equal(const NodeData& a, KeyType&& b) const
{
return !comp(keyFun(a), b) && !comp(b, keyFun(a));
}
};
} // namespace Seele
@@ -157,7 +157,6 @@ void BasePass::render()
command->bindDescriptor(descriptorSets); command->bindDescriptor(descriptorSets);
if (graphics->supportMeshShading()) if (graphics->supportMeshShading())
{ {
std::cout << "Num Meshes: " << instance.meshes.size() << std::endl;
command->dispatch(instance.meshes.size(), 1, 1); command->dispatch(instance.meshes.size(), 1, 1);
} }
else else
+3 -3
View File
@@ -113,10 +113,10 @@ void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer)
loadColors(id, col); loadColors(id, col);
} }
void StaticMeshVertexData::init(Gfx::PGraphics graphics) void StaticMeshVertexData::init(Gfx::PGraphics _graphics)
{ {
VertexData::init(graphics); VertexData::init(_graphics);
descriptorLayout = graphics->createDescriptorLayout("StaticMeshDescriptorLayout"); descriptorLayout = _graphics->createDescriptorLayout("StaticMeshDescriptorLayout");
descriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER); descriptorLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER); descriptorLayout->addDescriptorBinding(1, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
descriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER); descriptorLayout->addDescriptorBinding(2, Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER);
+7 -1
View File
@@ -6,7 +6,7 @@
#include "Graphics/Descriptor.h" #include "Graphics/Descriptor.h"
#include "Component/Mesh.h" #include "Component/Mesh.h"
#include "Graphics/Shader.h" #include "Graphics/Shader.h"
#include <set> #include "Containers/Set.h"
using namespace Seele; using namespace Seele;
@@ -248,6 +248,12 @@ VertexData::VertexData()
void Meshlet::build(const Array<uint32>& indices, Array<Meshlet>& meshlets) void Meshlet::build(const Array<uint32>& indices, Array<Meshlet>& meshlets)
{ {
Map<uint32, Set<uint32>> connectivity;
for (uint32 i = 0; i < indices.size(); i+=3)
{
connectivity[indices[i]].insert(indices[i + 1]);
connectivity[indices[i]].insert(indices[i + 2]);
}
Meshlet current = { Meshlet current = {
.numVertices = 0, .numVertices = 0,
.numPrimitives = 0, .numPrimitives = 0,
+3 -18
View File
@@ -23,29 +23,14 @@ public:
{ {
return Dependencies<>(); return Dependencies<>();
} }
template<typename... Dep>
auto mergeDependencies(Dep... deps)
{
return (deps | ...);
}
void setupView(Dependencies<>, ThreadPool& pool)
{
List<std::function<void()>> work;
registry.view<Components...>().each([&](Components&... comp){
work.add([&](){
update(comp...);
});
});
pool.runAndWait(std::move(work));
}
template<typename... Deps> template<typename... Deps>
void setupView(Dependencies<Deps...>, ThreadPool& pool) void setupView(Dependencies<Deps...>, ThreadPool& pool)
{ {
List<std::function<void()>> work; List<std::function<void()>> work;
registry.view<Components..., Deps...>().each([&](Components&... comp, Deps&... deps){ registry.view<Components..., Deps...>().each([&](Components&... comp, Deps&... deps){
work.add([&]() { work.add([&]() {
(accessComponent(deps) + ...); (accessComponent(deps), ...);
update((comp,...)); update(comp...);
}); });
}); });
pool.runAndWait(std::move(work)); pool.runAndWait(std::move(work));
@@ -53,7 +38,7 @@ public:
virtual void run(ThreadPool& pool, double delta) override virtual void run(ThreadPool& pool, double delta) override
{ {
SystemBase::run(pool, delta); SystemBase::run(pool, delta);
setupView(mergeDependencies((getDependencies<Components>(),...)), pool); setupView((getDependencies<Components>() | ...), pool);
} }
virtual void update() {} virtual void update() {}
virtual void update(Components&... components) = 0; virtual void update(Components&... components) = 0;
+1 -1
View File
@@ -12,7 +12,7 @@ class SystemBase
public: public:
SystemBase(PScene scene) : registry(scene->registry), scene(scene) {} SystemBase(PScene scene) : registry(scene->registry), scene(scene) {}
virtual ~SystemBase() {} virtual ~SystemBase() {}
virtual void run(ThreadPool& pool, double delta) virtual void run(ThreadPool&, double delta)
{ {
deltaTime = delta; deltaTime = delta;
update(); update();
+6 -9
View File
@@ -25,16 +25,14 @@ ThreadPool::~ThreadPool()
void ThreadPool::runAndWait(List<std::function<void()>> functions) void ThreadPool::runAndWait(List<std::function<void()>> functions)
{ {
{ std::unique_lock l(taskLock);
std::unique_lock l(taskLock); currentTask.numRemaining = functions.size();
currentTask.numRemaining = functions.size(); currentTask.functions = std::move(functions);
currentTask.functions = std::move(functions); taskCV.notify_all();
taskCV.notify_all();
}
while (currentTask.numRemaining > 0) while (currentTask.numRemaining > 0)
{ {
std::unique_lock l2(completedLock); completedCV.wait(l);
completedCV.wait(l2);
} }
} }
@@ -59,7 +57,6 @@ void ThreadPool::work()
currentTask.numRemaining--; currentTask.numRemaining--;
if (currentTask.numRemaining == 0) if (currentTask.numRemaining == 0)
{ {
std::unique_lock l2(completedLock);
completedCV.notify_one(); completedCV.notify_one();
} }
} }
-1
View File
@@ -21,7 +21,6 @@ private:
Array<std::thread> workers; Array<std::thread> workers;
std::mutex taskLock; std::mutex taskLock;
std::condition_variable taskCV; std::condition_variable taskCV;
std::mutex completedLock;
std::condition_variable completedCV; std::condition_variable completedCV;
Task currentTask; Task currentTask;
bool running = true; bool running = true;
+2 -1
View File
@@ -7,4 +7,5 @@ target_include_directories(SeeleUnitTests PUBLIC ./)
add_subdirectory(Containers/) add_subdirectory(Containers/)
add_subdirectory(Graphics/) add_subdirectory(Graphics/)
add_subdirectory(Math/) add_subdirectory(Math/)
add_subdirectory(Serialization/) add_subdirectory(Serialization/)
add_subdirectory(System/)
+3
View File
@@ -0,0 +1,3 @@
target_sources(SeeleUnitTests
PRIVATE
ComponentSystem.cpp)
+9
View File
@@ -0,0 +1,9 @@
#include "EngineTest.h"
#include "Component/Component.h"
#include "Component/Transform.h"
#include "Component/Camera.h"
#include "Component/DirectionalLight.h"
#include "System/ComponentSystem.h"
using namespace Seele;
using namespace Seele::System;
+2 -3
View File
@@ -8,14 +8,13 @@ TEST(ThreadPool, RunBatch)
uint32 test = 20; uint32 test = 20;
std::mutex m; std::mutex m;
List<std::function<void()>> work; List<std::function<void()>> work;
for (uint32 i = 0; i < 400; ++i) for (uint32 i = 0; i < 40000; ++i)
{ {
work.add([&]() { work.add([&]() {
std::unique_lock l(m); std::unique_lock l(m);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
test++; test++;
}); });
} }
t.runAndWait(std::move(work)); t.runAndWait(std::move(work));
ASSERT_EQ(test, 420); ASSERT_EQ(test, 40020);
} }