#include "commandpool.h"

#include "device.h"
#include "queue.h"
#include "zlog.h"
namespace vulkanapi {
	CommandPool::CommandPool(Device& device, VkCommandPoolCreateFlags queueFlags, uint32_t queueIndex)
		:mPtr(nullptr)
		,mDevice(device)
		,mPool([=]()->CommandBuffer {
			return std::move(CommandBuffer(*this, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
		}, 256)
	{
		VkCommandPoolCreateInfo pCreateInfo{
			VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
			nullptr,
			queueFlags,
			queueIndex
		};
		vkCreateCommandPool(device.Ptr(), &pCreateInfo, nullptr, &mPtr);
	}
	VkCommandBuffer CommandPool::AllocateBuffer(VkCommandBufferLevel level)
	{
		VkCommandBufferAllocateInfo allocInfo{
			VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, //sType
			nullptr,									    //pNext
			mPtr,                                           //commandPool
			level,                                          //level 
			1,                                              //commandBufferCount 
		};
		VkCommandBuffer cmd;
		vkAllocateCommandBuffers(mDevice.Ptr(), &allocInfo, &cmd);
		return cmd;
	}
	void CommandPool::FreeBuffer(VkCommandBuffer& buf)
	{
		vkFreeCommandBuffers(mDevice.Ptr(), mPtr, 1, &buf);
		buf = nullptr;
	}
	void CommandPool::FreeFence(VkFence& fence)
	{
		vkDestroyFence(mDevice.Ptr(), fence, NULL);
		fence = nullptr;
	}
	void CommandPool::ResetFence(VkFence& fence)
	{
		vkResetFences(mDevice.Ptr(), 1, &fence);
	}
	VkFence CommandPool::AllocateFence()
	{
		return mDevice.CreateFence();
	}
}

