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(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/)
set(Boost_USE_STATIC_LIBS OFF) 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}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE})
#Workaround for vs, because it places artifacts into an additional subfolder #Workaround for vs, because it places artifacts into an additional subfolder
+72 -72
View File
@@ -3,18 +3,18 @@ import Common;
struct ComputeShaderInput struct ComputeShaderInput
{ {
uint3 groupID : SV_GroupID; uint3 groupID : SV_GroupID;
uint3 groupThreadID : SV_GroupThreadID; uint3 groupThreadID : SV_GroupThreadID;
uint3 dispatchThreadID : SV_DispatchThreadID; uint3 dispatchThreadID : SV_DispatchThreadID;
uint groupIndex : SV_GroupIndex; uint groupIndex : SV_GroupIndex;
}; };
layout(set = INDEX_VIEW_PARAMS, binding = 1) layout(set = INDEX_VIEW_PARAMS, binding = 1)
cbuffer DispatchParams cbuffer DispatchParams
{ {
uint3 numThreadGroups; uint3 numThreadGroups;
uint pad0; uint pad0;
uint3 numThreads; uint3 numThreads;
uint pad1; uint pad1;
} }
layout(set = INDEX_VIEW_PARAMS, binding = 2) layout(set = INDEX_VIEW_PARAMS, binding = 2)
@@ -52,90 +52,90 @@ groupshared uint tLightList[1024];
void oAppendLight(uint lightIndex) void oAppendLight(uint lightIndex)
{ {
uint index; uint index;
InterlockedAdd(oLightCount, 1, index); InterlockedAdd(oLightCount, 1, index);
if(index < 1024) if(index < 1024)
{ {
oLightList[index] = lightIndex; oLightList[index] = lightIndex;
} }
} }
void tAppendLight(uint lightIndex) void tAppendLight(uint lightIndex)
{ {
uint index; uint index;
InterlockedAdd(tLightCount, 1, index); InterlockedAdd(tLightCount, 1, index);
if(index < 1024) if(index < 1024)
{ {
tLightList[index] = lightIndex; tLightList[index] = lightIndex;
} }
} }
[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)] [numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)]
[shader("compute")] [shader("compute")]
void cullLights(ComputeShaderInput in) void cullLights(ComputeShaderInput in)
{ {
int3 texCoord = int3(in.dispatchThreadID.xy, 0); int3 texCoord = int3(in.dispatchThreadID.xy, 0);
float fDepth = depthTextureVS.Load(texCoord).r; float fDepth = depthTextureVS.Load(texCoord).r;
uint uDepth = asuint(fDepth); uint uDepth = asuint(fDepth);
if(in.groupIndex == 0) if(in.groupIndex == 0)
{ {
uMinDepth = 0xffffffff; uMinDepth = 0xffffffff;
uMaxDepth = 0x0; uMaxDepth = 0x0;
oLightCount = 0; oLightCount = 0;
tLightCount = 0; tLightCount = 0;
groupFrustum = frustums[in.groupID.x + (in.groupID.y * numThreadGroups.x)]; groupFrustum = frustums[in.groupID.x + (in.groupID.y * numThreadGroups.x)];
} }
GroupMemoryBarrierWithGroupSync(); GroupMemoryBarrierWithGroupSync();
InterlockedMin(uMinDepth, uDepth); InterlockedMin(uMinDepth, uDepth);
InterlockedMax(uMaxDepth, uDepth); InterlockedMax(uMaxDepth, uDepth);
GroupMemoryBarrierWithGroupSync(); GroupMemoryBarrierWithGroupSync();
float fMinDepth = asfloat(uMinDepth); float fMinDepth = asfloat(uMinDepth);
float fMaxDepth = asfloat(uMaxDepth); float fMaxDepth = asfloat(uMaxDepth);
float minDepthVS = clipToView(float4(0, 0, fMinDepth, 1)).z; float minDepthVS = clipToView(float4(0, 0, fMinDepth, 1)).z;
float maxDepthVS = clipToView(float4(0, 0, fMaxDepth, 1)).z; float maxDepthVS = clipToView(float4(0, 0, fMaxDepth, 1)).z;
float nearClipVS = clipToView(float4(0, 0, 0, 1.0f)).z; float nearClipVS = clipToView(float4(0, 0, 0, 1.0f)).z;
Plane minPlane = {float3(0, 0, -1), -minDepthVS}; Plane minPlane = {float3(0, 0, -1), -minDepthVS};
for ( uint i = in.groupIndex; i < numPointLights; i += BLOCK_SIZE * BLOCK_SIZE ) for ( uint i = in.groupIndex; i < numPointLights; i += BLOCK_SIZE * BLOCK_SIZE )
{ {
PointLight light = pointLights[i]; PointLight light = pointLights[i];
//TODO: why doesn't this check go through? //TODO: why doesn't this check go through?
//if(light.insideFrustum(groupFrustum, nearClipVS, maxDepthVS)) //if(light.insideFrustum(groupFrustum, nearClipVS, maxDepthVS))
{ {
tAppendLight(i); tAppendLight(i);
if(!light.insidePlane(minPlane)) if(!light.insidePlane(minPlane))
{ {
oAppendLight(i); oAppendLight(i);
} }
} }
} }
GroupMemoryBarrierWithGroupSync();
if(in.groupIndex == 0)
{
InterlockedAdd(oLightIndexCounter[0], (uint)oLightCount, oLightIndexStartOffset);
oLightGrid[in.groupID.xy] = uint2(oLightIndexStartOffset, oLightCount);
InterlockedAdd(tLightIndexCounter[0], (uint)tLightCount, tLightIndexStartOffset);
tLightGrid[in.groupID.xy] = uint2(tLightIndexStartOffset, tLightCount);
}
GroupMemoryBarrierWithGroupSync();
for (uint j = in.groupIndex; j < (uint)oLightCount; j += BLOCK_SIZE * BLOCK_SIZE)
{
oLightIndexList[oLightIndexStartOffset + j] = oLightList[j];
}
// For transparent geometry. GroupMemoryBarrierWithGroupSync();
for ( uint k = in.groupIndex; k < (uint)tLightCount; k += BLOCK_SIZE * BLOCK_SIZE )
{ if(in.groupIndex == 0)
tLightIndexList[tLightIndexStartOffset + k] = tLightList[k]; {
} InterlockedAdd(oLightIndexCounter[0], (uint)oLightCount, oLightIndexStartOffset);
} oLightGrid[in.groupID.xy] = uint2(oLightIndexStartOffset, oLightCount);
InterlockedAdd(tLightIndexCounter[0], (uint)tLightCount, tLightIndexStartOffset);
tLightGrid[in.groupID.xy] = uint2(tLightIndexStartOffset, tLightCount);
}
GroupMemoryBarrierWithGroupSync();
for (uint j = in.groupIndex; j < (uint)oLightCount; j += BLOCK_SIZE * BLOCK_SIZE)
{
oLightIndexList[oLightIndexStartOffset + j] = oLightList[j];
}
// For transparent geometry.
for ( uint k = in.groupIndex; k < (uint)tLightCount; k += BLOCK_SIZE * BLOCK_SIZE )
{
tLightIndexList[tLightIndexStartOffset + k] = tLightList[k];
}
}
+1
View File
@@ -34,6 +34,7 @@ void TextureAsset::load()
setStatus(Status::Loading); setStatus(Status::Loading);
ktxTexture2* kTexture; ktxTexture2* kTexture;
// TODO: consider return // TODO: consider return
std::cout << "Loading texture " << getFullPath() << std::endl;
ktxTexture2_CreateFromNamedFile(getFullPath().c_str(), ktxTexture2_CreateFromNamedFile(getFullPath().c_str(),
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
&kTexture); &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()); PTextureAsset asset = new TextureAsset(assetFileName.replace_extension("asset").filename().generic_string());
asset->setStatus(Asset::Status::Loading); asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderAsset->getTexture()); asset->setTexture(placeholderAsset->getTexture());
std::cout << "Loading texture " << asset->getFileName() << std::endl;
AssetRegistry::get().registerTexture(asset); AssetRegistry::get().registerTexture(asset);
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable { futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
using namespace std::chrono_literals; using namespace std::chrono_literals;
+37 -41
View File
@@ -112,7 +112,7 @@ namespace Seele
assert(_data != nullptr); assert(_data != nullptr);
markIteratorDirty(); 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) : arraySize(size)
, allocated(size) , allocated(size)
, allocator(alloc) , allocator(alloc)
@@ -169,21 +169,16 @@ namespace Seele
{ {
if (this != &other) if (this != &other)
{ {
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
&& !std::allocator_traits<allocator_type>::is_always_equal::value)
{ {
if(allocator != other.allocator) if (!std::allocator_traits<allocator_type>::is_always_equal::value
&& allocator != other.allocator)
{ {
deallocateArray(_data, allocated); deallocateArray(_data, allocated);
_data = nullptr; _data = nullptr;
} }
}
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value)
{
allocator = other.allocator; allocator = other.allocator;
} }
else
{}
if(other.arraySize > allocated) if(other.arraySize > allocated)
{ {
if(_data != nullptr) if(_data != nullptr)
@@ -203,17 +198,14 @@ namespace Seele
{ {
if (this != &other) if (this != &other)
{ {
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
&& !std::allocator_traits<allocator_type>::is_always_equal::value)
{ {
if(allocator != other.allocator) if (!std::allocator_traits<allocator_type>::is_always_equal::value
&& allocator != other.allocator)
{ {
deallocateArray(_data, allocated); deallocateArray(_data, allocated);
_data = nullptr; _data = nullptr;
} }
}
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
{
allocator = std::move(other.allocator); allocator = std::move(other.allocator);
} }
if (_data != nullptr) if (_data != nullptr)
@@ -248,59 +240,59 @@ namespace Seele
return !(*this == other); return !(*this == other);
} }
constexpr Iterator find(const T &item) constexpr iterator find(const value_type &item)
{ {
for (uint32 i = 0; i < arraySize; ++i) for (uint32 i = 0; i < arraySize; ++i)
{ {
if (_data[i] == item) if (_data[i] == item)
{ {
return Iterator(&_data[i]); return iterator(&_data[i]);
} }
} }
return endIt; return endIt;
} }
constexpr Iterator find(T&& item) constexpr iterator find(value_type&& item)
{ {
for (uint32 i = 0; i < arraySize; ++i) for (uint32 i = 0; i < arraySize; ++i)
{ {
if (_data[i] == item) if (_data[i] == item)
{ {
return Iterator(&_data[i]); return iterator(&_data[i]);
} }
} }
return endIt; return endIt;
} }
constexpr Allocator get_allocator() const constexpr allocator_type get_allocator() const
{ {
return allocator; return allocator;
} }
constexpr Iterator begin() const constexpr iterator begin() const
{ {
return beginIt; return beginIt;
} }
constexpr Iterator end() const constexpr iterator end() const
{ {
return endIt; return endIt;
} }
constexpr ConstIterator cbegin() const constexpr const_iterator cbegin() const
{ {
return beginIt; return beginIt;
} }
constexpr ConstIterator cend() const constexpr const_iterator cend() const
{ {
return endIt; return endIt;
} }
constexpr T &add(const T &item = T()) constexpr reference add(const value_type &item = value_type())
{ {
return addInternal(item); return addInternal(item);
} }
constexpr T &add(T&& item) constexpr reference add(value_type&& item)
{ {
return addInternal(std::forward<T>(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) if((it = std::move(find(item))) != endIt)
{ {
return *it; return *it;
@@ -308,7 +300,7 @@ namespace Seele
return addInternal(item); return addInternal(item);
} }
template<typename... args> template<typename... args>
constexpr T &emplace(args... arguments) constexpr reference emplace(args... arguments)
{ {
if (arraySize == allocated) if (arraySize == allocated)
{ {
@@ -316,10 +308,8 @@ namespace Seele
allocated = calculateGrowth(newSize); allocated = calculateGrowth(newSize);
T *tempArray = allocateArray(allocated); T *tempArray = allocateArray(allocated);
assert(tempArray != nullptr); assert(tempArray != nullptr);
for (size_type i = 0; i < arraySize; ++i)
{ std::uninitialized_move(begin(), end(), Iterator(tempArray));
tempArray[i] = std::move(_data[i]);
}
deallocateArray(_data, arraySize); deallocateArray(_data, arraySize);
_data = tempArray; _data = tempArray;
} }
@@ -327,7 +317,7 @@ namespace Seele
markIteratorDirty(); markIteratorDirty();
return _data[arraySize - 1]; return _data[arraySize - 1];
} }
constexpr void remove(Iterator it, bool keepOrder = true) constexpr void remove(iterator it, bool keepOrder = true)
{ {
remove(it - beginIt, keepOrder); remove(it - beginIt, keepOrder);
} }
@@ -351,7 +341,7 @@ namespace Seele
{ {
resizeInternal(newSize, std::move(T())); 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); resizeInternal(newSize, value);
} }
@@ -367,11 +357,11 @@ namespace Seele
allocated = 0; allocated = 0;
markIteratorDirty(); markIteratorDirty();
} }
inline size_type indexOf(Iterator iterator) inline size_type indexOf(iterator iterator)
{ {
return iterator - beginIt; return iterator - beginIt;
} }
inline size_type indexOf(ConstIterator iterator) const inline size_type indexOf(const_iterator iterator) const
{ {
return iterator.p - beginIt.p; return iterator.p - beginIt.p;
} }
@@ -395,12 +385,18 @@ namespace Seele
{ {
return allocated; return allocated;
} }
inline T *data() const inline pointer data() const
{ {
return _data; 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]; return _data[arraySize - 1];
} }
void pop() void pop()
@@ -408,12 +404,12 @@ namespace Seele
arraySize--; arraySize--;
markIteratorDirty(); markIteratorDirty();
} }
constexpr inline T &operator[](size_type index) constexpr inline reference operator[](size_type index)
{ {
assert(index < arraySize); assert(index < arraySize);
return _data[index]; return _data[index];
} }
constexpr inline const T &operator[](size_type index) const constexpr inline const reference operator[](size_type index) const
{ {
assert(index < arraySize); assert(index < arraySize);
return _data[index]; return _data[index];
+24 -7
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "MinimalEngine.h" #include "MinimalEngine.h"
#include <xmemory> #include <memory_resource>
namespace Seele namespace Seele
{ {
@@ -187,11 +187,11 @@ public:
return *this; return *this;
} }
T &front() reference front()
{ {
return root->data; return root->data;
} }
T &back() reference back()
{ {
return tail->prev->data; return tail->prev->data;
} }
@@ -218,7 +218,7 @@ public:
root = allocateNode(); root = allocateNode();
tail = root; tail = root;
} }
tail->data = value; initializeNode(tail, value);
Node *newTail = allocateNode(); Node *newTail = allocateNode();
newTail->prev = tail; newTail->prev = tail;
newTail->next = nullptr; newTail->next = nullptr;
@@ -236,7 +236,7 @@ public:
root = allocateNode(); root = allocateNode();
tail = root; tail = root;
} }
tail->data = std::move(value); initializeNode(tail, std::move(value));
Node *newTail = allocateNode(); Node *newTail = allocateNode();
newTail->prev = tail; newTail->prev = tail;
newTail->next = nullptr; newTail->next = nullptr;
@@ -247,6 +247,13 @@ public:
_size++; _size++;
return insertedElement; return insertedElement;
} }
// front + popFront
value_type&& retrieve()
{
auto&& temp = std::move(root->data);
popFront();
return std::move(temp);
}
iterator remove(iterator pos) iterator remove(iterator pos)
{ {
_size--; _size--;
@@ -299,7 +306,7 @@ public:
} }
Node *tmp = pos.node->prev; Node *tmp = pos.node->prev;
Node *newNode = allocateNode(); Node *newNode = allocateNode();
newNode->data = value; initializeNode(newNode, value);
tmp->next = newNode; tmp->next = newNode;
newNode->prev = tmp; newNode->prev = tmp;
newNode->next = pos.node; newNode->next = pos.node;
@@ -346,10 +353,20 @@ private:
Node* allocateNode() Node* allocateNode()
{ {
Node* node = allocator.allocate(1); Node* node = allocator.allocate(1);
std::memset(node, 0, sizeof(Node));
assert(node != nullptr); assert(node != nullptr);
node->prev = nullptr;
node->next = nullptr;
return node; 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) void deallocateNode(Node* node)
{ {
allocator.deallocate(node, 1); allocator.deallocate(node, 1);
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -370,12 +370,14 @@ public:
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) override; virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) override;
virtual void setScrollCallback(std::function<void(double, double)> 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 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(KeyCode, InputAction, KeyModifier)> keyCallback;
std::function<void(double, double)> mouseMoveCallback; std::function<void(double, double)> mouseMoveCallback;
std::function<void(MouseButton, InputAction, KeyModifier)> mouseButtonCallback; std::function<void(MouseButton, InputAction, KeyModifier)> mouseButtonCallback;
std::function<void(double, double)> scrollCallback; std::function<void(double, double)> scrollCallback;
std::function<void(int, const char**)> fileCallback; std::function<void(int, const char**)> fileCallback;
std::function<void()> closeCallback;
protected: protected:
void advanceBackBuffer(); void advanceBackBuffer();
void recreateSwapchain(const WindowCreateInfo &createInfo); void recreateSwapchain(const WindowCreateInfo &createInfo);
@@ -38,6 +38,12 @@ void glfwFileCallback(GLFWwindow* handle, int count, const char** paths)
window->fileCallback(count, paths); window->fileCallback(count, paths);
} }
void glfwCloseCallback(GLFWwindow* handle)
{
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->closeCallback();
}
Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo) Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
: Gfx::Window(createInfo) : Gfx::Window(createInfo)
, graphics(graphics) , graphics(graphics)
@@ -56,6 +62,7 @@ Window::Window(PGraphics graphics, const WindowCreateInfo &createInfo)
glfwSetMouseButtonCallback(handle, &glfwMouseButtonCallback); glfwSetMouseButtonCallback(handle, &glfwMouseButtonCallback);
glfwSetScrollCallback(handle, &glfwScrollCallback); glfwSetScrollCallback(handle, &glfwScrollCallback);
glfwSetDropCallback(handle, &glfwFileCallback); glfwSetDropCallback(handle, &glfwFileCallback);
glfwSetWindowCloseCallback(handle, &glfwCloseCallback);
glfwCreateWindowSurface(instance, handle, nullptr, &surface); glfwCreateWindowSurface(instance, handle, nullptr, &surface);
@@ -134,6 +141,11 @@ void Window::setFileCallback(std::function<void(int, const char**)> callback)
fileCallback = callback; fileCallback = callback;
} }
void Window::setCloseCallback(std::function<void()> callback)
{
closeCallback = callback;
}
void Window::advanceBackBuffer() void Window::advanceBackBuffer()
{ {
VkResult res = VK_ERROR_OUT_OF_DATE_KHR; VkResult res = VK_ERROR_OUT_OF_DATE_KHR;
+2 -2
View File
@@ -54,9 +54,9 @@ public:
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)
{ {
+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].color = Vector4(1, 1, 1, 1);
lightEnv.directionalLights[0].direction = Vector4(1, 1, 0, 1); lightEnv.directionalLights[0].direction = Vector4(1, 1, 0, 1);
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1); lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 0; lightEnv.numDirectionalLights = 1;
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
for(uint32 i = 0; i < 16; ++i) for(uint32 i = 0; i < 16; ++i)
{ {
+80 -5
View File
@@ -2,20 +2,38 @@
using namespace Seele; 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 { bool Event::await_ready()
return Job { std::coroutine_handle<JobPromise>::from_promise(*this) }; {
return flag->load();
} }
ThreadPool::ThreadPool(uint32 threadCount) ThreadPool::ThreadPool(uint32 threadCount)
: workers(threadCount) : workers(threadCount)
{ {
running.store(true);
for(uint32 i = 0; i < threadCount; ++i)
{
workers[i] = std::thread(&ThreadPool::threadLoop, this, i == 0);
}
} }
ThreadPool::~ThreadPool() ThreadPool::~ThreadPool()
@@ -25,6 +43,63 @@ ThreadPool::~ThreadPool()
thread.join(); 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() ThreadPool& Seele::getGlobalThreadPool()
{ {
+88 -37
View File
@@ -1,35 +1,20 @@
#pragma once #pragma once
#include <thread> #include <thread>
#include <coroutine>
#include "Containers/List.h" #include "Containers/List.h"
namespace Seele namespace Seele
{ {
struct JobPromise; extern class ThreadPool& getGlobalThreadPool();
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;
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 {}; } std::suspend_never final_suspend() const noexcept { return {}; }
void return_void() noexcept {} void return_void() noexcept {}
@@ -38,23 +23,47 @@ struct JobPromise
exit(1); exit(1);
}; };
}; };
struct [[nodiscard]] Job template<bool MainJob>
struct JobBase
{ {
public: 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) : handle(handle)
{} {}
~Job() ~JobBase()
{ {
if(handle) if(handle)
{ {
handle.destroy(); handle.destroy();
} }
} }
void resume()
{
handle.resume();
}
private: 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 class ThreadPool
@@ -62,14 +71,56 @@ class ThreadPool
public: public:
ThreadPool(uint32 threadCount = std::thread::hardware_concurrency()); ThreadPool(uint32 threadCount = std::thread::hardware_concurrency());
virtual ~ThreadPool(); virtual ~ThreadPool();
void addJob(Job&& job) void addJob(Job&& job);
{ void addJob(MainJob&& job);
jobs.add(std::move(job)); void enqueueWaiting(Event* event, Job&& job);
} void enqueueWaiting(Event* event, MainJob&& job);
void notify(Event* event);
private: private:
std::atomic_bool running;
std::thread* mainThread;
std::vector<std::thread> workers; 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 } // namespace Seele
+85 -85
View File
@@ -8,49 +8,49 @@
using namespace Seele; using namespace Seele;
Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const ViewportCreateInfo &createInfo) Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const ViewportCreateInfo &createInfo)
: View(graphics, owner, createInfo) : View(graphics, owner, createInfo)
, activeCamera(new CameraActor()) , activeCamera(new CameraActor())
, depthPrepass(DepthPrepass(graphics, viewport, activeCamera)) , depthPrepass(DepthPrepass(graphics, viewport, activeCamera))
, lightCullingPass(LightCullingPass(graphics, viewport, activeCamera)) , lightCullingPass(LightCullingPass(graphics, viewport, activeCamera))
, basePass(BasePass(graphics, viewport, activeCamera)) , basePass(BasePass(graphics, viewport, activeCamera))
{ {
scene = new Scene(graphics); scene = new Scene(graphics);
scene->addActor(activeCamera); 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_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_Face_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png"); AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Lightmap.png"); AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Lightmap.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Tex_FaceLightmap.png"); AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Tex_FaceLightmap.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Ayaka.fbx"); AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Ayaka.fbx");
PPrimitiveComponent ayaka = new PrimitiveComponent(AssetRegistry::findMesh("Ayaka")); PPrimitiveComponent ayaka = new PrimitiveComponent(AssetRegistry::findMesh("Ayaka"));
ayaka->addWorldTranslation(Vector(0, 0, 100)); ayaka->addWorldTranslation(Vector(0, 0, 0));
ayaka->setWorldScale(Vector(0.1f, 0.1f, 0.1f)); ayaka->setWorldScale(Vector(10, 10, 10));
scene->addPrimitiveComponent(ayaka);*/ 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\\Cube\\cube.obj");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx"); AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane")); PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
plane->setWorldScale(Vector(100, 100, 100)); plane->setWorldScale(Vector(100, 100, 100));
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely")); PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely"));
arissa->addWorldTranslation(Vector(0, 0, 100)); arissa->addWorldTranslation(Vector(0, 0, 100));
arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f)); arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
scene->addPrimitiveComponent(plane); scene->addPrimitiveComponent(plane);
scene->addPrimitiveComponent(arissa); scene->addPrimitiveComponent(arissa);*/
PRenderGraphResources resources = new RenderGraphResources(); PRenderGraphResources resources = new RenderGraphResources();
depthPrepass.setResources(resources); depthPrepass.setResources(resources);
lightCullingPass.setResources(resources); lightCullingPass.setResources(resources);
basePass.setResources(resources); basePass.setResources(resources);
depthPrepass.publishOutputs(); depthPrepass.publishOutputs();
lightCullingPass.publishOutputs(); lightCullingPass.publishOutputs();
basePass.publishOutputs(); basePass.publishOutputs();
depthPrepass.createRenderPass(); depthPrepass.createRenderPass();
lightCullingPass.createRenderPass(); lightCullingPass.createRenderPass();
basePass.createRenderPass(); basePass.createRenderPass();
} }
Seele::SceneView::~SceneView() Seele::SceneView::~SceneView()
@@ -59,8 +59,8 @@ Seele::SceneView::~SceneView()
void SceneView::beginUpdate() void SceneView::beginUpdate()
{ {
View::beginUpdate(); View::beginUpdate();
scene->tick(Gfx::currentFrameDelta); scene->tick(Gfx::currentFrameDelta);
} }
void SceneView::update() void SceneView::update()
@@ -69,81 +69,81 @@ void SceneView::update()
void SceneView::commitUpdate() void SceneView::commitUpdate()
{ {
depthPrepassData.staticDrawList = scene->getStaticMeshes(); depthPrepassData.staticDrawList = scene->getStaticMeshes();
lightCullingPassData.lightEnv = scene->getLightBuffer(); lightCullingPassData.lightEnv = scene->getLightBuffer();
basePassData.staticDrawList = scene->getStaticMeshes(); basePassData.staticDrawList = scene->getStaticMeshes();
} }
void SceneView::prepareRender() void SceneView::prepareRender()
{ {
depthPrepass.updateViewFrame(depthPrepassData); depthPrepass.updateViewFrame(depthPrepassData);
lightCullingPass.updateViewFrame(lightCullingPassData); lightCullingPass.updateViewFrame(lightCullingPassData);
basePass.updateViewFrame(basePassData); basePass.updateViewFrame(basePassData);
} }
void SceneView::render() void SceneView::render()
{ {
depthPrepass.beginFrame(); depthPrepass.beginFrame();
lightCullingPass.beginFrame(); lightCullingPass.beginFrame();
basePass.beginFrame(); basePass.beginFrame();
depthPrepass.render(); depthPrepass.render();
lightCullingPass.render(); lightCullingPass.render();
basePass.render(); basePass.render();
depthPrepass.endFrame(); depthPrepass.endFrame();
lightCullingPass.endFrame(); lightCullingPass.endFrame();
basePass.endFrame(); basePass.endFrame();
} }
void SceneView::keyCallback(KeyCode code, InputAction action, KeyModifier) void SceneView::keyCallback(KeyCode code, InputAction action, KeyModifier)
{ {
if(action != InputAction::RELEASE) if(action != InputAction::RELEASE)
{ {
if(code == KeyCode::KEY_W) if(code == KeyCode::KEY_W)
{ {
activeCamera->getCameraComponent()->moveOrigin(1); activeCamera->getCameraComponent()->moveOrigin(1);
} }
if(code == KeyCode::KEY_S) if(code == KeyCode::KEY_S)
{ {
activeCamera->getCameraComponent()->moveOrigin(-1); activeCamera->getCameraComponent()->moveOrigin(-1);
} }
} }
} }
static bool mouseDown = false; static bool mouseDown = false;
void SceneView::mouseMoveCallback(double xPos, double yPos) void SceneView::mouseMoveCallback(double xPos, double yPos)
{ {
static double prevXPos = 0.0f, prevYPos = 0.0f; static double prevXPos = 0.0f, prevYPos = 0.0f;
double deltaX = prevXPos - xPos; double deltaX = prevXPos - xPos;
double deltaY = prevYPos - yPos; double deltaY = prevYPos - yPos;
prevXPos = xPos; prevXPos = xPos;
prevYPos = yPos; prevYPos = yPos;
if(mouseDown) if(mouseDown)
{ {
activeCamera->getCameraComponent()->mouseMove((float)deltaX, (float)deltaY); activeCamera->getCameraComponent()->mouseMove((float)deltaX, (float)deltaY);
} }
} }
void SceneView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier) void SceneView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier)
{ {
if(button == MouseButton::MOUSE_BUTTON_1 && action != InputAction::RELEASE) if(button == MouseButton::MOUSE_BUTTON_1 && action != InputAction::RELEASE)
{ {
mouseDown = true; mouseDown = true;
} }
else else
{ {
mouseDown = false; mouseDown = false;
} }
} }
void SceneView::scrollCallback(double, double yOffset) void SceneView::scrollCallback(double, double yOffset)
{ {
activeCamera->getCameraComponent()->mouseScroll(yOffset); activeCamera->getCameraComponent()->mouseScroll(yOffset);
} }
void SceneView::fileCallback(int, const char**) void SceneView::fileCallback(int, const char**)
{ {
} }
+19 -13
View File
@@ -1,10 +1,12 @@
#include "Window.h" #include "Window.h"
#include "WindowManager.h"
#include <functional> #include <functional>
using namespace Seele; using namespace Seele;
Window::Window(Gfx::PWindow handle) Window::Window(PWindowManager owner, Gfx::PWindow handle)
: gfxHandle(handle) : owner(owner)
, gfxHandle(handle)
{ {
} }
@@ -20,23 +22,17 @@ void Window::addView(PView view)
views.add(windowView); views.add(windowView);
} }
void Window::render() MainJob Window::render()
{ {
gfxHandle->beginFrame(); gfxHandle->beginFrame();
for(auto& windowView : views) for(auto& windowView : views)
{ {
windowView->view->beginUpdate(); viewWorker(windowView);
windowView->view->update(); co_await windowView->updateFinished;
{
std::unique_lock lock(windowView->workerMutex);
windowView->view->commitUpdate();
}
windowView->updateFinished.raise();
{ {
std::unique_lock lock(windowView->workerMutex); std::unique_lock lock(windowView->workerMutex);
windowView->view->prepareRender(); windowView->view->prepareRender();
} }
windowView->updateFinished.reset();
windowView->view->render(); windowView->view->render();
} }
gfxHandle->endFrame(); gfxHandle->endFrame();
@@ -59,12 +55,22 @@ void Window::setFocused(PView view)
gfxHandle->setMouseButtonCallback(mouseButtonFunction); gfxHandle->setMouseButtonCallback(mouseButtonFunction);
gfxHandle->setScrollCallback(scrollFunction); gfxHandle->setScrollCallback(scrollFunction);
gfxHandle->setFileCallback(fileFunction); gfxHandle->setFileCallback(fileFunction);
gfxHandle->setCloseCallback([this](){
owner->notifyWindowClosed(this);
});
} }
void Window::viewWorker(WindowView* windowView) Job Window::viewWorker(WindowView* windowView)
{ {
while(true) 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; PView view;
Event updateFinished; Event updateFinished;
std::thread worker;
std::mutex workerMutex; std::mutex workerMutex;
}; };
DEFINE_REF(WindowView) DEFINE_REF(WindowView)
DECLARE_REF(WindowManager)
// The logical window, with the graphics proxy // The logical window, with the graphics proxy
class Window class Window
{ {
public: public:
Window(Gfx::PWindow handle); Window(PWindowManager owner, Gfx::PWindow handle);
~Window(); ~Window();
void addView(PView view); void addView(PView view);
void render(); MainJob render();
Gfx::PWindow getGfxHandle(); Gfx::PWindow getGfxHandle();
void setFocused(PView view); void setFocused(PView view);
protected: protected:
PWindowManager owner;
Array<WindowView*> views; Array<WindowView*> views;
Gfx::PWindow gfxHandle; Gfx::PWindow gfxHandle;
void viewWorker(WindowView* view); Job viewWorker(WindowView* view);
}; };
DEFINE_REF(Window) DEFINE_REF(Window)
} // namespace Seele } // namespace Seele
+24 -19
View File
@@ -7,21 +7,21 @@ Gfx::PGraphics WindowManager::graphics;
WindowManager::WindowManager() WindowManager::WindowManager()
{ {
graphics = new Vulkan::Graphics(); graphics = new Vulkan::Graphics();
GraphicsInitializer initializer; GraphicsInitializer initializer;
graphics->init(initializer); graphics->init(initializer);
TextureCreateInfo info; TextureCreateInfo info;
info.width = 4096; info.width = 4096;
info.height = 4096; info.height = 4096;
Gfx::PTexture2D testTexture = graphics->createTexture2D(info); Gfx::PTexture2D testTexture = graphics->createTexture2D(info);
UniformBufferCreateInfo uniformInitializer; UniformBufferCreateInfo uniformInitializer;
uniformInitializer.resourceData.size = 4096; uniformInitializer.resourceData.size = 4096;
uniformInitializer.resourceData.data = new uint8[4096]; uniformInitializer.resourceData.data = new uint8[4096];
for (int i = 0; i < 4096; ++i) for (int i = 0; i < 4096; ++i)
{ {
uniformInitializer.resourceData.data[i] = (uint8)i; uniformInitializer.resourceData.data[i] = (uint8)i;
} }
Gfx::PUniformBuffer testUniform = graphics->createUniformBuffer(uniformInitializer); Gfx::PUniformBuffer testUniform = graphics->createUniformBuffer(uniformInitializer);
} }
WindowManager::~WindowManager() WindowManager::~WindowManager()
@@ -30,8 +30,13 @@ WindowManager::~WindowManager()
PWindow WindowManager::addWindow(const WindowCreateInfo &createInfo) PWindow WindowManager::addWindow(const WindowCreateInfo &createInfo)
{ {
Gfx::PWindow handle = graphics->createWindow(createInfo); Gfx::PWindow handle = graphics->createWindow(createInfo);
PWindow window = new Window(handle); PWindow window = new Window(this, handle);
windows.add(window); windows.add(window);
return window; return window;
}
void WindowManager::notifyWindowClosed(PWindow window)
{
windows.remove(windows.find(window));
} }
+1
View File
@@ -12,6 +12,7 @@ public:
WindowManager(); WindowManager();
~WindowManager(); ~WindowManager();
PWindow addWindow(const WindowCreateInfo &createInfo); PWindow addWindow(const WindowCreateInfo &createInfo);
void notifyWindowClosed(PWindow window);
static Gfx::PGraphics getGraphics() static Gfx::PGraphics getGraphics()
{ {
return graphics; return graphics;
+2 -2
View File
@@ -9,7 +9,7 @@ using namespace Seele;
int main() int main()
{ {
PWindowManager windowManager = new WindowManager(); PWindowManager windowManager = new WindowManager();
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\"); AssetRegistry::init("/home/dynamitos/TestSeeleProject");
WindowCreateInfo mainWindowInfo; WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine"; mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280; mainWindowInfo.width = 1280;
@@ -34,7 +34,7 @@ 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(true) while(windowManager->isActive())
{ {
window->render(); window->render();
} }