package net.halbear.Terrain4J.EngineCore.Vulkan.Rendering;

import net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanUtils;
import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo;
import org.lwjgl.vulkan.VkVertexInputAttributeDescription;
import org.lwjgl.vulkan.VkVertexInputBindingDescription;

import static org.lwjgl.vulkan.VK13.VK_FORMAT_R32G32B32_SFLOAT;
import static org.lwjgl.vulkan.VK13.VK_VERTEX_INPUT_RATE_VERTEX;

public class VertexBufferStructure {
    public static final int TEXT_COORD_COMPONENTS = 2;
    private static final int NUMBER_OF_ATTRIBUTES = 2;
    private final int POSITION_COMPONENTS = 3;

    private final VkPipelineVertexInputStateCreateInfo VertexInput;
    private final VkVertexInputAttributeDescription.Buffer VertexInputAttributes;
    private final VkVertexInputBindingDescription.Buffer VertexInputBindings;

    public VertexBufferStructure(){
        VertexInputAttributes = VkVertexInputAttributeDescription.calloc(NUMBER_OF_ATTRIBUTES);
        VertexInputBindings = VkVertexInputBindingDescription.calloc(1);
        VertexInput = VkPipelineVertexInputStateCreateInfo.calloc();

        int i =0;
        int Offset = 0;
        //vertex position
        VertexInputAttributes.get(i)
                .binding(0)
                .location(i)
                .format(VK_FORMAT_R32G32B32_SFLOAT)
                .offset(Offset);
        i++;
        Offset += POSITION_COMPONENTS * VulkanUtils.FLOAT_SIZE;
        //texture coordinates
        VertexInputAttributes.get(i)
                .binding(0)
                .location(i)
                .format(VK_FORMAT_R32G32B32_SFLOAT)
                .offset(Offset);

        int Stride = Offset + TEXT_COORD_COMPONENTS * VulkanUtils.FLOAT_SIZE;
        VertexInputBindings.get(0)
                .binding(0)
                .stride(Stride)
                .inputRate(VK_VERTEX_INPUT_RATE_VERTEX);
        VertexInput
                .sType$Default()
                .pVertexAttributeDescriptions(VertexInputAttributes)
                .pVertexBindingDescriptions(VertexInputBindings);
    }
    public void cleanup(){
        VertexInput.free();
        VertexInputAttributes.free();
    }
    public VkPipelineVertexInputStateCreateInfo getVertexInput(){
        return VertexInput;
    }
}
