/*
	VulkanFramework
	by John Ryland
	Copyright (c) 2023
*/

////////////////////////////////////////////////////////////////////////////////////
//	Vulkan Shader

#include "VulkanShader.h"
#include "Utilities.h"

namespace Vulkan {

Shader::Shader(Device* device, const char* fileName)
    : m_owner(device)
    , m_module(nullptr)
{
    auto shaderCode = Utilities::ReadFile(fileName);
    Create((uint32_t*)shaderCode.data(), shaderCode.size());
}

Shader::Shader(Device* device, uint32_t* code, size_t size)
    : m_owner(device)
    , m_module(nullptr)
{
    Create(code, size);
}

// virtual
Shader::~Shader()
{
    Destroy();
}

// virtual
void Shader::Create(uint32_t* code, size_t size)
{
    if (!m_module)
    {
        VkShaderModuleCreateInfo shaderCreateInfo {};
        shaderCreateInfo.sType    = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
        shaderCreateInfo.codeSize = size;
        shaderCreateInfo.pCode    = code;
        m_owner->CheckResult(vkCreateShaderModule(m_owner->m_device, &shaderCreateInfo, m_owner->m_allocator, &m_module), "failed to create shader module!");
    }
}

// virtual
void Shader::Destroy()
{
    if (m_module)
    {
        vkDestroyShaderModule(m_owner->m_device, m_module, m_owner->m_allocator);
        m_module = nullptr;
    }
}

} // Vulkan namespace
