From a91aeab309b9abfccd37b3bbc036c118231e235d Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Tue, 25 Mar 2025 09:17:08 +0100 Subject: [PATCH] maps dont work --- CMakeLists.txt | 2 +- src/Engine/Asset/AssetRegistry.cpp | 6 ++ src/Engine/Containers/Array.h | 8 +- src/Engine/Containers/MemoryResource.cpp | 29 +++--- src/Engine/Containers/MemoryResource.h | 6 ++ src/Engine/Containers/Tree.h | 106 ++++++++++------------ src/Engine/Graphics/Descriptor.cpp | 4 +- src/Engine/Graphics/Vulkan/Descriptor.cpp | 36 ++++---- tests/Engine/Containers/Array.cpp | 4 +- tests/Engine/Containers/List.cpp | 20 +--- 10 files changed, 103 insertions(+), 118 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b587e18..8cae02e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,7 +140,7 @@ target_compile_options(Engine PUBLIC "$<$:-DENABLE_VALIDATION>") target_compile_options(Engine PUBLIC "$<$:-DSEELE_DEBUG>") add_subdirectory(src/) -#add_subdirectory(tests/) +add_subdirectory(tests/) if(WIN32) add_custom_target(dll_copy ALL diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index 2b563b4..b07d4a0 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -7,6 +7,12 @@ #include "MeshAsset.h" #include "SVGAsset.h" #include "TextureAsset.h" +#include "Asset/TextureAsset.h" +#include "Asset/FontAsset.h" +#include "Asset/SVGAsset.h" +#include "Asset/MeshAsset.h" +#include "Asset/MaterialAsset.h" +#include "Asset/MaterialInstanceAsset.h" #include "Window/WindowManager.h" #include #include diff --git a/src/Engine/Containers/Array.h b/src/Engine/Containers/Array.h index c62a4cc..0f2e658 100644 --- a/src/Engine/Containers/Array.h +++ b/src/Engine/Containers/Array.h @@ -88,7 +88,7 @@ template struct Array { _data = allocateArray(DEFAULT_ALLOC_SIZE); assert(_data != nullptr); } - constexpr Array(size_type size, const value_type& value, const allocator_type& alloc = allocator_type()) + constexpr Array(size_type size, const value_type& value, const allocator_type& alloc = allocator_type(debug_resource::get("Array"))) : arraySize(size), allocated(size), allocator(alloc) { _data = allocateArray(size); assert(_data != nullptr); @@ -96,7 +96,7 @@ template struct Array { std::allocator_traits::construct(allocator, &_data[i], value); } } - constexpr explicit Array(size_type size, const allocator_type& alloc = allocator_type()) + constexpr explicit Array(size_type size, const allocator_type& alloc = allocator_type(debug_resource::get("Array"))) : arraySize(size), allocated(size), allocator(alloc) { _data = allocateArray(size); assert(_data != nullptr); @@ -108,14 +108,14 @@ template struct Array { } } } - constexpr Array(std::initializer_list init, const allocator_type& alloc = allocator_type()) + constexpr Array(std::initializer_list init, const allocator_type& alloc = allocator_type(debug_resource::get("Array"))) : arraySize(init.size()), allocated(init.size()), allocator(alloc) { _data = allocateArray(init.size()); assert(_data != nullptr); std::uninitialized_copy(init.begin(), init.end(), begin()); } template Range> - constexpr Array(std::from_range_t, Range&& rg, const allocator_type& alloc = allocator_type()) + constexpr Array(std::from_range_t, Range&& rg, const allocator_type& alloc = allocator_type(debug_resource::get("Array"))) : arraySize(0), allocated(0), allocator(alloc) { if constexpr (std::ranges::sized_range || std::ranges::forward_range) { arraySize = std::ranges::size(rg); diff --git a/src/Engine/Containers/MemoryResource.cpp b/src/Engine/Containers/MemoryResource.cpp index 51d31f4..b549c53 100644 --- a/src/Engine/Containers/MemoryResource.cpp +++ b/src/Engine/Containers/MemoryResource.cpp @@ -4,31 +4,34 @@ using namespace Seele; -std::mutex allocationMutex; -std::map allocationCounters; -std::mutex resourcesMutex; -std::map debugResources; std::atomic_uint64_t& MemoryManager::getAllocationCounter(const std::string& name) { - std::unique_lock l(allocationMutex); - return allocationCounters[name]; + std::unique_lock l(getInstance().allocationMutex); + return getInstance().allocationCounters[name]; } void MemoryManager::printAllocations() { - std::unique_lock l(allocationMutex); - for (const auto& [name, counter] : allocationCounters) { - if (name.empty()) - { + std::unique_lock l(getInstance().allocationMutex); + for (const auto& [name, counter] : getInstance().allocationCounters) { + if (name.empty()) { std::cout << name << ": " << counter << std::endl; - } - else - { + } else { std::cout << name << ": " << counter << std::endl; } } } +MemoryManager& MemoryManager::getInstance() { + static MemoryManager instance; + return instance; +} + debug_resource* debug_resource::get(const std::string& name) { + static std::mutex resourcesMutex; + static std::map debugResources; std::unique_lock l(resourcesMutex); + if (!debugResources.count(name)) { + debugResources.emplace(std::pair(name, debug_resource(name))); + } return &debugResources[name]; } \ No newline at end of file diff --git a/src/Engine/Containers/MemoryResource.h b/src/Engine/Containers/MemoryResource.h index 0a58f57..8faeac4 100644 --- a/src/Engine/Containers/MemoryResource.h +++ b/src/Engine/Containers/MemoryResource.h @@ -1,5 +1,6 @@ #pragma once #include +#include namespace Seele { class MemoryManager { @@ -8,6 +9,9 @@ class MemoryManager { static void printAllocations(); private: + static MemoryManager& getInstance(); + std::mutex allocationMutex; + std::map allocationCounters; }; class debug_resource : public std::pmr::memory_resource { @@ -17,7 +21,9 @@ class debug_resource : public std::pmr::memory_resource { debug_resource(std::string name, std::pmr::memory_resource* up = std::pmr::get_default_resource()) : name(name), upstream(up), counter(MemoryManager::getAllocationCounter(name)) {} debug_resource(const debug_resource& other) = delete; + debug_resource(debug_resource&& other) = default; debug_resource& operator=(const debug_resource& other) = delete; + debug_resource& operator=(debug_resource&& other) = default; void* do_allocate(size_t bytes, size_t alignment) override { counter += bytes; diff --git a/src/Engine/Containers/Tree.h b/src/Engine/Containers/Tree.h index bc099bb..b094948 100644 --- a/src/Engine/Containers/Tree.h +++ b/src/Engine/Containers/Tree.h @@ -1,23 +1,17 @@ #pragma once #include "Array.h" +#include "List.h" #include "Pair.h" -#include -#include -#include namespace Seele { template struct Tree { protected: struct Node { - Node(const NodeData& data) - : data(data) - {} - Node(NodeData&& data) - : data(std::move(data)) {} + Node(const NodeData& data) : data(data) {} + Node(NodeData&& data) : data(std::move(data)) {} - Node* parent = nullptr; - Node* leftChild; - Node* rightChild; + Node* leftChild = nullptr; + Node* rightChild = nullptr; NodeData data; }; using NodeAlloc = std::allocator_traits::template rebind_alloc; @@ -41,7 +35,7 @@ template && 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; @@ -52,22 +46,30 @@ template (const ConstIterator& other) { return node <=> other.node; } constexpr ConstIterator& operator++() noexcept { - node = node->rightChild; - while (node != nullptr && node->leftChild != nullptr) { - node = node->leftChild; + // 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 (node == nullptr && node->parent != nullptr) { - node = node->parent; + // 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--() { - node = node->leftChild; - while (node != nullptr && node->rightChild != nullptr) { - node = node->rightchild; + while (node->leftChild == nullptr) { + node = traversal.back(); + traversal.popBack(); } - if (node == nullptr && node->parent != nullptr) { - node = node->parent; + node = node->leftChild; + while (node->rightChild != nullptr) { + node = node->rightChild; } return *this; } @@ -86,6 +88,7 @@ template traversal; }; class Iterator : public ConstIterator { @@ -95,7 +98,7 @@ template && 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; @@ -128,25 +131,26 @@ template ; 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), iteratorsDirty(true), _size(), comp(other.comp) { + constexpr Tree(const Tree& other) + : alloc(other.alloc), root(nullptr), beginIt(nullptr, {}), endIt(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)), iteratorsDirty(true), _size(std::move(other._size)), - comp(std::move(other.comp)) { + : 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)) { other._size = 0; } constexpr ~Tree() noexcept { clear(); } @@ -217,7 +221,7 @@ template data, key)) { return end(); } - return iterator(root); + return iterator(root, {}); } constexpr iterator find(const key_type& key) const { Node* it = root; @@ -231,21 +235,21 @@ template insert(const NodeData& data) { const 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)); 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: @@ -263,21 +267,22 @@ template leftChild; + Node* beginNode = root; + List traversal; + while (beginNode->leftChild != nullptr) { + traversal.add(beginNode); + beginNode = beginNode->leftChild; } - if (begin->parent != nullptr) { - begin = begin->parent; - } - return Iterator(begin); + return Iterator(beginNode, std::move(traversal)); } constexpr Iterator calcEndIterator() const { Node* endNode = root; + List traversal; while (endNode != nullptr) { + traversal.add(endNode); endNode = endNode->rightChild; } - return Iterator(endNode); + return Iterator(endNode, std::move(traversal)); } NodeAlloc alloc; Node* root; @@ -290,17 +295,13 @@ template rightChild; x->rightChild = y->leftChild; - x->rightChild->parent = x; y->leftChild = x; - y->leftChild->parent = y; return y; } Node* rotateRight(Node* x) { Node* y = x->leftChild; x->leftChild = y->rightChild; - x->leftChild->parent = x; y->rightChild = x; - y->rightChild->parent = y; return y; } template Pair _insert(Node* r, NodeDataType&& data) { @@ -321,15 +322,11 @@ template data), keyFun(r->data))) { newNode->rightChild = r; - newNode->rightChild->parent = newNode; newNode->leftChild = r->leftChild; - newNode->leftChild->parent = newNode; r->leftChild = nullptr; } else { newNode->leftChild = r; - newNode->leftChild->parent = newNode; newNode->rightChild = r->rightChild; - newNode->rightChild->parent = newNode; r->rightChild = nullptr; } _size++; @@ -354,7 +351,6 @@ template leftChild, key); r->rightChild = temp->rightChild; - r->rightChild->parent = r; } std::allocator_traits::destroy(alloc, temp); alloc.deallocate(temp, 1); @@ -372,16 +368,13 @@ template leftChild->data))) { r->leftChild->leftChild = _splay(r->leftChild->leftChild, key); - r->leftChild->leftChild->parent = r->leftChild; r = rotateRight(r); } else if (comp(keyFun(r->leftChild->data), key)) { r->leftChild->rightChild = _splay(r->leftChild->rightChild, key); - r->leftChild->rightChild->parent = r->leftChild; if (r->leftChild->rightChild != nullptr) { r->leftChild = rotateLeft(r->leftChild); - r->leftChild->parent = r; } } return (r->leftChild == nullptr) ? r : rotateRight(r); @@ -391,15 +384,12 @@ template rightChild->data))) { r->rightChild->leftChild = _splay(r->rightChild->leftChild, key); - r->rightChild->leftChild->parent = r->rightChild; if (r->rightChild->leftChild != nullptr) { r->rightChild = rotateRight(r->rightChild); - r->rightChild->parent = r; } } else if (comp(keyFun(r->rightChild->data), key)) { r->rightChild->rightChild = _splay(r->rightChild->rightChild, key); - r->rightChild->rightChild->parent = r->rightChild; r = rotateLeft(r); } diff --git a/src/Engine/Graphics/Descriptor.cpp b/src/Engine/Graphics/Descriptor.cpp index db557dc..4d81705 100644 --- a/src/Engine/Graphics/Descriptor.cpp +++ b/src/Engine/Graphics/Descriptor.cpp @@ -38,6 +38,4 @@ void PipelineLayout::addDescriptorLayout(PDescriptorLayout layout) { descriptorS void PipelineLayout::addPushConstants(const SePushConstantRange& pushConstant) { pushConstants.add(pushConstant); } -void PipelineLayout::addMapping(std::string name, uint32 index) { - parameterMapping[name] = index; -} +void PipelineLayout::addMapping(std::string mappingName, uint32 index) { parameterMapping[mappingName] = index; } diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index 40d209e..386cc6f 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -199,14 +199,14 @@ DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner) DescriptorSet::~DescriptorSet() {} -void DescriptorSet::updateConstants(const std::string& name, uint32 offset, void* data) { - std::memcpy(constantData.data() + owner->getLayout()->mappings[name].constantOffset, (char*)data + offset, +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); } -void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PShaderBuffer shaderBuffer) { +void DescriptorSet::updateBuffer(const std::string& mappingName, uint32 index, Gfx::PShaderBuffer shaderBuffer) { PShaderBuffer vulkanBuffer = shaderBuffer.cast(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) { return; @@ -230,9 +230,9 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PSh boundResources[binding][index] = vulkanBuffer->getAlloc(); } -void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer indexBuffer) { +void DescriptorSet::updateBuffer(const std::string& mappingName, uint32 index, Gfx::PVertexBuffer indexBuffer) { PVertexBuffer vulkanBuffer = indexBuffer.cast(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) { return; @@ -257,9 +257,9 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PVe boundResources[binding][index] = vulkanBuffer->getAlloc(); } -void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) { +void DescriptorSet::updateBuffer(const std::string& mappingName, uint32 index, Gfx::PIndexBuffer indexBuffer) { PIndexBuffer vulkanBuffer = indexBuffer.cast(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanBuffer->getAlloc() || vulkanBuffer->getAlloc() == nullptr) { return; @@ -284,9 +284,9 @@ void DescriptorSet::updateBuffer(const std::string& name, uint32 index, Gfx::PIn boundResources[binding][index] = vulkanBuffer->getAlloc(); } -void DescriptorSet::updateSampler(const std::string& name, uint32 index, Gfx::PSampler samplerState) { +void DescriptorSet::updateSampler(const std::string& mappingName, uint32 index, Gfx::PSampler samplerState) { PSampler vulkanSampler = samplerState.cast(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanSampler->getHandle()) { return; @@ -312,9 +312,9 @@ void DescriptorSet::updateSampler(const std::string& name, uint32 index, Gfx::PS boundResources[binding][index] = vulkanSampler->getHandle(); } -void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTexture2D texture) { +void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, Gfx::PTexture2D texture) { TextureBase* vulkanTexture = texture.cast().getHandle(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanTexture->getHandle()) { return; @@ -340,9 +340,9 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT boundResources[binding][index] = vulkanTexture->getHandle(); } -void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTexture3D texture) { +void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, Gfx::PTexture3D texture) { TextureBase* vulkanTexture = texture.cast().getHandle(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanTexture->getHandle()) { return; @@ -368,9 +368,9 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT boundResources[binding][index] = vulkanTexture->getHandle(); } -void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PTextureCube texture) { +void DescriptorSet::updateTexture(const std::string& mappingName, uint32 index, Gfx::PTextureCube texture) { TextureBase* vulkanTexture = texture.cast().getHandle(); - const auto& map = owner->getLayout()->mappings[name]; + const auto& map = owner->getLayout()->mappings[mappingName]; uint32 binding = map.binding; if (boundResources[binding][index] == vulkanTexture->getHandle()) { return; @@ -396,9 +396,9 @@ void DescriptorSet::updateTexture(const std::string& name, uint32 index, Gfx::PT boundResources[binding][index] = vulkanTexture->getHandle(); } -void DescriptorSet::updateAccelerationStructure(const std::string& name, uint32 index, Gfx::PTopLevelAS as) { +void DescriptorSet::updateAccelerationStructure(const std::string& mappingName, uint32 index, Gfx::PTopLevelAS as) { auto tlas = as.cast(); - uint32 binding = owner->getLayout()->mappings[name].binding; + uint32 binding = owner->getLayout()->mappings[mappingName].binding; accelerationInfos.add(VkWriteDescriptorSetAccelerationStructureKHR{ .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR, .pNext = nullptr, diff --git a/tests/Engine/Containers/Array.cpp b/tests/Engine/Containers/Array.cpp index 9512bcf..2e78b80 100644 --- a/tests/Engine/Containers/Array.cpp +++ b/tests/Engine/Containers/Array.cpp @@ -88,7 +88,7 @@ TEST(ArraySuite, remove_iterator) array.add(3); array.add(4); array.add(5); - array.remove(array.find(1), false); + array.erase(array.find(1), false); ASSERT_EQ(array[0], 5); ASSERT_EQ(array[1], 2); ASSERT_EQ(array[2], 3); @@ -104,7 +104,7 @@ TEST(ArraySuite, remove_iterator_keep_order) array.add(3); array.add(4); array.add(5); - array.remove(array.find(1)); + array.erase(array.find(1)); ASSERT_EQ(array[0], 2); ASSERT_EQ(array[1], 3); ASSERT_EQ(array[2], 4); diff --git a/tests/Engine/Containers/List.cpp b/tests/Engine/Containers/List.cpp index 2d17c4d..ad6f80c 100644 --- a/tests/Engine/Containers/List.cpp +++ b/tests/Engine/Containers/List.cpp @@ -35,22 +35,4 @@ TEST(ListSuite, basic_remove) it = list.remove(it); ASSERT_EQ(*it, 4); ASSERT_EQ(list.size(), 2); -} - -TEST(ListSuite, list_join) -{ - List list1; - List list2; - list1.add(2); - list1.add(1); - list1.add(3); - list1.add(4); - list2.add(5); - list2.add(7); - list2.add(8); - list2.add(6); - list2.add(9); - list1.moveElements(std::move(list2)); - ASSERT_EQ(list1.size(), 9); - ASSERT_EQ(list2.size(), 0); -} +} \ No newline at end of file