Polymorphic Map and threadpool

This commit is contained in:
2021-11-01 20:25:16 +01:00
parent 9c48c48f8c
commit 1b7ab9b1f1
20 changed files with 1442 additions and 1280 deletions
+3 -1
View File
@@ -21,7 +21,9 @@ set(NSAM_ROOT ${EXTERNAL_ROOT}/aftermath)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_LIB_PREFIX lib)
if(WIN32)
set(Boost_LIB_PREFIX lib)
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE})
#Workaround for vs, because it places artifacts into an additional subfolder
+1
View File
@@ -34,6 +34,7 @@ void TextureAsset::load()
setStatus(Status::Loading);
ktxTexture2* kTexture;
// TODO: consider return
std::cout << "Loading texture " << getFullPath() << std::endl;
ktxTexture2_CreateFromNamedFile(getFullPath().c_str(),
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
&kTexture);
-1
View File
@@ -28,7 +28,6 @@ void TextureLoader::importAsset(const std::filesystem::path& filePath)
PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderAsset->getTexture());
std::cout << "Loading texture " << asset->getFileName() << std::endl;
AssetRegistry::get().registerTexture(asset);
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
using namespace std::chrono_literals;
+37 -41
View File
@@ -112,7 +112,7 @@ namespace Seele
assert(_data != nullptr);
markIteratorDirty();
}
constexpr Array(size_type size, const T& value, const allocator_type& alloc = allocator_type())
constexpr Array(size_type size, const value_type& value, const allocator_type& alloc = allocator_type())
: arraySize(size)
, allocated(size)
, allocator(alloc)
@@ -169,21 +169,16 @@ namespace Seele
{
if (this != &other)
{
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value
&& !std::allocator_traits<allocator_type>::is_always_equal::value)
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
{
if(allocator != other.allocator)
if (!std::allocator_traits<allocator_type>::is_always_equal::value
&& allocator != other.allocator)
{
deallocateArray(_data, allocated);
_data = nullptr;
}
}
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value)
{
allocator = other.allocator;
}
else
{}
if(other.arraySize > allocated)
{
if(_data != nullptr)
@@ -203,17 +198,14 @@ namespace Seele
{
if (this != &other)
{
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value
&& !std::allocator_traits<allocator_type>::is_always_equal::value)
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
{
if(allocator != other.allocator)
if (!std::allocator_traits<allocator_type>::is_always_equal::value
&& allocator != other.allocator)
{
deallocateArray(_data, allocated);
_data = nullptr;
}
}
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
{
allocator = std::move(other.allocator);
}
if (_data != nullptr)
@@ -248,59 +240,59 @@ namespace Seele
return !(*this == other);
}
constexpr Iterator find(const T &item)
constexpr iterator find(const value_type &item)
{
for (uint32 i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
return Iterator(&_data[i]);
return iterator(&_data[i]);
}
}
return endIt;
}
constexpr Iterator find(T&& item)
constexpr iterator find(value_type&& item)
{
for (uint32 i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
return Iterator(&_data[i]);
return iterator(&_data[i]);
}
}
return endIt;
}
constexpr Allocator get_allocator() const
constexpr allocator_type get_allocator() const
{
return allocator;
}
constexpr Iterator begin() const
constexpr iterator begin() const
{
return beginIt;
}
constexpr Iterator end() const
constexpr iterator end() const
{
return endIt;
}
constexpr ConstIterator cbegin() const
constexpr const_iterator cbegin() const
{
return beginIt;
}
constexpr ConstIterator cend() const
constexpr const_iterator cend() const
{
return endIt;
}
constexpr T &add(const T &item = T())
constexpr reference add(const value_type &item = value_type())
{
return addInternal(item);
}
constexpr T &add(T&& item)
constexpr reference add(value_type&& item)
{
return addInternal(std::forward<T>(item));
}
constexpr T &addUnique(const T &item = T())
constexpr reference addUnique(const value_type &item = value_type())
{
Iterator it;
iterator it;
if((it = std::move(find(item))) != endIt)
{
return *it;
@@ -308,7 +300,7 @@ namespace Seele
return addInternal(item);
}
template<typename... args>
constexpr T &emplace(args... arguments)
constexpr reference emplace(args... arguments)
{
if (arraySize == allocated)
{
@@ -316,10 +308,8 @@ namespace Seele
allocated = calculateGrowth(newSize);
T *tempArray = allocateArray(allocated);
assert(tempArray != nullptr);
for (size_type i = 0; i < arraySize; ++i)
{
tempArray[i] = std::move(_data[i]);
}
std::uninitialized_move(begin(), end(), Iterator(tempArray));
deallocateArray(_data, arraySize);
_data = tempArray;
}
@@ -327,7 +317,7 @@ namespace Seele
markIteratorDirty();
return _data[arraySize - 1];
}
constexpr void remove(Iterator it, bool keepOrder = true)
constexpr void remove(iterator it, bool keepOrder = true)
{
remove(it - beginIt, keepOrder);
}
@@ -351,7 +341,7 @@ namespace Seele
{
resizeInternal(newSize, std::move(T()));
}
constexpr void resize(size_type newSize, const T& value)
constexpr void resize(size_type newSize, const value_type& value)
{
resizeInternal(newSize, value);
}
@@ -367,11 +357,11 @@ namespace Seele
allocated = 0;
markIteratorDirty();
}
inline size_type indexOf(Iterator iterator)
inline size_type indexOf(iterator iterator)
{
return iterator - beginIt;
}
inline size_type indexOf(ConstIterator iterator) const
inline size_type indexOf(const_iterator iterator) const
{
return iterator.p - beginIt.p;
}
@@ -395,12 +385,18 @@ namespace Seele
{
return allocated;
}
inline T *data() const
inline pointer data() const
{
return _data;
}
inline T &back() const
inline reference front() const
{
assert(arraySize > 0);
return _data[0];
}
inline reference back() const
{
assert(arraySize > 0);
return _data[arraySize - 1];
}
void pop()
@@ -408,12 +404,12 @@ namespace Seele
arraySize--;
markIteratorDirty();
}
constexpr inline T &operator[](size_type index)
constexpr inline reference operator[](size_type index)
{
assert(index < arraySize);
return _data[index];
}
constexpr inline const T &operator[](size_type index) const
constexpr inline const reference operator[](size_type index) const
{
assert(index < arraySize);
return _data[index];
+24 -7
View File
@@ -1,6 +1,6 @@
#pragma once
#include "MinimalEngine.h"
#include <xmemory>
#include <memory_resource>
namespace Seele
{
@@ -187,11 +187,11 @@ public:
return *this;
}
T &front()
reference front()
{
return root->data;
}
T &back()
reference back()
{
return tail->prev->data;
}
@@ -218,7 +218,7 @@ public:
root = allocateNode();
tail = root;
}
tail->data = value;
initializeNode(tail, value);
Node *newTail = allocateNode();
newTail->prev = tail;
newTail->next = nullptr;
@@ -236,7 +236,7 @@ public:
root = allocateNode();
tail = root;
}
tail->data = std::move(value);
initializeNode(tail, std::move(value));
Node *newTail = allocateNode();
newTail->prev = tail;
newTail->next = nullptr;
@@ -247,6 +247,13 @@ public:
_size++;
return insertedElement;
}
// front + popFront
value_type&& retrieve()
{
auto&& temp = std::move(root->data);
popFront();
return std::move(temp);
}
iterator remove(iterator pos)
{
_size--;
@@ -299,7 +306,7 @@ public:
}
Node *tmp = pos.node->prev;
Node *newNode = allocateNode();
newNode->data = value;
initializeNode(newNode, value);
tmp->next = newNode;
newNode->prev = tmp;
newNode->next = pos.node;
@@ -346,10 +353,20 @@ private:
Node* allocateNode()
{
Node* node = allocator.allocate(1);
std::memset(node, 0, sizeof(Node));
assert(node != nullptr);
node->prev = nullptr;
node->next = nullptr;
return node;
}
template<typename Type>
void initializeNode(Node* node, Type&& data)
{
std::allocator_traits<NodeAllocator>::construct(allocator,
node,
node->prev,
node->next,
std::forward<Type>(data));
}
void deallocateNode(Node* node)
{
allocator.deallocate(node, 1);
+173 -180
View File
@@ -11,18 +11,26 @@ public:
Pair()
: key(K()), value(V())
{}
Pair(const Pair& other) = default;
Pair(Pair&& other) = default;
~Pair(){}
Pair& operator=(const Pair& other) = default;
Pair& operator=(Pair&& other) = default;
template<class KeyType>
explicit Pair(KeyType&& key)
: key(key), value(V())
: key(std::forward<KeyType>(key)), value(V())
{}
template<class KeyType, class ValueType>
explicit Pair(KeyType&& key, ValueType&& value)
: key(std::move(key)), value(std::move(value))
: key(std::forward<KeyType>(key)), value(std::forward<ValueType>(value))
{}
K key;
V value;
};
template <typename K, typename V>
template <typename K,
typename V,
typename Compare = std::less<K>,
typename Allocator = std::pmr::polymorphic_allocator<Pair<const K,V>>>
struct Map
{
private:
@@ -35,158 +43,49 @@ private:
: leftChild(nullptr), rightChild(nullptr), pair()
{
}
template<class KeyType, class ValueType>
explicit Node(KeyType&& key, ValueType&& value)
: leftChild(nullptr), rightChild(nullptr), pair(key, value)
{
}
Node(const Node& other)
: pair(other.pair.key, other.pair.value)
{
if(other.leftChild != nullptr)
{
leftChild = new Node(*other.leftChild);
}
if(other.rightChild != nullptr)
{
rightChild = new Node(*other.rightChild);
}
}
Node(Node&& other)
: leftChild(std::move(other.leftChild)), rightChild(std::move(other.rightChild))
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()
{
if (leftChild != nullptr)
{
delete leftChild;
}
if (rightChild != nullptr)
{
delete rightChild;
}
}
Node& operator=(const Node& other)
{
if(this != &other)
{
if(leftChild != nullptr)
{
delete leftChild;
}
if(rightChild != nullptr)
{
delete rightChild;
}
if(other.leftChild != nullptr)
{
leftChild = new Node(*other.leftChild);
}
if(other.rightChild != nullptr)
{
rightChild = new Node(*other.rightChild);
}
}
}
Node& operator=(Node&& other)
{
if(this != &other)
{
if(leftChild != nullptr)
{
delete leftChild;
}
if(rightChild != nullptr)
{
delete rightChild;
}
leftChild = std::move(other.leftChild);
rightChild = std::move(other.rightChild);
}
return *this;
}
};
using NodeAlloc = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
public:
Map()
: root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
{
}
Map(const Map& other)
: root(new Node(*other.root))
, _size(other._size)
{
refreshIterators();
}
Map(Map&& other)
: root(std::move(other.root))
, _size(other._size)
{
refreshIterators();
}
~Map()
{
delete root;
}
Map& operator=(const Map& other)
{
if(this != &other)
{
if(root != nullptr)
{
delete root;
}
root = new Node(*other.root);
_size = other.size;
markIteratorDirty();
}
return *this;
}
Map& operator=(Map&& other)
{
if(this != &other)
{
if(root != nullptr)
{
delete root;
}
root = new Node(std::move(*other.root));
_size = std::move(other._size);
markIteratorDirty();
}
return *this;
}
class Iterator
template<typename PairType>
class IteratorBase
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = Pair<K, V>;
using value_type = PairType;
using difference_type = std::ptrdiff_t;
using reference = Pair<K, V>&;
using pointer = Pair<K, V>*;
using reference = PairType&;
using pointer = PairType*;
Iterator(Node *x = nullptr)
IteratorBase(Node *x = nullptr)
: node(x)
{
}
Iterator(Node *x, Array<Node *> &&beginIt)
IteratorBase(Node *x, Array<Node *> &&beginIt)
: node(x), traversal(std::move(beginIt))
{
}
Iterator(const Iterator &i)
IteratorBase(const IteratorBase &i)
: node(i.node), traversal(i.traversal)
{
}
Iterator(Iterator&& i)
IteratorBase(IteratorBase&& i)
: node(std::move(i.node)), traversal(std::move(i.traversal))
{
}
Iterator& operator=(const Iterator& other)
IteratorBase& operator=(const IteratorBase& other)
{
if(this != &other)
{
@@ -195,7 +94,7 @@ public:
}
return *this;
}
Iterator& operator=(Iterator&& other)
IteratorBase& operator=(IteratorBase&& other)
{
if(this != &other)
{
@@ -212,15 +111,15 @@ public:
{
return &node->pair;
}
inline bool operator!=(const Iterator &other)
inline bool operator!=(const IteratorBase &other)
{
return node != other.node;
}
inline bool operator==(const Iterator &other)
inline bool operator==(const IteratorBase &other)
{
return node == other.node;
}
Iterator &operator++()
IteratorBase &operator++()
{
node = node->rightChild;
while (node != nullptr && node->leftChild != nullptr)
@@ -235,7 +134,7 @@ public:
}
return *this;
}
Iterator &operator--()
IteratorBase &operator--()
{
node = node->leftChild;
while (node != nullptr && node->rightchild != nullptr)
@@ -250,15 +149,15 @@ public:
}
return *this;
}
Iterator operator--(int)
IteratorBase operator--(int)
{
Iterator tmp(*this);
IteratorBase tmp(*this);
--*this;
return tmp;
}
Iterator operator++(int)
IteratorBase operator++(int)
{
Iterator tmp(*this);
IteratorBase tmp(*this);
++*this;
return tmp;
}
@@ -267,72 +166,164 @@ public:
Node *node;
Array<Node *> traversal;
};
inline V &operator[](const K& key)
using Iterator = IteratorBase<Pair<K,V>>;
using ConstIterator = IteratorBase<const Pair<K,V>>;
using key_type = K;
using mapped_type = V;
using value_type = Pair<K,V>;
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>;
Map()
: root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
, comp(Compare())
{
}
explicit Map(const Compare& comp,
const Allocator& alloc = Allocator())
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
, comp(comp)
{
}
explicit Map(const Allocator& alloc)
: nodeContainer(alloc)
, root(nullptr)
, beginIt(nullptr)
, endIt(nullptr)
, iteratorsDirty(false)
, _size(0)
, comp(Compare())
{
}
Map(const Map& other)
: nodeContainer(other.nodeContainer)
, _size(other._size)
, comp(other.comp)
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators();
}
Map(Map&& other)
: nodeContainer(other.nodeContainer)
, _size(std::move(other._size))
, comp(std::move(other.comp))
{
root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators();
}
~Map()
{
}
Map& operator=(const Map& other)
{
if(this != &other)
{
nodeContainer = other.nodeContainer;
root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = other._size;
comp = other.comp;
refreshIterators();
}
return *this;
}
Map& operator=(Map&& other)
{
if(this != &other)
{
nodeContainer = std::move(other.nodeContainer);
root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = std::move(other._size);
comp = std::move(other.comp);
refreshIterators();
}
return *this;
}
inline mapped_type& operator[](const key_type& key)
{
root = splay(root, key);
markIteratorDirty();
if (root == nullptr || root->pair.key < key || key < root->pair.key)
refreshIterators();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
root = insert(root, key);
_size++;
}
return root->pair.value;
}
inline V &operator[](K&& key)
inline mapped_type& operator[](key_type&& key)
{
root = splay(root, std::move(key));
markIteratorDirty();
if (root == nullptr || root->pair.key < key || key < root->pair.key)
refreshIterators();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
root = insert(root, std::move(key));
_size++;
}
return root->pair.value;
}
Iterator find(const K& key)
iterator find(const key_type& key)
{
root = splay(root, key);
markIteratorDirty();
if (root == nullptr || root->pair.key != key)
refreshIterators();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
return endIt;
}
return Iterator(root);
return iterator(root);
}
Iterator find(K&& key)
iterator find(key_type&& key)
{
root = splay(root, std::move(key));
markIteratorDirty();
if (root == nullptr || root->pair.key != key)
refreshIterators();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{
return endIt;
}
return Iterator(root);
return iterator(root);
}
Iterator erase(const K& key)
iterator erase(const key_type& key)
{
root = remove(root, key);
markIteratorDirty();
return Iterator(root);
refreshIterators();
return iterator(root);
}
Iterator erase(K&& key)
iterator erase(K&& key)
{
root = remove(root, std::move(key));
markIteratorDirty();
return Iterator(root);
refreshIterators();
return iterator(root);
}
void clear()
{
delete root;
nodeContainer.clear();
root = nullptr;
_size = 0;
markIteratorDirty();
refreshIterators();
}
bool exists(K&& key)
bool exists(key_type&& key)
{
return find(std::forward<K>(key)) != endIt;
}
Iterator begin()
iterator begin()
{
if(iteratorsDirty)
{
@@ -340,7 +331,7 @@ public:
}
return beginIt;
}
Iterator end()
iterator end()
{
if(iteratorsDirty)
{
@@ -348,7 +339,7 @@ public:
}
return endIt;
}
Iterator begin() const
iterator begin() const
{
if(iteratorsDirty)
{
@@ -356,7 +347,7 @@ public:
}
return beginIt;
}
Iterator end() const
iterator end() const
{
if(iteratorsDirty)
{
@@ -366,9 +357,9 @@ public:
}
bool empty() const
{
return root == nullptr;
return nodeContainer.empty();
}
uint32 size() const
size_type size() const
{
return _size;
}
@@ -422,11 +413,13 @@ private:
return Iterator(endNode, std::move(endTraversal));
}
}
Array<Node, NodeAlloc> nodeContainer;
Node *root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
uint32 _size;
Compare comp;
Node *rotateRight(Node *node)
{
Node *y = node->leftChild;
@@ -446,16 +439,16 @@ private:
{
if (r == nullptr)
{
return new Node(std::forward<KeyType>(key), V());
return &nodeContainer.emplace(std::forward<KeyType>(key));
}
r = splay(r, key);
if (!(r->pair.key < key || key < r->pair.key))
if (!(comp(r->pair.key, key) || comp(key, r->pair.key)))
return r;
Node *newNode = new Node(std::forward<KeyType>(key), V());
Node *newNode = &nodeContainer.emplace(std::forward<KeyType>(key));
if (key < r->pair.key)
if (comp(key, r->pair.key))
{
newNode->rightChild = r;
newNode->leftChild = r->leftChild;
@@ -478,7 +471,7 @@ private:
r = splay(r, key);
if (r->pair.key < key || key < r->pair.key)
if (comp(r->pair.key, key) || comp(key, r->pair.key))
return r;
if (!r->leftChild)
@@ -502,23 +495,23 @@ private:
template<class KeyType>
Node *splay(Node *r, KeyType&& key)
{
if (r == nullptr || !(r->pair.key < key || key < r->pair.key))
if (r == nullptr || !(comp(r->pair.key, key) || comp(key, r->pair.key)))
{
return r;
}
if (key < r->pair.key)
if (comp(key, r->pair.key))
{
if (r->leftChild == nullptr)
return r;
if (key < r->leftChild->pair.key)
if (comp(key, r->leftChild->pair.key))
{
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
r = rotateRight(r);
}
else if (r->leftChild->pair.key < key)
else if (comp(r->leftChild->pair.key, key))
{
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
@@ -534,7 +527,7 @@ private:
if (r->rightChild == nullptr)
return r;
if (key < r->rightChild->pair.key)
if (comp(key, r->rightChild->pair.key))
{
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
@@ -543,7 +536,7 @@ private:
r->rightChild = rotateRight(r->rightChild);
}
}
else if (r->rightChild->pair.key < key)
else if (comp(r->rightChild->pair.key, key))
{
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
r = rotateLeft(r);
+1
View File
@@ -581,6 +581,7 @@ public:
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) = 0;
virtual void setScrollCallback(std::function<void(double, double)> callback) = 0;
virtual void setFileCallback(std::function<void(int, const char**)> callback) = 0;
virtual void setCloseCallback(std::function<void()> callback) = 0;
SeFormat getSwapchainFormat() const
{
return pixelFormat;
@@ -370,12 +370,14 @@ public:
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) override;
virtual void setScrollCallback(std::function<void(double, double)> callback) override;
virtual void setFileCallback(std::function<void(int, const char**)> callback) override;
virtual void setCloseCallback(std::function<void()> callback);
std::function<void(KeyCode, InputAction, KeyModifier)> keyCallback;
std::function<void(double, double)> mouseMoveCallback;
std::function<void(MouseButton, InputAction, KeyModifier)> mouseButtonCallback;
std::function<void(double, double)> scrollCallback;
std::function<void(int, const char**)> fileCallback;
std::function<void()> closeCallback;
protected:
void advanceBackBuffer();
void recreateSwapchain(const WindowCreateInfo &createInfo);
@@ -38,6 +38,12 @@ void glfwFileCallback(GLFWwindow* handle, int count, const char** paths)
window->fileCallback(count, paths);
}
void glfwCloseCallback(GLFWwindow* handle)
{
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->closeCallback();
}
Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
: Gfx::Window(createInfo)
, graphics(graphics)
@@ -56,6 +62,7 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
glfwSetMouseButtonCallback(handle, &glfwMouseButtonCallback);
glfwSetScrollCallback(handle, &glfwScrollCallback);
glfwSetDropCallback(handle, &glfwFileCallback);
glfwSetWindowCloseCallback(handle, &glfwCloseCallback);
glfwCreateWindowSurface(instance, handle, nullptr, &surface);
@@ -134,6 +141,11 @@ void Window::setFileCallback(std::function<void(int, const char**)> callback)
fileCallback = callback;
}
void Window::setCloseCallback(std::function<void()> callback)
{
closeCallback = callback;
}
void Window::advanceBackBuffer()
{
VkResult res = VK_ERROR_OUT_OF_DATE_KHR;
+2 -2
View File
@@ -54,9 +54,9 @@ public:
std::unique_lock lock(registeredObjectsLock);
registeredObjects.erase(handle);
}
#pragma warning( disable: 4150)
// #pragma warning( disable: 4150)
delete handle;
#pragma warning( default: 4150)
// #pragma warning( default: 4150)
}
RefObject &operator=(const RefObject &rhs)
{
+1 -1
View File
@@ -18,7 +18,7 @@ Scene::Scene(Gfx::PGraphics graphics)
lightEnv.directionalLights[0].color = Vector4(1, 1, 1, 1);
lightEnv.directionalLights[0].direction = Vector4(1, 1, 0, 1);
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 0;
lightEnv.numDirectionalLights = 1;
srand((unsigned int)time(NULL));
for(uint32 i = 0; i < 16; ++i)
{
+80 -5
View File
@@ -2,20 +2,38 @@
using namespace Seele;
Event::Event()
: flag(new std::atomic_bool())
{}
void Event::await_suspend(std::coroutine_handle<JobPromise> h)
Event::Event(const std::string& name)
: name(name)
, flag(new std::atomic_bool())
{}
void Event::raise()
{
getGlobalThreadPool().addJob(Job(h));
flag->store(1);
getGlobalThreadPool().notify(this);
}
void Event::reset()
{
flag->store(0);
}
Job JobPromise::get_return_object() noexcept {
return Job { std::coroutine_handle<JobPromise>::from_promise(*this) };
bool Event::await_ready()
{
return flag->load();
}
ThreadPool::ThreadPool(uint32 threadCount)
: workers(threadCount)
{
running.store(true);
for(uint32 i = 0; i < threadCount; ++i)
{
workers[i] = std::thread(&ThreadPool::threadLoop, this, i == 0);
}
}
ThreadPool::~ThreadPool()
@@ -25,6 +43,63 @@ ThreadPool::~ThreadPool()
thread.join();
}
}
void ThreadPool::addJob(Job&& job)
{
std::unique_lock lock(jobQueueLock);
jobQueue.add(job);
}
void ThreadPool::addJob(MainJob&& job)
{
std::unique_lock lock(mainJobLock);
mainJobs.add(job);
}
void ThreadPool::enqueueWaiting(Event* event, Job&& job)
{
std::unique_lock lock(waitingLock);
waitingJobs[event].add(std::move(job));
}
void ThreadPool::enqueueWaiting(Event* event, MainJob&& job)
{
std::unique_lock lock(waitingMainLock);
waitingMainJobs[event].add(std::move(job));
}
void ThreadPool::notify(Event* event)
{
{
std::unique_lock lock(jobQueueLock);
std::unique_lock lock2(waitingLock);
for(auto&& job : waitingJobs[event])
{
jobQueue.add(job);
}
waitingJobs[event].clear();
}
{
std::unique_lock lock(mainJobLock);
std::unique_lock lock2(waitingMainLock);
for(auto&& job : waitingMainJobs[event])
{
mainJobs.add(job);
}
waitingMainJobs[event].clear();
}
event->reset();
}
void ThreadPool::threadLoop(const bool mainThread)
{
while(running.load())
{
if(mainThread)
{
std::unique_lock lock(mainJobLock);
if(!mainJobs.empty())
{
MainJob&& job = mainJobs.retrieve();
job.resume();
}
}
}
}
ThreadPool& Seele::getGlobalThreadPool()
{
+88 -37
View File
@@ -1,35 +1,20 @@
#pragma once
#include <thread>
#include <coroutine>
#include "Containers/List.h"
namespace Seele
{
struct JobPromise;
struct Event
{
public:
void raise()
{
flag.test_and_set();
flag.notify_all();
}
void reset()
{
flag.clear();
}
bool await_ready() { return flag.test(); }
void await_suspend(std::coroutine_handle<JobPromise> h);
void await_resume() {}
private:
std::atomic_flag flag;
};
struct [[nodiscard]] Job;
struct JobPromise
{
Job get_return_object() noexcept;
extern class ThreadPool& getGlobalThreadPool();
std::suspend_always initial_suspend() const noexcept { return {}; }
template<bool MainJob>
struct JobBase;
template<bool MainJob>
struct JobPromiseBase
{
JobBase<MainJob> get_return_object() noexcept;
inline auto initial_suspend() const noexcept;
std::suspend_never final_suspend() const noexcept { return {}; }
void return_void() noexcept {}
@@ -38,23 +23,47 @@ struct JobPromise
exit(1);
};
};
struct [[nodiscard]] Job
template<bool MainJob>
struct JobBase
{
public:
using promise_type = JobPromise;
using promise_type = JobPromiseBase<MainJob>;
explicit Job(std::coroutine_handle<JobPromise> handle)
explicit JobBase(std::coroutine_handle<promise_type> handle)
: handle(handle)
{}
~Job()
~JobBase()
{
if(handle)
{
handle.destroy();
}
}
void resume()
{
handle.resume();
}
private:
std::coroutine_handle<JobPromise> handle;
std::coroutine_handle<promise_type> handle;
};
using MainJob = JobBase<true>;
using Job = JobBase<false>;
struct Event
{
public:
Event();
Event(const std::string& name);
void raise();
void reset();
bool await_ready();
template<bool MainJob>
constexpr void await_suspend(std::coroutine_handle<JobPromiseBase<MainJob>> h);
constexpr void await_resume() {}
private:
std::string name;
RefPtr<std::atomic_bool> flag;
};
class ThreadPool
@@ -62,14 +71,56 @@ class ThreadPool
public:
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency());
virtual ~ThreadPool();
void addJob(Job&& job)
{
jobs.add(std::move(job));
}
void addJob(Job&& job);
void addJob(MainJob&& job);
void enqueueWaiting(Event* event, Job&& job);
void enqueueWaiting(Event* event, MainJob&& job);
void notify(Event* event);
private:
std::atomic_bool running;
std::thread* mainThread;
std::vector<std::thread> workers;
List<Job> jobs;
void threadLoop();
List<MainJob> mainJobs;
std::mutex mainJobLock;
List<Job> jobQueue;
std::mutex jobQueueLock;
Map<Event*, List<Job>> waitingJobs;
std::mutex waitingLock;
Map<Event*, List<MainJob>> waitingMainJobs;
std::mutex waitingMainLock;
void threadLoop(const bool isMainThread);
};
extern ThreadPool& getGlobalThreadPool();
template<bool MainJob>
inline JobBase<MainJob> JobPromiseBase<MainJob>::get_return_object() noexcept {
return JobBase<MainJob> { std::coroutine_handle<JobPromiseBase<MainJob>>::from_promise(*this) };
}
template<bool MainJob>
inline auto JobPromiseBase<MainJob>::initial_suspend() const noexcept
{
struct JobAwaitable
{
constexpr bool await_ready() { return false; }
constexpr void await_suspend(std::coroutine_handle<JobPromiseBase<MainJob>> h)
{
getGlobalThreadPool().addJob(JobBase<MainJob>(h));
}
constexpr void await_resume() {}
};
return JobAwaitable{};
}
template<bool MainJob>
inline constexpr void Event::await_suspend(std::coroutine_handle<JobPromiseBase<MainJob>> h)
{
getGlobalThreadPool().enqueueWaiting(this, JobBase<MainJob>(h));
}
} // namespace Seele
+6 -6
View File
@@ -16,7 +16,7 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
{
scene = new Scene(graphics);
scene->addActor(activeCamera);
/*AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Lightmap.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Face_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png");
@@ -24,11 +24,11 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Tex_FaceLightmap.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Ayaka.fbx");
PPrimitiveComponent ayaka = new PrimitiveComponent(AssetRegistry::findMesh("Ayaka"));
ayaka->addWorldTranslation(Vector(0, 0, 100));
ayaka->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
scene->addPrimitiveComponent(ayaka);*/
ayaka->addWorldTranslation(Vector(0, 0, 0));
ayaka->setWorldScale(Vector(10, 10, 10));
scene->addPrimitiveComponent(ayaka);
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
/*AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
@@ -37,7 +37,7 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
arissa->addWorldTranslation(Vector(0, 0, 100));
arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
scene->addPrimitiveComponent(plane);
scene->addPrimitiveComponent(arissa);
scene->addPrimitiveComponent(arissa);*/
PRenderGraphResources resources = new RenderGraphResources();
depthPrepass.setResources(resources);
+18 -12
View File
@@ -1,10 +1,12 @@
#include "Window.h"
#include "WindowManager.h"
#include <functional>
using namespace Seele;
Window::Window(Gfx::PWindow handle)
: gfxHandle(handle)
Window::Window(PWindowManager owner, Gfx::PWindow handle)
: owner(owner)
, gfxHandle(handle)
{
}
@@ -20,23 +22,17 @@ void Window::addView(PView view)
views.add(windowView);
}
void Window::render()
MainJob Window::render()
{
gfxHandle->beginFrame();
for(auto& windowView : views)
{
windowView->view->beginUpdate();
windowView->view->update();
{
std::unique_lock lock(windowView->workerMutex);
windowView->view->commitUpdate();
}
windowView->updateFinished.raise();
viewWorker(windowView);
co_await windowView->updateFinished;
{
std::unique_lock lock(windowView->workerMutex);
windowView->view->prepareRender();
}
windowView->updateFinished.reset();
windowView->view->render();
}
gfxHandle->endFrame();
@@ -59,12 +55,22 @@ void Window::setFocused(PView view)
gfxHandle->setMouseButtonCallback(mouseButtonFunction);
gfxHandle->setScrollCallback(scrollFunction);
gfxHandle->setFileCallback(fileFunction);
gfxHandle->setCloseCallback([this](){
owner->notifyWindowClosed(this);
});
}
void Window::viewWorker(WindowView* windowView)
Job Window::viewWorker(WindowView* windowView)
{
while(true)
{
windowView->view->beginUpdate();
windowView->view->update();
{
std::unique_lock lock(windowView->workerMutex);
windowView->view->commitUpdate();
}
windowView->updateFinished.raise();
}
}
+5 -4
View File
@@ -9,26 +9,27 @@ struct WindowView
{
PView view;
Event updateFinished;
std::thread worker;
std::mutex workerMutex;
};
DEFINE_REF(WindowView)
DECLARE_REF(WindowManager)
// The logical window, with the graphics proxy
class Window
{
public:
Window(Gfx::PWindow handle);
Window(PWindowManager owner, Gfx::PWindow handle);
~Window();
void addView(PView view);
void render();
MainJob render();
Gfx::PWindow getGfxHandle();
void setFocused(PView view);
protected:
PWindowManager owner;
Array<WindowView*> views;
Gfx::PWindow gfxHandle;
void viewWorker(WindowView* view);
Job viewWorker(WindowView* view);
};
DEFINE_REF(Window)
} // namespace Seele
+6 -1
View File
@@ -31,7 +31,12 @@ WindowManager::~WindowManager()
PWindow WindowManager::addWindow(const WindowCreateInfo &createInfo)
{
Gfx::PWindow handle = graphics->createWindow(createInfo);
PWindow window = new Window(handle);
PWindow window = new Window(this, handle);
windows.add(window);
return window;
}
void WindowManager::notifyWindowClosed(PWindow window)
{
windows.remove(windows.find(window));
}
+1
View File
@@ -12,6 +12,7 @@ public:
WindowManager();
~WindowManager();
PWindow addWindow(const WindowCreateInfo &createInfo);
void notifyWindowClosed(PWindow window);
static Gfx::PGraphics getGraphics()
{
return graphics;
+2 -2
View File
@@ -9,7 +9,7 @@ using namespace Seele;
int main()
{
PWindowManager windowManager = new WindowManager();
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\");
AssetRegistry::init("/home/dynamitos/TestSeeleProject");
WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280;
@@ -34,7 +34,7 @@ int main()
PInspectorView inspectorView = new InspectorView(windowManager->getGraphics(), window, inspectorViewInfo);
window->addView(inspectorView);
sceneView->setFocused();
while(true)
while(windowManager->isActive())
{
window->render();
}