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
+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()