#include <PipelineStage/PipelineStage.hpp>
#include <GraphicsContext/GraphicsContext.hpp>

PipelineStage::PipelineStage(const std::string &label) : label(label)
{
    std::shared_ptr<Vk::Device> device = GraphicsContext::instance->getDevice();
    if (device->areTimerQueriesEnabled())
    {
        queryPool = std::make_unique<Vk::QueryPool>(device, 2);
    }
}

void PipelineStage::encodeInto(Vk::CommandBuffer &primary)
{
    if (queryDone)
    {
        timeTracker.submitSample(queryPool->getDeltaTimeMS(0, 1));
    }

    if (queryPool != nullptr)
    {
        primary.resetQueryPool(*queryPool, 0, 2);
        primary.writeTimestamp(*queryPool, 0);
        queryDone = true;
    }

    recordStageCommands(primary);

    if (queryPool != nullptr)
    {
        primary.writeTimestamp(*queryPool, 1);
    }
}