Allocator Aware Array and List
This commit is contained in:
@@ -74,3 +74,4 @@ float4 fragmentMain(
|
|||||||
}
|
}
|
||||||
return float4(result, 1);
|
return float4(result, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ target_sources(SeeleEngine
|
|||||||
EngineTypes.h
|
EngineTypes.h
|
||||||
MinimalEngine.h
|
MinimalEngine.h
|
||||||
MinimalEngine.cpp
|
MinimalEngine.cpp
|
||||||
|
ThreadPool.h
|
||||||
|
ThreadPool.cpp
|
||||||
main.cpp)
|
main.cpp)
|
||||||
|
|
||||||
add_subdirectory(Asset/)
|
add_subdirectory(Asset/)
|
||||||
|
|||||||
+649
-549
File diff suppressed because it is too large
Load Diff
+357
-321
@@ -1,337 +1,373 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "MinimalEngine.h"
|
#include "MinimalEngine.h"
|
||||||
|
#include <xmemory>
|
||||||
|
|
||||||
namespace Seele
|
namespace Seele
|
||||||
{
|
{
|
||||||
template <typename T>
|
template <typename T, typename Allocator = std::pmr::polymorphic_allocator<T>>
|
||||||
class List
|
class List
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
Node *prev;
|
Node *prev;
|
||||||
Node *next;
|
Node *next;
|
||||||
T data;
|
T data;
|
||||||
};
|
};
|
||||||
|
using NodeAllocator = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
List()
|
template <typename X>
|
||||||
{
|
class IteratorBase
|
||||||
root = nullptr;
|
{
|
||||||
tail = nullptr;
|
public:
|
||||||
beginIt = Iterator(root);
|
using iterator_category = std::forward_iterator_tag;
|
||||||
endIt = Iterator(tail);
|
using value_type = X;
|
||||||
_size = 0;
|
using difference_type = std::ptrdiff_t;
|
||||||
}
|
using reference = X&;
|
||||||
List(const List& other)
|
using pointer = X*;
|
||||||
{
|
|
||||||
//TODO: improve
|
|
||||||
for(const auto& it : other)
|
|
||||||
{
|
|
||||||
add(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
List(List&& other)
|
|
||||||
: root(std::move(other.root))
|
|
||||||
, tail(std::move(other.tail))
|
|
||||||
, beginIt(std::move(other.beginIt))
|
|
||||||
, endIt(std::move(other.endIt))
|
|
||||||
, _size(std::move(other._size))
|
|
||||||
{
|
|
||||||
other._size = 0;
|
|
||||||
}
|
|
||||||
~List()
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
List& operator=(const List& other)
|
|
||||||
{
|
|
||||||
if(this != &other)
|
|
||||||
{
|
|
||||||
if(root != nullptr)
|
|
||||||
{
|
|
||||||
delete root;
|
|
||||||
}
|
|
||||||
if(tail != nullptr)
|
|
||||||
{
|
|
||||||
delete tail;
|
|
||||||
}
|
|
||||||
_size = 0;
|
|
||||||
for(const auto& it : other)
|
|
||||||
{
|
|
||||||
add(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
List& operator=(List&& other)
|
|
||||||
{
|
|
||||||
if(this != &other)
|
|
||||||
{
|
|
||||||
if(root != nullptr)
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
root = other.root;
|
|
||||||
tail = other.tail;
|
|
||||||
beginIt = other.beginIt;
|
|
||||||
endIt = other.endIt;
|
|
||||||
_size = other._size;
|
|
||||||
other._size = 0;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
template <typename X>
|
|
||||||
class IteratorBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef std::forward_iterator_tag iterator_category;
|
|
||||||
typedef X value_type;
|
|
||||||
typedef std::ptrdiff_t difference_type;
|
|
||||||
typedef X &reference;
|
|
||||||
typedef X *pointer;
|
|
||||||
|
|
||||||
IteratorBase(Node *x = nullptr)
|
IteratorBase(Node *x = nullptr)
|
||||||
: node(x)
|
: node(x)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase(const IteratorBase &i)
|
IteratorBase(const IteratorBase &i)
|
||||||
: node(i.node)
|
: node(i.node)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase(IteratorBase&& i)
|
IteratorBase(IteratorBase&& i)
|
||||||
: node(std::move(i.node))
|
: node(std::move(i.node))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
~IteratorBase()
|
~IteratorBase()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
IteratorBase& operator=(const IteratorBase& other)
|
IteratorBase& operator=(const IteratorBase& other)
|
||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
node = other.node;
|
node = other.node;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase& operator=(IteratorBase&& other)
|
IteratorBase& operator=(IteratorBase&& other)
|
||||||
{
|
{
|
||||||
if(this != &other)
|
if(this != &other)
|
||||||
{
|
{
|
||||||
node = std::move(other.node);
|
node = std::move(other.node);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
reference operator*() const
|
reference operator*() const
|
||||||
{
|
{
|
||||||
return node->data;
|
return node->data;
|
||||||
}
|
}
|
||||||
pointer operator->() const
|
pointer operator->() const
|
||||||
{
|
{
|
||||||
return &node->data;
|
return &node->data;
|
||||||
}
|
}
|
||||||
inline bool operator!=(const IteratorBase &other)
|
inline bool operator!=(const IteratorBase &other)
|
||||||
{
|
{
|
||||||
return node != other.node;
|
return node != other.node;
|
||||||
}
|
}
|
||||||
inline bool operator==(const IteratorBase &other)
|
inline bool operator==(const IteratorBase &other)
|
||||||
{
|
{
|
||||||
return node == other.node;
|
return node == other.node;
|
||||||
}
|
}
|
||||||
IteratorBase &operator--()
|
IteratorBase &operator--()
|
||||||
{
|
{
|
||||||
node = node->prev;
|
node = node->prev;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase operator--(int)
|
IteratorBase operator--(int)
|
||||||
{
|
{
|
||||||
IteratorBase tmp(*this);
|
IteratorBase tmp(*this);
|
||||||
--*this;
|
--*this;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
IteratorBase &operator++()
|
IteratorBase &operator++()
|
||||||
{
|
{
|
||||||
node = node->next;
|
node = node->next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
IteratorBase operator++(int)
|
IteratorBase operator++(int)
|
||||||
{
|
{
|
||||||
IteratorBase tmp(*this);
|
IteratorBase tmp(*this);
|
||||||
++*this;
|
++*this;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node *node;
|
Node *node;
|
||||||
friend class List<T>;
|
friend class List<T>;
|
||||||
};
|
};
|
||||||
typedef IteratorBase<T> Iterator;
|
|
||||||
typedef IteratorBase<const T> ConstIterator;
|
|
||||||
|
|
||||||
T &front()
|
using Iterator = IteratorBase<T>;
|
||||||
{
|
using ConstIterator = IteratorBase<const T>;
|
||||||
return root->data;
|
|
||||||
}
|
using value_type = T;
|
||||||
T &back()
|
using allocator_type = Allocator;
|
||||||
{
|
using size_type = std::size_t;
|
||||||
return tail->prev->data;
|
using difference_type = std::ptrdiff_t;
|
||||||
}
|
using reference = value_type&;
|
||||||
void clear()
|
using const_reference = const value_type&;
|
||||||
{
|
using pointer = T*;
|
||||||
if (empty())
|
using const_pointer = const T*;
|
||||||
{
|
|
||||||
return;
|
using iterator = Iterator;
|
||||||
}
|
using const_iterator = ConstIterator;
|
||||||
for (Node *tmp = root; tmp != tail;)
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||||
{
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||||
tmp = tmp->next;
|
|
||||||
delete tmp->prev;
|
List()
|
||||||
}
|
: root(nullptr)
|
||||||
delete tail;
|
, tail(nullptr)
|
||||||
tail = nullptr;
|
, beginIt(Iterator(root))
|
||||||
root = nullptr;
|
, endIt(Iterator(tail))
|
||||||
}
|
, _size(0)
|
||||||
//Insert at the end
|
, allocator(NodeAllocator())
|
||||||
Iterator add(const T &value)
|
{
|
||||||
{
|
}
|
||||||
if (root == nullptr)
|
List(const List& other)
|
||||||
{
|
{
|
||||||
root = new Node();
|
//TODO: improve
|
||||||
tail = root;
|
for(const auto& it : other)
|
||||||
}
|
{
|
||||||
tail->data = value;
|
add(it);
|
||||||
Node *newTail = new Node();
|
}
|
||||||
newTail->prev = tail;
|
}
|
||||||
newTail->next = nullptr;
|
List(List&& other)
|
||||||
tail->next = newTail;
|
: root(std::move(other.root))
|
||||||
Iterator insertedElement(tail);
|
, tail(std::move(other.tail))
|
||||||
tail = newTail;
|
, beginIt(std::move(other.beginIt))
|
||||||
markIteratorDirty();
|
, endIt(std::move(other.endIt))
|
||||||
_size++;
|
, _size(std::move(other._size))
|
||||||
return insertedElement;
|
{
|
||||||
}
|
other._size = 0;
|
||||||
Iterator add(T&& value)
|
}
|
||||||
{
|
~List()
|
||||||
if (root == nullptr)
|
{
|
||||||
{
|
clear();
|
||||||
root = new Node();
|
}
|
||||||
tail = root;
|
List& operator=(const List& other)
|
||||||
}
|
{
|
||||||
tail->data = std::move(value);
|
if(this != &other)
|
||||||
Node *newTail = new Node();
|
{
|
||||||
newTail->prev = tail;
|
if(root != nullptr)
|
||||||
newTail->next = nullptr;
|
{
|
||||||
tail->next = newTail;
|
delete root;
|
||||||
Iterator insertedElement(tail);
|
}
|
||||||
tail = newTail;
|
if(tail != nullptr)
|
||||||
markIteratorDirty();
|
{
|
||||||
_size++;
|
delete tail;
|
||||||
return insertedElement;
|
}
|
||||||
}
|
_size = 0;
|
||||||
Iterator remove(Iterator pos)
|
for(const auto& it : other)
|
||||||
{
|
{
|
||||||
_size--;
|
add(it);
|
||||||
Node *prev = pos.node->prev;
|
}
|
||||||
Node *next = pos.node->next;
|
}
|
||||||
if (prev == nullptr)
|
return *this;
|
||||||
{
|
}
|
||||||
root = next;
|
List& operator=(List&& other)
|
||||||
}
|
{
|
||||||
else
|
if(this != &other)
|
||||||
{
|
{
|
||||||
prev->next = next;
|
if(root != nullptr)
|
||||||
}
|
{
|
||||||
if(next == nullptr)
|
clear();
|
||||||
{
|
}
|
||||||
root = prev;
|
root = other.root;
|
||||||
}
|
tail = other.tail;
|
||||||
else
|
beginIt = other.beginIt;
|
||||||
{
|
endIt = other.endIt;
|
||||||
next->prev = prev;
|
_size = other._size;
|
||||||
}
|
other._size = 0;
|
||||||
delete pos.node;
|
}
|
||||||
markIteratorDirty();
|
return *this;
|
||||||
return Iterator(next);
|
}
|
||||||
}
|
|
||||||
void popBack()
|
T &front()
|
||||||
{
|
{
|
||||||
assert(_size > 0);
|
return root->data;
|
||||||
remove(Iterator(tail->prev));
|
}
|
||||||
}
|
T &back()
|
||||||
void popFront()
|
{
|
||||||
{
|
return tail->prev->data;
|
||||||
assert(_size > 0);
|
}
|
||||||
remove(Iterator(root));
|
void clear()
|
||||||
}
|
{
|
||||||
Iterator insert(Iterator pos, const T &value)
|
if (empty())
|
||||||
{
|
{
|
||||||
_size++;
|
return;
|
||||||
if (root == nullptr)
|
}
|
||||||
{
|
for (Node *tmp = root; tmp != tail;)
|
||||||
root = new Node();
|
{
|
||||||
root->data = value;
|
tmp = tmp->next;
|
||||||
tail = new Node();
|
deallocateNode(tmp->prev);
|
||||||
root->next = tail;
|
}
|
||||||
root->prev = nullptr;
|
deallocateNode(tail);
|
||||||
tail->prev = root;
|
tail = nullptr;
|
||||||
tail->next = nullptr;
|
root = nullptr;
|
||||||
markIteratorDirty();
|
}
|
||||||
return beginIt;
|
//Insert at the end
|
||||||
}
|
iterator add(const T &value)
|
||||||
Node *tmp = pos.node->prev;
|
{
|
||||||
Node *newNode = new Node();
|
if (root == nullptr)
|
||||||
newNode->data = value;
|
{
|
||||||
tmp->next = newNode;
|
root = allocateNode();
|
||||||
newNode->prev = tmp;
|
tail = root;
|
||||||
newNode->next = pos.node;
|
}
|
||||||
pos.node->prev = newNode;
|
tail->data = value;
|
||||||
return Iterator(newNode);
|
Node *newTail = allocateNode();
|
||||||
}
|
newTail->prev = tail;
|
||||||
Iterator find(const T &value)
|
newTail->next = nullptr;
|
||||||
{
|
tail->next = newTail;
|
||||||
for (Node *i = root; i != tail; i = i->next)
|
iterator insertedElement(tail);
|
||||||
{
|
tail = newTail;
|
||||||
if (!(i->data < value) && !(value < i->data))
|
markIteratorDirty();
|
||||||
{
|
_size++;
|
||||||
return Iterator(i);
|
return insertedElement;
|
||||||
}
|
}
|
||||||
}
|
iterator add(T&& value)
|
||||||
return endIt;
|
{
|
||||||
}
|
if (root == nullptr)
|
||||||
bool empty()
|
{
|
||||||
{
|
root = allocateNode();
|
||||||
return _size == 0;
|
tail = root;
|
||||||
}
|
}
|
||||||
uint32 size()
|
tail->data = std::move(value);
|
||||||
{
|
Node *newTail = allocateNode();
|
||||||
return _size;
|
newTail->prev = tail;
|
||||||
}
|
newTail->next = nullptr;
|
||||||
Iterator begin()
|
tail->next = newTail;
|
||||||
{
|
Iterator insertedElement(tail);
|
||||||
return beginIt;
|
tail = newTail;
|
||||||
}
|
markIteratorDirty();
|
||||||
const Iterator begin() const
|
_size++;
|
||||||
{
|
return insertedElement;
|
||||||
return beginIt;
|
}
|
||||||
}
|
iterator remove(iterator pos)
|
||||||
Iterator end()
|
{
|
||||||
{
|
_size--;
|
||||||
return endIt;
|
Node *prev = pos.node->prev;
|
||||||
}
|
Node *next = pos.node->next;
|
||||||
const Iterator end() const
|
if (prev == nullptr)
|
||||||
{
|
{
|
||||||
return endIt;
|
root = next;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prev->next = next;
|
||||||
|
}
|
||||||
|
if(next == nullptr)
|
||||||
|
{
|
||||||
|
root = prev;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
next->prev = prev;
|
||||||
|
}
|
||||||
|
delete pos.node;
|
||||||
|
markIteratorDirty();
|
||||||
|
return Iterator(next);
|
||||||
|
}
|
||||||
|
void popBack()
|
||||||
|
{
|
||||||
|
assert(_size > 0);
|
||||||
|
remove(Iterator(tail->prev));
|
||||||
|
}
|
||||||
|
void popFront()
|
||||||
|
{
|
||||||
|
assert(_size > 0);
|
||||||
|
remove(Iterator(root));
|
||||||
|
}
|
||||||
|
iterator insert(iterator pos, const T &value)
|
||||||
|
{
|
||||||
|
_size++;
|
||||||
|
if (root == nullptr)
|
||||||
|
{
|
||||||
|
root = allocateNode();
|
||||||
|
root->data = value;
|
||||||
|
tail = allocateNode();
|
||||||
|
root->next = tail;
|
||||||
|
root->prev = nullptr;
|
||||||
|
tail->prev = root;
|
||||||
|
tail->next = nullptr;
|
||||||
|
markIteratorDirty();
|
||||||
|
return beginIt;
|
||||||
|
}
|
||||||
|
Node *tmp = pos.node->prev;
|
||||||
|
Node *newNode = allocateNode();
|
||||||
|
newNode->data = value;
|
||||||
|
tmp->next = newNode;
|
||||||
|
newNode->prev = tmp;
|
||||||
|
newNode->next = pos.node;
|
||||||
|
pos.node->prev = newNode;
|
||||||
|
return Iterator(newNode);
|
||||||
|
}
|
||||||
|
iterator find(const T &value)
|
||||||
|
{
|
||||||
|
for (Node *i = root; i != tail; i = i->next)
|
||||||
|
{
|
||||||
|
if (!(i->data < value) && !(value < i->data))
|
||||||
|
{
|
||||||
|
return iterator(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return endIt;
|
||||||
|
}
|
||||||
|
bool empty()
|
||||||
|
{
|
||||||
|
return _size == 0;
|
||||||
|
}
|
||||||
|
size_type size()
|
||||||
|
{
|
||||||
|
return _size;
|
||||||
|
}
|
||||||
|
iterator begin()
|
||||||
|
{
|
||||||
|
return beginIt;
|
||||||
|
}
|
||||||
|
const_iterator begin() const
|
||||||
|
{
|
||||||
|
return cbeginIt;
|
||||||
|
}
|
||||||
|
iterator end()
|
||||||
|
{
|
||||||
|
return endIt;
|
||||||
|
}
|
||||||
|
const_iterator end() const
|
||||||
|
{
|
||||||
|
return cendIt;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void markIteratorDirty()
|
Node* allocateNode()
|
||||||
{
|
{
|
||||||
beginIt = Iterator(root);
|
Node* node = allocator.allocate(1);
|
||||||
endIt = Iterator(tail);
|
std::memset(node, 0, sizeof(Node));
|
||||||
}
|
assert(node != nullptr);
|
||||||
Node *root;
|
return node;
|
||||||
Node *tail;
|
}
|
||||||
Iterator beginIt;
|
void deallocateNode(Node* node)
|
||||||
Iterator endIt;
|
{
|
||||||
uint32 _size;
|
allocator.deallocate(node, 1);
|
||||||
|
}
|
||||||
|
void markIteratorDirty()
|
||||||
|
{
|
||||||
|
beginIt = Iterator(root);
|
||||||
|
endIt = Iterator(tail);
|
||||||
|
cbeginIt = ConstIterator(root);
|
||||||
|
cendIt = ConstIterator(tail);
|
||||||
|
}
|
||||||
|
Node *root;
|
||||||
|
Node *tail;
|
||||||
|
Iterator beginIt;
|
||||||
|
Iterator endIt;
|
||||||
|
ConstIterator cbeginIt;
|
||||||
|
ConstIterator cendIt;
|
||||||
|
uint32 _size;
|
||||||
|
NodeAllocator allocator;
|
||||||
};
|
};
|
||||||
} // namespace Seele
|
} // namespace Seele
|
||||||
@@ -171,7 +171,7 @@ public:
|
|||||||
using pointer = Pair<K, V>*;
|
using pointer = Pair<K, V>*;
|
||||||
|
|
||||||
Iterator(Node *x = nullptr)
|
Iterator(Node *x = nullptr)
|
||||||
: node(x), traversal(Init_t::NO_INIT)
|
: node(x)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
Iterator(Node *x, Array<Node *> &&beginIt)
|
Iterator(Node *x, Array<Node *> &&beginIt)
|
||||||
|
|||||||
@@ -70,13 +70,9 @@ ShaderCollection& ShaderMap::createShaders(
|
|||||||
modifyRenderPassMacros(renderPass, createInfo.defines);
|
modifyRenderPassMacros(renderPass, createInfo.defines);
|
||||||
createInfo.name = getShaderNameFromRenderPassType(renderPass) + " Material " + material->getName();
|
createInfo.name = getShaderNameFromRenderPassType(renderPass) + " Material " + material->getName();
|
||||||
|
|
||||||
std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass), std::ios::ate);
|
std::ifstream codeStream("./shaders/" + getShaderNameFromRenderPassType(renderPass));
|
||||||
auto fileSize = codeStream.tellg();
|
|
||||||
codeStream.seekg(0);
|
|
||||||
Array<char> buffer(static_cast<uint32>(fileSize));
|
|
||||||
codeStream.read(buffer.data(), fileSize);
|
|
||||||
|
|
||||||
createInfo.shaderCode.add(std::string(buffer.data(), 0, fileSize));
|
createInfo.shaderCode.add(std::string(std::istreambuf_iterator<char>{codeStream}, {}));
|
||||||
|
|
||||||
collection.vertexShader = graphics->createVertexShader(createInfo);
|
collection.vertexShader = graphics->createVertexShader(createInfo);
|
||||||
|
|
||||||
|
|||||||
+314
-313
@@ -2,27 +2,28 @@
|
|||||||
#include "Containers/Map.h"
|
#include "Containers/Map.h"
|
||||||
#include "EngineTypes.h"
|
#include "EngineTypes.h"
|
||||||
#include "Math/Math.h"
|
#include "Math/Math.h"
|
||||||
|
//#include "ThreadPool.h"
|
||||||
|
|
||||||
#define DEFINE_REF(x) \
|
#define DEFINE_REF(x) \
|
||||||
typedef RefPtr<x> P##x; \
|
typedef RefPtr<x> P##x; \
|
||||||
typedef UniquePtr<x> UP##x; \
|
typedef UniquePtr<x> UP##x; \
|
||||||
typedef WeakPtr<x> W##x;
|
typedef WeakPtr<x> W##x;
|
||||||
|
|
||||||
#define DECLARE_REF(x) \
|
#define DECLARE_REF(x) \
|
||||||
class x; \
|
class x; \
|
||||||
typedef RefPtr<x> P##x; \
|
typedef RefPtr<x> P##x; \
|
||||||
typedef UniquePtr<x> UP##x; \
|
typedef UniquePtr<x> UP##x; \
|
||||||
typedef WeakPtr<x> W##x;
|
typedef WeakPtr<x> W##x;
|
||||||
|
|
||||||
|
|
||||||
#define DECLARE_NAME_REF(nmsp, x) \
|
#define DECLARE_NAME_REF(nmsp, x) \
|
||||||
namespace nmsp \
|
namespace nmsp \
|
||||||
{ \
|
{ \
|
||||||
class x; \
|
class x; \
|
||||||
typedef RefPtr<x> P##x; \
|
typedef RefPtr<x> P##x; \
|
||||||
typedef UniquePtr<x> UP##x; \
|
typedef UniquePtr<x> UP##x; \
|
||||||
typedef WeakPtr<x> W##x; \
|
typedef WeakPtr<x> W##x; \
|
||||||
}
|
}
|
||||||
|
|
||||||
extern Seele::Map<void *, void *> registeredObjects;
|
extern Seele::Map<void *, void *> registeredObjects;
|
||||||
extern std::mutex registeredObjectsLock;
|
extern std::mutex registeredObjectsLock;
|
||||||
@@ -34,332 +35,332 @@ template <typename T>
|
|||||||
class RefObject
|
class RefObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RefObject(T *ptr)
|
RefObject(T *ptr)
|
||||||
: handle(ptr), refCount(1)
|
: handle(ptr), refCount(1)
|
||||||
{
|
{
|
||||||
registeredObjects[ptr] = this;
|
registeredObjects[ptr] = this;
|
||||||
}
|
}
|
||||||
inline RefObject(const RefObject &rhs)
|
inline RefObject(const RefObject &rhs)
|
||||||
: handle(rhs.handle), refCount(rhs.refCount)
|
: handle(rhs.handle), refCount(rhs.refCount)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
RefObject(RefObject &&rhs)
|
RefObject(RefObject &&rhs)
|
||||||
: handle(std::move(rhs.handle)), refCount(std::move(rhs.refCount))
|
: handle(std::move(rhs.handle)), refCount(std::move(rhs.refCount))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
~RefObject()
|
~RefObject()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::unique_lock lock(registeredObjectsLock);
|
std::unique_lock lock(registeredObjectsLock);
|
||||||
registeredObjects.erase(handle);
|
registeredObjects.erase(handle);
|
||||||
}
|
}
|
||||||
#pragma warning( disable: 4150)
|
#pragma warning( disable: 4150)
|
||||||
delete handle;
|
delete handle;
|
||||||
#pragma warning( default: 4150)
|
#pragma warning( default: 4150)
|
||||||
}
|
}
|
||||||
RefObject &operator=(const RefObject &rhs)
|
RefObject &operator=(const RefObject &rhs)
|
||||||
{
|
{
|
||||||
if (*this != rhs)
|
if (*this != rhs)
|
||||||
{
|
{
|
||||||
handle = rhs.handle;
|
handle = rhs.handle;
|
||||||
refCount = rhs.refCount;
|
refCount = rhs.refCount;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
RefObject &operator=(RefObject &&rhs)
|
RefObject &operator=(RefObject &&rhs)
|
||||||
{
|
{
|
||||||
if (*this != rhs)
|
if (*this != rhs)
|
||||||
{
|
{
|
||||||
handle = std::move(rhs.handle);
|
handle = std::move(rhs.handle);
|
||||||
refCount = std::move(rhs.refCount);
|
refCount = std::move(rhs.refCount);
|
||||||
rhs.handle = nullptr;
|
rhs.handle = nullptr;
|
||||||
rhs.refCount = 0;
|
rhs.refCount = 0;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
inline bool operator==(const RefObject &other) const
|
inline bool operator==(const RefObject &other) const
|
||||||
{
|
{
|
||||||
return handle == other.handle;
|
return handle == other.handle;
|
||||||
}
|
}
|
||||||
inline bool operator!=(const RefObject &other) const
|
inline bool operator!=(const RefObject &other) const
|
||||||
{
|
{
|
||||||
return handle != other.handle;
|
return handle != other.handle;
|
||||||
}
|
}
|
||||||
inline bool operator<(const RefObject &other) const
|
inline bool operator<(const RefObject &other) const
|
||||||
{
|
{
|
||||||
return handle < other.handle;
|
return handle < other.handle;
|
||||||
}
|
}
|
||||||
void addRef()
|
void addRef()
|
||||||
{
|
{
|
||||||
refCount++;
|
refCount++;
|
||||||
}
|
}
|
||||||
inline void removeRef()
|
inline void removeRef()
|
||||||
{
|
{
|
||||||
refCount--;
|
refCount--;
|
||||||
if (refCount == 0)
|
if (refCount == 0)
|
||||||
{
|
{
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
T *getHandle() const
|
T *getHandle() const
|
||||||
{
|
{
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
T *handle;
|
T *handle;
|
||||||
std::atomic_uint64_t refCount;
|
std::atomic_uint64_t refCount;
|
||||||
friend class RefPtr<T>;
|
friend class RefPtr<T>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class RefPtr
|
class RefPtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RefPtr()
|
RefPtr()
|
||||||
{
|
{
|
||||||
object = nullptr;
|
object = nullptr;
|
||||||
}
|
}
|
||||||
RefPtr(nullptr_t)
|
RefPtr(nullptr_t)
|
||||||
{
|
{
|
||||||
object = nullptr;
|
object = nullptr;
|
||||||
}
|
}
|
||||||
RefPtr(T *ptr)
|
RefPtr(T *ptr)
|
||||||
{
|
{
|
||||||
std::unique_lock l(registeredObjectsLock);
|
std::unique_lock l(registeredObjectsLock);
|
||||||
auto registeredObj = registeredObjects.find(ptr);
|
auto registeredObj = registeredObjects.find(ptr);
|
||||||
// get here for thread safetly
|
// get here for thread safetly
|
||||||
auto registeredEnd = registeredObjects.end();
|
auto registeredEnd = registeredObjects.end();
|
||||||
if (registeredObj == registeredEnd)
|
if (registeredObj == registeredEnd)
|
||||||
{
|
{
|
||||||
object = new RefObject<T>(ptr);
|
object = new RefObject<T>(ptr);
|
||||||
l.unlock();
|
l.unlock();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
l.unlock();
|
l.unlock();
|
||||||
object = (RefObject<T> *)registeredObj->value;
|
object = (RefObject<T> *)registeredObj->value;
|
||||||
object->addRef();
|
object->addRef();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
explicit RefPtr(RefObject<T> *other)
|
explicit RefPtr(RefObject<T> *other)
|
||||||
: object(other)
|
: object(other)
|
||||||
{
|
{
|
||||||
object->addRef();
|
object->addRef();
|
||||||
}
|
}
|
||||||
inline RefPtr(const RefPtr &other)
|
inline RefPtr(const RefPtr &other)
|
||||||
: object(other.object)
|
: object(other.object)
|
||||||
{
|
{
|
||||||
if (object != nullptr)
|
if (object != nullptr)
|
||||||
{
|
{
|
||||||
object->addRef();
|
object->addRef();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RefPtr(RefPtr &&rhs)
|
RefPtr(RefPtr &&rhs)
|
||||||
: object(std::move(rhs.object))
|
: object(std::move(rhs.object))
|
||||||
{
|
{
|
||||||
rhs.object = nullptr;
|
rhs.object = nullptr;
|
||||||
//Dont change references, they stay the same
|
//Dont change references, they stay the same
|
||||||
}
|
}
|
||||||
template <typename F>
|
template <typename F>
|
||||||
RefPtr(const RefPtr<F> &other)
|
RefPtr(const RefPtr<F> &other)
|
||||||
{
|
{
|
||||||
F *f = other.getObject()->getHandle();
|
F *f = other.getObject()->getHandle();
|
||||||
assert(static_cast<T *>(f));
|
assert(static_cast<T *>(f));
|
||||||
object = (RefObject<T> *)other.getObject();
|
object = (RefObject<T> *)other.getObject();
|
||||||
object->addRef();
|
object->addRef();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
RefPtr<F> cast()
|
RefPtr<F> cast()
|
||||||
{
|
{
|
||||||
T *t = object->getHandle();
|
T *t = object->getHandle();
|
||||||
F *f = dynamic_cast<F *>(t);
|
F *f = dynamic_cast<F *>(t);
|
||||||
if (f == nullptr)
|
if (f == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
RefObject<F> *newObject = (RefObject<F> *)object;
|
RefObject<F> *newObject = (RefObject<F> *)object;
|
||||||
return RefPtr<F>(newObject);
|
return RefPtr<F>(newObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
const RefPtr<F> cast() const
|
const RefPtr<F> cast() const
|
||||||
{
|
{
|
||||||
T *t = object->getHandle();
|
T *t = object->getHandle();
|
||||||
F *f = dynamic_cast<F *>(t);
|
F *f = dynamic_cast<F *>(t);
|
||||||
if (f == nullptr)
|
if (f == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
RefObject<F> *newObject = (RefObject<F> *)object;
|
RefObject<F> *newObject = (RefObject<F> *)object;
|
||||||
return RefPtr<F>(newObject);
|
return RefPtr<F>(newObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr &operator=(const RefPtr &other)
|
RefPtr &operator=(const RefPtr &other)
|
||||||
{
|
{
|
||||||
if (*this != other)
|
if (this != &other)
|
||||||
{
|
{
|
||||||
if (object != nullptr)
|
if (object != nullptr)
|
||||||
{
|
{
|
||||||
object->removeRef();
|
object->removeRef();
|
||||||
}
|
}
|
||||||
object = other.object;
|
object = other.object;
|
||||||
if (object != nullptr)
|
if (object != nullptr)
|
||||||
{
|
{
|
||||||
object->addRef();
|
object->addRef();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
RefPtr &operator=(RefPtr &&rhs)
|
RefPtr &operator=(RefPtr &&rhs)
|
||||||
{
|
{
|
||||||
if (*this != rhs)
|
if (this != &rhs)
|
||||||
{
|
{
|
||||||
if (object != nullptr)
|
if (object != nullptr)
|
||||||
{
|
{
|
||||||
object->removeRef();
|
object->removeRef();
|
||||||
}
|
}
|
||||||
object = std::move(rhs.object);
|
object = std::move(rhs.object);
|
||||||
rhs.object = nullptr;
|
rhs.object = nullptr;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
~RefPtr()
|
~RefPtr()
|
||||||
{
|
{
|
||||||
if (object != nullptr)
|
if (object != nullptr)
|
||||||
{
|
{
|
||||||
object->removeRef();
|
object->removeRef();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inline bool operator==(const RefPtr &other) const
|
inline bool operator==(const RefPtr &other) const
|
||||||
{
|
{
|
||||||
return object == other.object;
|
return object == other.object;
|
||||||
}
|
}
|
||||||
inline bool operator!=(const RefPtr &other) const
|
inline bool operator!=(const RefPtr &other) const
|
||||||
{
|
{
|
||||||
return object != other.object;
|
return object != other.object;
|
||||||
}
|
}
|
||||||
bool operator<(const RefPtr &other) const
|
bool operator<(const RefPtr &other) const
|
||||||
{
|
{
|
||||||
return object < other.object;
|
return object < other.object;
|
||||||
}
|
}
|
||||||
inline T *operator->()
|
inline T *operator->()
|
||||||
{
|
{
|
||||||
assert(object != nullptr);
|
assert(object != nullptr);
|
||||||
return object->handle;
|
return object->handle;
|
||||||
}
|
}
|
||||||
inline const T *operator->() const
|
inline const T *operator->() const
|
||||||
{
|
{
|
||||||
assert(object != nullptr);
|
assert(object != nullptr);
|
||||||
return object->handle;
|
return object->handle;
|
||||||
}
|
}
|
||||||
RefObject<T> *getObject() const
|
RefObject<T> *getObject() const
|
||||||
{
|
{
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
inline T *getHandle()
|
inline T *getHandle()
|
||||||
{
|
{
|
||||||
return object->getHandle();
|
return object->getHandle();
|
||||||
}
|
}
|
||||||
inline const T *getHandle() const
|
inline const T *getHandle() const
|
||||||
{
|
{
|
||||||
return object->getHandle();
|
return object->getHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefObject<T> *object;
|
RefObject<T> *object;
|
||||||
friend class boost::serialization::access;
|
friend class boost::serialization::access;
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive& ar, const unsigned int)
|
void serialize(Archive& ar, const unsigned int)
|
||||||
{
|
{
|
||||||
ar & *object->getHandle();
|
ar & *object->getHandle();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class UniquePtr
|
class UniquePtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UniquePtr()
|
UniquePtr()
|
||||||
: handle(nullptr)
|
: handle(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
UniquePtr(nullptr_t)
|
UniquePtr(nullptr_t)
|
||||||
: handle(nullptr)
|
: handle(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
UniquePtr(T *ptr)
|
UniquePtr(T *ptr)
|
||||||
: handle(ptr)
|
: handle(ptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
UniquePtr(const UniquePtr &rhs) = delete;
|
UniquePtr(const UniquePtr &rhs) = delete;
|
||||||
UniquePtr(UniquePtr &&rhs) noexcept
|
UniquePtr(UniquePtr &&rhs) noexcept
|
||||||
: handle(rhs.handle)
|
: handle(rhs.handle)
|
||||||
{
|
{
|
||||||
rhs.handle = nullptr;
|
rhs.handle = nullptr;
|
||||||
}
|
}
|
||||||
UniquePtr &operator=(const UniquePtr &rhs) = delete;
|
UniquePtr &operator=(const UniquePtr &rhs) = delete;
|
||||||
UniquePtr &operator=(UniquePtr &&rhs)
|
UniquePtr &operator=(UniquePtr &&rhs)
|
||||||
{
|
{
|
||||||
handle = rhs.handle;
|
handle = rhs.handle;
|
||||||
rhs.handle = nullptr;
|
rhs.handle = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
~UniquePtr()
|
~UniquePtr()
|
||||||
{
|
{
|
||||||
delete handle;
|
delete handle;
|
||||||
}
|
}
|
||||||
inline bool operator==(const UniquePtr &other) const
|
inline bool operator==(const UniquePtr &other) const
|
||||||
{
|
{
|
||||||
return handle == other.handle;
|
return handle == other.handle;
|
||||||
}
|
}
|
||||||
inline bool operator!=(const UniquePtr &other) const
|
inline bool operator!=(const UniquePtr &other) const
|
||||||
{
|
{
|
||||||
return handle != other.handle;
|
return handle != other.handle;
|
||||||
}
|
}
|
||||||
inline T *operator->()
|
inline T *operator->()
|
||||||
{
|
{
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
bool isValid()
|
bool isValid()
|
||||||
{
|
{
|
||||||
return handle != nullptr;
|
return handle != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T *handle;
|
T *handle;
|
||||||
friend class boost::serialization::access;
|
friend class boost::serialization::access;
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive& ar, const unsigned int version)
|
void serialize(Archive& ar, const unsigned int version)
|
||||||
{
|
{
|
||||||
ar & *handle;
|
ar & *handle;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//A weak pointer has no ownership over an object and thus cant delete it
|
//A weak pointer has no ownership over an object and thus cant delete it
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class WeakPtr
|
class WeakPtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WeakPtr()
|
WeakPtr()
|
||||||
: pointer(nullptr)
|
: pointer(nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
WeakPtr(RefPtr<T> &sharedPtr)
|
WeakPtr(RefPtr<T> &sharedPtr)
|
||||||
: pointer(sharedPtr)
|
: pointer(sharedPtr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
WeakPtr &operator=(WeakPtr<T> &weakPtr)
|
WeakPtr &operator=(WeakPtr<T> &weakPtr)
|
||||||
{
|
{
|
||||||
pointer = weakPtr.pointer;
|
pointer = weakPtr.pointer;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
WeakPtr &operator=(RefPtr<T> &sharedPtr)
|
WeakPtr &operator=(RefPtr<T> &sharedPtr)
|
||||||
{
|
{
|
||||||
pointer = sharedPtr;
|
pointer = sharedPtr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefPtr<T> pointer;
|
RefPtr<T> pointer;
|
||||||
};
|
};
|
||||||
} // namespace Seele
|
} // namespace Seele
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "MinimalEngine.h"
|
||||||
#include "Containers/List.h"
|
#include "Containers/List.h"
|
||||||
#include <semaphore>
|
#include <semaphore>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
using namespace Seele;
|
using namespace Seele;
|
||||||
|
|
||||||
static ThreadPool gThreadPool;
|
|
||||||
|
void Event::await_suspend(std::coroutine_handle<JobPromise> h)
|
||||||
|
{
|
||||||
|
getGlobalThreadPool().addJob(Job(h));
|
||||||
|
}
|
||||||
|
|
||||||
Job JobPromise::get_return_object() noexcept {
|
Job JobPromise::get_return_object() noexcept {
|
||||||
return Job { std::coroutine_handle<JobPromise>::from_promise(*this) };
|
return Job { std::coroutine_handle<JobPromise>::from_promise(*this) };
|
||||||
@@ -14,7 +18,16 @@ ThreadPool::ThreadPool(uint32 threadCount)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ThreadPool& getGlobalThreadPool()
|
ThreadPool::~ThreadPool()
|
||||||
{
|
{
|
||||||
return gThreadPool;
|
for(auto& thread : workers)
|
||||||
|
{
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadPool& Seele::getGlobalThreadPool()
|
||||||
|
{
|
||||||
|
static ThreadPool threadPool;
|
||||||
|
return threadPool;
|
||||||
}
|
}
|
||||||
+26
-16
@@ -1,23 +1,35 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
#include "MinimalEngine.h"
|
#include "Containers/List.h"
|
||||||
|
|
||||||
namespace Seele
|
namespace Seele
|
||||||
{
|
{
|
||||||
enum class JobStage
|
struct JobPromise;
|
||||||
|
struct Event
|
||||||
{
|
{
|
||||||
INPUT,
|
public:
|
||||||
GAMELOGIC,
|
void raise()
|
||||||
RENDER,
|
{
|
||||||
DONT_CARE
|
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 [[nodiscard]] Job;
|
||||||
struct JobPromise
|
struct JobPromise
|
||||||
{
|
{
|
||||||
Job get_return_object() noexcept;
|
Job get_return_object() noexcept;
|
||||||
|
|
||||||
std::suspend_never initial_suspend() const noexcept { return {}; }
|
std::suspend_always initial_suspend() const noexcept { return {}; }
|
||||||
std::suspend_never final_suspend() const noexcept { return {}; }
|
std::suspend_never final_suspend() const noexcept { return {}; }
|
||||||
|
|
||||||
void return_void() noexcept {}
|
void return_void() noexcept {}
|
||||||
@@ -31,9 +43,8 @@ struct [[nodiscard]] Job
|
|||||||
public:
|
public:
|
||||||
using promise_type = JobPromise;
|
using promise_type = JobPromise;
|
||||||
|
|
||||||
explicit Job(std::coroutine_handle<task_promise> handle, JobStage stage = JobStage::DONT_CARE)
|
explicit Job(std::coroutine_handle<JobPromise> handle)
|
||||||
: handle(handle)
|
: handle(handle)
|
||||||
, stage(stage)
|
|
||||||
{}
|
{}
|
||||||
~Job()
|
~Job()
|
||||||
{
|
{
|
||||||
@@ -44,22 +55,21 @@ public:
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::coroutine_handle<JobPromise> handle;
|
std::coroutine_handle<JobPromise> handle;
|
||||||
JobStage stage;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ThreadPool
|
class ThreadPool
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency);
|
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency());
|
||||||
virtual ~ThreadPool();
|
virtual ~ThreadPool();
|
||||||
void schedule(std::function<void()> function)
|
void addJob(Job&& job)
|
||||||
{
|
{
|
||||||
|
jobs.add(std::move(job));
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
Array<std::thread> workers;
|
std::vector<std::thread> workers;
|
||||||
Map<JobStage, List<Job>> jobs;
|
List<Job> jobs;
|
||||||
void threadLoop();
|
void threadLoop();
|
||||||
};
|
};
|
||||||
static ThreadPool& getGlobalThreadPool();
|
extern ThreadPool& getGlobalThreadPool();
|
||||||
} // namespace Seele
|
} // namespace Seele
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ void Window::addView(PView view)
|
|||||||
{
|
{
|
||||||
WindowView* windowView = new WindowView();
|
WindowView* windowView = new WindowView();
|
||||||
windowView->view = view;
|
windowView->view = view;
|
||||||
windowView->worker = std::thread(&Window::viewWorker, this, windowView);
|
//windowView->worker = std::thread(&Window::viewWorker, this, windowView);
|
||||||
views.add(windowView);
|
views.add(windowView);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +27,16 @@ void Window::render()
|
|||||||
{
|
{
|
||||||
windowView->view->beginUpdate();
|
windowView->view->beginUpdate();
|
||||||
windowView->view->update();
|
windowView->view->update();
|
||||||
windowView->view->commitUpdate();
|
{
|
||||||
windowView->view->prepareRender();
|
std::unique_lock lock(windowView->workerMutex);
|
||||||
|
windowView->view->commitUpdate();
|
||||||
|
}
|
||||||
|
windowView->updateFinished.raise();
|
||||||
|
{
|
||||||
|
std::unique_lock lock(windowView->workerMutex);
|
||||||
|
windowView->view->prepareRender();
|
||||||
|
}
|
||||||
|
windowView->updateFinished.reset();
|
||||||
windowView->view->render();
|
windowView->view->render();
|
||||||
}
|
}
|
||||||
gfxHandle->endFrame();
|
gfxHandle->endFrame();
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Graphics/GraphicsResources.h"
|
#include "Graphics/GraphicsResources.h"
|
||||||
#include "View.h"
|
#include "View.h"
|
||||||
|
#include "ThreadPool.h"
|
||||||
|
|
||||||
namespace Seele
|
namespace Seele
|
||||||
{
|
{
|
||||||
struct WindowView
|
struct WindowView
|
||||||
{
|
{
|
||||||
PView view;
|
PView view;
|
||||||
|
Event updateFinished;
|
||||||
std::thread worker;
|
std::thread worker;
|
||||||
std::mutex workerMutex;
|
std::mutex workerMutex;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,12 +35,3 @@ PWindow WindowManager::addWindow(const WindowCreateInfo &createInfo)
|
|||||||
windows.add(window);
|
windows.add(window);
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowManager::render()
|
|
||||||
{
|
|
||||||
for(auto window : windows)
|
|
||||||
{
|
|
||||||
window->render();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ public:
|
|||||||
WindowManager();
|
WindowManager();
|
||||||
~WindowManager();
|
~WindowManager();
|
||||||
PWindow addWindow(const WindowCreateInfo &createInfo);
|
PWindow addWindow(const WindowCreateInfo &createInfo);
|
||||||
void render();
|
|
||||||
static Gfx::PGraphics getGraphics()
|
static Gfx::PGraphics getGraphics()
|
||||||
{
|
{
|
||||||
return graphics;
|
return graphics;
|
||||||
|
|||||||
+2
-2
@@ -34,9 +34,9 @@ int main()
|
|||||||
PInspectorView inspectorView = new InspectorView(windowManager->getGraphics(), window, inspectorViewInfo);
|
PInspectorView inspectorView = new InspectorView(windowManager->getGraphics(), window, inspectorViewInfo);
|
||||||
window->addView(inspectorView);
|
window->addView(inspectorView);
|
||||||
sceneView->setFocused();
|
sceneView->setFocused();
|
||||||
while (windowManager->isActive())
|
while(true)
|
||||||
{
|
{
|
||||||
windowManager->render();
|
window->render();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user