package net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DeviceLayers;
import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
import org.tinylog.Logger;

import java.nio.LongBuffer;

import static net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanUtils.vkCheck;
import static org.lwjgl.vulkan.VK13.*;
public class CommandPool {
    private final long VulkanCommandPool;
    public CommandPool(VulkanContext vulkanContext, int QueueFamilyIndex, boolean SupportReset){
        Logger.debug("Creating Vulkan Command Pool");
        try(var MemStack = MemoryStack.stackPush()){
            var CommandPoolInfo = VkCommandPoolCreateInfo.calloc(MemStack)
                    .sType$Default()
                    .queueFamilyIndex(QueueFamilyIndex);
            if (SupportReset){
                CommandPoolInfo.flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
            }
            LongBuffer LongPointer = MemStack.mallocLong(1);
            vkCheck(vkCreateCommandPool(vulkanContext.GetDevice().FetchVulkanDevice(), CommandPoolInfo, null, LongPointer),"Failed to create Command Pool");
            VulkanCommandPool = LongPointer.get(0);
        }
    }
    public void cleanup(VulkanContext vulkanContext){
        Logger.debug("Destroying Vulkan Command Pool");
        vkDestroyCommandPool(vulkanContext.GetDevice().FetchVulkanDevice(), VulkanCommandPool,null);
    }
    public long GetVulkanCommandPool(){
        return VulkanCommandPool;
    }
    public void Reset(VulkanContext vulkanContext){
        vkResetCommandPool(vulkanContext.GetDevice().FetchVulkanDevice(), VulkanCommandPool, 0);
    }
}
