/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "ShaderRegistry.hpp"
#include <cassert>

extern "C"
{
#include "GeneratedShaderData_OpenVulkano.h"
}

namespace OpenVulkano
{
	ShaderRegistry& ShaderRegistry::GetInstance()
	{
		static ShaderRegistry instance;
		return instance;
	}

	ShaderRegistry::ShaderRegistry()
	{
		assert(std::size(entriesOpenVulkano) == std::size(entriesCountOpenVulkano));
		const unsigned int sz = std::size(entriesOpenVulkano);
		for (unsigned int i = 0; i < sz; i++)
		{
			RegisterShaderTable(reinterpret_cast<const ShaderTableEntry*>(entriesOpenVulkano[i]), *entriesCountOpenVulkano[i]);
		}
	}

	void ShaderRegistry::RegisterShaderTable(const ShaderTableEntry* table, uint32_t size)
	{
		for (uint32_t i = 0; i < size; i++)
		{
			RegisterShader(table[i].entryName, { table[i].data, static_cast<size_t>(table[i].size) });
		}
	}
	
	void ShaderRegistry::RegisterShader(std::string_view name, std::pair<const uint8_t*, size_t> shader)
	{
		m_shaderMap.emplace(name, shader);
	}

	std::pair<const uint8_t*, size_t> ShaderRegistry::GetShader(std::string_view shaderName)
	{
		auto it = m_shaderMap.find(shaderName);
		if (it == m_shaderMap.end()) return { 0, 0 };
		return it->second;
	}
}