diff --git a/openVulkanoCpp/Shader/ShaderRegistry.cpp b/openVulkanoCpp/Shader/ShaderRegistry.cpp new file mode 100644 index 0000000..47079d8 --- /dev/null +++ b/openVulkanoCpp/Shader/ShaderRegistry.cpp @@ -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(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(table[i].size) }); + } + } + + void ShaderRegistry::RegisterShader(std::string_view name, std::pair shader) + { + m_shaderMap.emplace(name, shader); + } + + std::pair ShaderRegistry::GetShader(std::string_view shaderName) + { + auto it = m_shaderMap.find(shaderName); + if (it == m_shaderMap.end()) return { 0, 0 }; + return it->second; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/Shader/ShaderRegistry.hpp b/openVulkanoCpp/Shader/ShaderRegistry.hpp new file mode 100644 index 0000000..31efe61 --- /dev/null +++ b/openVulkanoCpp/Shader/ShaderRegistry.hpp @@ -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 +#include +#include + +namespace openVulkanoCpp +{ + struct ShaderTableEntry final + { + const char *entryName; + const unsigned char *data; + long int size; + }; + + class ShaderRegistry final + { + std::map> m_shaderMap; + + ShaderRegistry(); + + public: + static ShaderRegistry& GetInstance(); + + void RegisterShaderTable(const ShaderTableEntry* table, uint32_t size); + + void RegisterShader(std::string_view name, std::pair); + + std::pair GetShader(std::string_view shaderName); + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/Vulkan/Device.cpp b/openVulkanoCpp/Vulkan/Device.cpp index 3203fe4..3d8a4d5 100644 --- a/openVulkanoCpp/Vulkan/Device.cpp +++ b/openVulkanoCpp/Vulkan/Device.cpp @@ -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 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(fileTable[i].data); - size = fileTable[i].size; - } - } + auto shader = ShaderRegistry::GetInstance().GetShader(filename.substr(7)); + data = shader.first; + size = shader.second; } if (!data) {