3700 FPS here we go

This commit is contained in:
Dynamitos
2021-04-25 23:43:40 +02:00
parent 312ae2af9a
commit 4a078bd24c
35 changed files with 160 additions and 73 deletions
+2 -7
View File
@@ -45,17 +45,12 @@ PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
auto it = get().meshes.find(filePath);
assert(it != get().meshes.end());
return it->value;
return it->second;
}
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
PTextureAsset result = get().textures[filePath];
if(result == nullptr)
{
return get().textureLoader->getPlaceholderTexture();
}
return result;
return get().textures[filePath];
}
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
+5 -3
View File
@@ -2,6 +2,7 @@
#include "MinimalEngine.h"
#include "Asset.h"
#include <string>
#include <map>
namespace Seele
{
@@ -46,9 +47,10 @@ private:
std::ifstream internalCreateReadStream(const std::string& relaitvePath, std::ios_base::openmode openmode = std::ios::in);
std::filesystem::path rootFolder;
Map<std::string, PTextureAsset> textures;
Map<std::string, PMeshAsset> meshes;
Map<std::string, PMaterialAsset> materials;
//Todo: Seele::Map doesn't really work with strings for some reason, so just use std::map for now
std::map<std::string, PTextureAsset> textures;
std::map<std::string, PMeshAsset> meshes;
std::map<std::string, PMaterialAsset> materials;
UPTextureLoader textureLoader;
UPMeshLoader meshLoader;
UPMaterialLoader materialLoader;
+1
View File
@@ -19,6 +19,7 @@ public:
}
Gfx::PTexture getTexture()
{
std::scoped_lock lck(lock);
return texture;
}
private:
+3 -2
View File
@@ -16,6 +16,7 @@ TextureLoader::TextureLoader(Gfx::PGraphics graphics)
placeholderTexture = import("./textures/placeholder.png");
placeholderAsset->setTexture(placeholderTexture);
placeholderAsset->setStatus(Asset::Status::Ready);
AssetRegistry::get().textures[""] = placeholderAsset;
}
TextureLoader::~TextureLoader()
@@ -28,13 +29,13 @@ 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(placeholderTexture);
std::cout << "Loading texture, placeholder" << std::endl;
AssetRegistry::get().textures[asset->getFileName()] = asset;
futures.add(std::async(std::launch::async, [this, filePath, asset] () mutable {
using namespace std::chrono_literals;
std::this_thread::sleep_for(5s);
Gfx::PTexture2D texture = import(filePath);
asset->setTexture(texture);
asset->setStatus(Asset::Status::Ready);
std::cout << "Finished loading texture" << std::endl;
}));
}
+2 -2
View File
@@ -280,11 +280,11 @@ public:
}
inline V &operator[](K&& key)
{
root = splay(root, std::forward<K>(key));
root = splay(root, std::move(key));
markIteratorDirty();
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
root = insert(root, std::forward<K>(key));
root = insert(root, std::move(key));
_size++;
}
return root->pair.value;
+1
View File
@@ -4,6 +4,7 @@ target_sources(SeeleEngine
GraphicsResources.cpp
GraphicsInitializer.h
GraphicsEnums.h
GraphicsEnums.cpp
Graphics.h
Graphics.cpp
Mesh.h
+6
View File
@@ -0,0 +1,6 @@
#include "GraphicsEnums.h"
using namespace Gfx;
uint32 Gfx::currentFrameIndex = 0;
double Gfx::currentFrameDelta = 0;
+2 -1
View File
@@ -164,7 +164,8 @@ namespace Gfx
static constexpr bool useAsyncCompute = true;
static constexpr bool waitIdleOnSubmit = false;
static constexpr uint32 numFramesBuffered = 8;
static uint32 currentFrameIndex = 0;
extern uint32 currentFrameIndex;
extern double currentFrameDelta;
enum class MaterialShadingModel
{
+10 -6
View File
@@ -168,12 +168,11 @@ Buffer::~Buffer()
UniformBuffer::UniformBuffer(QueueFamilyMapping mapping, const BulkResourceData& resourceData)
: Buffer(mapping, resourceData.owner)
, size(resourceData.size)
, contents(resourceData.size)
{
if(resourceData.data != nullptr)
{
contents = new uint8[size];
std::memcpy(contents, resourceData.data, size);
std::memcpy(contents.data(), resourceData.data, contents.size());
}
}
@@ -181,10 +180,15 @@ UniformBuffer::~UniformBuffer()
{
}
void UniformBuffer::updateContents(const BulkResourceData& resourceData)
bool UniformBuffer::updateContents(const BulkResourceData& resourceData)
{
assert(size == resourceData.size);
std::memcpy(contents, resourceData.data, size);
assert(contents.size() == resourceData.size);
if(std::memcmp(contents.data(), resourceData.data, contents.size()) == 0)
{
return false;
}
std::memcpy(contents.data(), resourceData.data, contents.size());
return true;
}
StructuredBuffer::StructuredBuffer(QueueFamilyMapping mapping, QueueType startQueueType)
+5 -5
View File
@@ -321,26 +321,26 @@ class UniformBuffer : public Buffer
public:
UniformBuffer(QueueFamilyMapping mapping, const BulkResourceData& resourceData);
virtual ~UniformBuffer();
virtual void updateContents(const BulkResourceData& resourceData);
// returns true if an update was performed, false if the old contents == new contents
virtual bool updateContents(const BulkResourceData& resourceData);
bool isDataEquals(UniformBuffer* other)
{
if(other == nullptr)
{
return false;
}
if(size != other->size)
if(contents.size() != other->contents.size())
{
return false;
}
if(std::memcmp(contents, other->contents, size) != 0)
if(std::memcmp(contents.data(), other->contents.data(), contents.size()) != 0)
{
return false;
}
return true;
}
protected:
void* contents;
uint32 size;
Array<uint8> contents;
// Inherited via QueueOwnedResource
virtual void executeOwnershipBarrier(QueueType newOwner) = 0;
};
+7 -3
View File
@@ -106,7 +106,7 @@ BasePass::BasePass(const PScene scene, Gfx::PGraphics graphics, Gfx::PViewport v
lightLayout = graphics->createDescriptorLayout();
lightLayout->addDescriptorBinding(0, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformInitializer.resourceData.size = sizeof(LightEnv);
uniformInitializer.resourceData.data = (uint8*)&scene->getLightEnvironment();
uniformInitializer.resourceData.data = nullptr;
uniformInitializer.bDynamic = true;
lightUniform = graphics->createUniformBuffer(uniformInitializer);
lightLayout->create();
@@ -163,14 +163,18 @@ void BasePass::beginFrame()
descriptorSets[1]->updateBuffer(0, viewParamBuffer);
descriptorSets[1]->updateBuffer(1, screenToViewParamBuffer);
descriptorSets[1]->writeChanges();
for(auto &&meshBatch : scene->getStaticMeshes())
{
meshBatch.material->updateDescriptorData();
}
}
void BasePass::render()
{
graphics->beginRenderPass(renderPass);
for (auto &&primitive : scene->getStaticMeshes())
for (auto &&meshBatch : scene->getStaticMeshes())
{
processor->addMeshBatch(primitive, renderPass, basePassLayout, primitiveLayout, descriptorSets);
processor->addMeshBatch(meshBatch, renderPass, basePassLayout, primitiveLayout, descriptorSets);
}
graphics->executeCommands(processor->getRenderCommands());
graphics->endRenderPass();
@@ -265,6 +265,18 @@ UniformBuffer::~UniformBuffer()
{
}
bool UniformBuffer::updateContents(const BulkResourceData &resourceData)
{
if(!Gfx::UniformBuffer::updateContents(resourceData))
{
// no update was performed, skip
return false;
}
void* data = lock();
std::memcpy(data, resourceData.data, resourceData.size);
unlock();
return true;
}
void* UniformBuffer::lock(bool bWriteOnly)
{
if(dedicatedStagingBuffer != nullptr)
@@ -92,11 +92,9 @@ void CmdBuffer::executeCommands(Array<Gfx::PRenderCommand> commands)
{
auto command = commands[i].cast<SecondaryCmdBuffer>();
// Cache array and size to save on pointer access
DescriptorSet** boundDescriptors = command->boundDescriptors.data();
size_t numDescriptors = command->boundDescriptors.size();
for(size_t i = 0; i < numDescriptors; ++i)
for(auto boundDescriptor : command->boundDescriptors)
{
boundDescriptors[i]->currentlyBound = true;
boundDescriptor->currentlyBound = this;
}
command->end();
executingCommands.add(command);
@@ -182,11 +180,9 @@ void SecondaryCmdBuffer::end()
void SecondaryCmdBuffer::reset()
{
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
size_t numBoundDescriptors = boundDescriptors.size();
DescriptorSet** boundDescriptorSets = boundDescriptors.data();
for(size_t i = 0; i < numBoundDescriptors; ++i)
for(auto boundDescriptor : boundDescriptors)
{
boundDescriptorSets[i]->currentlyBound = false;
boundDescriptor->currentlyBound = nullptr;
}
boundDescriptors.clear();
ready = true;
@@ -116,16 +116,17 @@ void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PUniformBuffer uniformBu
{
PUniformBuffer vulkanBuffer = uniformBuffer.cast<UniformBuffer>();
UniformBuffer* cachedBuffer = reinterpret_cast<UniformBuffer*>(cachedData[Gfx::currentFrameIndex][binding]);
if(vulkanBuffer->isDataEquals(cachedBuffer))
/*if(vulkanBuffer->isDataEquals(cachedBuffer))
{
std::cout << "uniform data equal, skip" << std::endl;
return;
}
}*/
bufferInfos.add(init::DescriptorBufferInfo(vulkanBuffer->getHandle(), 0, vulkanBuffer->getSize()));
VkWriteDescriptorSet writeDescriptor = init::WriteDescriptorSet(setHandle[Gfx::currentFrameIndex], VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, binding, &bufferInfos.back());
writeDescriptors.add(writeDescriptor);
cachedData[Gfx::currentFrameIndex][binding] = vulkanBuffer.getHandle();
cachedData[Gfx::currentFrameIndex][binding] = new UniformBuffer(*vulkanBuffer.getHandle());
}
void DescriptorSet::updateBuffer(uint32_t binding, Gfx::PStructuredBuffer uniformBuffer)
@@ -171,10 +172,8 @@ void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::
TextureHandle* cachedTexture = reinterpret_cast<TextureHandle*>(cachedData[Gfx::currentFrameIndex][binding]);
if(vulkanTexture == cachedTexture)
{
std::cout << "Cached texture is same as new one, skipping update" << std::endl;
return;
}
std::cout << "Texture changed, updating" << std::endl;
//It is assumed that the image is in the correct layout
VkDescriptorImageInfo imageInfo =
init::DescriptorImageInfo(
@@ -187,7 +186,12 @@ void DescriptorSet::updateTexture(uint32_t binding, Gfx::PTexture texture, Gfx::
imageInfo.sampler = vulkanSampler->sampler;
}
imageInfos.add(imageInfo);
VkWriteDescriptorSet writeDescriptor = init::WriteDescriptorSet(setHandle[Gfx::currentFrameIndex], samplerState != nullptr ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, binding, &imageInfos.back());
VkWriteDescriptorSet writeDescriptor =
init::WriteDescriptorSet(
setHandle[Gfx::currentFrameIndex],
samplerState != nullptr ? VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER : VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
binding,
&imageInfos.back());
if (vulkanTexture->getUsage() & VK_IMAGE_USAGE_STORAGE_BIT)
{
writeDescriptor.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
@@ -212,7 +216,11 @@ void DescriptorSet::writeChanges()
{
if (writeDescriptors.size() > 0)
{
assert(!isCurrentlyBound());
if(isCurrentlyBound())
{
graphics->getGraphicsCommands()->waitForCommands(currentlyBound);
currentlyBound = nullptr;
}
vkUpdateDescriptorSets(graphics->getDevice(), (uint32)writeDescriptors.size(), writeDescriptors.data(), 0, nullptr);
writeDescriptors.clear();
imageInfos.clear();
@@ -73,7 +73,7 @@ public:
inline bool isCurrentlyBound() const
{
return currentlyBound;
return currentlyBound != nullptr;
}
inline bool isCurrentlyInUse() const
{
@@ -90,8 +90,8 @@ public:
virtual uint32 getSetIndex() const;
private:
Array<VkDescriptorImageInfo> imageInfos;
Array<VkDescriptorBufferInfo> bufferInfos;
List<VkDescriptorImageInfo> imageInfos;
List<VkDescriptorBufferInfo> bufferInfos;
Array<VkWriteDescriptorSet> writeDescriptors;
// contains the previously bound resources at every binding
// since the layout is fixed, trying to bind a texture to a buffer
@@ -100,7 +100,7 @@ private:
VkDescriptorSet setHandle[Gfx::numFramesBuffered];
PGraphics graphics;
PDescriptorAllocator owner;
bool currentlyBound;
PCmdBuffer currentlyBound;
bool currentlyInUse;
friend class DescriptorAllocator;
friend class CmdBuffer;
@@ -49,13 +49,6 @@ void QueueOwnedResourceDeletion::run()
}
}
void UniformBuffer::updateContents(const BulkResourceData &resourceData)
{
Gfx::UniformBuffer::updateContents(resourceData);
void* data = lock();
std::memcpy(data, resourceData.data, resourceData.size);
unlock();
}
Semaphore::Semaphore(PGraphics graphics)
: graphics(graphics)
@@ -133,7 +133,7 @@ class UniformBuffer : public Gfx::UniformBuffer, public ShaderBuffer
public:
UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo &resourceData);
virtual ~UniformBuffer();
virtual void updateContents(const BulkResourceData &resourceData);
virtual bool updateContents(const BulkResourceData &resourceData);
virtual void* lock(bool bWriteOnly = true) override;
virtual void unlock() override;
@@ -213,6 +213,11 @@ void Window::present()
presentResult = vkQueuePresentKHR(graphics->getGraphicsCommands()->getQueue()->getHandle(), &info);
}
Gfx::currentFrameIndex = (Gfx::currentFrameIndex + 1)%Gfx::numFramesBuffered;
static double lastFrameTime = 0.f;
double currentTime = glfwGetTime();
double currentDelta = currentTime - lastFrameTime;
Gfx::currentFrameDelta = currentDelta;
lastFrameTime = currentTime;
}
void Window::createSwapchain()
+1 -2
View File
@@ -111,6 +111,7 @@ void Material::compile()
{
p->data = AssetRegistry::findTexture(""); // this will return placeholder texture
}
assert(p->data != nullptr);
parameters.add(p);
}
else if(type.compare("SamplerState") == 0)
@@ -137,8 +138,6 @@ void Material::compile()
uniformBuffer = WindowManager::getGraphics()->createUniformBuffer(uniformInitializer);
}
layout->create();
descriptorSet = layout->allocatedDescriptorSet();
updateDescriptorData();
BRDF* brdf = BRDF::getBRDFByName(profile);
brdf->generateMaterialCode(codeStream, j["code"]);
codeStream << "};";
-1
View File
@@ -27,7 +27,6 @@ private:
static std::mutex shaderMapLock;
std::string materialName;
Gfx::PDescriptorLayout layout;
friend class MaterialLoader;
friend class MaterialInstance;
};
+2
View File
@@ -31,6 +31,8 @@ void MaterialAsset::endFrame()
void MaterialAsset::updateDescriptorData()
{
layout->reset();
descriptorSet = layout->allocatedDescriptorSet();
BulkResourceData uniformUpdate;
uniformUpdate.size = uniformDataSize;
uniformUpdate.data = uniformData;
+3
View File
@@ -24,11 +24,14 @@ public:
// This needs to be called while the descriptorset is unused
void updateDescriptorData();
void resetDescriptorSet();
const Gfx::PDescriptorSet getDescriptor() const;
protected:
//For now its simply the collection of parameters, since there is no point for expressions
Array<PShaderParameter> parameters;
Gfx::PDescriptorSet descriptorSet;
Gfx::PDescriptorLayout layout;
Gfx::PUniformBuffer uniformBuffer;
uint32 uniformDataSize;
uint8* uniformData;
-1
View File
@@ -34,7 +34,6 @@ void MaterialInstance::load()
uniformInitializer.resourceData.size = baseMaterial->uniformDataSize;
uniformInitializer.resourceData.data = nullptr;
uniformBuffer = WindowManager::getGraphics()->createUniformBuffer(uniformInitializer);
descriptorSet = baseMaterial->layout->allocatedDescriptorSet();
}
const Material* MaterialInstance::getRenderMaterial() const
+16
View File
@@ -3,6 +3,22 @@
using namespace Seele;
std::ostream& Seele::operator<<(std::ostream& stream, const Vector2& vector)
{
stream << "(" << vector.x << ", " << vector.y << ")";
return stream;
}
std::ostream& Seele::operator<<(std::ostream& stream, const Vector& vector)
{
stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")";
return stream;
}
std::ostream& Seele::operator<<(std::ostream& stream, const Vector4& vector)
{
stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << ")";
return stream;
}
Vector Seele::parseVector(const char* str)
{
//regex pattern consisting of 'float3(xComp, yComp, zComp)', more also matches for invalid floats, but that will throw later
+4
View File
@@ -22,6 +22,10 @@ typedef glm::quat Quaternion;
Vector parseVector(const char*);
std::ostream& operator<<(std::ostream& stream, const Vector2& vector);
std::ostream& operator<<(std::ostream& stream, const Vector& vector);
std::ostream& operator<<(std::ostream& stream, const Vector4& vector);
static inline float square(float x)
{
return x * x;
+8 -4
View File
@@ -37,7 +37,6 @@ public:
RefObject(T *ptr)
: handle(ptr), refCount(1)
{
std::scoped_lock lock(registeredObjectsLock);
registeredObjects[ptr] = this;
}
inline RefObject(const RefObject &rhs)
@@ -54,7 +53,9 @@ public:
std::scoped_lock lock(registeredObjectsLock);
registeredObjects.erase(handle);
}
#pragma warning( disable: 4150)
delete handle;
#pragma warning( default: 4150)
}
RefObject &operator=(const RefObject &rhs)
{
@@ -126,13 +127,16 @@ public:
{
std::unique_lock l(registeredObjectsLock);
auto registeredObj = registeredObjects.find(ptr);
l.unlock();
if (registeredObj == registeredObjects.end())
// get here for thread safetly
auto registeredEnd = registeredObjects.end();
if (registeredObj == registeredEnd)
{
object = new RefObject<T>(ptr);
l.unlock();
}
else
{
{
l.unlock();
object = (RefObject<T> *)registeredObj->value;
object->addRef();
}
+3 -1
View File
@@ -1,7 +1,9 @@
target_sources(SeeleEngine
PRIVATE
Scene.cpp
Scene.h)
Scene.h
SceneUpdater.h
SceneUpdater.cpp)
add_subdirectory(Actor/)
add_subdirectory(Components/)
+8 -4
View File
@@ -11,7 +11,7 @@ Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics)
{
lightEnv.directionalLights[0].color = Vector4(1, 0, 0, 1);
lightEnv.directionalLights[0].direction = Vector4(1, 1, 0, 1);
lightEnv.directionalLights[0].direction = Vector4(0, 0, 0, 1);
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 1;
lightEnv.numPointLights = 0;
@@ -22,11 +22,15 @@ Scene::~Scene()
{
}
void Scene::tick(float deltaTime)
void Scene::tick(double deltaTime)
{
lightEnv.directionalLights[0].direction.x += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
lightEnv.directionalLights[0].direction.y += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
lightEnv.directionalLights[0].direction.z += ((rand() / (double)RAND_MAX) - 0.5f) * 100.f * deltaTime;
std::cout << lightEnv.directionalLights[0].direction << std::endl;
for (auto actor : rootActors)
{
actor->tick(deltaTime);
actor->tick(static_cast<float>(deltaTime));
}
}
@@ -39,7 +43,7 @@ void Scene::addActor(PActor actor)
void Scene::addPrimitiveComponent(PPrimitiveComponent comp)
{
primitives.add(comp);
for(auto& batch : comp->staticMeshes)
for(auto& batch : comp->getStaticMeshes())
{
PrimitiveUniformBuffer data;
data.actorWorldPosition = Vector4(comp->getTransform().getPosition(), 1);
+1 -1
View File
@@ -39,7 +39,7 @@ class Scene
public:
Scene(Gfx::PGraphics graphics);
~Scene();
void tick(float deltaTime);
void tick(double deltaTime);
void addActor(PActor actor);
void addPrimitiveComponent(PPrimitiveComponent comp);
View File
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include "Containers/Array.h"
namespace Seele
{
class SceneUpdater
{
public:
SceneUpdater();
~SceneUpdater();
private:
Array<std::thread> workers;
List<std::function<void(float)> pendingUpdates;
void work();
};
} // namespace Seele
+1 -1
View File
@@ -23,7 +23,7 @@ Seele::SceneView::~SceneView()
void SceneView::beginFrame()
{
View::beginFrame();
scene->tick(0);//TODO: update in separate thread
scene->tick(Gfx::currentFrameDelta);//TODO: update in separate thread
}
void SceneView::keyCallback(KeyCode code, InputAction action, KeyModifier)
+1 -1
View File
@@ -9,7 +9,7 @@ class SceneView : public View
public:
SceneView(Gfx::PGraphics graphics, PWindow owner, const ViewportCreateInfo &createInfo);
~SceneView();
virtual void beginFrame();
virtual void beginFrame() override;
PScene getScene() const { return scene; }
private:
PScene scene;