Files
Seele/src/Engine/Graphics/Vulkan/Command.h
T

154 lines
5.0 KiB
C++
Raw Normal View History

2020-03-24 21:05:32 +01:00
#pragma once
2023-10-26 18:37:29 +02:00
#include "Buffer.h"
2024-04-10 10:23:06 +02:00
#include "Graphics/Command.h"
#include "Queue.h"
2024-01-16 19:24:49 +01:00
#include <thread>
2024-04-10 10:23:06 +02:00
namespace Seele {
namespace Vulkan {
2021-04-01 16:40:14 +02:00
DECLARE_REF(RenderPass)
DECLARE_REF(Framebuffer)
DECLARE_REF(RenderCommand)
DECLARE_REF(ComputeCommand)
DECLARE_REF(DescriptorSet)
2023-11-15 00:06:00 +01:00
DECLARE_REF(CommandPool)
2024-04-10 10:23:06 +02:00
class Command {
2020-04-12 15:47:19 +02:00
public:
2024-04-10 10:23:06 +02:00
Command(PGraphics graphics, PCommandPool pool);
~Command();
constexpr VkCommandBuffer getHandle() { return handle; }
void reset();
void begin();
void end();
void beginRenderPass(PRenderPass renderPass, PFramebuffer framebuffer);
void endRenderPass();
2024-04-12 09:27:30 +02:00
void executeCommands(Array<Gfx::ORenderCommand> secondaryCommands);
void executeCommands(Array<Gfx::OComputeCommand> secondaryCommands);
2024-04-10 10:23:06 +02:00
void waitForSemaphore(VkPipelineStageFlags stages, PSemaphore waitSemaphore);
void checkFence();
void waitForCommand(uint32 timeToWait = 1000000u);
PFence getFence();
PCommandPool getPool();
enum State {
Init,
Begin,
RenderPass,
End,
Submit,
};
2020-04-12 15:47:19 +02:00
private:
2024-04-10 10:23:06 +02:00
PGraphics graphics;
PCommandPool pool;
OFence fence;
OSemaphore signalSemaphore;
State state;
VkViewport currentViewport;
VkRect2D currentScissor;
VkCommandBuffer handle;
PRenderPass boundRenderPass;
PFramebuffer boundFramebuffer;
Array<PSemaphore> waitSemaphores;
Array<VkPipelineStageFlags> waitFlags;
2024-04-12 09:27:30 +02:00
Array<ORenderCommand> executingRenders;
Array<OComputeCommand> executingComputes;
2024-04-10 10:23:06 +02:00
Array<PDescriptorSet> boundDescriptors;
friend class RenderCommand;
friend class CommandPool;
friend class Queue;
2020-04-12 15:47:19 +02:00
};
2023-11-15 00:06:00 +01:00
DEFINE_REF(Command)
2020-04-12 15:47:19 +02:00
2021-04-01 16:40:14 +02:00
DECLARE_REF(GraphicsPipeline)
2021-05-10 23:57:55 +02:00
DECLARE_REF(ComputePipeline)
2024-04-10 10:23:06 +02:00
class RenderCommand : public Gfx::RenderCommand {
2021-05-10 23:57:55 +02:00
public:
2024-04-10 10:23:06 +02:00
RenderCommand(PGraphics graphics, VkCommandPool cmdPool);
virtual ~RenderCommand();
constexpr VkCommandBuffer getHandle() { return handle; }
void begin(PRenderPass renderPass, PFramebuffer framebuffer);
void end();
void reset();
bool isReady();
virtual void setViewport(Gfx::PViewport viewport) override;
virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override;
2024-04-23 08:11:44 +02:00
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) override;
virtual void bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) override;
2024-04-10 10:23:06 +02:00
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override;
2024-04-23 08:11:44 +02:00
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset,
2024-04-10 10:23:06 +02:00
uint32 firstInstance) override;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override;
2020-04-12 15:47:19 +02:00
private:
2024-04-10 10:23:06 +02:00
PGraphicsPipeline pipeline;
bool ready;
Array<PDescriptorSet> boundDescriptors;
VkViewport currentViewport;
VkRect2D currentScissor;
PGraphics graphics;
std::thread::id threadId;
VkCommandBuffer handle;
VkCommandPool owner;
friend class Command;
2020-04-12 15:47:19 +02:00
};
2021-05-10 23:57:55 +02:00
DEFINE_REF(RenderCommand)
2020-04-12 15:47:19 +02:00
2024-04-10 10:23:06 +02:00
class ComputeCommand : public Gfx::ComputeCommand {
2021-05-10 23:57:55 +02:00
public:
2024-04-10 10:23:06 +02:00
ComputeCommand(PGraphics graphics, VkCommandPool cmdPool);
virtual ~ComputeCommand();
inline VkCommandBuffer getHandle() { return handle; }
void begin();
void end();
void reset();
bool isReady();
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set) override;
2024-04-23 08:11:44 +02:00
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
2020-04-12 15:47:19 +02:00
private:
2024-04-10 10:23:06 +02:00
PComputePipeline pipeline;
bool ready;
2024-04-12 09:27:30 +02:00
Array<PDescriptorSet> boundDescriptors;
2024-04-10 10:23:06 +02:00
VkViewport currentViewport;
VkRect2D currentScissor;
PGraphics graphics;
std::thread::id threadId;
VkCommandBuffer handle;
VkCommandPool owner;
friend class Command;
};
DEFINE_REF(ComputeCommand)
class CommandPool {
public:
CommandPool(PGraphics graphics, PQueue queue);
virtual ~CommandPool();
constexpr PQueue getQueue() const { return queue; }
PCommand getCommands();
void cacheCommands(Array<ORenderCommand> commands);
void cacheCommands(Array<OComputeCommand> commands);
2024-04-23 08:11:44 +02:00
ORenderCommand createRenderCommand(const std::string& name);
OComputeCommand createComputeCommand(const std::string& name);
2024-04-10 10:23:06 +02:00
constexpr VkCommandPool getHandle() const { return commandPool; }
void submitCommands(PSemaphore signalSemaphore = nullptr);
private:
PGraphics graphics;
VkCommandPool commandPool;
PQueue queue;
uint32 queueFamilyIndex;
PCommand command;
Array<OCommand> allocatedBuffers;
Array<ORenderCommand> allocatedRenderCommands;
Array<OComputeCommand> allocatedComputeCommands;
2020-04-12 15:47:19 +02:00
};
2023-11-15 00:06:00 +01:00
DEFINE_REF(CommandPool)
2020-04-12 15:47:19 +02:00
} // namespace Vulkan
} // namespace Seele