#include "renderpass.h"
#include "device.h"
#include "commandbuffer.h"
#include "../pass/target.h"
#include "framebuffer.h"
namespace vulkanapi {
	RenderPass::RenderPass(Device& device, const char* name,
		vector<VkAttachmentDescription>& attachments,
		vector<VkSubpassDescription>& subpasses,
		vector<VkSubpassDependency>& dependencies)
		:mAttachments(attachments)
	{
		//pAttachments
		VkRenderPassCreateInfo create_info{
			VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
			nullptr,
			0,
			(uint32_t)attachments.size(),
			attachments.data(),
			(uint32_t)subpasses.size(),
			subpasses.data(),
			(uint32_t)dependencies.size(),
			dependencies.data()
		};
		vkCreateRenderPass(device.Ptr(), &create_info, nullptr, &mPtr);

		mPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
		mPassBeginInfo.renderPass = mPtr;
	}
	void RenderPass::BeginPass(CommandBuffer& cmd,int frame)
	{
		mPassBeginInfo.framebuffer = mTarget->frames.at(frame).Ptr();
		vkCmdBeginRenderPass(cmd.Ptr(), &mPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
	}
	void RenderPass::EndPass(CommandBuffer& cmd)
	{
		vkCmdEndRenderPass(cmd.Ptr());
	}
	void RenderPass::SetViewPort(uint32_t width, uint32_t height, int32_t xoffset, int32_t yoffset)
	{
		// 这个render area定义了shader将要加载和存储的位置
		mPassBeginInfo.renderArea.offset = { xoffset, yoffset };
		// 一般来说大小(extend)是和framebuffer的attachment一致的，如果小了会浪费，大了超出去的部分是一些未定义数值
		mPassBeginInfo.renderArea.extent = { width, height };
	}
}
