diff --git a/src/Engine/Containers/Tree.h b/src/Engine/Containers/Tree.h index b094948..e6a83b6 100644 --- a/src/Engine/Containers/Tree.h +++ b/src/Engine/Containers/Tree.h @@ -7,16 +7,88 @@ namespace Seele { template struct Tree { protected: struct Node { - Node(const NodeData& data) : data(data) {} - Node(NodeData&& data) : data(std::move(data)) {} - - Node* leftChild = nullptr; - Node* rightChild = nullptr; + Node(Node* left, Node* right, const NodeData& nodeData) : leftChild(left), rightChild(right), data(nodeData) {} + Node(Node* left, Node* right, NodeData&& nodeData) : leftChild(left), rightChild(right), data(std::move(nodeData)) {} + Node* leftChild; + Node* rightChild; NodeData data; }; using NodeAlloc = std::allocator_traits::template rebind_alloc; public: + template 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&& beginIt = Array()) : node(x), traversal(std::move(beginIt)) {} + constexpr IteratorBase(const IteratorBase& i) : node(i.node), traversal(i.traversal) {} + constexpr IteratorBase(IteratorBase&& i) noexcept : 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) noexcept { + 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 std::strong_ordering 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 traversal; + }; + using Iterator = IteratorBase; + using ConstIterator = IteratorBase; + using key_type = KeyType; using value_type = NodeData; using allocator_type = Allocator; @@ -27,130 +99,31 @@ template ::pointer; using const_pointer = std::allocator_traits::const_pointer; - class ConstIterator { - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = value_type; - using difference_type = difference_type; - using pointer = const_pointer; - using reference = const value_type&; - - constexpr ConstIterator(Node* x, List&& traversal) : node(x), traversal(std::move(traversal)) {} - constexpr ConstIterator(const ConstIterator& i) = default; - constexpr ConstIterator(ConstIterator&& i) noexcept = default; - constexpr ConstIterator& operator=(const ConstIterator& other) = default; - constexpr ConstIterator& operator=(ConstIterator&& other) noexcept = default; - constexpr reference operator*() const noexcept { return node->data; } - constexpr pointer operator->() const noexcept { return std::pointer_traits::pointer_to(**this); } - constexpr bool operator==(const ConstIterator& other) { return node == other.node; } - constexpr bool operator!=(const ConstIterator& other) { return node != other.node; } - constexpr std::strong_ordering operator<=>(const ConstIterator& other) { return node <=> other.node; } - constexpr ConstIterator& operator++() noexcept { - // walk up tree until we can go a single step to the right - while (traversal.size() > 0 && node->rightChild == nullptr) { - node = traversal.back(); - traversal.popBack(); - } - // if there is a right subtree we havent visited yet - if (node->rightChild != nullptr) { - // go that single step - node = node->rightChild; - // then to the leftmost node of that right subtree - while (node->leftChild != nullptr) { - node = node->leftChild; - } - } - return *this; - } - constexpr ConstIterator& operator--() { - while (node->leftChild == nullptr) { - node = traversal.back(); - traversal.popBack(); - } - node = node->leftChild; - while (node->rightChild != nullptr) { - node = node->rightChild; - } - return *this; - } - constexpr ConstIterator operator--(int) { - ConstIterator tmp(*this); - --*this; - return tmp; - } - constexpr ConstIterator operator++(int) { - ConstIterator tmp(*this); - ++*this; - return tmp; - } - - Node* getNode() { return node; } - - protected: - Node* node; - List traversal; - }; - - class Iterator : public ConstIterator { - public: - using value_type = value_type; - using difference_type = difference_type; - using pointer = pointer; - using reference = value_type&; - - constexpr Iterator(Node* x, List&& traversal) : ConstIterator(x, std::move(traversal)) {} - constexpr Iterator(const Iterator& i) = default; - constexpr Iterator(Iterator&& i) noexcept = default; - constexpr Iterator& operator=(const Iterator& other) = default; - constexpr Iterator& operator=(Iterator&& other) noexcept = default; - constexpr reference operator*() const noexcept { return const_cast(ConstIterator::operator*()); } - constexpr pointer operator->() const noexcept { return std::pointer_traits::pointer_to(**this); } - constexpr Iterator& operator++() noexcept { - ConstIterator::operator++(); - return *this; - } - constexpr Iterator& operator--() noexcept { - ConstIterator::operator--(); - return *this; - } - constexpr Iterator& operator++(int) noexcept { - Iterator tmp = *this; - ConstIterator::operator++(); - return tmp; - } - constexpr Iterator& operator--(int) noexcept { - Iterator tmp = *this; - ConstIterator::operator--(); - return tmp; - } - }; - using iterator = Iterator; using const_iterator = ConstIterator; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr Tree() noexcept - : alloc(NodeAlloc()), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(Compare()) {} + : alloc(NodeAlloc()), 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) {} + : 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()) {} + : alloc(alloc), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(Compare()) {} constexpr Tree(std::initializer_list init) - : alloc(NodeAlloc()), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(0), comp(Compare()) { + : alloc(NodeAlloc()), root(nullptr), beginIt(nullptr), endIt(nullptr), iteratorsDirty(true), _size(0), comp(Compare()) { for (const auto& it : init) { insert(it); } } - constexpr Tree(const Tree& other) - : alloc(other.alloc), root(nullptr), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), _size(), comp(other.comp) { + constexpr Tree(const Tree& other) : alloc(other.alloc), root(nullptr), iteratorsDirty(true), _size(), comp(other.comp) { for (const auto& elem : other) { insert(elem); } } constexpr Tree(Tree&& other) noexcept - : alloc(std::move(other.alloc)), root(std::move(other.root)), beginIt(nullptr, {}), endIt(nullptr, {}), iteratorsDirty(true), - _size(std::move(other._size)), comp(std::move(other.comp)) { + : alloc(std::move(other.alloc)), root(std::move(other.root)), iteratorsDirty(true), _size(std::move(other._size)), + comp(std::move(other.comp)) { other._size = 0; } constexpr ~Tree() noexcept { clear(); } @@ -221,7 +194,7 @@ template data, key)) { return end(); } - return iterator(root, {}); + return iterator(root); } constexpr iterator find(const key_type& key) const { Node* it = root; @@ -235,21 +208,21 @@ template insert(const NodeData& data) { - const auto& [r, inserted] = _insert(root, data); + auto [r, inserted] = _insert(root, data); root = r; - return Pair(iterator(root, {}), inserted); + return Pair(iterator(root), inserted); } constexpr Pair insert(NodeData&& data) { - const auto& [r, inserted] = _insert(root, std::move(data)); + auto [r, inserted] = _insert(root, std::move(data)); root = r; - return Pair(iterator(root, {}), inserted); + return Pair(iterator(root), inserted); } constexpr iterator remove(const key_type& key) { root = _remove(root, key); - return iterator(root, {}); + return iterator(root); } private: @@ -267,22 +240,26 @@ template traversal; - while (beginNode->leftChild != nullptr) { - traversal.add(beginNode); - beginNode = beginNode->leftChild; + Node* begin = root; + Array beginTraversal; + while (begin != nullptr) { + beginTraversal.add(begin); + begin = begin->leftChild; } - return Iterator(beginNode, std::move(traversal)); + if (!beginTraversal.empty()) { + begin = beginTraversal.back(); + beginTraversal.pop(); + } + return Iterator(begin, std::move(beginTraversal)); } constexpr Iterator calcEndIterator() const { - Node* endNode = root; - List traversal; - while (endNode != nullptr) { - traversal.add(endNode); - endNode = endNode->rightChild; + Node* endIndex = root; + Array endTraversal; + while (endIndex != nullptr) { + endTraversal.add(endIndex); + endIndex = endIndex->rightChild; } - return Iterator(endNode, std::move(traversal)); + return Iterator(endIndex, std::move(endTraversal)); } NodeAlloc alloc; Node* root; @@ -308,7 +285,7 @@ template ::construct(alloc, root, std::forward(data)); + std::allocator_traits::construct(alloc, root, nullptr, nullptr, std::forward(data)); _size++; return Pair(root, true); } @@ -318,7 +295,7 @@ template (r, false); Node* newNode = alloc.allocate(1); - std::allocator_traits::construct(alloc, newNode, std::forward(data)); + std::allocator_traits::construct(alloc, newNode, nullptr, nullptr, std::forward(data)); if (comp(keyFun(newNode->data), keyFun(r->data))) { newNode->rightChild = r; diff --git a/src/Engine/Graphics/Vulkan/Buffer.cpp b/src/Engine/Graphics/Vulkan/Buffer.cpp index 44c3509..c938c5c 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.cpp +++ b/src/Engine/Graphics/Vulkan/Buffer.cpp @@ -102,7 +102,7 @@ void BufferAllocation::updateContents(uint64 regionOffset, uint64 regionSize, vo .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, .usage = VMA_MEMORY_USAGE_AUTO, }; - OBufferAllocation staging = new BufferAllocation(graphics, "UpdateStaging", stagingInfo, stagingAlloc, Gfx::QueueType::GRAPHICS); + OBufferAllocation staging = new BufferAllocation(graphics, fmt::format("{0}UpdateStaging", name), stagingInfo, stagingAlloc, Gfx::QueueType::GRAPHICS); uint8* data; VK_CHECK(vmaMapMemory(graphics->getAllocator(), staging->allocation, (void**)&data)); diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index 386cc6f..2d20d37 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -201,7 +201,7 @@ DescriptorSet::~DescriptorSet() {} void DescriptorSet::updateConstants(const std::string& mappingName, uint32 offset, void* data) { std::memcpy(constantData.data() + owner->getLayout()->mappings[mappingName].constantOffset, (char*)data + offset, - owner->getLayout()->mappings[name].constantSize); + owner->getLayout()->mappings[mappingName].constantSize); } void DescriptorSet::updateBuffer(const std::string& mappingName, uint32 index, Gfx::PShaderBuffer shaderBuffer) { @@ -418,8 +418,7 @@ void DescriptorSet::updateAccelerationStructure(const std::string& mappingName, void DescriptorSet::writeChanges() { if (constantData.size() > 0) { - if (constantsBuffer != nullptr) - { + if (constantsBuffer != nullptr) { graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer)); } constantsBuffer = new BufferAllocation(graphics, owner->getLayout()->getName(), @@ -452,7 +451,7 @@ void DescriptorSet::writeChanges() { .pBufferInfo = &bufferInfos.back(), }); } - + if (writeDescriptors.size() > 0) { if (isCurrentlyBound()) { std::cout << "Descriptor currently bound, allocate a new one instead" << std::endl;