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

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.VK10.*;

public class GuiVertexBufferStruct {

    public static final int VERTEX_SIZE = VulkanUtils.FLOAT_SIZE * 5;
    private static final int NUMBER_OF_ATTRIBUTES = 3;

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

    public GuiVertexBufferStruct(){
        VertexInputAttributes = VkVertexInputAttributeDescription.calloc(NUMBER_OF_ATTRIBUTES);
        VertexInputBindings = VkVertexInputBindingDescription.calloc(1);
        VertexInput = VkPipelineVertexInputStateCreateInfo.calloc();
        int i = 0;
        int offset = 0;
        VertexInputAttributes.get(i) // Position
                .binding(0)
                .location(i)
                .format(VK_FORMAT_R32G32_SFLOAT)
                .offset(offset);
        i++;
        offset+= VulkanUtils.FLOAT_SIZE * 2; // Texture Coords Attribute
        VertexInputAttributes.get(i)
                .binding(0)
                .location(i)
                .format(VK_FORMAT_R32G32_SFLOAT)
                .offset(offset);
        i++;
        offset+= VulkanUtils.FLOAT_SIZE * 2; // Colour Attribute
        VertexInputAttributes.get(i)
                .binding(0)
                .location(i)
                .format(VK_FORMAT_R8G8B8A8_UNORM)
                .offset(offset);

        VertexInputBindings.get(0)
                .binding(0)
                .stride(VERTEX_SIZE)
                .inputRate(VK_VERTEX_INPUT_RATE_VERTEX);
        VertexInput
                .sType(VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO)
                .pVertexBindingDescriptions(VertexInputBindings)
                .pVertexAttributeDescriptions(VertexInputAttributes);
    }

    public VkPipelineVertexInputStateCreateInfo GetVertexInput(){
        return VertexInput;
    }

    public void CleanUp(){
        VertexInputAttributes.free();
        VertexInputBindings.free();
        VertexInput.free();
    }
}
