-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaders.cpp
43 lines (41 loc) · 1.34 KB
/
shaders.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Created by daily on 27-12-23.
//
#include "shaders.hpp"
namespace vkutil
{
std::vector<char> readFile(std::string filename, bool debug)
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if(debug && !file.is_open())
{
std::cout << "Failed to load \"" << filename << "\"" << std::endl;
}
size_t filesize { static_cast<size_t>(file.tellg()) };
std::vector<char> buffer(filesize);
file.seekg(0);
file.read(buffer.data(), static_cast<std::streamsize>(filesize));
file.close();
return buffer;
}
vk::ShaderModule createModule(const std::string& filename, vk::Device device, bool debug)
{
std::vector<char> sourceCode = readFile(filename, debug);
vk::ShaderModuleCreateInfo moduleInfo = {};
moduleInfo.flags = vk::ShaderModuleCreateFlags();
moduleInfo.codeSize = sourceCode.size();
moduleInfo.pCode = reinterpret_cast<const uint32_t*>(sourceCode.data());
try
{
return device.createShaderModule(moduleInfo);
}
catch(vk::SystemError &err)
{
if(debug)
{
std::cout << "Failed to create shader module for \"" << filename << "\"" << std::endl;
}
return vk::ShaderModule{};
}
}
}