#include <Vk/Framebuffer/Framebuffer.hpp>
#include <Utilities/Utilities.hpp>

Vk::Framebuffer::Framebuffer(
    std::shared_ptr<Vk::Device> device,
    std::shared_ptr<Vk::RenderPass> renderPass,
    std::vector<std::shared_ptr<Vk::ImageView>> attachments,
    int width, int height
) :
    m_device(device),
    m_renderPass(renderPass),
    m_attachments(attachments)
{
    std::vector<VkImageView> innerImageViews;
    for (std::shared_ptr<Vk::ImageView> attachment : attachments)
    {
        innerImageViews.push_back(attachment->inner());
    }

    VkFramebufferCreateInfo framebufferInfo{};
    framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    framebufferInfo.renderPass = renderPass->inner();
    framebufferInfo.attachmentCount = innerImageViews.size();
    framebufferInfo.pAttachments = innerImageViews.data();
    framebufferInfo.width = width;
    framebufferInfo.height = height;
    framebufferInfo.layers = 1;

    if (vkCreateFramebuffer(device->inner(), &framebufferInfo, nullptr, &m_framebuffer) != VK_SUCCESS)
    {
        crash("Failed to create framebuffer!");
    }
}

Vk::Framebuffer::~Framebuffer()
{
    vkDestroyFramebuffer(m_device->inner(), m_framebuffer, nullptr);
}

VkFramebuffer Vk::Framebuffer::inner()
{
    return m_framebuffer;
}