#include "forwardpass.h"
#include "../wrapper/renderpass.h"
#include "../wrapper/image.h"
#include "../wrapper/framebuffer.h"
#include "target.h"
namespace vulkanapi {
	void ForwardPass::Record()
	{

	}
	void ForwardPass::InitTarget(Device& device, GeometryBuffer& gBuffer, RenderTarget* target)
	{
		mTarget = target;
		target->frames.reserve(target->size);
		for (int i = 0; i < target->size; i++) {
			auto outputView = target->surfaces[i]->View(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_VIEW_TYPE_2D);
			auto normalView = gBuffer.normals[i]->View(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_VIEW_TYPE_2D);
			vector<VkImageView> surfaces = { outputView, normalView };
			target->frames.emplace_back(device, target, this, surfaces);
		}
		mClearValues.resize(2);
		mClearValues[0].color = { { 0, 0, 0, 0 } };
		mClearValues[1].depthStencil = { 1, 0 };
		mPassBeginInfo.pClearValues = mClearValues.data();
		mPassBeginInfo.clearValueCount = 2;
	}
	ForwardPass* ForwardPass::MakePass(Device& device, GeometryBuffer& gBuffer, RenderTarget* target)
	{
		VkAttachmentDescription positionAttachment{};
		positionAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
		positionAttachment.format = target->surfaces[0]->Format();
		positionAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
		positionAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
		positionAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
		positionAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
		positionAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
		positionAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

		VkAttachmentDescription normalAttachment{};
		normalAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
		normalAttachment.format = gBuffer.normals[0]->Format();
		normalAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
		normalAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
		normalAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
		normalAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
		normalAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
		normalAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

		VkAttachmentReference colorAttachmentRef{};
		colorAttachmentRef.attachment = 0;
		colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

		VkSubpassDescription subpass{};
		subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
		subpass.colorAttachmentCount = 1;
		subpass.pColorAttachments = &colorAttachmentRef;

		vector<VkAttachmentDescription> attachments = { positionAttachment , normalAttachment };
		vector<VkSubpassDescription> subpasses = { subpass };
		vector<VkSubpassDependency> dependencies;
		
		auto forwad = new ForwardPass(device, "forward", attachments, subpasses, dependencies);
		forwad->InitTarget(device, gBuffer, target);
		return forwad;
	}
}