Update shader loading

This commit is contained in:
2021-01-10 23:39:13 +01:00
parent 5afb752fca
commit e90c043c6b
4 changed files with 22 additions and 10 deletions

View File

@@ -10,6 +10,8 @@
#include <Windows.h>
#else
#include <pthread.h>
#include <fstream>
#endif
namespace openVulkanoCpp
@@ -24,4 +26,16 @@ namespace openVulkanoCpp
pthread_setname_np(pthread_self(), name.c_str());
#endif
}
Array<char> Utils::ReadFile(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::ate | std::ios::binary);
if (!file.is_open()) throw std::runtime_error("Failed to open file '" + filePath + "'!");
const size_t fileSize = static_cast<size_t>(file.tellg());
Array<char> data(fileSize);
file.seekg(0);
file.read(data.Data(), fileSize);
file.close();
return data;
}
}

View File

@@ -10,6 +10,7 @@
#include <string>
#include <set>
#include <algorithm>
#include "Data/Containers/Array.hpp"
namespace openVulkanoCpp
{
@@ -124,5 +125,7 @@ namespace openVulkanoCpp
{
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}
static Array<char> ReadFile(const std::string& filePath);
};
}

View File

@@ -5,6 +5,7 @@
*/
#include "Device.hpp"
#include "Base/Utils.hpp"
namespace openVulkanoCpp::Vulkan
{
@@ -85,16 +86,10 @@ namespace openVulkanoCpp::Vulkan
device.freeCommandBuffers(graphicsCommandPool, commandBuffer);
}
vk::ShaderModule Device::CreateShaderModule(const std::string& filename)
vk::ShaderModule Device::CreateShaderModule(const std::string& filename) const
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if (!file.is_open()) throw std::runtime_error("Failed to open shader file!");
const size_t fileSize = static_cast<size_t>(file.tellg());
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
vk::ShaderModuleCreateInfo smci = { {}, buffer.size(), reinterpret_cast<const uint32_t*>(buffer.data()) };
Array<char> buffer = Utils::ReadFile(filename);
vk::ShaderModuleCreateInfo smci = { {}, buffer.Size(), reinterpret_cast<const uint32_t*>(buffer.Data()) };
return CreateShaderModule(smci);
}

View File

@@ -75,7 +75,7 @@ namespace openVulkanoCpp
void ExecuteNow(const std::function<void(const vk::CommandBuffer& commandBuffer)>& function) const;
vk::ShaderModule CreateShaderModule(const std::string& filename);
vk::ShaderModule CreateShaderModule(const std::string& filename) const;
vk::ShaderModule CreateShaderModule(vk::ShaderModuleCreateInfo& createInfo) const;