blob: d7d98273a987bcbaeac15cc0df2c6e786f2a46a9 [file] [log] [blame]
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001/*
Hans-Kristian Arntzen18c37bc2017-01-28 09:00:40 +01002 * Copyright 2015-2017 ARM Limited
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Hans-Kristian Arntzen147e53a2016-04-04 09:36:04 +020017#include "spirv_cross.hpp"
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010018#include "GLSL.std.450.h"
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +010019#include "spirv_cfg.hpp"
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010020#include <algorithm>
Hans-Kristian Arntzen5ea59bd2016-05-23 13:30:02 +020021#include <cstring>
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010022#include <utility>
23
24using namespace std;
25using namespace spv;
Hans-Kristian Arntzen147e53a2016-04-04 09:36:04 +020026using namespace spirv_cross;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010027
28#define log(...) fprintf(stderr, __VA_ARGS__)
29
Hans-Kristian Arntzenfd432f82017-03-18 10:52:41 +010030static string ensure_valid_identifier(const string &name)
31{
32 // Functions in glslangValidator are mangled with name(<mangled> stuff.
33 // Normally, we would never see '(' in any legal identifiers, so just strip them out.
34 auto str = name.substr(0, name.find('('));
35
36 for (uint32_t i = 0; i < str.size(); i++)
37 {
38 auto &c = str[i];
39
40 // _<num> variables are reserved by the internal implementation,
41 // otherwise, make sure the name is a valid identifier.
42 if (i == 0 || (str[0] == '_' && i == 1))
43 c = isalpha(c) ? c : '_';
44 else
45 c = isalnum(c) ? c : '_';
46 }
47 return str;
48}
49
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010050Instruction::Instruction(const vector<uint32_t> &spirv, uint32_t &index)
51{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020052 op = spirv[index] & 0xffff;
53 count = (spirv[index] >> 16) & 0xffff;
Hans-Kristian Arntzen416566b2016-07-08 10:47:03 +020054
55 if (count == 0)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +010056 SPIRV_CROSS_THROW("SPIR-V instructions cannot consume 0 words. Invalid SPIR-V file.");
Hans-Kristian Arntzen416566b2016-07-08 10:47:03 +020057
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020058 offset = index + 1;
59 length = count - 1;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010060
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020061 index += count;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010062
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020063 if (index > spirv.size())
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +010064 SPIRV_CROSS_THROW("SPIR-V instruction goes out of bounds.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010065}
66
67Compiler::Compiler(vector<uint32_t> ir)
68 : spirv(move(ir))
69{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020070 parse();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010071}
72
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020073string Compiler::compile()
74{
Endre Oma6ad8b302017-01-11 15:57:05 +010075 // Force a classic "C" locale, reverts when function returns
76 ClassicLocale classic_locale;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020077 return "";
78}
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010079
80bool Compiler::variable_storage_is_aliased(const SPIRVariable &v)
81{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020082 auto &type = get<SPIRType>(v.basetype);
83 bool ssbo = (meta[type.self].decoration.decoration_flags & (1ull << DecorationBufferBlock)) != 0;
84 bool image = type.basetype == SPIRType::Image;
85 bool counter = type.basetype == SPIRType::AtomicCounter;
Hans-Kristian Arntzen7d8add32016-07-12 15:00:10 +020086 bool is_restrict = (meta[v.self].decoration.decoration_flags & (1ull << DecorationRestrict)) != 0;
87 return !is_restrict && (ssbo || image || counter);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010088}
89
90bool Compiler::block_is_pure(const SPIRBlock &block)
91{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020092 for (auto &i : block.ops)
93 {
94 auto ops = stream(i);
95 auto op = static_cast<Op>(i.op);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +010096
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +020097 switch (op)
98 {
99 case OpFunctionCall:
100 {
101 uint32_t func = ops[2];
102 if (!function_is_pure(get<SPIRFunction>(func)))
103 return false;
104 break;
105 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100106
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +0100107 case OpCopyMemory:
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200108 case OpStore:
109 {
110 auto &type = expression_type(ops[0]);
111 if (type.storage != StorageClassFunction)
112 return false;
113 break;
114 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100115
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200116 case OpImageWrite:
117 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100118
Hans-Kristian Arntzen5af1a512016-05-05 09:51:42 +0200119 // Atomics are impure.
120 case OpAtomicLoad:
121 case OpAtomicStore:
122 case OpAtomicExchange:
123 case OpAtomicCompareExchange:
124 case OpAtomicIIncrement:
125 case OpAtomicIDecrement:
126 case OpAtomicIAdd:
127 case OpAtomicISub:
128 case OpAtomicSMin:
129 case OpAtomicUMin:
130 case OpAtomicSMax:
131 case OpAtomicUMax:
132 case OpAtomicAnd:
133 case OpAtomicOr:
134 case OpAtomicXor:
135 return false;
136
137 // Geometry shader builtins modify global state.
138 case OpEndPrimitive:
139 case OpEmitStreamVertex:
140 case OpEndStreamPrimitive:
141 case OpEmitVertex:
142 return false;
143
144 // Barriers disallow any reordering, so we should treat blocks with barrier as writing.
145 case OpControlBarrier:
146 case OpMemoryBarrier:
147 return false;
148
149 // OpExtInst is potentially impure depending on extension, but GLSL builtins are at least pure.
150
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200151 default:
152 break;
153 }
154 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100155
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200156 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100157}
158
Hans-Kristian Arntzen61c31c62017-03-07 13:27:04 +0100159string Compiler::to_name(uint32_t id, bool allow_alias) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100160{
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +0200161 if (allow_alias && ids.at(id).get_type() == TypeType)
162 {
163 // If this type is a simple alias, emit the
164 // name of the original type instead.
165 // We don't want to override the meta alias
166 // as that can be overridden by the reflection APIs after parse.
167 auto &type = get<SPIRType>(id);
168 if (type.type_alias)
169 return to_name(type.type_alias);
170 }
171
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200172 if (meta[id].decoration.alias.empty())
173 return join("_", id);
174 else
175 return meta.at(id).decoration.alias;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100176}
177
178bool Compiler::function_is_pure(const SPIRFunction &func)
179{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200180 for (auto block : func.blocks)
181 {
182 if (!block_is_pure(get<SPIRBlock>(block)))
183 {
184 //fprintf(stderr, "Function %s is impure!\n", to_name(func.self).c_str());
185 return false;
186 }
187 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100188
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200189 //fprintf(stderr, "Function %s is pure!\n", to_name(func.self).c_str());
190 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100191}
192
193void Compiler::register_global_read_dependencies(const SPIRBlock &block, uint32_t id)
194{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200195 for (auto &i : block.ops)
196 {
197 auto ops = stream(i);
198 auto op = static_cast<Op>(i.op);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100199
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200200 switch (op)
201 {
202 case OpFunctionCall:
203 {
204 uint32_t func = ops[2];
205 register_global_read_dependencies(get<SPIRFunction>(func), id);
206 break;
207 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100208
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200209 case OpLoad:
210 case OpImageRead:
211 {
212 // If we're in a storage class which does not get invalidated, adding dependencies here is no big deal.
213 auto *var = maybe_get_backing_variable(ops[2]);
214 if (var && var->storage != StorageClassFunction)
215 {
216 auto &type = get<SPIRType>(var->basetype);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100217
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200218 // InputTargets are immutable.
219 if (type.basetype != SPIRType::Image && type.image.dim != DimSubpassData)
220 var->dependees.push_back(id);
221 }
222 break;
223 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100224
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200225 default:
226 break;
227 }
228 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100229}
230
231void Compiler::register_global_read_dependencies(const SPIRFunction &func, uint32_t id)
232{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200233 for (auto block : func.blocks)
234 register_global_read_dependencies(get<SPIRBlock>(block), id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100235}
236
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200237SPIRVariable *Compiler::maybe_get_backing_variable(uint32_t chain)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100238{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200239 auto *var = maybe_get<SPIRVariable>(chain);
240 if (!var)
241 {
242 auto *cexpr = maybe_get<SPIRExpression>(chain);
243 if (cexpr)
244 var = maybe_get<SPIRVariable>(cexpr->loaded_from);
245 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100246
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200247 return var;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100248}
249
250void Compiler::register_read(uint32_t expr, uint32_t chain, bool forwarded)
251{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200252 auto &e = get<SPIRExpression>(expr);
253 auto *var = maybe_get_backing_variable(chain);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100254
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200255 if (var)
256 {
257 e.loaded_from = var->self;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100258
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200259 // If the backing variable is immutable, we do not need to depend on the variable.
260 if (forwarded && !is_immutable(var->self))
261 var->dependees.push_back(e.self);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100262
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200263 // If we load from a parameter, make sure we create "inout" if we also write to the parameter.
264 // The default is "in" however, so we never invalidate our compilation by reading.
265 if (var && var->parameter)
266 var->parameter->read_count++;
267 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100268}
269
270void Compiler::register_write(uint32_t chain)
271{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200272 auto *var = maybe_get<SPIRVariable>(chain);
273 if (!var)
274 {
275 // If we're storing through an access chain, invalidate the backing variable instead.
276 auto *expr = maybe_get<SPIRExpression>(chain);
277 if (expr && expr->loaded_from)
278 var = maybe_get<SPIRVariable>(expr->loaded_from);
279 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100280
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200281 if (var)
282 {
283 // If our variable is in a storage class which can alias with other buffers,
284 // invalidate all variables which depend on aliased variables.
285 if (variable_storage_is_aliased(*var))
286 flush_all_aliased_variables();
287 else if (var)
288 flush_dependees(*var);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100289
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200290 // We tried to write to a parameter which is not marked with out qualifier, force a recompile.
291 if (var->parameter && var->parameter->write_count == 0)
292 {
293 var->parameter->write_count++;
294 force_recompile = true;
295 }
296 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100297}
298
299void Compiler::flush_dependees(SPIRVariable &var)
300{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200301 for (auto expr : var.dependees)
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200302 invalid_expressions.insert(expr);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200303 var.dependees.clear();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100304}
305
306void Compiler::flush_all_aliased_variables()
307{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200308 for (auto aliased : aliased_variables)
309 flush_dependees(get<SPIRVariable>(aliased));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100310}
311
312void Compiler::flush_all_atomic_capable_variables()
313{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200314 for (auto global : global_variables)
315 flush_dependees(get<SPIRVariable>(global));
316 flush_all_aliased_variables();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100317}
318
319void Compiler::flush_all_active_variables()
320{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200321 // Invalidate all temporaries we read from variables in this block since they were forwarded.
322 // Invalidate all temporaries we read from globals.
323 for (auto &v : current_function->local_variables)
324 flush_dependees(get<SPIRVariable>(v));
325 for (auto &arg : current_function->arguments)
326 flush_dependees(get<SPIRVariable>(arg.id));
327 for (auto global : global_variables)
328 flush_dependees(get<SPIRVariable>(global));
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100329
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200330 flush_all_aliased_variables();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100331}
332
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200333const SPIRType &Compiler::expression_type(uint32_t id) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100334{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200335 switch (ids[id].get_type())
336 {
337 case TypeVariable:
338 return get<SPIRType>(get<SPIRVariable>(id).basetype);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100339
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200340 case TypeExpression:
341 return get<SPIRType>(get<SPIRExpression>(id).expression_type);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100342
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200343 case TypeConstant:
344 return get<SPIRType>(get<SPIRConstant>(id).constant_type);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100345
Hans-Kristian Arntzen7e8afa82016-10-03 15:54:02 +0200346 case TypeConstantOp:
347 return get<SPIRType>(get<SPIRConstantOp>(id).basetype);
348
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200349 case TypeUndef:
350 return get<SPIRType>(get<SPIRUndef>(id).basetype);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100351
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200352 default:
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100353 SPIRV_CROSS_THROW("Cannot resolve expression type.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200354 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100355}
356
357bool Compiler::expression_is_lvalue(uint32_t id) const
358{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200359 auto &type = expression_type(id);
360 switch (type.basetype)
361 {
362 case SPIRType::SampledImage:
363 case SPIRType::Image:
364 case SPIRType::Sampler:
365 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100366
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200367 default:
368 return true;
369 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100370}
371
372bool Compiler::is_immutable(uint32_t id) const
373{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200374 if (ids[id].get_type() == TypeVariable)
375 {
376 auto &var = get<SPIRVariable>(id);
Hans-Kristian Arntzen92134e42016-04-01 19:58:26 +0200377
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200378 // Anything we load from the UniformConstant address space is guaranteed to be immutable.
379 bool pointer_to_const = var.storage == StorageClassUniformConstant;
Hans-Kristian Arntzen36a0b632016-07-12 14:33:04 +0200380 return pointer_to_const || var.phi_variable || !expression_is_lvalue(id);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200381 }
382 else if (ids[id].get_type() == TypeExpression)
383 return get<SPIRExpression>(id).immutable;
Hans-Kristian Arntzen7e8afa82016-10-03 15:54:02 +0200384 else if (ids[id].get_type() == TypeConstant || ids[id].get_type() == TypeConstantOp ||
385 ids[id].get_type() == TypeUndef)
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200386 return true;
387 else
388 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100389}
390
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200391static inline bool storage_class_is_interface(spv::StorageClass storage)
392{
393 switch (storage)
394 {
395 case StorageClassInput:
396 case StorageClassOutput:
397 case StorageClassUniform:
398 case StorageClassUniformConstant:
399 case StorageClassAtomicCounter:
400 case StorageClassPushConstant:
401 return true;
402
403 default:
404 return false;
405 }
406}
407
408bool Compiler::is_hidden_variable(const SPIRVariable &var, bool include_builtins) const
409{
410 if ((is_builtin_variable(var) && !include_builtins) || var.remapped_variable)
411 return true;
412
Hans-Kristian Arntzen1b5ca8d2016-09-10 16:20:19 +0200413 // Combined image samplers are always considered active as they are "magic" variables.
414 if (find_if(begin(combined_image_samplers), end(combined_image_samplers), [&var](const CombinedImageSampler &samp) {
415 return samp.combined_id == var.self;
416 }) != end(combined_image_samplers))
417 {
418 return false;
419 }
420
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200421 bool hidden = false;
422 if (check_active_interface_variables && storage_class_is_interface(var.storage))
423 hidden = active_interface_variables.find(var.self) == end(active_interface_variables);
424 return hidden;
425}
426
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100427bool Compiler::is_builtin_variable(const SPIRVariable &var) const
428{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200429 if (var.compat_builtin || meta[var.self].decoration.builtin)
430 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100431
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200432 // We can have builtin structs as well. If one member of a struct is builtin, the struct must also be builtin.
433 for (auto &m : meta[get<SPIRType>(var.basetype).self].members)
434 if (m.builtin)
435 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100436
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200437 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100438}
439
440bool Compiler::is_member_builtin(const SPIRType &type, uint32_t index, BuiltIn *builtin) const
441{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200442 auto &memb = meta[type.self].members;
443 if (index < memb.size() && memb[index].builtin)
444 {
445 if (builtin)
446 *builtin = memb[index].builtin_type;
447 return true;
448 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100449
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200450 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100451}
452
Bill Hollings103aabf2016-04-06 17:42:27 -0400453bool Compiler::is_scalar(const SPIRType &type) const
454{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200455 return type.vecsize == 1 && type.columns == 1;
Bill Hollings103aabf2016-04-06 17:42:27 -0400456}
457
458bool Compiler::is_vector(const SPIRType &type) const
459{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200460 return type.vecsize > 1 && type.columns == 1;
Bill Hollings103aabf2016-04-06 17:42:27 -0400461}
462
463bool Compiler::is_matrix(const SPIRType &type) const
464{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200465 return type.vecsize > 1 && type.columns > 1;
Bill Hollings103aabf2016-04-06 17:42:27 -0400466}
467
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100468ShaderResources Compiler::get_shader_resources() const
469{
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200470 return get_shader_resources(nullptr);
471}
472
473ShaderResources Compiler::get_shader_resources(const unordered_set<uint32_t> &active_variables) const
474{
475 return get_shader_resources(&active_variables);
476}
477
478bool Compiler::InterfaceVariableAccessHandler::handle(Op opcode, const uint32_t *args, uint32_t length)
479{
480 uint32_t variable = 0;
481 switch (opcode)
482 {
483 // Need this first, otherwise, GCC complains about unhandled switch statements.
484 default:
485 break;
486
487 case OpFunctionCall:
488 {
489 // Invalid SPIR-V.
490 if (length < 3)
491 return false;
492
493 uint32_t count = length - 3;
494 args += 3;
495 for (uint32_t i = 0; i < count; i++)
496 {
497 auto *var = compiler.maybe_get<SPIRVariable>(args[i]);
498 if (var && storage_class_is_interface(var->storage))
499 variables.insert(args[i]);
500 }
501 break;
502 }
503
504 case OpAtomicStore:
505 case OpStore:
506 // Invalid SPIR-V.
507 if (length < 1)
508 return false;
509 variable = args[0];
510 break;
511
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +0100512 case OpCopyMemory:
513 {
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +0100514 if (length < 2)
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +0100515 return false;
516
517 auto *var = compiler.maybe_get<SPIRVariable>(args[0]);
518 if (var && storage_class_is_interface(var->storage))
519 variables.insert(variable);
520
521 var = compiler.maybe_get<SPIRVariable>(args[1]);
522 if (var && storage_class_is_interface(var->storage))
523 variables.insert(variable);
524 break;
525 }
526
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200527 case OpAccessChain:
528 case OpInBoundsAccessChain:
529 case OpLoad:
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +0100530 case OpCopyObject:
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200531 case OpImageTexelPointer:
532 case OpAtomicLoad:
533 case OpAtomicExchange:
534 case OpAtomicCompareExchange:
535 case OpAtomicIIncrement:
536 case OpAtomicIDecrement:
537 case OpAtomicIAdd:
538 case OpAtomicISub:
539 case OpAtomicSMin:
540 case OpAtomicUMin:
541 case OpAtomicSMax:
542 case OpAtomicUMax:
543 case OpAtomicAnd:
544 case OpAtomicOr:
545 case OpAtomicXor:
546 // Invalid SPIR-V.
547 if (length < 3)
548 return false;
549 variable = args[2];
550 break;
551 }
552
553 if (variable)
554 {
555 auto *var = compiler.maybe_get<SPIRVariable>(variable);
556 if (var && storage_class_is_interface(var->storage))
557 variables.insert(variable);
558 }
559 return true;
560}
561
562unordered_set<uint32_t> Compiler::get_active_interface_variables() const
563{
564 // Traverse the call graph and find all interface variables which are in use.
565 unordered_set<uint32_t> variables;
566 InterfaceVariableAccessHandler handler(*this, variables);
567 traverse_all_reachable_opcodes(get<SPIRFunction>(entry_point), handler);
568 return variables;
569}
570
571void Compiler::set_enabled_interface_variables(std::unordered_set<uint32_t> active_variables)
572{
573 active_interface_variables = move(active_variables);
574 check_active_interface_variables = true;
575}
576
577ShaderResources Compiler::get_shader_resources(const unordered_set<uint32_t> *active_variables) const
578{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200579 ShaderResources res;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100580
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200581 for (auto &id : ids)
582 {
583 if (id.get_type() != TypeVariable)
584 continue;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100585
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200586 auto &var = id.get<SPIRVariable>();
587 auto &type = get<SPIRType>(var.basetype);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100588
Hans-Kristian Arntzend5dc5f32016-07-05 13:21:26 +0200589 // It is possible for uniform storage classes to be passed as function parameters, so detect
590 // that. To detect function parameters, check of StorageClass of variable is function scope.
591 if (var.storage == StorageClassFunction || !type.pointer || is_builtin_variable(var))
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200592 continue;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100593
Hans-Kristian Arntzenf61a5d12016-08-26 12:58:50 +0200594 if (active_variables && active_variables->find(var.self) == end(*active_variables))
595 continue;
596
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200597 // Input
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200598 if (var.storage == StorageClassInput && interface_variable_exists_in_entry_point(var.self))
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200599 {
600 if (meta[type.self].decoration.decoration_flags & (1ull << DecorationBlock))
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200601 res.stage_inputs.push_back({ var.self, var.basetype, type.self, meta[type.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200602 else
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200603 res.stage_inputs.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200604 }
605 // Subpass inputs
606 else if (var.storage == StorageClassUniformConstant && type.image.dim == DimSubpassData)
607 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200608 res.subpass_inputs.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200609 }
610 // Outputs
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +0200611 else if (var.storage == StorageClassOutput && interface_variable_exists_in_entry_point(var.self))
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200612 {
613 if (meta[type.self].decoration.decoration_flags & (1ull << DecorationBlock))
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200614 res.stage_outputs.push_back({ var.self, var.basetype, type.self, meta[type.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200615 else
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200616 res.stage_outputs.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200617 }
618 // UBOs
619 else if (type.storage == StorageClassUniform &&
620 (meta[type.self].decoration.decoration_flags & (1ull << DecorationBlock)))
621 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200622 res.uniform_buffers.push_back({ var.self, var.basetype, type.self, meta[type.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200623 }
624 // SSBOs
625 else if (type.storage == StorageClassUniform &&
626 (meta[type.self].decoration.decoration_flags & (1ull << DecorationBufferBlock)))
627 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200628 res.storage_buffers.push_back({ var.self, var.basetype, type.self, meta[type.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200629 }
630 // Push constant blocks
631 else if (type.storage == StorageClassPushConstant)
632 {
633 // There can only be one push constant block, but keep the vector in case this restriction is lifted
634 // in the future.
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200635 res.push_constant_buffers.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200636 }
637 // Images
Hans-Kristian Arntzene9202082016-09-10 13:05:35 +0200638 else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Image &&
639 type.image.sampled == 2)
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200640 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200641 res.storage_images.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200642 }
Hans-Kristian Arntzene9202082016-09-10 13:05:35 +0200643 // Separate images
644 else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Image &&
645 type.image.sampled == 1)
646 {
647 res.separate_images.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
648 }
649 // Separate samplers
650 else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Sampler)
651 {
652 res.separate_samplers.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
653 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200654 // Textures
655 else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::SampledImage)
656 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200657 res.sampled_images.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200658 }
659 // Atomic counters
660 else if (type.storage == StorageClassAtomicCounter)
661 {
Hans-Kristian Arntzen5c24d992016-07-12 21:20:18 +0200662 res.atomic_counters.push_back({ var.self, var.basetype, type.self, meta[var.self].decoration.alias });
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200663 }
664 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100665
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200666 return res;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100667}
668
669static inline uint32_t swap_endian(uint32_t v)
670{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200671 return ((v >> 24) & 0x000000ffu) | ((v >> 8) & 0x0000ff00u) | ((v << 8) & 0x00ff0000u) | ((v << 24) & 0xff000000u);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100672}
673
674static string extract_string(const vector<uint32_t> &spirv, uint32_t offset)
675{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200676 string ret;
677 for (uint32_t i = offset; i < spirv.size(); i++)
678 {
679 uint32_t w = spirv[i];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100680
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200681 for (uint32_t j = 0; j < 4; j++, w >>= 8)
682 {
683 char c = w & 0xff;
684 if (c == '\0')
685 return ret;
686 ret += c;
687 }
688 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100689
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100690 SPIRV_CROSS_THROW("String was not terminated before EOF");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100691}
692
Hans-Kristian Arntzen45ad58a2016-05-10 23:39:41 +0200693static bool is_valid_spirv_version(uint32_t version)
694{
695 switch (version)
696 {
697 // Allow v99 since it tends to just work.
698 case 99:
699 case 0x10000: // SPIR-V 1.0
700 case 0x10100: // SPIR-V 1.1
701 return true;
702
703 default:
704 return false;
705 }
706}
707
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100708void Compiler::parse()
709{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200710 auto len = spirv.size();
711 if (len < 5)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100712 SPIRV_CROSS_THROW("SPIRV file too small.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100713
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200714 auto s = spirv.data();
Hans-Kristian Arntzen5ac88272016-04-11 13:38:18 +0200715
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200716 // Endian-swap if we need to.
717 if (s[0] == swap_endian(MagicNumber))
Hans-Kristian Arntzen5ea59bd2016-05-23 13:30:02 +0200718 transform(begin(spirv), end(spirv), begin(spirv), [](uint32_t c) { return swap_endian(c); });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100719
Hans-Kristian Arntzen45ad58a2016-05-10 23:39:41 +0200720 if (s[0] != MagicNumber || !is_valid_spirv_version(s[1]))
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100721 SPIRV_CROSS_THROW("Invalid SPIRV format.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100722
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200723 uint32_t bound = s[3];
724 ids.resize(bound);
725 meta.resize(bound);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100726
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200727 uint32_t offset = 5;
728 while (offset < len)
729 inst.emplace_back(spirv, offset);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100730
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200731 for (auto &i : inst)
732 parse(i);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100733
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200734 if (current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100735 SPIRV_CROSS_THROW("Function was not terminated.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200736 if (current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100737 SPIRV_CROSS_THROW("Block was not terminated.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100738}
739
740void Compiler::flatten_interface_block(uint32_t id)
741{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200742 auto &var = get<SPIRVariable>(id);
743 auto &type = get<SPIRType>(var.basetype);
744 auto flags = meta.at(type.self).decoration.decoration_flags;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100745
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200746 if (!type.array.empty())
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100747 SPIRV_CROSS_THROW("Type is array of UBOs.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200748 if (type.basetype != SPIRType::Struct)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100749 SPIRV_CROSS_THROW("Type is not a struct.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200750 if ((flags & (1ull << DecorationBlock)) == 0)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100751 SPIRV_CROSS_THROW("Type is not a block.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200752 if (type.member_types.empty())
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100753 SPIRV_CROSS_THROW("Member list of struct is empty.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100754
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200755 uint32_t t = type.member_types[0];
756 for (auto &m : type.member_types)
757 if (t != m)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100758 SPIRV_CROSS_THROW("Types in block differ.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100759
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200760 auto &mtype = get<SPIRType>(t);
761 if (!mtype.array.empty())
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100762 SPIRV_CROSS_THROW("Member type cannot be arrays.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200763 if (mtype.basetype == SPIRType::Struct)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +0100764 SPIRV_CROSS_THROW("Member type cannot be struct.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100765
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200766 // Inherit variable name from interface block name.
767 meta.at(var.self).decoration.alias = meta.at(type.self).decoration.alias;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100768
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200769 auto storage = var.storage;
770 if (storage == StorageClassUniform)
771 storage = StorageClassUniformConstant;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100772
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200773 // Change type definition in-place into an array instead.
774 // Access chains will still work as-is.
775 uint32_t array_size = uint32_t(type.member_types.size());
776 type = mtype;
777 type.array.push_back(array_size);
778 type.pointer = true;
779 type.storage = storage;
780 var.storage = storage;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100781}
782
783void Compiler::update_name_cache(unordered_set<string> &cache, string &name)
784{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200785 if (name.empty())
786 return;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100787
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200788 if (cache.find(name) == end(cache))
789 {
790 cache.insert(name);
791 return;
792 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100793
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200794 uint32_t counter = 0;
795 auto tmpname = name;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100796
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200797 // If there is a collision (very rare),
798 // keep tacking on extra identifier until it's unique.
799 do
800 {
801 counter++;
802 name = tmpname + "_" + convert_to_string(counter);
803 } while (cache.find(name) != end(cache));
804 cache.insert(name);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100805}
806
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200807void Compiler::set_name(uint32_t id, const std::string &name)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100808{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200809 auto &str = meta.at(id).decoration.alias;
810 str.clear();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100811
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200812 if (name.empty())
813 return;
814 // Reserved for temporaries.
Hans-Kristian Arntzenc9728942016-07-06 11:19:20 +0200815 if (name[0] == '_' && name.size() >= 2 && isdigit(name[1]))
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200816 return;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100817
Hans-Kristian Arntzenfd432f82017-03-18 10:52:41 +0100818 str = ensure_valid_identifier(name);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100819}
820
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200821const SPIRType &Compiler::get_type(uint32_t id) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100822{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200823 return get<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100824}
825
Robert Konrad451bdee2016-09-24 22:17:01 +0200826const SPIRType &Compiler::get_type_from_variable(uint32_t id) const
827{
828 return get<SPIRType>(get<SPIRVariable>(id).basetype);
829}
830
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100831void Compiler::set_member_decoration(uint32_t id, uint32_t index, Decoration decoration, uint32_t argument)
832{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200833 meta.at(id).members.resize(max(meta[id].members.size(), size_t(index) + 1));
834 auto &dec = meta.at(id).members[index];
835 dec.decoration_flags |= 1ull << decoration;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100836
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200837 switch (decoration)
838 {
839 case DecorationBuiltIn:
840 dec.builtin = true;
841 dec.builtin_type = static_cast<BuiltIn>(argument);
842 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100843
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200844 case DecorationLocation:
845 dec.location = argument;
846 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100847
Bill Hollings81757502017-01-29 13:28:20 -0500848 case DecorationBinding:
849 dec.binding = argument;
850 break;
851
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200852 case DecorationOffset:
853 dec.offset = argument;
854 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100855
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +0200856 case DecorationSpecId:
857 dec.spec_id = argument;
858 break;
859
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +0100860 case DecorationMatrixStride:
861 dec.matrix_stride = argument;
862 break;
863
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200864 default:
865 break;
866 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100867}
868
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200869void Compiler::set_member_name(uint32_t id, uint32_t index, const std::string &name)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100870{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200871 meta.at(id).members.resize(max(meta[id].members.size(), size_t(index) + 1));
Hans-Kristian Arntzenfd432f82017-03-18 10:52:41 +0100872
873 auto &str = meta.at(id).members[index].alias;
874 str.clear();
875 if (name.empty())
876 return;
877
878 // Reserved for unnamed members.
879 if (name[0] == '_' && name.size() >= 2 && isdigit(name[1]))
880 return;
881
882 str = ensure_valid_identifier(name);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100883}
884
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200885const std::string &Compiler::get_member_name(uint32_t id, uint32_t index) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100886{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200887 auto &m = meta.at(id);
888 if (index >= m.members.size())
889 {
890 static string empty;
891 return empty;
892 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100893
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200894 return m.members[index].alias;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100895}
896
Bill Hollingsc45e74f2016-07-08 12:39:22 -0400897void Compiler::set_member_qualified_name(uint32_t id, uint32_t index, const std::string &name)
898{
Bill Hollingsac00c602016-10-24 09:24:24 -0400899 meta.at(id).members.resize(max(meta[id].members.size(), size_t(index) + 1));
900 meta.at(id).members[index].qualified_alias = name;
Bill Hollingsc45e74f2016-07-08 12:39:22 -0400901}
902
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100903uint32_t Compiler::get_member_decoration(uint32_t id, uint32_t index, Decoration decoration) const
904{
Hans-Kristian Arntzen15679c72016-08-17 11:35:34 +0200905 auto &m = meta.at(id);
906 if (index >= m.members.size())
907 return 0;
908
909 auto &dec = m.members[index];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200910 if (!(dec.decoration_flags & (1ull << decoration)))
911 return 0;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100912
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200913 switch (decoration)
914 {
915 case DecorationBuiltIn:
916 return dec.builtin_type;
917 case DecorationLocation:
918 return dec.location;
Bill Hollings81757502017-01-29 13:28:20 -0500919 case DecorationBinding:
920 return dec.binding;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200921 case DecorationOffset:
922 return dec.offset;
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +0200923 case DecorationSpecId:
924 return dec.spec_id;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200925 default:
Graham Wihlidal9b1ee8f2017-01-05 21:01:49 +0100926 return 1;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200927 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100928}
929
930uint64_t Compiler::get_member_decoration_mask(uint32_t id, uint32_t index) const
931{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200932 auto &m = meta.at(id);
933 if (index >= m.members.size())
934 return 0;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100935
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200936 return m.members[index].decoration_flags;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100937}
938
Bill Hollingsdc694272017-03-11 12:17:22 -0500939bool Compiler::has_member_decoration(uint32_t id, uint32_t index, Decoration decoration) const
Bill Hollings484931d2017-02-28 21:44:36 -0500940{
Bill Hollingsdc694272017-03-11 12:17:22 -0500941 return get_member_decoration_mask(id, index) & (1ull << decoration);
Bill Hollings484931d2017-02-28 21:44:36 -0500942}
943
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100944void Compiler::unset_member_decoration(uint32_t id, uint32_t index, Decoration decoration)
945{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200946 auto &m = meta.at(id);
947 if (index >= m.members.size())
948 return;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100949
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200950 auto &dec = m.members[index];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100951
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200952 dec.decoration_flags &= ~(1ull << decoration);
953 switch (decoration)
954 {
955 case DecorationBuiltIn:
956 dec.builtin = false;
957 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100958
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200959 case DecorationLocation:
960 dec.location = 0;
961 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100962
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200963 case DecorationOffset:
964 dec.offset = 0;
965 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100966
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +0200967 case DecorationSpecId:
968 dec.spec_id = 0;
969 break;
970
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200971 default:
972 break;
973 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100974}
975
976void Compiler::set_decoration(uint32_t id, Decoration decoration, uint32_t argument)
977{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200978 auto &dec = meta.at(id).decoration;
979 dec.decoration_flags |= 1ull << decoration;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100980
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200981 switch (decoration)
982 {
983 case DecorationBuiltIn:
984 dec.builtin = true;
985 dec.builtin_type = static_cast<BuiltIn>(argument);
986 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100987
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200988 case DecorationLocation:
989 dec.location = argument;
990 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100991
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200992 case DecorationOffset:
993 dec.offset = argument;
994 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100995
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +0200996 case DecorationArrayStride:
997 dec.array_stride = argument;
998 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +0100999
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001000 case DecorationMatrixStride:
1001 dec.matrix_stride = argument;
1002 break;
1003
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001004 case DecorationBinding:
1005 dec.binding = argument;
1006 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001007
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001008 case DecorationDescriptorSet:
1009 dec.set = argument;
1010 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001011
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001012 case DecorationInputAttachmentIndex:
1013 dec.input_attachment = argument;
1014 break;
Hans-Kristian Arntzen12cfbb22016-05-04 13:41:04 +02001015
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +02001016 case DecorationSpecId:
1017 dec.spec_id = argument;
1018 break;
1019
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001020 default:
1021 break;
1022 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001023}
1024
1025StorageClass Compiler::get_storage_class(uint32_t id) const
1026{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001027 return get<SPIRVariable>(id).storage;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001028}
1029
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001030const std::string &Compiler::get_name(uint32_t id) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001031{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001032 return meta.at(id).decoration.alias;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001033}
1034
1035uint64_t Compiler::get_decoration_mask(uint32_t id) const
1036{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001037 auto &dec = meta.at(id).decoration;
1038 return dec.decoration_flags;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001039}
1040
Bill Hollingsdc694272017-03-11 12:17:22 -05001041bool Compiler::has_decoration(uint32_t id, Decoration decoration) const
Bill Hollings484931d2017-02-28 21:44:36 -05001042{
Bill Hollingsdc694272017-03-11 12:17:22 -05001043 return get_decoration_mask(id) & (1ull << decoration);
Bill Hollings484931d2017-02-28 21:44:36 -05001044}
1045
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001046uint32_t Compiler::get_decoration(uint32_t id, Decoration decoration) const
1047{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001048 auto &dec = meta.at(id).decoration;
1049 if (!(dec.decoration_flags & (1ull << decoration)))
1050 return 0;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001051
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001052 switch (decoration)
1053 {
1054 case DecorationBuiltIn:
1055 return dec.builtin_type;
1056 case DecorationLocation:
1057 return dec.location;
1058 case DecorationOffset:
1059 return dec.offset;
1060 case DecorationBinding:
1061 return dec.binding;
1062 case DecorationDescriptorSet:
1063 return dec.set;
1064 case DecorationInputAttachmentIndex:
1065 return dec.input_attachment;
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +02001066 case DecorationSpecId:
1067 return dec.spec_id;
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001068 case DecorationArrayStride:
1069 return dec.array_stride;
1070 case DecorationMatrixStride:
1071 return dec.matrix_stride;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001072 default:
Graham Wihlidal9b1ee8f2017-01-05 21:01:49 +01001073 return 1;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001074 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001075}
1076
1077void Compiler::unset_decoration(uint32_t id, Decoration decoration)
1078{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001079 auto &dec = meta.at(id).decoration;
1080 dec.decoration_flags &= ~(1ull << decoration);
1081 switch (decoration)
1082 {
1083 case DecorationBuiltIn:
1084 dec.builtin = false;
1085 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001086
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001087 case DecorationLocation:
1088 dec.location = 0;
1089 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001090
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001091 case DecorationOffset:
1092 dec.offset = 0;
1093 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001094
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001095 case DecorationBinding:
1096 dec.binding = 0;
1097 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001098
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001099 case DecorationDescriptorSet:
1100 dec.set = 0;
1101 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001102
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +02001103 case DecorationInputAttachmentIndex:
1104 dec.input_attachment = 0;
1105 break;
1106
1107 case DecorationSpecId:
1108 dec.spec_id = 0;
1109 break;
1110
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001111 default:
1112 break;
1113 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001114}
1115
Hans-Kristian Arntzen926916d2016-05-05 09:15:25 +02001116void Compiler::parse(const Instruction &instruction)
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001117{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001118 auto ops = stream(instruction);
1119 auto op = static_cast<Op>(instruction.op);
1120 uint32_t length = instruction.length;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001121
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001122 switch (op)
1123 {
1124 case OpMemoryModel:
1125 case OpSourceExtension:
1126 case OpNop:
Robert Konrad8f7c1af2016-08-10 02:43:51 +02001127 case OpLine:
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001128 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001129
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001130 case OpSource:
1131 {
1132 auto lang = static_cast<SourceLanguage>(ops[0]);
1133 switch (lang)
1134 {
1135 case SourceLanguageESSL:
1136 source.es = true;
1137 source.version = ops[1];
1138 source.known = true;
1139 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001140
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001141 case SourceLanguageGLSL:
1142 source.es = false;
1143 source.version = ops[1];
1144 source.known = true;
1145 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001146
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001147 default:
1148 source.known = false;
1149 break;
1150 }
1151 break;
1152 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001153
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001154 case OpUndef:
1155 {
1156 uint32_t result_type = ops[0];
1157 uint32_t id = ops[1];
1158 set<SPIRUndef>(id, result_type);
1159 break;
1160 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001161
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001162 case OpCapability:
1163 {
1164 uint32_t cap = ops[0];
1165 if (cap == CapabilityKernel)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001166 SPIRV_CROSS_THROW("Kernel capability not supported.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001167 break;
1168 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001169
Hans-Kristian Arntzen299e19f2017-03-21 16:33:54 +01001170 case OpExtension:
1171 // Ignore extensions
1172 break;
1173
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001174 case OpExtInstImport:
1175 {
1176 uint32_t id = ops[0];
1177 auto ext = extract_string(spirv, instruction.offset + 1);
1178 if (ext == "GLSL.std.450")
1179 set<SPIRExtension>(id, SPIRExtension::GLSL);
1180 else
Hans-Kristian Arntzen299e19f2017-03-21 16:33:54 +01001181 set<SPIRExtension>(id, SPIRExtension::Unsupported);
1182
1183 // Other SPIR-V extensions currently not supported.
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001184
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001185 break;
1186 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001187
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001188 case OpEntryPoint:
1189 {
Bill Hollingsac00c602016-10-24 09:24:24 -04001190 auto itr =
1191 entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]),
1192 extract_string(spirv, instruction.offset + 2))));
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02001193 auto &e = itr.first->second;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001194
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02001195 // Strings need nul-terminator and consume the whole word.
Hans-Kristian Arntzen7630d3c2016-11-21 12:14:02 +01001196 uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2);
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02001197 e.interface_variables.insert(end(e.interface_variables), ops + strlen_words + 2, ops + instruction.length);
1198
Bill Hollingsac00c602016-10-24 09:24:24 -04001199 // Set the name of the entry point in case OpName is not provided later
1200 set_name(ops[1], e.name);
Bill Hollings0943d9f2016-10-23 21:42:54 -04001201
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02001202 // If we don't have an entry, make the first one our "default".
1203 if (!entry_point)
1204 entry_point = ops[1];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001205 break;
1206 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001207
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001208 case OpExecutionMode:
1209 {
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02001210 auto &execution = entry_points[ops[0]];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001211 auto mode = static_cast<ExecutionMode>(ops[1]);
1212 execution.flags |= 1ull << mode;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001213
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001214 switch (mode)
1215 {
1216 case ExecutionModeInvocations:
1217 execution.invocations = ops[2];
1218 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001219
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001220 case ExecutionModeLocalSize:
1221 execution.workgroup_size.x = ops[2];
1222 execution.workgroup_size.y = ops[3];
1223 execution.workgroup_size.z = ops[4];
1224 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001225
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001226 case ExecutionModeOutputVertices:
1227 execution.output_vertices = ops[2];
1228 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001229
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001230 default:
1231 break;
1232 }
1233 break;
1234 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001235
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001236 case OpName:
1237 {
1238 uint32_t id = ops[0];
1239 set_name(id, extract_string(spirv, instruction.offset + 1));
1240 break;
1241 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001242
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001243 case OpMemberName:
1244 {
1245 uint32_t id = ops[0];
1246 uint32_t member = ops[1];
1247 set_member_name(id, member, extract_string(spirv, instruction.offset + 2));
1248 break;
1249 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001250
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001251 case OpDecorate:
1252 {
1253 uint32_t id = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001254
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001255 auto decoration = static_cast<Decoration>(ops[1]);
1256 if (length >= 3)
1257 set_decoration(id, decoration, ops[2]);
1258 else
1259 set_decoration(id, decoration);
Bill Hollings0943d9f2016-10-23 21:42:54 -04001260
Bill Hollingsac00c602016-10-24 09:24:24 -04001261 break;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001262 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001263
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001264 case OpMemberDecorate:
1265 {
1266 uint32_t id = ops[0];
1267 uint32_t member = ops[1];
1268 auto decoration = static_cast<Decoration>(ops[2]);
1269 if (length >= 4)
1270 set_member_decoration(id, member, decoration, ops[3]);
1271 else
1272 set_member_decoration(id, member, decoration);
1273 break;
1274 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001275
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001276 // Build up basic types.
1277 case OpTypeVoid:
1278 {
1279 uint32_t id = ops[0];
1280 auto &type = set<SPIRType>(id);
1281 type.basetype = SPIRType::Void;
1282 break;
1283 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001284
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001285 case OpTypeBool:
1286 {
1287 uint32_t id = ops[0];
1288 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen5f629272016-06-05 20:13:45 +02001289 type.basetype = SPIRType::Boolean;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001290 type.width = 1;
1291 break;
1292 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001293
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001294 case OpTypeFloat:
1295 {
1296 uint32_t id = ops[0];
1297 uint32_t width = ops[1];
1298 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001299 type.basetype = width > 32 ? SPIRType::Double : SPIRType::Float;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001300 type.width = width;
1301 break;
1302 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001303
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001304 case OpTypeInt:
1305 {
1306 uint32_t id = ops[0];
1307 uint32_t width = ops[1];
1308 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzenfc2230f2016-07-27 11:27:00 +02001309 type.basetype =
1310 ops[2] ? (width > 32 ? SPIRType::Int64 : SPIRType::Int) : (width > 32 ? SPIRType::UInt64 : SPIRType::UInt);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001311 type.width = width;
1312 break;
1313 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001314
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001315 // Build composite types by "inheriting".
1316 // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing
1317 // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc.
1318 case OpTypeVector:
1319 {
1320 uint32_t id = ops[0];
1321 uint32_t vecsize = ops[2];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001322
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001323 auto &base = get<SPIRType>(ops[1]);
1324 auto &vecbase = set<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001325
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001326 vecbase = base;
1327 vecbase.vecsize = vecsize;
1328 vecbase.self = id;
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001329 vecbase.parent_type = ops[1];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001330 break;
1331 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001332
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001333 case OpTypeMatrix:
1334 {
1335 uint32_t id = ops[0];
1336 uint32_t colcount = ops[2];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001337
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001338 auto &base = get<SPIRType>(ops[1]);
1339 auto &matrixbase = set<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001340
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001341 matrixbase = base;
1342 matrixbase.columns = colcount;
1343 matrixbase.self = id;
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001344 matrixbase.parent_type = ops[1];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001345 break;
1346 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001347
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001348 case OpTypeArray:
1349 {
1350 uint32_t id = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001351
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001352 auto &base = get<SPIRType>(ops[1]);
1353 auto &arraybase = set<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001354
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001355 arraybase = base;
Hans-Kristian Arntzen5d4bb682016-10-03 17:17:11 +02001356
1357 auto *c = maybe_get<SPIRConstant>(ops[2]);
1358 bool literal = c && !c->specialization;
1359
1360 arraybase.array_size_literal.push_back(literal);
1361 arraybase.array.push_back(literal ? c->scalar() : ops[2]);
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001362 arraybase.parent_type = ops[1];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001363 // Do NOT set arraybase.self!
1364 break;
1365 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001366
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001367 case OpTypeRuntimeArray:
1368 {
1369 uint32_t id = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001370
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001371 auto &base = get<SPIRType>(ops[1]);
1372 auto &arraybase = set<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001373
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001374 arraybase = base;
1375 arraybase.array.push_back(0);
Hans-Kristian Arntzen5d4bb682016-10-03 17:17:11 +02001376 arraybase.array_size_literal.push_back(true);
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001377 arraybase.parent_type = ops[1];
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001378 // Do NOT set arraybase.self!
1379 break;
1380 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001381
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001382 case OpTypeImage:
1383 {
1384 uint32_t id = ops[0];
1385 auto &type = set<SPIRType>(id);
1386 type.basetype = SPIRType::Image;
1387 type.image.type = ops[1];
1388 type.image.dim = static_cast<Dim>(ops[2]);
1389 type.image.depth = ops[3] != 0;
1390 type.image.arrayed = ops[4] != 0;
1391 type.image.ms = ops[5] != 0;
1392 type.image.sampled = ops[6];
1393 type.image.format = static_cast<ImageFormat>(ops[7]);
Robert Konrad98028232017-01-15 22:42:22 +01001394 break;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001395 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001396
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001397 case OpTypeSampledImage:
1398 {
1399 uint32_t id = ops[0];
1400 uint32_t imagetype = ops[1];
1401 auto &type = set<SPIRType>(id);
1402 type = get<SPIRType>(imagetype);
1403 type.basetype = SPIRType::SampledImage;
1404 type.self = id;
1405 break;
1406 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001407
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001408 // Not really used.
1409 case OpTypeSampler:
1410 {
1411 uint32_t id = ops[0];
1412 auto &type = set<SPIRType>(id);
1413 type.basetype = SPIRType::Sampler;
1414 break;
1415 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001416
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001417 case OpTypePointer:
1418 {
1419 uint32_t id = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001420
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001421 auto &base = get<SPIRType>(ops[2]);
1422 auto &ptrbase = set<SPIRType>(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001423
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001424 ptrbase = base;
1425 if (ptrbase.pointer)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001426 SPIRV_CROSS_THROW("Cannot make pointer-to-pointer type.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001427 ptrbase.pointer = true;
1428 ptrbase.storage = static_cast<StorageClass>(ops[1]);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001429
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001430 if (ptrbase.storage == StorageClassAtomicCounter)
1431 ptrbase.basetype = SPIRType::AtomicCounter;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001432
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01001433 ptrbase.parent_type = ops[2];
1434
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001435 // Do NOT set ptrbase.self!
1436 break;
1437 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001438
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001439 case OpTypeStruct:
1440 {
1441 uint32_t id = ops[0];
1442 auto &type = set<SPIRType>(id);
1443 type.basetype = SPIRType::Struct;
1444 for (uint32_t i = 1; i < length; i++)
1445 type.member_types.push_back(ops[i]);
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02001446
1447 // Check if we have seen this struct type before, with just different
1448 // decorations.
Hans-Kristian Arntzen5ad43402016-05-28 09:47:52 +02001449 //
1450 // Add workaround for issue #17 as well by looking at OpName for the struct
1451 // types, which we shouldn't normally do.
1452 // We should not normally have to consider type aliases like this to begin with
1453 // however ... glslang issues #304, #307 cover this.
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02001454 for (auto &other : global_struct_cache)
1455 {
Hans-Kristian Arntzen5ad43402016-05-28 09:47:52 +02001456 if (get_name(type.self) == get_name(other) && types_are_logically_equivalent(type, get<SPIRType>(other)))
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02001457 {
1458 type.type_alias = other;
1459 break;
1460 }
1461 }
1462
1463 if (type.type_alias == 0)
1464 global_struct_cache.push_back(id);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001465 break;
1466 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001467
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001468 case OpTypeFunction:
1469 {
1470 uint32_t id = ops[0];
1471 uint32_t ret = ops[1];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001472
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001473 auto &func = set<SPIRFunctionPrototype>(id, ret);
1474 for (uint32_t i = 2; i < length; i++)
1475 func.parameter_types.push_back(ops[i]);
1476 break;
1477 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001478
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001479 // Variable declaration
1480 // All variables are essentially pointers with a storage qualifier.
1481 case OpVariable:
1482 {
1483 uint32_t type = ops[0];
1484 uint32_t id = ops[1];
1485 auto storage = static_cast<StorageClass>(ops[2]);
1486 uint32_t initializer = length == 4 ? ops[3] : 0;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001487
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001488 if (storage == StorageClassFunction)
1489 {
1490 if (!current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001491 SPIRV_CROSS_THROW("No function currently in scope");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001492 current_function->add_local_variable(id);
1493 }
1494 else if (storage == StorageClassPrivate || storage == StorageClassWorkgroup || storage == StorageClassOutput)
1495 {
1496 global_variables.push_back(id);
1497 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001498
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001499 auto &var = set<SPIRVariable>(id, type, storage, initializer);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001500
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001501 if (variable_storage_is_aliased(var))
1502 aliased_variables.push_back(var.self);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001503
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001504 break;
1505 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001506
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001507 // OpPhi
1508 // OpPhi is a fairly magical opcode.
1509 // It selects temporary variables based on which parent block we *came from*.
1510 // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local
1511 // variable to emulate SSA Phi.
1512 case OpPhi:
1513 {
1514 if (!current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001515 SPIRV_CROSS_THROW("No function currently in scope");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001516 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001517 SPIRV_CROSS_THROW("No block currently in scope");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001518
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001519 uint32_t result_type = ops[0];
1520 uint32_t id = ops[1];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001521
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001522 // Instead of a temporary, create a new function-wide temporary with this ID instead.
1523 auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction);
1524 var.phi_variable = true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001525
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001526 current_function->add_local_variable(id);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001527
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001528 for (uint32_t i = 2; i + 2 <= length; i += 2)
1529 current_block->phi_variables.push_back({ ops[i], ops[i + 1], id });
1530 break;
1531 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001532
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001533 // Constants
1534 case OpSpecConstant:
1535 case OpConstant:
1536 {
1537 uint32_t id = ops[1];
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001538 auto &type = get<SPIRType>(ops[0]);
Hans-Kristian Arntzenfc2230f2016-07-27 11:27:00 +02001539 if (type.width > 32)
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001540 set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32)).specialization = op == OpSpecConstant;
1541 else
1542 set<SPIRConstant>(id, ops[0], ops[2]).specialization = op == OpSpecConstant;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001543 break;
1544 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001545
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001546 case OpSpecConstantFalse:
1547 case OpConstantFalse:
1548 {
1549 uint32_t id = ops[1];
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001550 set<SPIRConstant>(id, ops[0], uint32_t(0)).specialization = op == OpSpecConstantFalse;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001551 break;
1552 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001553
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001554 case OpSpecConstantTrue:
1555 case OpConstantTrue:
1556 {
1557 uint32_t id = ops[1];
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001558 set<SPIRConstant>(id, ops[0], uint32_t(1)).specialization = op == OpSpecConstantTrue;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001559 break;
1560 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001561
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001562 case OpSpecConstantComposite:
1563 case OpConstantComposite:
1564 {
1565 uint32_t id = ops[1];
1566 uint32_t type = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001567
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001568 auto &ctype = get<SPIRType>(type);
1569 SPIRConstant *constant = nullptr;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001570
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001571 // We can have constants which are structs and arrays.
1572 // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we
1573 // can refer to.
1574 if (ctype.basetype == SPIRType::Struct || !ctype.array.empty())
1575 {
1576 constant = &set<SPIRConstant>(id, type, ops + 2, length - 2);
1577 constant->specialization = op == OpSpecConstantComposite;
1578 break;
1579 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001580
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001581 bool type_64bit = ctype.width > 32;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001582 bool matrix = ctype.columns > 1;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001583
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001584 if (matrix)
1585 {
1586 switch (length - 2)
1587 {
1588 case 1:
1589 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).vector());
1590 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001591
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001592 case 2:
1593 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).vector(),
1594 get<SPIRConstant>(ops[3]).vector());
1595 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001596
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001597 case 3:
1598 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).vector(),
1599 get<SPIRConstant>(ops[3]).vector(), get<SPIRConstant>(ops[4]).vector());
1600 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001601
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001602 case 4:
1603 constant =
1604 &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).vector(), get<SPIRConstant>(ops[3]).vector(),
1605 get<SPIRConstant>(ops[4]).vector(), get<SPIRConstant>(ops[5]).vector());
1606 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001607
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001608 default:
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001609 SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 columns.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001610 }
1611 }
1612 else
1613 {
1614 switch (length - 2)
1615 {
1616 case 1:
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001617 if (type_64bit)
1618 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar_u64());
1619 else
1620 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar());
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001621 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001622
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001623 case 2:
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001624 if (type_64bit)
1625 {
1626 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar_u64(),
1627 get<SPIRConstant>(ops[3]).scalar_u64());
1628 }
1629 else
1630 {
1631 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar(),
1632 get<SPIRConstant>(ops[3]).scalar());
1633 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001634 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001635
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001636 case 3:
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001637 if (type_64bit)
1638 {
1639 constant = &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar_u64(),
1640 get<SPIRConstant>(ops[3]).scalar_u64(),
1641 get<SPIRConstant>(ops[4]).scalar_u64());
1642 }
1643 else
1644 {
1645 constant =
1646 &set<SPIRConstant>(id, type, get<SPIRConstant>(ops[2]).scalar(),
1647 get<SPIRConstant>(ops[3]).scalar(), get<SPIRConstant>(ops[4]).scalar());
1648 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001649 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001650
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001651 case 4:
Hans-Kristian Arntzenfa0255c2016-07-27 10:59:00 +02001652 if (type_64bit)
1653 {
1654 constant = &set<SPIRConstant>(
1655 id, type, get<SPIRConstant>(ops[2]).scalar_u64(), get<SPIRConstant>(ops[3]).scalar_u64(),
1656 get<SPIRConstant>(ops[4]).scalar_u64(), get<SPIRConstant>(ops[5]).scalar_u64());
1657 }
1658 else
1659 {
1660 constant = &set<SPIRConstant>(
1661 id, type, get<SPIRConstant>(ops[2]).scalar(), get<SPIRConstant>(ops[3]).scalar(),
1662 get<SPIRConstant>(ops[4]).scalar(), get<SPIRConstant>(ops[5]).scalar());
1663 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001664 break;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001665
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001666 default:
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001667 SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 components.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001668 }
1669 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001670
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001671 constant->specialization = op == OpSpecConstantComposite;
1672 break;
1673 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001674
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001675 // Functions
1676 case OpFunction:
1677 {
1678 uint32_t res = ops[0];
1679 uint32_t id = ops[1];
1680 // Control
1681 uint32_t type = ops[3];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001682
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001683 if (current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001684 SPIRV_CROSS_THROW("Must end a function before starting a new one!");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001685
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001686 current_function = &set<SPIRFunction>(id, res, type);
1687 break;
1688 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001689
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001690 case OpFunctionParameter:
1691 {
1692 uint32_t type = ops[0];
1693 uint32_t id = ops[1];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001694
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001695 if (!current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001696 SPIRV_CROSS_THROW("Must be in a function!");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001697
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001698 current_function->add_parameter(type, id);
1699 set<SPIRVariable>(id, type, StorageClassFunction);
1700 break;
1701 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001702
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001703 case OpFunctionEnd:
1704 {
Hans-Kristian Arntzen526d06d2016-11-12 10:03:18 +01001705 if (current_block)
1706 {
1707 // Very specific error message, but seems to come up quite often.
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001708 SPIRV_CROSS_THROW(
Hans-Kristian Arntzen526d06d2016-11-12 10:03:18 +01001709 "Cannot end a function before ending the current block.\n"
1710 "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid.");
1711 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001712 current_function = nullptr;
1713 break;
1714 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001715
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001716 // Blocks
1717 case OpLabel:
1718 {
1719 // OpLabel always starts a block.
1720 if (!current_function)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001721 SPIRV_CROSS_THROW("Blocks cannot exist outside functions!");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001722
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001723 uint32_t id = ops[0];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001724
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001725 current_function->blocks.push_back(id);
1726 if (!current_function->entry_block)
1727 current_function->entry_block = id;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001728
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001729 if (current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001730 SPIRV_CROSS_THROW("Cannot start a block before ending the current block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001731
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001732 current_block = &set<SPIRBlock>(id);
1733 break;
1734 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001735
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001736 // Branch instructions end blocks.
1737 case OpBranch:
1738 {
1739 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001740 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001741
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001742 uint32_t target = ops[0];
1743 current_block->terminator = SPIRBlock::Direct;
1744 current_block->next_block = target;
1745 current_block = nullptr;
1746 break;
1747 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001748
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001749 case OpBranchConditional:
1750 {
1751 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001752 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001753
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001754 current_block->condition = ops[0];
1755 current_block->true_block = ops[1];
1756 current_block->false_block = ops[2];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001757
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001758 current_block->terminator = SPIRBlock::Select;
1759 current_block = nullptr;
1760 break;
1761 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001762
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001763 case OpSwitch:
1764 {
1765 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001766 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001767
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001768 if (current_block->merge == SPIRBlock::MergeNone)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001769 SPIRV_CROSS_THROW("Switch statement is not structured");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001770
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001771 current_block->terminator = SPIRBlock::MultiSelect;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001772
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001773 current_block->condition = ops[0];
1774 current_block->default_block = ops[1];
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001775
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001776 for (uint32_t i = 2; i + 2 <= length; i += 2)
1777 current_block->cases.push_back({ ops[i], ops[i + 1] });
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001778
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001779 // If we jump to next block, make it break instead since we're inside a switch case block at that point.
1780 multiselect_merge_targets.insert(current_block->next_block);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001781
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001782 current_block = nullptr;
1783 break;
1784 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001785
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001786 case OpKill:
1787 {
1788 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001789 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001790 current_block->terminator = SPIRBlock::Kill;
1791 current_block = nullptr;
1792 break;
1793 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001794
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001795 case OpReturn:
1796 {
1797 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001798 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001799 current_block->terminator = SPIRBlock::Return;
1800 current_block = nullptr;
1801 break;
1802 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001803
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001804 case OpReturnValue:
1805 {
1806 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001807 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001808 current_block->terminator = SPIRBlock::Return;
1809 current_block->return_value = ops[0];
1810 current_block = nullptr;
1811 break;
1812 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001813
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001814 case OpUnreachable:
1815 {
1816 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001817 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001818 current_block->terminator = SPIRBlock::Unreachable;
1819 current_block = nullptr;
1820 break;
1821 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001822
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001823 case OpSelectionMerge:
1824 {
1825 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001826 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001827
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001828 current_block->next_block = ops[0];
1829 current_block->merge = SPIRBlock::MergeSelection;
1830 selection_merge_targets.insert(current_block->next_block);
1831 break;
1832 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001833
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001834 case OpLoopMerge:
1835 {
1836 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001837 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001838
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001839 current_block->merge_block = ops[0];
1840 current_block->continue_block = ops[1];
1841 current_block->merge = SPIRBlock::MergeLoop;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001842
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001843 loop_blocks.insert(current_block->self);
1844 loop_merge_targets.insert(current_block->merge_block);
Hans-Kristian Arntzen97f81ba2016-04-01 12:37:29 +02001845
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001846 // Don't add loop headers to continue blocks,
1847 // which would make it impossible branch into the loop header since
1848 // they are treated as continues.
1849 if (current_block->continue_block != current_block->self)
1850 continue_blocks.insert(current_block->continue_block);
1851 break;
1852 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001853
Hans-Kristian Arntzen7e8afa82016-10-03 15:54:02 +02001854 case OpSpecConstantOp:
1855 {
1856 if (length < 3)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001857 SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments.");
Hans-Kristian Arntzen7e8afa82016-10-03 15:54:02 +02001858
1859 uint32_t result_type = ops[0];
1860 uint32_t id = ops[1];
1861 auto spec_op = static_cast<Op>(ops[2]);
1862
1863 set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3);
1864 break;
1865 }
1866
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001867 // Actual opcodes.
1868 default:
1869 {
1870 if (!current_block)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01001871 SPIRV_CROSS_THROW("Currently no block to insert opcode.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001872
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001873 current_block->ops.push_back(instruction);
1874 break;
1875 }
1876 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001877}
1878
1879bool Compiler::block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const
1880{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001881 // Tried and failed.
1882 if (block.disable_block_optimization || block.complex_continue)
1883 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001884
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001885 if (method == SPIRBlock::MergeToSelectForLoop)
1886 {
1887 // Try to detect common for loop pattern
1888 // which the code backend can use to create cleaner code.
1889 // for(;;) { if (cond) { some_body; } else { break; } }
1890 // is the pattern we're looking for.
1891 bool ret = block.terminator == SPIRBlock::Select && block.merge == SPIRBlock::MergeLoop &&
1892 block.true_block != block.merge_block && block.true_block != block.self &&
1893 block.false_block == block.merge_block;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001894
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001895 // If we have OpPhi which depends on branches which came from our own block,
1896 // we need to flush phi variables in else block instead of a trivial break,
1897 // so we cannot assume this is a for loop candidate.
1898 if (ret)
1899 {
1900 for (auto &phi : block.phi_variables)
1901 if (phi.parent == block.self)
1902 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001903
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001904 auto *merge = maybe_get<SPIRBlock>(block.merge_block);
1905 if (merge)
1906 for (auto &phi : merge->phi_variables)
1907 if (phi.parent == block.self)
1908 return false;
1909 }
1910 return ret;
1911 }
1912 else if (method == SPIRBlock::MergeToDirectForLoop)
1913 {
1914 // Empty loop header that just sets up merge target
1915 // and branches to loop body.
1916 bool ret = block.terminator == SPIRBlock::Direct && block.merge == SPIRBlock::MergeLoop && block.ops.empty();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001917
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001918 if (!ret)
1919 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001920
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001921 auto &child = get<SPIRBlock>(block.next_block);
1922 ret = child.terminator == SPIRBlock::Select && child.merge == SPIRBlock::MergeNone &&
1923 child.false_block == block.merge_block && child.true_block != block.merge_block &&
1924 child.true_block != block.self;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001925
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001926 // If we have OpPhi which depends on branches which came from our own block,
1927 // we need to flush phi variables in else block instead of a trivial break,
1928 // so we cannot assume this is a for loop candidate.
1929 if (ret)
1930 {
1931 for (auto &phi : block.phi_variables)
1932 if (phi.parent == block.self || phi.parent == child.self)
1933 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001934
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001935 for (auto &phi : child.phi_variables)
1936 if (phi.parent == block.self)
1937 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001938
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001939 auto *merge = maybe_get<SPIRBlock>(block.merge_block);
1940 if (merge)
1941 for (auto &phi : merge->phi_variables)
1942 if (phi.parent == block.self || phi.parent == child.false_block)
1943 return false;
1944 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001945
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001946 return ret;
1947 }
1948 else
1949 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001950}
1951
1952bool Compiler::block_is_outside_flow_control_from_block(const SPIRBlock &from, const SPIRBlock &to)
1953{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001954 auto *start = &from;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001955
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001956 if (start->self == to.self)
1957 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001958
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001959 // Break cycles.
1960 if (is_continue(start->self))
1961 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001962
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001963 // If our select block doesn't merge, we must break or continue in these blocks,
1964 // so if continues occur branchless within these blocks, consider them branchless as well.
1965 // This is typically used for loop control.
1966 if (start->terminator == SPIRBlock::Select && start->merge == SPIRBlock::MergeNone &&
1967 (block_is_outside_flow_control_from_block(get<SPIRBlock>(start->true_block), to) ||
1968 block_is_outside_flow_control_from_block(get<SPIRBlock>(start->false_block), to)))
1969 {
1970 return true;
1971 }
1972 else if (start->merge_block && block_is_outside_flow_control_from_block(get<SPIRBlock>(start->merge_block), to))
1973 {
1974 return true;
1975 }
1976 else if (start->next_block && block_is_outside_flow_control_from_block(get<SPIRBlock>(start->next_block), to))
1977 {
1978 return true;
1979 }
1980 else
1981 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001982}
1983
1984bool Compiler::execution_is_noop(const SPIRBlock &from, const SPIRBlock &to) const
1985{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001986 if (!execution_is_branchless(from, to))
1987 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001988
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001989 auto *start = &from;
1990 for (;;)
1991 {
1992 if (start->self == to.self)
1993 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001994
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001995 if (!start->ops.empty())
1996 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01001997
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02001998 start = &get<SPIRBlock>(start->next_block);
1999 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002000}
2001
2002bool Compiler::execution_is_branchless(const SPIRBlock &from, const SPIRBlock &to) const
2003{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002004 auto *start = &from;
2005 for (;;)
2006 {
2007 if (start->self == to.self)
2008 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002009
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002010 if (start->terminator == SPIRBlock::Direct && start->merge == SPIRBlock::MergeNone)
2011 start = &get<SPIRBlock>(start->next_block);
2012 else
2013 return false;
2014 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002015}
2016
Hans-Kristian Arntzen926916d2016-05-05 09:15:25 +02002017SPIRBlock::ContinueBlockType Compiler::continue_block_type(const SPIRBlock &block) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002018{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002019 // The block was deemed too complex during code emit, pick conservative fallback paths.
2020 if (block.complex_continue)
2021 return SPIRBlock::ComplexLoop;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002022
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002023 // In older glslang output continue block can be equal to the loop header.
2024 // In this case, execution is clearly branchless, so just assume a while loop header here.
2025 if (block.merge == SPIRBlock::MergeLoop)
2026 return SPIRBlock::WhileLoop;
Hans-Kristian Arntzen97f81ba2016-04-01 12:37:29 +02002027
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002028 auto &dominator = get<SPIRBlock>(block.loop_dominator);
Hans-Kristian Arntzen97f81ba2016-04-01 12:37:29 +02002029
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002030 if (execution_is_noop(block, dominator))
2031 return SPIRBlock::WhileLoop;
2032 else if (execution_is_branchless(block, dominator))
2033 return SPIRBlock::ForLoop;
2034 else
2035 {
2036 if (block.merge == SPIRBlock::MergeNone && block.terminator == SPIRBlock::Select &&
2037 block.true_block == dominator.self && block.false_block == dominator.merge_block)
2038 {
2039 return SPIRBlock::DoWhileLoop;
2040 }
2041 else
2042 return SPIRBlock::ComplexLoop;
2043 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002044}
2045
2046bool Compiler::traverse_all_reachable_opcodes(const SPIRBlock &block, OpcodeHandler &handler) const
2047{
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002048 handler.set_current_block(block);
2049
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002050 // Ideally, perhaps traverse the CFG instead of all blocks in order to eliminate dead blocks,
2051 // but this shouldn't be a problem in practice unless the SPIR-V is doing insane things like recursing
2052 // inside dead blocks ...
2053 for (auto &i : block.ops)
2054 {
2055 auto ops = stream(i);
2056 auto op = static_cast<Op>(i.op);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002057
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002058 if (!handler.handle(op, ops, i.length))
2059 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002060
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002061 if (op == OpFunctionCall)
2062 {
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002063 auto &func = get<SPIRFunction>(ops[2]);
2064 if (handler.follow_function_call(func))
2065 {
2066 if (!handler.begin_function_scope(ops, i.length))
2067 return false;
2068 if (!traverse_all_reachable_opcodes(get<SPIRFunction>(ops[2]), handler))
2069 return false;
2070 if (!handler.end_function_scope(ops, i.length))
2071 return false;
2072 }
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002073 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002074 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002075
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002076 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002077}
2078
2079bool Compiler::traverse_all_reachable_opcodes(const SPIRFunction &func, OpcodeHandler &handler) const
2080{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002081 for (auto block : func.blocks)
2082 if (!traverse_all_reachable_opcodes(get<SPIRBlock>(block), handler))
2083 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002084
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002085 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002086}
2087
2088uint32_t Compiler::type_struct_member_offset(const SPIRType &type, uint32_t index) const
2089{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002090 // Decoration must be set in valid SPIR-V, otherwise throw.
2091 auto &dec = meta[type.self].members.at(index);
2092 if (dec.decoration_flags & (1ull << DecorationOffset))
2093 return dec.offset;
2094 else
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002095 SPIRV_CROSS_THROW("Struct member does not have Offset set.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002096}
2097
2098uint32_t Compiler::type_struct_member_array_stride(const SPIRType &type, uint32_t index) const
2099{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002100 // Decoration must be set in valid SPIR-V, otherwise throw.
2101 // ArrayStride is part of the array type not OpMemberDecorate.
2102 auto &dec = meta[type.member_types[index]].decoration;
2103 if (dec.decoration_flags & (1ull << DecorationArrayStride))
2104 return dec.array_stride;
2105 else
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002106 SPIRV_CROSS_THROW("Struct member does not have ArrayStride set.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002107}
2108
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002109uint32_t Compiler::type_struct_member_matrix_stride(const SPIRType &type, uint32_t index) const
2110{
2111 // Decoration must be set in valid SPIR-V, otherwise throw.
2112 // MatrixStride is part of OpMemberDecorate.
2113 auto &dec = meta[type.self].members[index];
2114 if (dec.decoration_flags & (1ull << DecorationMatrixStride))
2115 return dec.matrix_stride;
2116 else
2117 SPIRV_CROSS_THROW("Struct member does not have MatrixStride set.");
2118}
2119
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002120size_t Compiler::get_declared_struct_size(const SPIRType &type) const
2121{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002122 uint32_t last = uint32_t(type.member_types.size() - 1);
2123 size_t offset = type_struct_member_offset(type, last);
2124 size_t size = get_declared_struct_member_size(type, last);
2125 return offset + size;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002126}
2127
2128size_t Compiler::get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const
2129{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002130 auto flags = get_member_decoration_mask(struct_type.self, index);
2131 auto &type = get<SPIRType>(struct_type.member_types[index]);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002132
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002133 switch (type.basetype)
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002134 {
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002135 case SPIRType::Unknown:
2136 case SPIRType::Void:
2137 case SPIRType::Boolean: // Bools are purely logical, and cannot be used for externally visible types.
2138 case SPIRType::AtomicCounter:
2139 case SPIRType::Image:
2140 case SPIRType::SampledImage:
2141 case SPIRType::Sampler:
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002142 SPIRV_CROSS_THROW("Querying size for object with opaque size.");
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002143
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002144 default:
2145 break;
2146 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002147
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002148 if (!type.array.empty())
2149 {
2150 // For arrays, we can use ArrayStride to get an easy check.
2151 return type_struct_member_array_stride(struct_type, index) * type.array.back();
2152 }
2153 else if (type.basetype == SPIRType::Struct)
2154 {
2155 return get_declared_struct_size(type);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002156 }
2157 else
2158 {
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002159 unsigned vecsize = type.vecsize;
2160 unsigned columns = type.columns;
Hans-Kristian Arntzen2d79d362016-11-28 15:01:36 +01002161
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002162 // Vectors.
2163 if (columns == 1)
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002164 {
2165 size_t component_size = type.width / 8;
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002166 return vecsize * component_size;
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002167 }
Hans-Kristian Arntzen2d79d362016-11-28 15:01:36 +01002168 else
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002169 {
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002170 uint32_t matrix_stride = type_struct_member_matrix_stride(struct_type, index);
Hans-Kristian Arntzen2d79d362016-11-28 15:01:36 +01002171
Hans-Kristian Arntzend3cad992017-01-21 11:30:33 +01002172 // Per SPIR-V spec, matrices must be tightly packed and aligned up for vec3 accesses.
2173 if (flags & (1ull << DecorationRowMajor))
2174 return matrix_stride * vecsize;
2175 else if (flags & (1ull << DecorationColMajor))
2176 return matrix_stride * columns;
2177 else
2178 SPIRV_CROSS_THROW("Either row-major or column-major must be declared for matrices.");
Arseny Kapoulkinef63e7c52017-01-17 01:52:12 -08002179 }
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002180 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002181}
2182
2183bool Compiler::BufferAccessHandler::handle(Op opcode, const uint32_t *args, uint32_t length)
2184{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002185 if (opcode != OpAccessChain && opcode != OpInBoundsAccessChain)
2186 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002187
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002188 // Invalid SPIR-V.
2189 if (length < 4)
2190 return false;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002191
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002192 if (args[2] != id)
2193 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002194
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002195 // Don't bother traversing the entire access chain tree yet.
2196 // If we access a struct member, assume we access the entire member.
2197 uint32_t index = compiler.get<SPIRConstant>(args[3]).scalar();
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002198
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002199 // Seen this index already.
2200 if (seen.find(index) != end(seen))
2201 return true;
2202 seen.insert(index);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002203
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002204 auto &type = compiler.expression_type(id);
2205 uint32_t offset = compiler.type_struct_member_offset(type, index);
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002206
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002207 size_t range;
2208 // If we have another member in the struct, deduce the range by looking at the next member.
2209 // This is okay since structs in SPIR-V can have padding, but Offset decoration must be
2210 // monotonically increasing.
2211 // Of course, this doesn't take into account if the SPIR-V for some reason decided to add
2212 // very large amounts of padding, but that's not really a big deal.
2213 if (index + 1 < type.member_types.size())
2214 {
2215 range = compiler.type_struct_member_offset(type, index + 1) - offset;
2216 }
2217 else
2218 {
2219 // No padding, so just deduce it from the size of the member directly.
2220 range = compiler.get_declared_struct_member_size(type, index);
2221 }
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002222
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002223 ranges.push_back({ index, offset, range });
2224 return true;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002225}
2226
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002227std::vector<BufferRange> Compiler::get_active_buffer_ranges(uint32_t id) const
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002228{
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002229 std::vector<BufferRange> ranges;
2230 BufferAccessHandler handler(*this, ranges, id);
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002231 traverse_all_reachable_opcodes(get<SPIRFunction>(entry_point), handler);
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002232 return ranges;
Hans-Kristian Arntzen75471fb2016-03-02 18:09:16 +01002233}
2234
Bill Hollings103aabf2016-04-06 17:42:27 -04002235// Increase the number of IDs by the specified incremental amount.
2236// Returns the value of the first ID available for use in the expanded bound.
2237uint32_t Compiler::increase_bound_by(uint32_t incr_amount)
2238{
Hans-Kristian Arntzen14bc1ff2016-09-10 18:07:52 +02002239 auto curr_bound = ids.size();
2240 auto new_bound = curr_bound + incr_amount;
Hans-Kristian Arntzen4b8ed532016-05-05 09:33:18 +02002241 ids.resize(new_bound);
2242 meta.resize(new_bound);
Hans-Kristian Arntzen14bc1ff2016-09-10 18:07:52 +02002243 return uint32_t(curr_bound);
Bill Hollings103aabf2016-04-06 17:42:27 -04002244}
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02002245
2246bool Compiler::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const
2247{
2248 if (a.basetype != b.basetype)
2249 return false;
2250 if (a.width != b.width)
2251 return false;
2252 if (a.vecsize != b.vecsize)
2253 return false;
2254 if (a.columns != b.columns)
2255 return false;
2256 if (a.array.size() != b.array.size())
2257 return false;
2258
Hans-Kristian Arntzen46892ab2016-05-23 13:45:20 +02002259 size_t array_count = a.array.size();
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02002260 if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0)
2261 return false;
2262
2263 if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage)
2264 {
2265 if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0)
2266 return false;
2267 }
2268
2269 if (a.member_types.size() != b.member_types.size())
2270 return false;
2271
Hans-Kristian Arntzen46892ab2016-05-23 13:45:20 +02002272 size_t member_types = a.member_types.size();
2273 for (size_t i = 0; i < member_types; i++)
Hans-Kristian Arntzenf05373b2016-05-23 10:57:22 +02002274 {
2275 if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i])))
2276 return false;
2277 }
2278
2279 return true;
2280}
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002281
2282uint64_t Compiler::get_execution_mode_mask() const
2283{
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002284 return get_entry_point().flags;
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002285}
2286
2287void Compiler::set_execution_mode(ExecutionMode mode, uint32_t arg0, uint32_t arg1, uint32_t arg2)
2288{
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002289 auto &execution = get_entry_point();
2290
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002291 execution.flags |= 1ull << mode;
2292 switch (mode)
2293 {
2294 case ExecutionModeLocalSize:
2295 execution.workgroup_size.x = arg0;
2296 execution.workgroup_size.y = arg1;
2297 execution.workgroup_size.z = arg2;
2298 break;
2299
2300 case ExecutionModeInvocations:
2301 execution.invocations = arg0;
2302 break;
2303
2304 case ExecutionModeOutputVertices:
2305 execution.output_vertices = arg0;
2306 break;
2307
2308 default:
2309 break;
2310 }
2311}
2312
2313void Compiler::unset_execution_mode(ExecutionMode mode)
2314{
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002315 auto &execution = get_entry_point();
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002316 execution.flags &= ~(1ull << mode);
2317}
2318
2319uint32_t Compiler::get_execution_mode_argument(spv::ExecutionMode mode, uint32_t index) const
2320{
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002321 auto &execution = get_entry_point();
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002322 switch (mode)
2323 {
2324 case ExecutionModeLocalSize:
2325 switch (index)
2326 {
2327 case 0:
2328 return execution.workgroup_size.x;
2329 case 1:
2330 return execution.workgroup_size.y;
2331 case 2:
2332 return execution.workgroup_size.z;
2333 default:
2334 return 0;
2335 }
2336
2337 case ExecutionModeInvocations:
2338 return execution.invocations;
2339
2340 case ExecutionModeOutputVertices:
2341 return execution.output_vertices;
2342
2343 default:
2344 return 0;
2345 }
2346}
2347
2348ExecutionModel Compiler::get_execution_model() const
2349{
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002350 auto &execution = get_entry_point();
Hans-Kristian Arntzen3c285a12016-07-04 13:30:05 +02002351 return execution.model;
2352}
Hans-Kristian Arntzen8e63c772016-07-06 09:58:01 +02002353
2354void Compiler::set_remapped_variable_state(uint32_t id, bool remap_enable)
2355{
2356 get<SPIRVariable>(id).remapped_variable = remap_enable;
2357}
2358
2359bool Compiler::get_remapped_variable_state(uint32_t id) const
2360{
2361 return get<SPIRVariable>(id).remapped_variable;
2362}
Hans-Kristian Arntzen078eec52016-07-06 11:04:06 +02002363
2364void Compiler::set_subpass_input_remapped_components(uint32_t id, uint32_t components)
2365{
2366 get<SPIRVariable>(id).remapped_components = components;
2367}
2368
2369uint32_t Compiler::get_subpass_input_remapped_components(uint32_t id) const
2370{
2371 return get<SPIRVariable>(id).remapped_components;
2372}
Hans-Kristian Arntzen36a0b632016-07-12 14:33:04 +02002373
2374void Compiler::inherit_expression_dependencies(uint32_t dst, uint32_t source_expression)
2375{
Hans-Kristian Arntzen75391f92017-03-20 22:38:05 +01002376 // Don't inherit any expression dependencies if the expression in dst
2377 // is not a forwarded temporary.
2378 if (forwarded_temporaries.find(dst) == end(forwarded_temporaries) ||
2379 forced_temporaries.find(dst) != end(forced_temporaries))
2380 {
2381 return;
2382 }
2383
Hans-Kristian Arntzen36a0b632016-07-12 14:33:04 +02002384 auto &e = get<SPIRExpression>(dst);
2385 auto *s = maybe_get<SPIRExpression>(source_expression);
2386 if (!s)
2387 return;
2388
2389 auto &e_deps = e.expression_dependencies;
2390 auto &s_deps = s->expression_dependencies;
2391
2392 // If we depend on a expression, we also depend on all sub-dependencies from source.
2393 e_deps.push_back(source_expression);
2394 e_deps.insert(end(e_deps), begin(s_deps), end(s_deps));
2395
2396 // Eliminate duplicated dependencies.
2397 e_deps.erase(unique(begin(e_deps), end(e_deps)), end(e_deps));
2398}
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002399
2400vector<string> Compiler::get_entry_points() const
2401{
2402 vector<string> entries;
2403 for (auto &entry : entry_points)
2404 entries.push_back(entry.second.name);
2405 return entries;
2406}
2407
2408void Compiler::set_entry_point(const std::string &name)
2409{
2410 auto &entry = get_entry_point(name);
2411 entry_point = entry.self;
2412}
2413
2414SPIREntryPoint &Compiler::get_entry_point(const std::string &name)
2415{
2416 auto itr =
2417 find_if(begin(entry_points), end(entry_points),
2418 [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { return entry.second.name == name; });
2419
2420 if (itr == end(entry_points))
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002421 SPIRV_CROSS_THROW("Entry point does not exist.");
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002422
2423 return itr->second;
2424}
2425
2426const SPIREntryPoint &Compiler::get_entry_point(const std::string &name) const
2427{
2428 auto itr =
2429 find_if(begin(entry_points), end(entry_points),
2430 [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { return entry.second.name == name; });
2431
2432 if (itr == end(entry_points))
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002433 SPIRV_CROSS_THROW("Entry point does not exist.");
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002434
2435 return itr->second;
2436}
2437
2438const SPIREntryPoint &Compiler::get_entry_point() const
2439{
2440 return entry_points.find(entry_point)->second;
2441}
2442
2443SPIREntryPoint &Compiler::get_entry_point()
2444{
2445 return entry_points.find(entry_point)->second;
2446}
2447
2448bool Compiler::interface_variable_exists_in_entry_point(uint32_t id) const
2449{
2450 auto &var = get<SPIRVariable>(id);
Robert Konrada778c362017-01-15 16:39:03 +01002451 if (var.storage != StorageClassInput && var.storage != StorageClassOutput &&
2452 var.storage != StorageClassUniformConstant)
Hans-Kristian Arntzen24df8f02017-02-04 10:26:26 +01002453 SPIRV_CROSS_THROW("Only Input, Output variables and Uniform constants are part of a shader linking interface.");
Hans-Kristian Arntzen042475e2016-07-28 11:16:02 +02002454
2455 // This is to avoid potential problems with very old glslang versions which did
2456 // not emit input/output interfaces properly.
2457 // We can assume they only had a single entry point, and single entry point
2458 // shaders could easily be assumed to use every interface variable anyways.
2459 if (entry_points.size() <= 1)
2460 return true;
2461
2462 auto &execution = get_entry_point();
2463 return find(begin(execution.interface_variables), end(execution.interface_variables), id) !=
2464 end(execution.interface_variables);
2465}
Hans-Kristian Arntzenbcb55602016-09-10 13:56:36 +02002466
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002467void Compiler::CombinedImageSamplerHandler::push_remap_parameters(const SPIRFunction &func, const uint32_t *args,
2468 uint32_t length)
2469{
2470 // If possible, pipe through a remapping table so that parameters know
2471 // which variables they actually bind to in this scope.
2472 unordered_map<uint32_t, uint32_t> remapping;
2473 for (uint32_t i = 0; i < length; i++)
2474 remapping[func.arguments[i].id] = remap_parameter(args[i]);
2475 parameter_remapping.push(move(remapping));
2476}
2477
2478void Compiler::CombinedImageSamplerHandler::pop_remap_parameters()
2479{
2480 parameter_remapping.pop();
2481}
2482
2483uint32_t Compiler::CombinedImageSamplerHandler::remap_parameter(uint32_t id)
2484{
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002485 auto *var = compiler.maybe_get_backing_variable(id);
2486 if (var)
2487 id = var->self;
2488
Hans-Kristian Arntzen901b45e2016-09-10 22:21:57 +02002489 if (parameter_remapping.empty())
2490 return id;
2491
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002492 auto &remapping = parameter_remapping.top();
2493 auto itr = remapping.find(id);
2494 if (itr != end(remapping))
2495 return itr->second;
2496 else
2497 return id;
2498}
2499
2500bool Compiler::CombinedImageSamplerHandler::begin_function_scope(const uint32_t *args, uint32_t length)
2501{
2502 if (length < 3)
2503 return false;
2504
2505 auto &callee = compiler.get<SPIRFunction>(args[2]);
2506 args += 3;
2507 length -= 3;
2508 push_remap_parameters(callee, args, length);
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002509 functions.push(&callee);
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002510 return true;
2511}
2512
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002513bool Compiler::CombinedImageSamplerHandler::end_function_scope(const uint32_t *args, uint32_t length)
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002514{
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002515 if (length < 3)
2516 return false;
2517
2518 auto &callee = compiler.get<SPIRFunction>(args[2]);
2519 args += 3;
2520 length -= 3;
2521
2522 // There are two types of cases we have to handle,
2523 // a callee might call sampler2D(texture2D, sampler) directly where
2524 // one or more parameters originate from parameters.
2525 // Alternatively, we need to provide combined image samplers to our callees,
2526 // and in this case we need to add those as well.
2527
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002528 pop_remap_parameters();
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002529
2530 // Our callee has now been processed at least once.
2531 // No point in doing it again.
2532 callee.do_combined_parameters = false;
2533
2534 auto &params = functions.top()->combined_parameters;
2535 functions.pop();
2536 if (functions.empty())
2537 return true;
2538
2539 auto &caller = *functions.top();
2540 if (caller.do_combined_parameters)
2541 {
2542 for (auto &param : params)
2543 {
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002544 uint32_t image_id = param.global_image ? param.image_id : args[param.image_id];
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002545 uint32_t sampler_id = param.global_sampler ? param.sampler_id : args[param.sampler_id];
2546
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002547 auto *i = compiler.maybe_get_backing_variable(image_id);
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002548 auto *s = compiler.maybe_get_backing_variable(sampler_id);
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002549 if (i)
2550 image_id = i->self;
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002551 if (s)
2552 sampler_id = s->self;
2553
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002554 register_combined_image_sampler(caller, image_id, sampler_id);
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002555 }
2556 }
2557
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002558 return true;
2559}
2560
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002561void Compiler::CombinedImageSamplerHandler::register_combined_image_sampler(SPIRFunction &caller, uint32_t image_id,
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002562 uint32_t sampler_id)
2563{
2564 // We now have a texture ID and a sampler ID which will either be found as a global
2565 // or a parameter in our own function. If both are global, they will not need a parameter,
2566 // otherwise, add it to our list.
2567 SPIRFunction::CombinedImageSamplerParameter param = {
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002568 0u, image_id, sampler_id, true, true,
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002569 };
2570
2571 auto texture_itr = find_if(begin(caller.arguments), end(caller.arguments),
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002572 [image_id](const SPIRFunction::Parameter &p) { return p.id == image_id; });
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002573 auto sampler_itr = find_if(begin(caller.arguments), end(caller.arguments),
2574 [sampler_id](const SPIRFunction::Parameter &p) { return p.id == sampler_id; });
2575
2576 if (texture_itr != end(caller.arguments))
2577 {
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002578 param.global_image = false;
Hans-Kristian Arntzen7630d3c2016-11-21 12:14:02 +01002579 param.image_id = uint32_t(texture_itr - begin(caller.arguments));
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002580 }
2581
2582 if (sampler_itr != end(caller.arguments))
2583 {
2584 param.global_sampler = false;
Hans-Kristian Arntzen7630d3c2016-11-21 12:14:02 +01002585 param.sampler_id = uint32_t(sampler_itr - begin(caller.arguments));
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002586 }
2587
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002588 if (param.global_image && param.global_sampler)
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002589 return;
2590
2591 auto itr = find_if(begin(caller.combined_parameters), end(caller.combined_parameters),
2592 [&param](const SPIRFunction::CombinedImageSamplerParameter &p) {
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002593 return param.image_id == p.image_id && param.sampler_id == p.sampler_id &&
2594 param.global_image == p.global_image && param.global_sampler == p.global_sampler;
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002595 });
2596
2597 if (itr == end(caller.combined_parameters))
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002598 {
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002599 uint32_t id = compiler.increase_bound_by(3);
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002600 auto type_id = id + 0;
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002601 auto ptr_type_id = id + 1;
2602 auto combined_id = id + 2;
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002603 auto &base = compiler.expression_type(image_id);
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002604 auto &type = compiler.set<SPIRType>(type_id);
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002605 auto &ptr_type = compiler.set<SPIRType>(ptr_type_id);
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002606
2607 type = base;
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002608 type.self = type_id;
2609 type.basetype = SPIRType::SampledImage;
2610 type.pointer = false;
2611 type.storage = StorageClassGeneric;
2612
2613 ptr_type = type;
2614 ptr_type.pointer = true;
2615 ptr_type.storage = StorageClassUniformConstant;
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002616
2617 // Build new variable.
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002618 compiler.set<SPIRVariable>(combined_id, ptr_type_id, StorageClassFunction, 0);
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002619
2620 // Inherit RelaxedPrecision (and potentially other useful flags if deemed relevant).
2621 auto &new_flags = compiler.meta[combined_id].decoration.decoration_flags;
2622 auto old_flags = compiler.meta[sampler_id].decoration.decoration_flags;
2623 new_flags = old_flags & (1ull << DecorationRelaxedPrecision);
2624
2625 param.id = combined_id;
2626
2627 compiler.set_name(combined_id,
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002628 join("SPIRV_Cross_Combined", compiler.to_name(image_id), compiler.to_name(sampler_id)));
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002629
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002630 caller.combined_parameters.push_back(param);
Hans-Kristian Arntzen9cb86162017-02-05 10:50:14 +01002631 caller.shadow_arguments.push_back({ ptr_type_id, combined_id, 0u, 0u, true });
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002632 }
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002633}
2634
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002635bool Compiler::CombinedImageSamplerHandler::handle(Op opcode, const uint32_t *args, uint32_t length)
2636{
2637 // We need to figure out where samplers and images are loaded from, so do only the bare bones compilation we need.
2638 switch (opcode)
2639 {
2640 case OpLoad:
2641 {
2642 if (length < 3)
2643 return false;
2644
2645 uint32_t result_type = args[0];
2646
2647 auto &type = compiler.get<SPIRType>(result_type);
2648 bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1;
2649 bool separate_sampler = type.basetype == SPIRType::Sampler;
2650
Hans-Kristian Arntzen378fbe82016-09-11 13:47:06 +02002651 // If not separate image or sampler, don't bother.
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002652 if (!separate_image && !separate_sampler)
2653 return true;
2654
2655 uint32_t id = args[1];
2656 uint32_t ptr = args[2];
2657 compiler.set<SPIRExpression>(id, "", result_type, true);
2658 compiler.register_read(id, ptr, true);
2659 return true;
2660 }
2661
2662 case OpInBoundsAccessChain:
2663 case OpAccessChain:
2664 {
2665 if (length < 3)
2666 return false;
2667
2668 // Technically, it is possible to have arrays of textures and arrays of samplers and combine them, but this becomes essentially
2669 // impossible to implement, since we don't know which concrete sampler we are accessing.
2670 // One potential way is to create a combinatorial explosion where N textures and M samplers are combined into N * M sampler2Ds,
2671 // but this seems ridiculously complicated for a problem which is easy to work around.
2672 // Checking access chains like this assumes we don't have samplers or textures inside uniform structs, but this makes no sense.
2673
2674 auto &type = compiler.get<SPIRType>(args[0]);
2675 bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1;
2676 bool separate_sampler = type.basetype == SPIRType::Sampler;
2677 if (separate_image)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002678 SPIRV_CROSS_THROW(
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002679 "Attempting to use arrays of separate images. This is not possible to statically remap to plain GLSL.");
2680 if (separate_sampler)
Panagiotis Christopoulos Charitos946f7792016-12-12 22:33:22 +01002681 SPIRV_CROSS_THROW("Attempting to use arrays of separate samplers. This is not possible to statically "
2682 "remap to plain GLSL.");
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002683 return true;
2684 }
2685
2686 case OpSampledImage:
2687 // Do it outside.
2688 break;
2689
2690 default:
2691 return true;
2692 }
2693
2694 if (length < 4)
2695 return false;
2696
Hans-Kristian Arntzendfb65972016-09-11 12:05:20 +02002697 // Registers sampler2D calls used in case they are parameters so
2698 // that their callees know which combined image samplers to propagate down the call stack.
2699 if (!functions.empty())
2700 {
2701 auto &callee = *functions.top();
2702 if (callee.do_combined_parameters)
2703 {
2704 uint32_t image_id = args[2];
2705
2706 auto *image = compiler.maybe_get_backing_variable(image_id);
2707 if (image)
2708 image_id = image->self;
2709
2710 uint32_t sampler_id = args[3];
2711 auto *sampler = compiler.maybe_get_backing_variable(sampler_id);
2712 if (sampler)
2713 sampler_id = sampler->self;
2714
2715 register_combined_image_sampler(callee, image_id, sampler_id);
2716 }
2717 }
2718
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002719 // For function calls, we need to remap IDs which are function parameters into global variables.
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002720 // This information is statically known from the current place in the call stack.
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002721 // Function parameters are not necessarily pointers, so if we don't have a backing variable, remapping will know
2722 // which backing variable the image/sample came from.
Hans-Kristian Arntzendfb65972016-09-11 12:05:20 +02002723 uint32_t image_id = remap_parameter(args[2]);
2724 uint32_t sampler_id = remap_parameter(args[3]);
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002725
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002726 auto itr = find_if(begin(compiler.combined_image_samplers), end(compiler.combined_image_samplers),
2727 [image_id, sampler_id](const CombinedImageSampler &combined) {
2728 return combined.image_id == image_id && combined.sampler_id == sampler_id;
2729 });
2730
2731 if (itr == end(compiler.combined_image_samplers))
2732 {
2733 auto id = compiler.increase_bound_by(2);
2734 auto type_id = id + 0;
2735 auto combined_id = id + 1;
2736 auto sampled_type = args[0];
2737
2738 // Make a new type, pointer to OpTypeSampledImage, so we can make a variable of this type.
2739 // We will probably have this type lying around, but it doesn't hurt to make duplicates for internal purposes.
2740 auto &type = compiler.set<SPIRType>(type_id);
2741 auto &base = compiler.get<SPIRType>(sampled_type);
2742 type = base;
2743 type.pointer = true;
2744 type.storage = StorageClassUniformConstant;
2745
2746 // Build new variable.
Hans-Kristian Arntzen948930b2016-09-11 12:36:12 +02002747 compiler.set<SPIRVariable>(combined_id, type_id, StorageClassUniformConstant, 0);
Hans-Kristian Arntzen14bc1ff2016-09-10 18:07:52 +02002748
2749 // Inherit RelaxedPrecision (and potentially other useful flags if deemed relevant).
2750 auto &new_flags = compiler.meta[combined_id].decoration.decoration_flags;
2751 auto old_flags = compiler.meta[sampler_id].decoration.decoration_flags;
2752 new_flags = old_flags & (1ull << DecorationRelaxedPrecision);
2753
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002754 compiler.combined_image_samplers.push_back({ combined_id, image_id, sampler_id });
2755 }
Hans-Kristian Arntzendd1513b2016-09-10 21:52:22 +02002756
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002757 return true;
2758}
2759
Hans-Kristian Arntzenbcb55602016-09-10 13:56:36 +02002760void Compiler::build_combined_image_samplers()
2761{
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002762 for (auto &id : ids)
2763 {
2764 if (id.get_type() == TypeFunction)
2765 {
2766 auto &func = id.get<SPIRFunction>();
2767 func.combined_parameters.clear();
Hans-Kristian Arntzen313cb5f2016-09-11 12:54:08 +02002768 func.shadow_arguments.clear();
Hans-Kristian Arntzened98a8e2016-09-11 11:39:20 +02002769 func.do_combined_parameters = true;
2770 }
2771 }
2772
Hans-Kristian Arntzen71bacc42016-09-10 17:48:52 +02002773 combined_image_samplers.clear();
2774 CombinedImageSamplerHandler handler(*this);
2775 traverse_all_reachable_opcodes(get<SPIRFunction>(entry_point), handler);
Hans-Kristian Arntzenbcb55602016-09-10 13:56:36 +02002776}
Hans-Kristian Arntzen6bd545b2016-09-17 15:16:07 +02002777
2778vector<SpecializationConstant> Compiler::get_specialization_constants() const
2779{
2780 vector<SpecializationConstant> spec_consts;
2781 for (auto &id : ids)
2782 {
2783 if (id.get_type() == TypeConstant)
2784 {
2785 auto &c = id.get<SPIRConstant>();
2786 if (c.specialization)
2787 {
2788 spec_consts.push_back({ c.self, get_decoration(c.self, DecorationSpecId) });
2789 }
2790 }
2791 }
2792 return spec_consts;
2793}
2794
2795SPIRConstant &Compiler::get_constant(uint32_t id)
2796{
2797 return get<SPIRConstant>(id);
2798}
2799
2800const SPIRConstant &Compiler::get_constant(uint32_t id) const
2801{
2802 return get<SPIRConstant>(id);
2803}
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002804
Hans-Kristian Arntzenb2c2e642017-03-25 16:25:30 +01002805static bool exists_unaccessed_path_to_return(const CFG &cfg, uint32_t block,
2806 const unordered_set<uint32_t> &blocks)
2807{
2808 // This block accesses the variable.
2809 if (blocks.find(block) != end(blocks))
2810 return false;
2811
2812 // We are at the end of the CFG.
2813 if (cfg.get_succeeding_edges(block).empty())
2814 return true;
2815
2816 // If any of our successors have a path to the end, there exists a path from block.
2817 for (auto &succ : cfg.get_succeeding_edges(block))
2818 if (exists_unaccessed_path_to_return(cfg, succ, blocks))
2819 return true;
2820
2821 return false;
2822}
2823
2824void Compiler::analyze_parameter_preservation(SPIRFunction &entry, const CFG &cfg,
2825 const unordered_map<uint32_t, unordered_set<uint32_t>> &variable_to_blocks)
2826{
2827 for (auto &arg : entry.arguments)
2828 {
2829 // Non-pointers are always inputs.
2830 auto &type = get<SPIRType>(arg.type);
2831 if (!type.pointer)
2832 continue;
2833
2834 // Opaque argument types are always in
2835 bool potential_preserve;
2836 switch (type.basetype)
2837 {
2838 case SPIRType::Sampler:
2839 case SPIRType::Image:
2840 case SPIRType::SampledImage:
2841 case SPIRType::AtomicCounter:
2842 potential_preserve = false;
2843 break;
2844
2845 default:
2846 potential_preserve = true;
2847 break;
2848 }
2849
2850 if (!potential_preserve)
2851 continue;
2852
2853 auto itr = variable_to_blocks.find(arg.id);
2854 if (itr == end(variable_to_blocks))
2855 {
2856 // Variable is never accessed.
2857 continue;
2858 }
2859
2860 // If there is a path through the CFG where no block writes to the variable, the variable will be in an undefined state
2861 // when the function returns. We therefore need to implicitly preserve the variable in case there are writers in the function.
2862 // Major case here is if a function is
2863 // void foo(int &var) { if (cond) var = 10; }
2864 // Using read/write counts, we will think it's just an out variable, but it really needs to be inout,
2865 // because if we don't write anything whatever we put into the function must return back to the caller.
2866 if (exists_unaccessed_path_to_return(cfg, entry.entry_block, itr->second))
2867 arg.read_count++;
2868 }
2869}
2870
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002871void Compiler::analyze_variable_scope(SPIRFunction &entry)
2872{
2873 struct AccessHandler : OpcodeHandler
2874 {
2875 public:
2876 AccessHandler(Compiler &compiler_)
2877 : compiler(compiler_)
2878 {
2879 }
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +01002880
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002881 bool follow_function_call(const SPIRFunction &)
2882 {
2883 // Only analyze within this function.
2884 return false;
2885 }
2886
2887 void set_current_block(const SPIRBlock &block)
2888 {
2889 current_block = &block;
Hans-Kristian Arntzenedbe8672016-11-17 22:15:07 +01002890
2891 // If we're branching to a block which uses OpPhi, in GLSL
2892 // this will be a variable write when we branch,
2893 // so we need to track access to these variables as well to
2894 // have a complete picture.
2895 const auto test_phi = [this, &block](uint32_t to) {
2896 auto &next = compiler.get<SPIRBlock>(to);
2897 for (auto &phi : next.phi_variables)
2898 if (phi.parent == block.self)
2899 accessed_variables_to_block[phi.function_variable].insert(block.self);
2900 };
2901
2902 switch (block.terminator)
2903 {
2904 case SPIRBlock::Direct:
2905 test_phi(block.next_block);
2906 break;
2907
2908 case SPIRBlock::Select:
2909 test_phi(block.true_block);
2910 test_phi(block.false_block);
2911 break;
2912
2913 case SPIRBlock::MultiSelect:
2914 for (auto &target : block.cases)
2915 test_phi(target.block);
2916 if (block.default_block)
2917 test_phi(block.default_block);
2918 break;
2919
2920 default:
2921 break;
2922 }
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002923 }
2924
2925 bool handle(spv::Op op, const uint32_t *args, uint32_t length)
2926 {
2927 switch (op)
2928 {
2929 case OpStore:
2930 {
2931 if (length < 2)
2932 return false;
2933
2934 uint32_t ptr = args[0];
2935 auto *var = compiler.maybe_get_backing_variable(ptr);
2936 if (var && var->storage == StorageClassFunction)
2937 accessed_variables_to_block[var->self].insert(current_block->self);
2938 break;
2939 }
2940
2941 case OpAccessChain:
2942 case OpInBoundsAccessChain:
2943 {
2944 if (length < 3)
2945 return false;
2946
2947 uint32_t ptr = args[2];
2948 auto *var = compiler.maybe_get<SPIRVariable>(ptr);
2949 if (var && var->storage == StorageClassFunction)
2950 accessed_variables_to_block[var->self].insert(current_block->self);
2951 break;
2952 }
2953
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +01002954 case OpCopyMemory:
2955 {
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01002956 if (length < 2)
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +01002957 return false;
2958
2959 uint32_t lhs = args[0];
2960 uint32_t rhs = args[1];
2961 auto *var = compiler.maybe_get_backing_variable(lhs);
2962 if (var && var->storage == StorageClassFunction)
2963 accessed_variables_to_block[var->self].insert(current_block->self);
2964
2965 var = compiler.maybe_get_backing_variable(rhs);
2966 if (var && var->storage == StorageClassFunction)
2967 accessed_variables_to_block[var->self].insert(current_block->self);
2968 break;
2969 }
2970
2971 case OpCopyObject:
2972 {
2973 if (length < 3)
2974 return false;
2975
2976 auto *var = compiler.maybe_get_backing_variable(args[2]);
2977 if (var && var->storage == StorageClassFunction)
2978 accessed_variables_to_block[var->self].insert(current_block->self);
2979 break;
2980 }
2981
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01002982 case OpLoad:
2983 {
2984 if (length < 3)
2985 return false;
2986 uint32_t ptr = args[2];
2987 auto *var = compiler.maybe_get_backing_variable(ptr);
2988 if (var && var->storage == StorageClassFunction)
2989 accessed_variables_to_block[var->self].insert(current_block->self);
2990 break;
2991 }
2992
Hans-Kristian Arntzen0c9683c2016-11-18 09:59:54 +01002993 case OpFunctionCall:
2994 {
2995 if (length < 3)
2996 return false;
2997
2998 length -= 3;
2999 args += 3;
3000 for (uint32_t i = 0; i < length; i++)
3001 {
3002 auto *var = compiler.maybe_get_backing_variable(args[i]);
3003 if (var && var->storage == StorageClassFunction)
3004 accessed_variables_to_block[var->self].insert(current_block->self);
3005 }
3006 break;
3007 }
3008
3009 case OpPhi:
3010 {
3011 if (length < 2)
3012 return false;
3013
3014 // Phi nodes are implemented as function variables, so register an access here.
3015 accessed_variables_to_block[args[1]].insert(current_block->self);
3016 break;
3017 }
3018
3019 // Atomics shouldn't be able to access function-local variables.
3020 // Some GLSL builtins access a pointer.
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003021
3022 default:
3023 break;
3024 }
3025 return true;
3026 }
3027
3028 Compiler &compiler;
3029 std::unordered_map<uint32_t, std::unordered_set<uint32_t>> accessed_variables_to_block;
3030 const SPIRBlock *current_block = nullptr;
3031 } handler(*this);
3032
3033 // First, we map out all variable access within a function.
3034 // Essentially a map of block -> { variables accessed in the basic block }
Hans-Kristian Arntzen7630d3c2016-11-21 12:14:02 +01003035 this->traverse_all_reachable_opcodes(entry, handler);
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003036
3037 // Compute the control flow graph for this function.
3038 CFG cfg(*this, entry);
3039
Hans-Kristian Arntzenb2c2e642017-03-25 16:25:30 +01003040 // Analyze if there are parameters which need to be implicitly preserved with an "in" qualifier.
3041 analyze_parameter_preservation(entry, cfg, handler.accessed_variables_to_block);
3042
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003043 unordered_map<uint32_t, uint32_t> potential_loop_variables;
3044
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003045 // For each variable which is statically accessed.
3046 for (auto &var : handler.accessed_variables_to_block)
3047 {
3048 DominatorBuilder builder(cfg);
3049 auto &blocks = var.second;
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003050 auto &type = this->expression_type(var.first);
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003051
3052 // Figure out which block is dominating all accesses of those variables.
3053 for (auto &block : blocks)
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003054 {
Hans-Kristian Arntzena714d422016-12-16 12:43:12 +01003055 // If we're accessing a variable inside a continue block, this variable might be a loop variable.
3056 // We can only use loop variables with scalars, as we cannot track static expressions for vectors.
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003057 if (this->is_continue(block) && type.vecsize == 1 && type.columns == 1)
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003058 {
3059 // The variable is used in multiple continue blocks, this is not a loop
3060 // candidate, signal that by setting block to -1u.
3061 auto &potential = potential_loop_variables[var.first];
3062
3063 if (potential == 0)
3064 potential = block;
3065 else
Graham Wihlidalfadc1f92017-01-05 20:14:53 +01003066 potential = ~(0u);
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003067 }
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003068 builder.add_block(block);
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003069 }
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003070
Hans-Kristian Arntzen5ff11cc2016-11-18 16:45:11 +01003071 builder.lift_continue_block_dominator();
3072
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003073 // Add it to a per-block list of variables.
3074 uint32_t dominating_block = builder.get_dominator();
Hans-Kristian Arntzenedbe8672016-11-17 22:15:07 +01003075 // If all blocks here are dead code, this will be 0, so the variable in question
3076 // will be completely eliminated.
3077 if (dominating_block)
3078 {
Hans-Kristian Arntzen7630d3c2016-11-21 12:14:02 +01003079 auto &block = this->get<SPIRBlock>(dominating_block);
Hans-Kristian Arntzenedbe8672016-11-17 22:15:07 +01003080 block.dominated_variables.push_back(var.first);
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003081 this->get<SPIRVariable>(var.first).dominator = dominating_block;
Hans-Kristian Arntzenedbe8672016-11-17 22:15:07 +01003082 }
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003083 }
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003084
3085 // Now, try to analyze whether or not these variables are actually loop variables.
3086 for (auto &loop_variable : potential_loop_variables)
3087 {
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003088 auto &var = this->get<SPIRVariable>(loop_variable.first);
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003089 auto dominator = var.dominator;
3090 auto block = loop_variable.second;
3091
3092 // The variable was accessed in multiple continue blocks, ignore.
Graham Wihlidalfadc1f92017-01-05 20:14:53 +01003093 if (block == ~(0u) || block == 0)
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003094 continue;
3095
3096 // Dead code.
3097 if (dominator == 0)
3098 continue;
3099
3100 uint32_t header = 0;
3101
3102 // Find the loop header for this block.
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003103 for (auto b : this->loop_blocks)
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003104 {
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003105 auto &potential_header = this->get<SPIRBlock>(b);
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003106 if (potential_header.continue_block == block)
3107 {
3108 header = b;
3109 break;
3110 }
3111 }
3112
3113 assert(header);
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003114 auto &header_block = this->get<SPIRBlock>(header);
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003115
3116 // Now, there are two conditions we need to meet for the variable to be a loop variable.
3117 // 1. The dominating block must have a branch-free path to the loop header,
3118 // this way we statically know which expression should be part of the loop variable initializer.
3119
3120 // Walk from the dominator, if there is one straight edge connecting
3121 // dominator and loop header, we statically know the loop initializer.
3122 bool static_loop_init = true;
3123 while (dominator != header)
3124 {
3125 auto &succ = cfg.get_succeeding_edges(dominator);
3126 if (succ.size() != 1)
3127 {
3128 static_loop_init = false;
3129 break;
3130 }
3131
3132 auto &pred = cfg.get_preceding_edges(succ.front());
3133 if (pred.size() != 1 || pred.front() != dominator)
3134 {
3135 static_loop_init = false;
3136 break;
3137 }
3138
3139 dominator = succ.front();
3140 }
3141
3142 if (!static_loop_init)
3143 continue;
3144
3145 // The second condition we need to meet is that no access after the loop
3146 // merge can occur. Walk the CFG to see if we find anything.
3147 auto &blocks = handler.accessed_variables_to_block[loop_variable.first];
3148 cfg.walk_from(header_block.merge_block, [&](uint32_t walk_block) {
3149 // We found a block which accesses the variable outside the loop.
3150 if (blocks.find(walk_block) != end(blocks))
3151 static_loop_init = false;
3152 });
3153
3154 if (!static_loop_init)
3155 continue;
3156
3157 // We have a loop variable.
3158 header_block.loop_variables.push_back(loop_variable.first);
Hans-Kristian Arntzen44b32162016-12-16 14:01:09 +01003159 // Need to sort here as variables come from an unordered container, and pushing stuff in wrong order
3160 // will break reproducability in regression runs.
3161 sort(begin(header_block.loop_variables), end(header_block.loop_variables));
Hans-Kristian Arntzence3fe292017-01-12 10:57:44 +01003162 this->get<SPIRVariable>(loop_variable.first).loop_variable = true;
Hans-Kristian Arntzen4f07a322016-12-15 17:14:47 +01003163 }
Hans-Kristian Arntzendad4a342016-11-11 18:04:14 +01003164}
Hans-Kristian Arntzen016b1d82017-01-21 10:07:38 +01003165
3166uint64_t Compiler::get_buffer_block_flags(const SPIRVariable &var)
3167{
3168 auto &type = get<SPIRType>(var.basetype);
Hans-Kristian Arntzen8a80e622017-01-22 08:51:24 +01003169 assert(type.basetype == SPIRType::Struct);
Hans-Kristian Arntzen016b1d82017-01-21 10:07:38 +01003170
3171 // Some flags like non-writable, non-readable are actually found
3172 // as member decorations. If all members have a decoration set, propagate
3173 // the decoration up as a regular variable decoration.
3174 uint64_t base_flags = meta[var.self].decoration.decoration_flags;
Hans-Kristian Arntzen016b1d82017-01-21 10:07:38 +01003175
Hans-Kristian Arntzen8a80e622017-01-22 08:51:24 +01003176 if (type.member_types.empty())
3177 return base_flags;
3178
3179 uint64_t all_members_flag_mask = ~(0ull);
3180 for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++)
3181 all_members_flag_mask &= get_member_decoration_mask(type.self, i);
3182
3183 return base_flags | all_members_flag_mask;
Hans-Kristian Arntzen016b1d82017-01-21 10:07:38 +01003184}
Hans-Kristian Arntzen95409792017-01-21 12:29:20 +01003185
3186bool Compiler::get_common_basic_type(const SPIRType &type, SPIRType::BaseType &base_type)
3187{
3188 if (type.basetype == SPIRType::Struct)
3189 {
3190 base_type = SPIRType::Unknown;
3191 for (auto &member_type : type.member_types)
3192 {
3193 SPIRType::BaseType member_base;
3194 if (!get_common_basic_type(get<SPIRType>(member_type), member_base))
3195 return false;
3196
3197 if (base_type == SPIRType::Unknown)
3198 base_type = member_base;
3199 else if (base_type != member_base)
3200 return false;
3201 }
3202 return true;
3203 }
3204 else
3205 {
3206 base_type = type.basetype;
3207 return true;
3208 }
3209}
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01003210
3211bool Compiler::ActiveBuiltinHandler::handle(spv::Op opcode, const uint32_t *args, uint32_t length)
3212{
3213 const auto add_if_builtin = [&](uint32_t id) {
3214 // Only handles variables here.
3215 // Builtins which are part of a block are handled in AccessChain.
3216 auto *var = compiler.maybe_get<SPIRVariable>(id);
3217 if (var && compiler.meta[id].decoration.builtin)
Hans-Kristian Arntzenbdea1a42017-03-06 16:50:15 +01003218 {
3219 auto &type = compiler.get<SPIRType>(var->basetype);
3220 auto &flags =
Hans-Kristian Arntzen2ebe1a82017-03-06 16:50:46 +01003221 type.storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins;
Hans-Kristian Arntzenbdea1a42017-03-06 16:50:15 +01003222 flags |= 1ull << compiler.meta[id].decoration.builtin_type;
3223 }
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01003224 };
3225
3226 switch (opcode)
3227 {
3228 case OpStore:
3229 if (length < 1)
3230 return false;
3231
3232 add_if_builtin(args[0]);
3233 break;
3234
3235 case OpCopyMemory:
3236 if (length < 2)
3237 return false;
3238
3239 add_if_builtin(args[0]);
3240 add_if_builtin(args[1]);
3241 break;
3242
3243 case OpCopyObject:
3244 case OpLoad:
3245 if (length < 3)
3246 return false;
3247
3248 add_if_builtin(args[2]);
3249 break;
3250
3251 case OpFunctionCall:
3252 {
3253 if (length < 3)
3254 return false;
3255
3256 uint32_t count = length - 3;
3257 args += 3;
3258 for (uint32_t i = 0; i < count; i++)
3259 add_if_builtin(args[i]);
3260 break;
3261 }
3262
3263 case OpAccessChain:
3264 case OpInBoundsAccessChain:
3265 {
3266 if (length < 4)
3267 return false;
3268
3269 // Only consider global variables, cannot consider variables in functions yet, or other
3270 // access chains as they have not been created yet.
3271 auto *var = compiler.maybe_get<SPIRVariable>(args[2]);
3272 if (!var)
3273 break;
3274
3275 auto *type = &compiler.get<SPIRType>(var->basetype);
3276
3277 // Start traversing type hierarchy at the proper non-pointer types.
3278 while (type->pointer)
3279 {
3280 assert(type->parent_type);
3281 type = &compiler.get<SPIRType>(type->parent_type);
3282 }
3283
Hans-Kristian Arntzen2ebe1a82017-03-06 16:50:46 +01003284 auto &flags =
3285 type->storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins;
Hans-Kristian Arntzenbdea1a42017-03-06 16:50:15 +01003286
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01003287 uint32_t count = length - 3;
3288 args += 3;
3289 for (uint32_t i = 0; i < count; i++)
3290 {
3291 // Arrays
3292 if (!type->array.empty())
3293 {
3294 type = &compiler.get<SPIRType>(type->parent_type);
3295 }
3296 // Structs
3297 else if (type->basetype == SPIRType::Struct)
3298 {
3299 uint32_t index = compiler.get<SPIRConstant>(args[i]).scalar();
3300
3301 if (index < uint32_t(compiler.meta[type->self].members.size()))
3302 {
3303 auto &decorations = compiler.meta[type->self].members[index];
3304 if (decorations.builtin)
Hans-Kristian Arntzenbdea1a42017-03-06 16:50:15 +01003305 flags |= 1ull << decorations.builtin_type;
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01003306 }
3307
3308 type = &compiler.get<SPIRType>(type->member_types[index]);
3309 }
3310 else
3311 {
3312 // No point in traversing further. We won't find any extra builtins.
3313 break;
3314 }
3315 }
3316 break;
3317 }
3318
3319 default:
3320 break;
3321 }
3322
3323 return true;
3324}
3325
3326void Compiler::update_active_builtins()
3327{
Hans-Kristian Arntzenbdea1a42017-03-06 16:50:15 +01003328 active_input_builtins = 0;
3329 active_output_builtins = 0;
Hans-Kristian Arntzen099f3072017-03-06 15:21:00 +01003330 ActiveBuiltinHandler handler(*this);
3331 traverse_all_reachable_opcodes(get<SPIRFunction>(entry_point), handler);
3332}