-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
107 lines (97 loc) · 3.85 KB
/
Shader.cpp
File metadata and controls
107 lines (97 loc) · 3.85 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Shader.hpp"
#include <algorithm>
#include <filesystem>
Shader::Shader(
vk::raii::Device& device, const std::string& _name,
const vk::ShaderModuleCreateInfo& vertShaderCreateInfo,
const vk::ShaderModuleCreateInfo& fragShaderCreateInfo)
: name(_name), vertShaderModule(device, vertShaderCreateInfo),
fragShaderModule(device, fragShaderCreateInfo)
{
}
std::vector<Shader> LoadShaders(vk::raii::Device& device)
{
std::vector<Shader> shaders;
std::filesystem::path shader_directory =
std::filesystem::path(WORKING_DIRECTORY).append("build").append("shader");
for (std::filesystem::directory_entry entry :
std::filesystem::directory_iterator(shader_directory))
{
// shaderc::Compiler c;
if (entry.path().extension() == ".spv") // a.frag(.spv)
{
std::string name =
entry.path().stem().stem().string(); // (a).frag.spv
// check if shader already exists
if (std::find_if(
shaders.begin(), shaders.end(),
[name](const Shader& shader)
{ return name == shader.name; }) != shaders.end())
{
continue;
}
// if it doesnt look for the missing half
std::filesystem::path shaderType =
entry.path().stem().extension(); // a(.frag).spv
std::filesystem::path vertShaderPath = shader_directory;
vertShaderPath.append(name + ".vert.spv");
std::filesystem::path fragShaderPath = shader_directory;
fragShaderPath.append(name + ".frag.spv");
if (shaderType == ".vert")
{
if (!std::filesystem::exists(fragShaderPath))
{
LogWarning(fmt::format(
"Could not find fragment shader for shader {}", name));
continue;
}
}
else if (shaderType == ".frag")
{
if (!std::filesystem::exists(vertShaderPath))
{
LogWarning(fmt::format(
"Could not find vertex shader for shader {}", name));
continue;
}
}
else
{
continue;
}
std::ifstream vertShaderCodeFile(vertShaderPath, std::ios::binary);
if (!vertShaderCodeFile.is_open())
{
LogWarning(fmt::format(
"Requested vertex shader {} missing!",
vertShaderPath.string()));
}
std::ifstream fragShaderCodeFile(fragShaderPath, std::ios::binary);
if (!fragShaderCodeFile.is_open())
{
LogWarning(fmt::format(
"Requested fragment shader {} missing!",
fragShaderPath.string()));
}
const std::string vertShaderCode(
(std::istreambuf_iterator<char>(vertShaderCodeFile)),
std::istreambuf_iterator<char>());
std::string fragShaderCode(
(std::istreambuf_iterator<char>(fragShaderCodeFile)),
std::istreambuf_iterator<char>());
vk::ShaderModuleCreateInfo vertShaderCreateInfo(
{}, static_cast<uint32_t>(vertShaderCode.size()),
reinterpret_cast<const uint32_t*>(vertShaderCode.data()));
vk::ShaderModuleCreateInfo fragShaderCreateInfo(
{}, static_cast<uint32_t>(fragShaderCode.size()),
reinterpret_cast<const uint32_t*>(fragShaderCode.data()));
shaders.emplace_back(
device, name, vertShaderCreateInfo, fragShaderCreateInfo);
}
}
if (shaders.size() == 0)
{
LogWarning("No shaders constructed!");
}
return shaders;
}