Removing multiple inheritance from cmd buffers

This commit is contained in:
Dynamitos
2021-10-19 11:00:39 +02:00
parent 1e742c1f45
commit a0693daa67
14 changed files with 131 additions and 121 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ void MeshAsset::load()
void MeshAsset::addMesh(PMesh mesh)
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
meshes.add(mesh);
referencedMaterials.add(mesh->referencedMaterial);
}
+2 -2
View File
@@ -14,12 +14,12 @@ public:
virtual void load() override;
void setTexture(Gfx::PTexture texture)
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
this->texture = texture;
}
Gfx::PTexture getTexture()
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
return texture;
}
private:
+1 -1
View File
@@ -162,7 +162,7 @@ typedef uint32 KeyModifierFlags;
namespace Gfx
{
static constexpr bool useAsyncCompute = true;
static constexpr bool waitIdleOnSubmit = false;
static constexpr bool waitIdleOnSubmit = true;
static constexpr uint32 numFramesBuffered = 8;
extern uint32 currentFrameIndex;
extern double currentFrameDelta;
+1 -1
View File
@@ -276,7 +276,7 @@ static Map<uint32, PVertexDeclaration> vertexDeclarationCache;
PVertexDeclaration VertexDeclaration::createDeclaration(PGraphics graphics, const Array<VertexElement>& elementList)
{
std::scoped_lock lock(vertexDeclarationLock);
std::unique_lock lock(vertexDeclarationLock);
boost::crc_32_type result;
result.process_bytes(&elementList, sizeof(VertexElement) * elementList.size());
uint32 key = result.checksum();
@@ -74,7 +74,7 @@ Allocation::~Allocation()
PSubAllocation Allocation::getSuballocation(VkDeviceSize requestedSize, VkDeviceSize alignment)
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
if (isDedicated)
{
if (activeAllocations.empty() && requestedSize == bytesAllocated)
@@ -131,7 +131,7 @@ void Allocation::markFree(SubAllocation *allocation)
VkDeviceSize lowerBound = allocation->allocatedOffset;
VkDeviceSize upperBound = allocation->allocatedOffset + allocation->allocatedSize;
PSubAllocation allocHandle;
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
//Join lower bound
for (auto freeRange : freeRanges)
{
@@ -193,7 +193,7 @@ Allocator::Allocator(PGraphics graphics)
Allocator::~Allocator()
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
for (auto heap : heaps)
{
for (auto alloc : heap.allocations)
@@ -208,7 +208,7 @@ Allocator::~Allocator()
PSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2, VkMemoryPropertyFlags properties, VkMemoryDedicatedAllocateInfo *dedicatedInfo)
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
const VkMemoryRequirements &requirements = memRequirements2.memoryRequirements;
uint8 memoryTypeIndex;
VK_CHECK(findMemoryType(requirements.memoryTypeBits, properties, &memoryTypeIndex));
@@ -241,7 +241,7 @@ PSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
void Allocator::free(Allocation *allocation)
{
std::scoped_lock lck(lock);
std::unique_lock lck(lock);
for (auto heap : heaps)
{
for (uint32 i = 0; i < heap.allocations.size(); ++i)
@@ -296,7 +296,7 @@ void StagingManager::clearPending()
PStagingBuffer StagingManager::allocateStagingBuffer(uint32 size, VkBufferUsageFlags usage, bool bCPURead)
{
std::scoped_lock l(lock);
std::unique_lock l(lock);
for (auto it = freeBuffers.begin(); it != freeBuffers.end(); ++it)
{
auto freeBuffer = *it;
@@ -344,7 +344,7 @@ PStagingBuffer StagingManager::allocateStagingBuffer(uint32 size, VkBufferUsageF
void StagingManager::releaseStagingBuffer(PStagingBuffer buffer)
{
std::scoped_lock l(lock);
std::unique_lock l(lock);
freeBuffers.add(buffer);
activeBuffers.remove(activeBuffers.find(buffer.getHandle()));
}
@@ -12,22 +12,14 @@
using namespace Seele;
using namespace Seele::Vulkan;
CmdBufferBase::CmdBufferBase(PGraphics graphics, VkCommandPool cmdPool)
: graphics(graphics), owner(cmdPool)
{
}
CmdBufferBase::~CmdBufferBase()
{
graphics = nullptr;
}
CmdBuffer::CmdBuffer(PGraphics graphics, VkCommandPool cmdPool, PCommandBufferManager manager)
: CmdBufferBase(graphics, cmdPool)
: graphics(graphics)
, manager(manager)
, renderPass(nullptr)
, framebuffer(nullptr)
, subpassIndex(0)
, owner(cmdPool)
{
VkCommandBufferAllocateInfo allocInfo =
init::CommandBufferAllocateInfo(cmdPool,
@@ -174,9 +166,9 @@ PCommandBufferManager CmdBuffer::getManager()
return manager;
}
SecondaryCmdBuffer::SecondaryCmdBuffer(PGraphics graphics, VkCommandPool cmdPool)
: CmdBufferBase(graphics, cmdPool)
, ready(true)
RenderCommand::RenderCommand(PGraphics graphics, VkCommandPool cmdPool)
: graphics(graphics)
, owner(cmdPool)
{
VkCommandBufferAllocateInfo allocInfo =
init::CommandBufferAllocateInfo(cmdPool,
@@ -185,31 +177,10 @@ SecondaryCmdBuffer::SecondaryCmdBuffer(PGraphics graphics, VkCommandPool cmdPool
VK_CHECK(vkAllocateCommandBuffers(graphics->getDevice(), &allocInfo, &handle));
}
SecondaryCmdBuffer::~SecondaryCmdBuffer()
{
vkFreeCommandBuffers(graphics->getDevice(), owner, 1, &handle);
}
void SecondaryCmdBuffer::end()
{
VK_CHECK(vkEndCommandBuffer(handle));
}
void SecondaryCmdBuffer::reset()
{
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
boundDescriptors.clear();
ready = true;
}
RenderCommand::RenderCommand(PGraphics graphics, VkCommandPool cmdPool)
: SecondaryCmdBuffer(graphics, cmdPool)
{}
RenderCommand::~RenderCommand()
{
vkFreeCommandBuffers(graphics->getDevice(), owner, 1, &handle);
}
void RenderCommand::begin(PCmdBuffer parent)
@@ -227,6 +198,17 @@ void RenderCommand::begin(PCmdBuffer parent)
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
}
void RenderCommand::end()
{
VK_CHECK(vkEndCommandBuffer(handle));
}
void RenderCommand::reset()
{
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
boundDescriptors.clear();
ready = true;
}
bool RenderCommand::isReady()
{
@@ -250,7 +232,7 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
{
auto descriptor = descriptorSet.cast<DescriptorSet>();
boundDescriptors.add(descriptor.getHandle());
descriptor->bind(this);
descriptor->bind();
VkDescriptorSet setHandle = descriptor->getHandle();
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr);
@@ -261,7 +243,7 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
for(uint32 i = 0; i < descriptorSets.size(); ++i)
{
auto descriptorSet = descriptorSets[i].cast<DescriptorSet>();
descriptorSet->bind(this);
descriptorSet->bind();
boundDescriptors.add(descriptorSet.getHandle());
sets[descriptorSet->getSetIndex()] = descriptorSet->getHandle();
@@ -297,12 +279,20 @@ void RenderCommand::draw(uint32 vertexCount, uint32 instanceCount, int32 firstVe
}
ComputeCommand::ComputeCommand(PGraphics graphics, VkCommandPool cmdPool)
: SecondaryCmdBuffer(graphics, cmdPool)
: graphics(graphics)
, owner(cmdPool)
{
VkCommandBufferAllocateInfo allocInfo =
init::CommandBufferAllocateInfo(cmdPool,
VK_COMMAND_BUFFER_LEVEL_SECONDARY,
1);
VK_CHECK(vkAllocateCommandBuffers(graphics->getDevice(), &allocInfo, &handle));
}
ComputeCommand::~ComputeCommand()
{
vkFreeCommandBuffers(graphics->getDevice(), owner, 1, &handle);
}
void ComputeCommand::begin(PCmdBuffer)
@@ -319,6 +309,17 @@ void ComputeCommand::begin(PCmdBuffer)
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
}
void ComputeCommand::end()
{
VK_CHECK(vkEndCommandBuffer(handle));
}
void ComputeCommand::reset()
{
vkResetCommandBuffer(handle, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);
boundDescriptors.clear();
ready = true;
}
bool ComputeCommand::isReady()
{
return ready;
@@ -334,7 +335,7 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
{
auto descriptor = descriptorSet.cast<DescriptorSet>();
boundDescriptors.add(descriptor.getHandle());
descriptor->bind(this);
descriptor->bind();
//std::cout << "Binding descriptor " << descriptor->getHandle() << " to cmd " << handle << std::endl;
VkDescriptorSet setHandle = descriptor->getHandle();
vkCmdBindDescriptorSets(handle, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline->getLayout(), descriptorSet->getSetIndex(), 1, &setHandle, 0, nullptr);
@@ -347,7 +348,7 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
{
auto descriptorSet = descriptorSets[i].cast<DescriptorSet>();
boundDescriptors.add(descriptorSet.getHandle());
descriptorSet->bind(this);
descriptorSet->bind();
//std::cout << "Binding descriptor " << descriptorSet->getHandle() << " to cmd " << handle << std::endl;
sets[descriptorSet->getSetIndex()] = descriptorSet->getHandle();
}
@@ -390,7 +391,7 @@ PCmdBuffer CommandBufferManager::getCommands()
PRenderCommand CommandBufferManager::createRenderCommand(const std::string& name)
{
std::scoped_lock lck(allocatedRenderLock);
std::unique_lock lck(allocatedRenderLock);
for (uint32 i = 0; i < allocatedRenderCommands.size(); ++i)
{
PRenderCommand cmdBuffer = allocatedRenderCommands[i];
@@ -402,18 +403,18 @@ PRenderCommand CommandBufferManager::createRenderCommand(const std::string& name
}
PRenderCommand result = new RenderCommand(graphics, commandPool);
result->name = name;
allocatedRenderCommands.add(result);
result->begin(activeCmdBuffer);
allocatedRenderCommands.add(result);
return result;
}
PComputeCommand CommandBufferManager::createComputeCommand(const std::string& name)
{
std::scoped_lock lck(allocatedComputeLock);
std::unique_lock lck(allocatedComputeLock);
for (uint32 i = 0; i < allocatedComputeCommands.size(); ++i)
{
PComputeCommand cmdBuffer = allocatedComputeCommands[i];
if (cmdBuffer->ready)
if (cmdBuffer->isReady())
{
cmdBuffer->begin(activeCmdBuffer);
return cmdBuffer;
@@ -421,8 +422,8 @@ PComputeCommand CommandBufferManager::createComputeCommand(const std::string& na
}
PComputeCommand result = new ComputeCommand(graphics, commandPool);
result->name = name;
allocatedComputeCommands.add(result);
result->begin(activeCmdBuffer);
allocatedComputeCommands.add(result);
return result;
}
@@ -445,7 +446,7 @@ void CommandBufferManager::submitCommands(PSemaphore signalSemaphore)
queue->submitCommandBuffer(activeCmdBuffer);
}
}
std::scoped_lock lock(allocatedBufferLock);
std::unique_lock lock(allocatedBufferLock);
for (uint32 i = 0; i < allocatedBuffers.size(); ++i)
{
PCmdBuffer cmdBuffer = allocatedBuffers[i];
@@ -9,35 +9,20 @@ namespace Vulkan
{
DECLARE_REF(RenderPass)
DECLARE_REF(Framebuffer)
class CmdBufferBase
DECLARE_REF(RenderCommand)
DECLARE_REF(ComputeCommand)
DECLARE_REF(DescriptorSet)
DECLARE_REF(CommandBufferManager)
class CmdBuffer
{
public:
CmdBufferBase(PGraphics graphics, VkCommandPool cmdPool);
virtual ~CmdBufferBase();
CmdBuffer(PGraphics graphics, VkCommandPool cmdPool, PCommandBufferManager manager);
virtual ~CmdBuffer();
inline VkCommandBuffer getHandle()
{
return handle;
}
void reset();
VkViewport currentViewport;
VkRect2D currentScissor;
protected:
PGraphics graphics;
VkCommandBuffer handle;
VkCommandPool owner;
};
DEFINE_REF(CmdBufferBase)
DECLARE_REF(RenderCommand)
DECLARE_REF(ComputeCommand)
DECLARE_REF(DescriptorSet)
DECLARE_REF(CommandBufferManager)
class CmdBuffer : public CmdBufferBase
{
public:
CmdBuffer(PGraphics graphics, VkCommandPool cmdPool, PCommandBufferManager manager);
virtual ~CmdBuffer();
void begin();
void end();
void beginRenderPass(PRenderPass renderPass, PFramebuffer framebuffer);
@@ -59,12 +44,17 @@ public:
};
private:
PGraphics graphics;
PCommandBufferManager manager;
PRenderPass renderPass;
PFramebuffer framebuffer;
PFence fence;
uint32 subpassIndex;
State state;
VkViewport currentViewport;
VkRect2D currentScissor;
VkCommandBuffer handle;
VkCommandPool owner;
Array<PSemaphore> waitSemaphores;
Array<VkPipelineStageFlags> waitFlags;
Array<PRenderCommand> executingRenders;
@@ -78,29 +68,18 @@ DEFINE_REF(CmdBuffer)
DECLARE_REF(GraphicsPipeline)
DECLARE_REF(ComputePipeline)
class SecondaryCmdBuffer: public CmdBufferBase
{
public:
SecondaryCmdBuffer(PGraphics graphics, VkCommandPool cmdPool);
virtual ~SecondaryCmdBuffer();
virtual void begin(PCmdBuffer parent) = 0;
void end();
void reset();
bool ready;
protected:
Array<DescriptorSet*> boundDescriptors;
friend class CmdBuffer;
};
DEFINE_REF(SecondaryCmdBuffer);
class RenderCommand : public Gfx::RenderCommand, public SecondaryCmdBuffer
class RenderCommand : public Gfx::RenderCommand
{
public:
RenderCommand(PGraphics graphics, VkCommandPool cmdPool);
virtual ~RenderCommand();
virtual void begin(PCmdBuffer parent) override;
inline VkCommandBuffer getHandle()
{
return handle;
}
void begin(PCmdBuffer parent);
void end();
void reset();
virtual bool isReady() override;
virtual void setViewport(Gfx::PViewport viewport) override;
virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override;
@@ -112,16 +91,29 @@ public:
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
private:
PGraphicsPipeline pipeline;
bool ready;
Array<DescriptorSet*> boundDescriptors;
VkViewport currentViewport;
VkRect2D currentScissor;
PGraphics graphics;
VkCommandBuffer handle;
VkCommandPool owner;
friend class CmdBuffer;
};
DEFINE_REF(RenderCommand)
class ComputeCommand : public Gfx::ComputeCommand, public SecondaryCmdBuffer
class ComputeCommand : public Gfx::ComputeCommand
{
public:
ComputeCommand(PGraphics graphics, VkCommandPool cmdPool);
virtual ~ComputeCommand();
virtual void begin(PCmdBuffer parent) override;
inline VkCommandBuffer getHandle()
{
return handle;
}
void begin(PCmdBuffer parent);
void end();
void reset();
virtual bool isReady() override;
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set) override;
@@ -129,6 +121,13 @@ public:
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
private:
PComputePipeline pipeline;
bool ready;
Array<DescriptorSet*> boundDescriptors;
VkViewport currentViewport;
VkRect2D currentScissor;
PGraphics graphics;
VkCommandBuffer handle;
VkCommandPool owner;
friend class CmdBuffer;
};
DEFINE_REF(ComputeCommand)
@@ -55,7 +55,6 @@ private:
};
DEFINE_REF(PipelineLayout)
DECLARE_REF(SecondaryCmdBuffer)
class DescriptorSet : public Gfx::DescriptorSet
{
public:
@@ -63,7 +62,6 @@ public:
: setHandle(VK_NULL_HANDLE)
, graphics(graphics)
, owner(owner)
, boundBuffer(nullptr)
, currentlyBound(false)
, currentlyInUse(false)
{
@@ -84,14 +82,12 @@ public:
{
return currentlyInUse;
}
void bind(PSecondaryCmdBuffer cmdBuffer)
void bind()
{
boundBuffer = cmdBuffer;
currentlyBound = true;
}
void unbind()
{
boundBuffer = nullptr;
currentlyBound = false;
}
void allocate()
@@ -119,12 +115,10 @@ private:
VkDescriptorSet setHandle;
PGraphics graphics;
PDescriptorAllocator owner;
PSecondaryCmdBuffer boundBuffer;
bool currentlyBound;
bool currentlyInUse;
friend class DescriptorAllocator;
friend class CmdBuffer;
friend class SecondaryCmdBuffer;
};
DEFINE_REF(DescriptorSet)
@@ -309,7 +309,7 @@ PCommandBufferManager Graphics::getQueueCommands(Gfx::QueueType queueType)
std::mutex graphicsCommandLock;
PCommandBufferManager Graphics::getGraphicsCommands()
{
std::scoped_lock lock(graphicsCommandLock);
std::unique_lock lock(graphicsCommandLock);
auto id = std::this_thread::get_id();
if(graphicsCommands.find(id) == graphicsCommands.end())
{
@@ -320,7 +320,7 @@ PCommandBufferManager Graphics::getGraphicsCommands()
std::mutex computeCommandLock;
PCommandBufferManager Graphics::getComputeCommands()
{
std::scoped_lock lock(computeCommandLock);
std::unique_lock lock(computeCommandLock);
auto id = std::this_thread::get_id();
if(computeCommands.find(id) == computeCommands.end())
{
@@ -331,7 +331,7 @@ PCommandBufferManager Graphics::getComputeCommands()
std::mutex transferCommandLock;
PCommandBufferManager Graphics::getTransferCommands()
{
std::scoped_lock lock(transferCommandLock);
std::unique_lock lock(transferCommandLock);
auto id = std::this_thread::get_id();
if(transferCommands.find(id) == transferCommands.end())
{
@@ -342,7 +342,7 @@ PCommandBufferManager Graphics::getTransferCommands()
std::mutex dedicatedCommandLock;
PCommandBufferManager Graphics::getDedicatedTransferCommands()
{
std::scoped_lock lock(dedicatedCommandLock);
std::unique_lock lock(dedicatedCommandLock);
auto id = std::this_thread::get_id();
if(dedicatedTransferCommands.find(id) == dedicatedTransferCommands.end())
{
+1 -1
View File
@@ -22,7 +22,7 @@ Queue::~Queue()
void Queue::submitCommandBuffer(PCmdBuffer cmdBuffer, uint32 numSignalSemaphores, VkSemaphore *signalSemaphores)
{
std::scoped_lock lck(queueLock);
std::unique_lock lck(queueLock);
assert(cmdBuffer->state == CmdBuffer::State::Ended);
PFence fence = cmdBuffer->fence;
+3 -3
View File
@@ -50,12 +50,12 @@ public:
~RefObject()
{
{
std::scoped_lock lock(registeredObjectsLock);
std::unique_lock lock(registeredObjectsLock);
registeredObjects.erase(handle);
}
// #pragma warning( disable: 4150)
#pragma warning( disable: 4150)
delete handle;
// #pragma warning( default: 4150)
#pragma warning( default: 4150)
}
RefObject &operator=(const RefObject &rhs)
{
+13 -2
View File
@@ -16,7 +16,7 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
{
scene = new Scene(graphics);
scene->addActor(activeCamera);
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
/*AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Body_Lightmap.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Face_Diffuse.png");
AssetRegistry::importFile("/home/dynamitos/Assets/Ayaka/Avatar_Girl_Sword_Ayaka_Tex_Hair_Diffuse.png");
@@ -26,7 +26,18 @@ Seele::SceneView::SceneView(Gfx::PGraphics graphics, PWindow owner, const Viewpo
PPrimitiveComponent ayaka = new PrimitiveComponent(AssetRegistry::findMesh("Ayaka"));
ayaka->addWorldTranslation(Vector(0, 0, 100));
ayaka->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
scene->addPrimitiveComponent(ayaka);
scene->addPrimitiveComponent(ayaka);*/
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Ely\\Ely.fbx");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Cube\\cube.obj");
AssetRegistry::importFile("D:\\Private\\Programming\\Unreal Engine\\Assets\\Plane\\plane.fbx");
PPrimitiveComponent plane = new PrimitiveComponent(AssetRegistry::findMesh("plane"));
plane->setWorldScale(Vector(100, 100, 100));
PPrimitiveComponent arissa = new PrimitiveComponent(AssetRegistry::findMesh("Ely"));
arissa->addWorldTranslation(Vector(0, 0, 100));
arissa->setWorldScale(Vector(0.1f, 0.1f, 0.1f));
scene->addPrimitiveComponent(plane);
scene->addPrimitiveComponent(arissa);
PRenderGraphResources resources = new RenderGraphResources();
depthPrepass.setResources(resources);
+1 -1
View File
@@ -9,7 +9,7 @@ using namespace Seele;
int main()
{
PWindowManager windowManager = new WindowManager();
AssetRegistry::init("/home/dynamitos/TestSeeleProject");
AssetRegistry::init("D:\\Private\\Programming\\C++\\TestSeeleProject\\");
WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280;