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
|
/* This file is part of the sirit project.
* Copyright (c) 2018 ReinUsesLisp
* This software may be used and distributed according to the terms of the GNU
* Lesser General Public License version 3 or any later version.
*/
#include "sirit/sirit.h"
#include "common_types.h"
#include "op.h"
#include "stream.h"
#include <algorithm>
#include <cassert>
namespace Sirit {
template <typename T> static void WriteSet(Stream& stream, const T& set) {
for (const auto& item : set) {
item->Write(stream);
}
}
Module::Module(u32 version) : version(version) {}
Module::~Module() = default;
std::vector<u8> Module::Assemble() const {
std::vector<u8> bytes;
Stream stream{bytes};
stream.Write(spv::MagicNumber);
stream.Write(version);
stream.Write(GENERATOR_MAGIC_NUMBER);
stream.Write(bound);
stream.Write(static_cast<u32>(0));
for (const auto capability : capabilities) {
Op op(spv::Op::OpCapability);
op.Add(static_cast<u32>(capability));
op.Write(stream);
}
for (const auto& extension_name : extensions) {
Op op(spv::Op::OpExtension);
op.Add(extension_name);
op.Write(stream);
}
if (glsl_std_450) {
glsl_std_450->Write(stream);
}
Op memory_model_ref{spv::Op::OpMemoryModel};
memory_model_ref.Add(static_cast<u32>(addressing_model));
memory_model_ref.Add(static_cast<u32>(memory_model));
memory_model_ref.Write(stream);
WriteSet(stream, entry_points);
WriteSet(stream, execution_modes);
WriteSet(stream, debug);
WriteSet(stream, annotations);
WriteSet(stream, declarations);
WriteSet(stream, global_variables);
WriteSet(stream, code);
return bytes;
}
void Module::AddExtension(const std::string& extension_name) {
extensions.insert(extension_name);
}
void Module::AddCapability(spv::Capability capability) {
capabilities.insert(capability);
}
void Module::SetMemoryModel(spv::AddressingModel addressing_model,
spv::MemoryModel memory_model) {
this->addressing_model = addressing_model;
this->memory_model = memory_model;
}
void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point,
const std::string& name,
const std::vector<Id>& interfaces) {
auto op{std::make_unique<Op>(spv::Op::OpEntryPoint)};
op->Add(static_cast<u32>(execution_model));
op->Add(entry_point);
op->Add(name);
op->Add(interfaces);
entry_points.push_back(std::move(op));
}
void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode,
const std::vector<Literal>& literals) {
auto op{std::make_unique<Op>(spv::Op::OpExecutionMode)};
op->Add(entry_point);
op->Add(static_cast<u32>(mode));
op->Add(literals);
execution_modes.push_back(std::move(op));
}
Id Module::Emit(Id op) {
code.push_back(op);
return op;
}
Id Module::AddGlobalVariable(Id variable) {
assert(variable);
global_variables.push_back(variable);
return variable;
}
Id Module::AddCode(std::unique_ptr<Op> op) {
const auto id = op.get();
code_store.push_back(std::move(op));
return id;
}
Id Module::AddCode(spv::Op opcode, std::optional<u32> id) {
return AddCode(std::make_unique<Op>(opcode, id));
}
Id Module::AddDeclaration(std::unique_ptr<Op> op) {
const auto& found{
std::find_if(declarations.begin(), declarations.end(),
[&op](const auto& other) { return *other == *op; })};
if (found != declarations.end()) {
return found->get();
}
const auto id = op.get();
declarations.push_back(std::move(op));
bound++;
return id;
}
void Module::AddAnnotation(std::unique_ptr<Op> op) {
const auto id = op.get();
annotations.push_back(std::move(op));
}
Id Module::GetGLSLstd450() {
if (!glsl_std_450) {
glsl_std_450 = std::make_unique<Op>(spv::Op::OpExtInstImport, bound++);
glsl_std_450->Add("GLSL.std.450");
}
return glsl_std_450.get();
}
} // namespace Sirit
|