package net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DeviceLayers.GPUSynchronisation;

import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkSemaphoreCreateInfo;
import org.tinylog.Logger;

import java.nio.LongBuffer;

import static net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanUtils.vkCheck;
import static org.lwjgl.vulkan.VK13.vkCreateSemaphore;
import static org.lwjgl.vulkan.VK13.vkDestroySemaphore;

public class Semaphore {
    private final long VulkanSemaphore;
    public Semaphore(VulkanContext vulkanContext){
        try(var MemStack = MemoryStack.stackPush()){
            var SemaphoreCreateInfo = VkSemaphoreCreateInfo.calloc(MemStack).sType$Default();
            LongBuffer LongPointers = MemStack.mallocLong(1);
            vkCheck(vkCreateSemaphore(vulkanContext.GetDevice().FetchVulkanDevice(), SemaphoreCreateInfo, null, LongPointers),"Failed to Create Semaphore");
            VulkanSemaphore = LongPointers.get(0);
        }
    }

    public void cleanup(VulkanContext vulkanContext){
        Logger.debug("Destroying Vulkan Semaphore");
        vkDestroySemaphore(vulkanContext.GetDevice().FetchVulkanDevice(), VulkanSemaphore, null);
    }
    public long GetSemaphore(){
        return VulkanSemaphore;
    }
}
