Add ShaderRegistry

This commit is contained in:
2023-09-02 16:57:23 +02:00
parent 74823ad636
commit b328db0075
3 changed files with 86 additions and 12 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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"
extern "C" {
#include "Shaders.h"
}
namespace openVulkanoCpp
{
ShaderRegistry& ShaderRegistry::GetInstance()
{
static ShaderRegistry instance;
return instance;
}
ShaderRegistry::ShaderRegistry()
{
RegisterShaderTable(reinterpret_cast<const ShaderTableEntry*>(fileTable), fileTableSize);
}
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;
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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/.
*/
#pragma once
#include <string_view>
#include <map>
#include <cinttypes>
namespace openVulkanoCpp
{
struct ShaderTableEntry final
{
const char *entryName;
const unsigned char *data;
long int size;
};
class ShaderRegistry final
{
std::map<std::string_view, std::pair<const uint8_t*, size_t>> m_shaderMap;
ShaderRegistry();
public:
static ShaderRegistry& GetInstance();
void RegisterShaderTable(const ShaderTableEntry* table, uint32_t size);
void RegisterShader(std::string_view name, std::pair<const uint8_t*, size_t>);
std::pair<const uint8_t*, size_t> GetShader(std::string_view shaderName);
};
}

View File

@@ -6,9 +6,7 @@
#include "Device.hpp"
#include "Base/Utils.hpp"
extern "C" {
#include "Shader/Shaders.h"
}
#include "Shader/ShaderRegistry.hpp"
namespace openVulkanoCpp::Vulkan
{
@@ -92,18 +90,13 @@ namespace openVulkanoCpp::Vulkan
vk::ShaderModule Device::CreateShaderModule(const std::string& filename) const
{
Array<char> buffer;
void* data = nullptr;
const void* data = nullptr;
size_t size = 0;
if (Utils::StartsWith(filename, "Shader/"))
{
for(uint32_t i = 0; i < fileTableSize; i++)
{
if (Utils::EndsWith(filename, fileTable[i].entryName))
{
data = const_cast<unsigned char*>(fileTable[i].data);
size = fileTable[i].size;
}
}
auto shader = ShaderRegistry::GetInstance().GetShader(filename.substr(7));
data = shader.first;
size = shader.second;
}
if (!data)
{