Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/re-spirv/re-spirv.h
14709 views
1
//
2
// re-spirv
3
//
4
// Copyright (c) 2024 renderbag and contributors. All rights reserved.
5
// Licensed under the MIT license. See LICENSE file for details.
6
//
7
8
#pragma once
9
10
#include <cstdint>
11
#include <memory>
12
#include <vector>
13
14
namespace respv {
15
struct SpecConstant {
16
uint32_t specId = 0;
17
std::vector<uint32_t> values;
18
19
SpecConstant() {
20
// Empty constructor.
21
}
22
23
SpecConstant(uint32_t pSpecId, const std::vector<uint32_t> &pValues) {
24
specId = pSpecId;
25
values = pValues;
26
}
27
};
28
29
struct Instruction {
30
uint32_t wordIndex = UINT32_MAX;
31
uint32_t blockIndex = UINT32_MAX;
32
33
Instruction(uint32_t pWordIndex, uint32_t pBlockIndex) {
34
wordIndex = pWordIndex;
35
blockIndex = pBlockIndex;
36
}
37
};
38
39
struct Block {
40
uint32_t labelInstructionIndex = UINT32_MAX;
41
uint32_t terminatorInstructionIndex = UINT32_MAX;
42
43
Block() {
44
// Empty.
45
}
46
47
Block(uint32_t pLabelInstructionIndex, uint32_t pTerminatorInstructionIndex) {
48
labelInstructionIndex = pLabelInstructionIndex;
49
terminatorInstructionIndex = pTerminatorInstructionIndex;
50
}
51
};
52
53
struct Function {
54
uint32_t instructionIndex = UINT32_MAX;
55
uint32_t labelInstructionIndex = UINT32_MAX;
56
57
Function() {
58
// Empty.
59
}
60
61
Function(uint32_t pInstructionIndex, uint32_t pLabelInstructionIndex) {
62
instructionIndex = pInstructionIndex;
63
labelInstructionIndex = pLabelInstructionIndex;
64
}
65
};
66
67
struct Result {
68
uint32_t instructionIndex = UINT32_MAX;
69
70
Result() {
71
// Empty.
72
}
73
74
Result(uint32_t pInstructionIndex) {
75
instructionIndex = pInstructionIndex;
76
}
77
};
78
79
struct Specialization {
80
uint32_t constantInstructionIndex = UINT32_MAX;
81
uint32_t decorationInstructionIndex = UINT32_MAX;
82
83
Specialization() {
84
// Empty.
85
}
86
87
Specialization(uint32_t pConstantInstructionIndex, uint32_t pDecorationInstructionIndex) {
88
constantInstructionIndex = pConstantInstructionIndex;
89
decorationInstructionIndex = pDecorationInstructionIndex;
90
}
91
};
92
93
struct Decoration {
94
uint32_t instructionIndex = UINT32_MAX;
95
96
Decoration() {
97
// Empty.
98
}
99
100
Decoration(uint32_t pInstructionIndex) {
101
instructionIndex = pInstructionIndex;
102
}
103
};
104
105
struct Variable {
106
uint32_t instructionIndex = UINT32_MAX;
107
108
Variable() {
109
// Empty.
110
}
111
112
Variable(uint32_t pInstructionIndex) {
113
instructionIndex = pInstructionIndex;
114
}
115
};
116
117
struct AccessChain {
118
uint32_t instructionIndex = UINT32_MAX;
119
120
AccessChain() {
121
// Empty.
122
}
123
124
AccessChain(uint32_t pInstructionIndex) {
125
instructionIndex = pInstructionIndex;
126
}
127
};
128
129
struct Phi {
130
uint32_t instructionIndex = UINT32_MAX;
131
132
Phi() {
133
// Empty.
134
}
135
136
Phi(uint32_t pInstructionIndex) {
137
instructionIndex = pInstructionIndex;
138
}
139
};
140
141
struct LoopHeader {
142
uint32_t instructionIndex = UINT32_MAX;
143
uint32_t blockInstructionIndex = UINT32_MAX;
144
145
LoopHeader() {
146
// Empty.
147
}
148
149
LoopHeader(uint32_t pInstructionIndex, uint32_t pBlockInstructionIndex) {
150
instructionIndex = pInstructionIndex;
151
blockInstructionIndex = pBlockInstructionIndex;
152
}
153
};
154
155
struct ListNode {
156
uint32_t instructionIndex = UINT32_MAX;
157
uint32_t nextListIndex = UINT32_MAX;
158
159
ListNode() {
160
// Empty.
161
}
162
163
ListNode(uint32_t pInstructionIndex, uint32_t pNextListIndex) {
164
instructionIndex = pInstructionIndex;
165
nextListIndex = pNextListIndex;
166
}
167
};
168
169
struct Shader {
170
const uint32_t *extSpirvWords = nullptr;
171
size_t extSpirvWordCount = 0;
172
std::vector<uint32_t> inlinedSpirvWords;
173
std::vector<Instruction> instructions;
174
std::vector<uint32_t> instructionAdjacentListIndices;
175
std::vector<uint32_t> instructionInDegrees;
176
std::vector<uint32_t> instructionOutDegrees;
177
std::vector<uint32_t> instructionOrder;
178
std::vector<Block> blocks;
179
std::vector<uint32_t> blockPreOrderIndices;
180
std::vector<uint32_t> blockPostOrderIndices;
181
std::vector<Function> functions;
182
std::vector<uint32_t> variableOrder;
183
std::vector<Result> results;
184
std::vector<Specialization> specializations;
185
std::vector<Decoration> decorations;
186
std::vector<Phi> phis;
187
std::vector<LoopHeader> loopHeaders;
188
std::vector<ListNode> listNodes;
189
uint32_t defaultSwitchOpConstantInt = UINT32_MAX;
190
191
Shader();
192
193
// Data is only copied if pInlineFunctions is true. An extra processing pass is required if inlining is enabled.
194
// This step is usually not required unless the shader compiler has disabled optimizations.
195
Shader(const void *pData, size_t pSize, bool pInlineFunctions);
196
void clear();
197
bool checkData(const void *pData, size_t pSize);
198
bool inlineData(const void *pData, size_t pSize);
199
bool parseData(const void *pData, size_t pSize);
200
bool parse(const void *pData, size_t pSize, bool pInlineFunctions);
201
bool process(const void *pData, size_t pSize);
202
bool sort(const void *pData, size_t pSize);
203
bool empty() const;
204
};
205
206
struct Options {
207
bool removeDeadCode = true;
208
};
209
210
struct Optimizer {
211
static bool run(const Shader &pShader, const SpecConstant *pNewSpecConstants, uint32_t pNewSpecConstantCount, std::vector<uint8_t> &pOptimizedData, Options pOptions = Options());
212
};
213
};
214
215