-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathShaderModule.cpp
More file actions
320 lines (267 loc) · 11.3 KB
/
ShaderModule.cpp
File metadata and controls
320 lines (267 loc) · 11.3 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/core/Exception.h>
#include <vsg/core/compare.h>
#include <vsg/io/read.h>
#include <vsg/state/ShaderModule.h>
#include <vsg/vk/Context.h>
using namespace vsg;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ShaderCompileSettings
//
int ShaderCompileSettings::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
const auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_value(vulkanVersion, rhs.vulkanVersion))) return result;
if ((result = compare_value(clientInputVersion, rhs.clientInputVersion))) return result;
if ((result = compare_value(language, rhs.language))) return result;
if ((result = compare_value(defaultVersion, rhs.defaultVersion))) return result;
if ((result = compare_value(target, rhs.target))) return result;
if ((result = compare_value(forwardCompatible, rhs.forwardCompatible))) return result;
if ((result = compare_value(generateDebugInfo, rhs.generateDebugInfo))) return result;
if ((result = compare_value(optimize, rhs.optimize))) return result;
return compare_container(defines, rhs.defines);
}
void ShaderCompileSettings::read(Input& input)
{
input.read("vulkanVersion", vulkanVersion);
input.read("clientInputVersion", clientInputVersion);
input.readValue<int>("language", language);
input.read("defaultVersion", defaultVersion);
input.readValue<int>("target", target);
input.read("forwardCompatible", forwardCompatible);
if (input.version_greater_equal(1, 0, 4))
{
input.read("generateDebugInfo", generateDebugInfo);
}
if (input.version_greater_equal(1, 1, 11))
{
input.read("optimize", optimize);
}
input.readValues("defines", defines);
}
void ShaderCompileSettings::write(Output& output) const
{
output.write("vulkanVersion", vulkanVersion);
output.write("clientInputVersion", clientInputVersion);
output.writeValue<int>("language", language);
output.write("defaultVersion", defaultVersion);
output.writeValue<int>("target", target);
output.write("forwardCompatible", forwardCompatible);
if (output.version_greater_equal(1, 0, 4))
{
output.write("generateDebugInfo", generateDebugInfo);
}
if (output.version_greater_equal(1, 1, 11))
{
output.write("optimize", optimize);
}
output.writeValues("defines", defines);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Shader
//
ShaderModule::ShaderModule()
{
}
ShaderModule::ShaderModule(const std::string& in_source, ref_ptr<ShaderCompileSettings> in_hints) :
source(in_source),
hints(in_hints)
{
}
ShaderModule::ShaderModule(const SPIRV& in_code) :
code(in_code)
{
}
ShaderModule::ShaderModule(const std::string& in_source, const SPIRV& in_code) :
source(in_source),
code(in_code)
{
}
ShaderModule::~ShaderModule()
{
}
int ShaderModule::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
const auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_value(source, rhs.source))) return result;
if ((result = compare_pointer(hints, rhs.hints))) return result;
return compare_value(code, rhs.code);
}
void ShaderModule::read(Input& input)
{
Object::read(input);
input.readObject("hints", hints);
input.read("source", source);
code.resize(input.readValue<uint32_t>("code"));
input.read(code.size(), code.data());
}
void ShaderModule::write(Output& output) const
{
Object::write(output);
output.writeObject("hints", hints);
output.write("source", source);
output.writeValue<uint32_t>("code", code.size());
output.writePropertyName(""); // convenient way of forcing an indent to the appropriate column when writing out to ascii, doesn't require a matching readPropertyName() in ShaderModule::read(..).
output.write(code.size(), code.data());
output.writeEndOfLine();
}
void ShaderModule::compile(Context& context)
{
if (!_implementation[context.deviceID]) _implementation[context.deviceID] = Implementation::create(context.device, this);
}
ShaderModule::Implementation::Implementation(Device* device, ShaderModule* shaderModule) :
_device(device)
{
VkShaderModuleCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.flags = 0;
createInfo.codeSize = shaderModule->code.size() * sizeof(ShaderModule::SPIRV::value_type);
createInfo.pCode = shaderModule->code.data();
if (VkResult result = vkCreateShaderModule(*device, &createInfo, _device->getAllocationCallbacks(), &_shaderModule); result != VK_SUCCESS)
{
throw Exception{"Error: vsg::ShaderModule::create(...) failed to create shader module.", result};
}
if (isExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME) && device->getInstance()->getExtensions()->vkSetDebugUtilsObjectNameEXT)
{
std::string name = "A shader module";
shaderModule->getValue("DebugUtilsName", name);
std::stringstream nameStream;
nameStream << name;
if (shaderModule->hints && !shaderModule->hints->defines.empty())
{
nameStream << " with defines: ";
for (const auto& define : shaderModule->hints->defines)
{
nameStream << define << " ";
}
}
else
{
nameStream << " ";
}
nameStream << "(" << std::hex << _shaderModule << ")";
name = nameStream.str();
VkDebugUtilsObjectNameInfoEXT nameInfo = {};
nameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
nameInfo.objectType = VK_OBJECT_TYPE_SHADER_MODULE;
nameInfo.objectHandle = reinterpret_cast<uint64_t>(_shaderModule);
nameInfo.pObjectName = name.c_str();
device->getInstance()->getExtensions()->vkSetDebugUtilsObjectNameEXT(*device, &nameInfo);
}
}
ShaderModule::Implementation::~Implementation()
{
vkDestroyShaderModule(*_device, _shaderModule, _device->getAllocationCallbacks());
}
std::string vsg::insertIncludes(const std::string& source, ref_ptr<const Options> options)
{
std::string code = source;
std::string startOfIncludeMarker("// Start of include code : ");
std::string endOfIncludeMarker("// End of include code : ");
std::string failedLoadMarker("// Failed to load include code : ");
#if defined(__APPLE__)
std::string endOfLine("\r");
#elif defined(_WIN32)
std::string endOfLine("\r\n");
#else
std::string endOfLine("\n");
#endif
std::string::size_type pos = 0;
std::string::size_type pragma_pos = 0;
std::string::size_type include_pos = 0;
while ((pos != std::string::npos) && (((pragma_pos = code.find("#pragma", pos)) != std::string::npos) || (include_pos = code.find("#include", pos)) != std::string::npos))
{
pos = (pragma_pos != std::string::npos) ? pragma_pos : include_pos;
std::string::size_type start_of_pragma_line = pos;
std::string::size_type end_of_line = code.find_first_of("\n\r", pos);
if (pragma_pos != std::string::npos)
{
// we have #pragma usage so skip to the start of the first non white space
pos = code.find_first_not_of(" \t", pos + 7);
if (pos == std::string::npos) break;
// check for include part of #pragma include usage
if (code.compare(pos, 7, "include") != 0)
{
pos = end_of_line;
continue;
}
// found include entry so skip to next non white space
pos = code.find_first_not_of(" \t", pos + 7);
if (pos == std::string::npos) break;
}
else
{
// we have #include usage so skip to next non white space
pos = code.find_first_not_of(" \t", pos + 8);
if (pos == std::string::npos) break;
}
std::string::size_type num_characters = (end_of_line == std::string::npos) ? code.size() - pos : end_of_line - pos;
if (num_characters == 0) continue;
// prune trailing white space
while (num_characters > 0 && (code[pos + num_characters - 1] == ' ' || code[pos + num_characters - 1] == '\t')) --num_characters;
if (code[pos] == '\"')
{
if (code[pos + num_characters - 1] != '\"')
{
num_characters -= 1;
}
else
{
num_characters -= 2;
}
++pos;
}
std::string filename(code, pos, num_characters);
code.erase(start_of_pragma_line, (end_of_line == std::string::npos) ? code.size() - start_of_pragma_line : end_of_line - start_of_pragma_line);
pos = start_of_pragma_line;
auto includedSource = vsg::read_cast<ShaderModule>(filename, options);
if (includedSource)
{
if (!startOfIncludeMarker.empty())
{
code.insert(pos, startOfIncludeMarker);
pos += startOfIncludeMarker.size();
code.insert(pos, filename);
pos += filename.size();
code.insert(pos, endOfLine);
pos += endOfLine.size();
}
code.insert(pos, includedSource->source);
pos += includedSource->source.size();
if (!endOfIncludeMarker.empty())
{
code.insert(pos, endOfIncludeMarker);
pos += endOfIncludeMarker.size();
code.insert(pos, filename);
pos += filename.size();
code.insert(pos, endOfLine);
pos += endOfLine.size();
}
}
else
{
if (!failedLoadMarker.empty())
{
code.insert(pos, failedLoadMarker);
pos += failedLoadMarker.size();
code.insert(pos, filename);
pos += filename.size();
code.insert(pos, endOfLine);
pos += endOfLine.size();
}
}
}
return code;
}