1 | /* |
2 | * Copyright 2018-2021 Bradley Austin Davis |
3 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | |
18 | /* |
19 | * At your option, you may choose to accept this material under either: |
20 | * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or |
21 | * 2. The MIT License, found at <http://opensource.org/licenses/MIT>. |
22 | */ |
23 | |
24 | #include "spirv_reflect.hpp" |
25 | #include "spirv_glsl.hpp" |
26 | #include <iomanip> |
27 | |
28 | using namespace spv; |
29 | using namespace SPIRV_CROSS_NAMESPACE; |
30 | using namespace std; |
31 | |
32 | namespace simple_json |
33 | { |
34 | enum class Type |
35 | { |
36 | Object, |
37 | Array, |
38 | }; |
39 | |
40 | using State = std::pair<Type, bool>; |
41 | using Stack = std::stack<State>; |
42 | |
43 | class Stream |
44 | { |
45 | Stack stack; |
46 | StringStream<> buffer; |
47 | uint32_t indent{ 0 }; |
48 | char current_locale_radix_character = '.'; |
49 | |
50 | public: |
51 | void set_current_locale_radix_character(char c) |
52 | { |
53 | current_locale_radix_character = c; |
54 | } |
55 | |
56 | void begin_json_object(); |
57 | void end_json_object(); |
58 | void emit_json_key(const std::string &key); |
59 | void emit_json_key_value(const std::string &key, const std::string &value); |
60 | void emit_json_key_value(const std::string &key, bool value); |
61 | void emit_json_key_value(const std::string &key, uint32_t value); |
62 | void emit_json_key_value(const std::string &key, int32_t value); |
63 | void emit_json_key_value(const std::string &key, float value); |
64 | void emit_json_key_object(const std::string &key); |
65 | void emit_json_key_array(const std::string &key); |
66 | |
67 | void begin_json_array(); |
68 | void end_json_array(); |
69 | void emit_json_array_value(const std::string &value); |
70 | void emit_json_array_value(uint32_t value); |
71 | void emit_json_array_value(bool value); |
72 | |
73 | std::string str() const |
74 | { |
75 | return buffer.str(); |
76 | } |
77 | |
78 | private: |
79 | inline void statement_indent() |
80 | { |
81 | for (uint32_t i = 0; i < indent; i++) |
82 | buffer << " " ; |
83 | } |
84 | |
85 | template <typename T> |
86 | inline void statement_inner(T &&t) |
87 | { |
88 | buffer << std::forward<T>(t); |
89 | } |
90 | |
91 | template <typename T, typename... Ts> |
92 | inline void statement_inner(T &&t, Ts &&... ts) |
93 | { |
94 | buffer << std::forward<T>(t); |
95 | statement_inner(std::forward<Ts>(ts)...); |
96 | } |
97 | |
98 | template <typename... Ts> |
99 | inline void statement(Ts &&... ts) |
100 | { |
101 | statement_indent(); |
102 | statement_inner(std::forward<Ts>(ts)...); |
103 | buffer << '\n'; |
104 | } |
105 | |
106 | template <typename... Ts> |
107 | void statement_no_return(Ts &&... ts) |
108 | { |
109 | statement_indent(); |
110 | statement_inner(std::forward<Ts>(ts)...); |
111 | } |
112 | }; |
113 | } // namespace simple_json |
114 | |
115 | using namespace simple_json; |
116 | |
117 | // Hackery to emit JSON without using nlohmann/json C++ library (which requires a |
118 | // higher level of compiler compliance than is required by SPIRV-Cross |
119 | void Stream::begin_json_array() |
120 | { |
121 | if (!stack.empty() && stack.top().second) |
122 | { |
123 | statement_inner(t: ",\n" ); |
124 | } |
125 | statement(ts: "[" ); |
126 | ++indent; |
127 | stack.emplace(args: Type::Array, args: false); |
128 | } |
129 | |
130 | void Stream::end_json_array() |
131 | { |
132 | if (stack.empty() || stack.top().first != Type::Array) |
133 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
134 | if (stack.top().second) |
135 | { |
136 | statement_inner(t: "\n" ); |
137 | } |
138 | --indent; |
139 | statement_no_return(ts: "]" ); |
140 | stack.pop(); |
141 | if (!stack.empty()) |
142 | { |
143 | stack.top().second = true; |
144 | } |
145 | } |
146 | |
147 | void Stream::emit_json_array_value(const std::string &value) |
148 | { |
149 | if (stack.empty() || stack.top().first != Type::Array) |
150 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
151 | |
152 | if (stack.top().second) |
153 | statement_inner(t: ",\n" ); |
154 | |
155 | statement_no_return(ts: "\"" , ts: value, ts: "\"" ); |
156 | stack.top().second = true; |
157 | } |
158 | |
159 | void Stream::emit_json_array_value(uint32_t value) |
160 | { |
161 | if (stack.empty() || stack.top().first != Type::Array) |
162 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
163 | if (stack.top().second) |
164 | statement_inner(t: ",\n" ); |
165 | statement_no_return(ts: std::to_string(val: value)); |
166 | stack.top().second = true; |
167 | } |
168 | |
169 | void Stream::emit_json_array_value(bool value) |
170 | { |
171 | if (stack.empty() || stack.top().first != Type::Array) |
172 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
173 | if (stack.top().second) |
174 | statement_inner(t: ",\n" ); |
175 | statement_no_return(ts: value ? "true" : "false" ); |
176 | stack.top().second = true; |
177 | } |
178 | |
179 | void Stream::begin_json_object() |
180 | { |
181 | if (!stack.empty() && stack.top().second) |
182 | { |
183 | statement_inner(t: ",\n" ); |
184 | } |
185 | statement(ts: "{" ); |
186 | ++indent; |
187 | stack.emplace(args: Type::Object, args: false); |
188 | } |
189 | |
190 | void Stream::end_json_object() |
191 | { |
192 | if (stack.empty() || stack.top().first != Type::Object) |
193 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
194 | if (stack.top().second) |
195 | { |
196 | statement_inner(t: "\n" ); |
197 | } |
198 | --indent; |
199 | statement_no_return(ts: "}" ); |
200 | stack.pop(); |
201 | if (!stack.empty()) |
202 | { |
203 | stack.top().second = true; |
204 | } |
205 | } |
206 | |
207 | void Stream::emit_json_key(const std::string &key) |
208 | { |
209 | if (stack.empty() || stack.top().first != Type::Object) |
210 | SPIRV_CROSS_THROW("Invalid JSON state" ); |
211 | |
212 | if (stack.top().second) |
213 | statement_inner(t: ",\n" ); |
214 | statement_no_return(ts: "\"" , ts: key, ts: "\" : " ); |
215 | stack.top().second = true; |
216 | } |
217 | |
218 | void Stream::emit_json_key_value(const std::string &key, const std::string &value) |
219 | { |
220 | emit_json_key(key); |
221 | statement_inner(t: "\"" , ts: value, ts: "\"" ); |
222 | } |
223 | |
224 | void Stream::emit_json_key_value(const std::string &key, uint32_t value) |
225 | { |
226 | emit_json_key(key); |
227 | statement_inner(t&: value); |
228 | } |
229 | |
230 | void Stream::emit_json_key_value(const std::string &key, int32_t value) |
231 | { |
232 | emit_json_key(key); |
233 | statement_inner(t&: value); |
234 | } |
235 | |
236 | void Stream::emit_json_key_value(const std::string &key, float value) |
237 | { |
238 | emit_json_key(key); |
239 | statement_inner(t: convert_to_string(t: value, locale_radix_point: current_locale_radix_character)); |
240 | } |
241 | |
242 | void Stream::emit_json_key_value(const std::string &key, bool value) |
243 | { |
244 | emit_json_key(key); |
245 | statement_inner(t: value ? "true" : "false" ); |
246 | } |
247 | |
248 | void Stream::emit_json_key_object(const std::string &key) |
249 | { |
250 | emit_json_key(key); |
251 | statement_inner(t: "{\n" ); |
252 | ++indent; |
253 | stack.emplace(args: Type::Object, args: false); |
254 | } |
255 | |
256 | void Stream::emit_json_key_array(const std::string &key) |
257 | { |
258 | emit_json_key(key); |
259 | statement_inner(t: "[\n" ); |
260 | ++indent; |
261 | stack.emplace(args: Type::Array, args: false); |
262 | } |
263 | |
264 | void CompilerReflection::set_format(const std::string &format) |
265 | { |
266 | if (format != "json" ) |
267 | { |
268 | SPIRV_CROSS_THROW("Unsupported format" ); |
269 | } |
270 | } |
271 | |
272 | string CompilerReflection::compile() |
273 | { |
274 | json_stream = std::make_shared<simple_json::Stream>(); |
275 | json_stream->set_current_locale_radix_character(current_locale_radix_character); |
276 | json_stream->begin_json_object(); |
277 | reorder_type_alias(); |
278 | emit_entry_points(); |
279 | emit_types(); |
280 | emit_resources(); |
281 | emit_specialization_constants(); |
282 | json_stream->end_json_object(); |
283 | return json_stream->str(); |
284 | } |
285 | |
286 | static bool naturally_emit_type(const SPIRType &type) |
287 | { |
288 | return type.basetype == SPIRType::Struct && !type.pointer && type.array.empty(); |
289 | } |
290 | |
291 | bool CompilerReflection::type_is_reference(const SPIRType &type) const |
292 | { |
293 | // Physical pointers and arrays of physical pointers need to refer to the pointee's type. |
294 | return type_is_top_level_physical_pointer(type) || |
295 | (!type.array.empty() && type_is_top_level_physical_pointer(type: get<SPIRType>(id: type.parent_type))); |
296 | } |
297 | |
298 | void CompilerReflection::emit_types() |
299 | { |
300 | bool emitted_open_tag = false; |
301 | |
302 | SmallVector<uint32_t> physical_pointee_types; |
303 | |
304 | // If we have physical pointers or arrays of physical pointers, it's also helpful to emit the pointee type |
305 | // and chain the type hierarchy. For POD, arrays can emit the entire type in-place. |
306 | ir.for_each_typed_id<SPIRType>(op: [&](uint32_t self, SPIRType &type) { |
307 | if (naturally_emit_type(type)) |
308 | { |
309 | emit_type(type_id: self, emitted_open_tag); |
310 | } |
311 | else if (type_is_reference(type)) |
312 | { |
313 | if (!naturally_emit_type(type: this->get<SPIRType>(id: type.parent_type)) && |
314 | find(first: physical_pointee_types.begin(), last: physical_pointee_types.end(), val: type.parent_type) == |
315 | physical_pointee_types.end()) |
316 | { |
317 | physical_pointee_types.push_back(t: type.parent_type); |
318 | } |
319 | } |
320 | }); |
321 | |
322 | for (uint32_t pointee_type : physical_pointee_types) |
323 | emit_type(type_id: pointee_type, emitted_open_tag); |
324 | |
325 | if (emitted_open_tag) |
326 | { |
327 | json_stream->end_json_object(); |
328 | } |
329 | } |
330 | |
331 | void CompilerReflection::emit_type(uint32_t type_id, bool &emitted_open_tag) |
332 | { |
333 | auto &type = get<SPIRType>(id: type_id); |
334 | auto name = type_to_glsl(type); |
335 | |
336 | if (!emitted_open_tag) |
337 | { |
338 | json_stream->emit_json_key_object(key: "types" ); |
339 | emitted_open_tag = true; |
340 | } |
341 | json_stream->emit_json_key_object(key: "_" + std::to_string(val: type_id)); |
342 | json_stream->emit_json_key_value(key: "name" , value: name); |
343 | |
344 | if (type_is_top_level_physical_pointer(type)) |
345 | { |
346 | json_stream->emit_json_key_value(key: "type" , value: "_" + std::to_string(val: type.parent_type)); |
347 | json_stream->emit_json_key_value(key: "physical_pointer" , value: true); |
348 | } |
349 | else if (!type.array.empty()) |
350 | { |
351 | emit_type_array(type); |
352 | json_stream->emit_json_key_value(key: "type" , value: "_" + std::to_string(val: type.parent_type)); |
353 | json_stream->emit_json_key_value(key: "array_stride" , value: get_decoration(id: type_id, decoration: DecorationArrayStride)); |
354 | } |
355 | else |
356 | { |
357 | json_stream->emit_json_key_array(key: "members" ); |
358 | // FIXME ideally we'd like to emit the size of a structure as a |
359 | // convenience to people parsing the reflected JSON. The problem |
360 | // is that there's no implicit size for a type. It's final size |
361 | // will be determined by the top level declaration in which it's |
362 | // included. So there might be one size for the struct if it's |
363 | // included in a std140 uniform block and another if it's included |
364 | // in a std430 uniform block. |
365 | // The solution is to include *all* potential sizes as a map of |
366 | // layout type name to integer, but that will probably require |
367 | // some additional logic being written in this class, or in the |
368 | // parent CompilerGLSL class. |
369 | auto size = type.member_types.size(); |
370 | for (uint32_t i = 0; i < size; ++i) |
371 | { |
372 | emit_type_member(type, index: i); |
373 | } |
374 | json_stream->end_json_array(); |
375 | } |
376 | |
377 | json_stream->end_json_object(); |
378 | } |
379 | |
380 | void CompilerReflection::emit_type_member(const SPIRType &type, uint32_t index) |
381 | { |
382 | auto &membertype = get<SPIRType>(id: type.member_types[index]); |
383 | json_stream->begin_json_object(); |
384 | auto name = to_member_name(type, index); |
385 | // FIXME we'd like to emit the offset of each member, but such offsets are |
386 | // context dependent. See the comment above regarding structure sizes |
387 | json_stream->emit_json_key_value(key: "name" , value: name); |
388 | |
389 | if (type_is_reference(type: membertype)) |
390 | { |
391 | json_stream->emit_json_key_value(key: "type" , value: "_" + std::to_string(val: membertype.parent_type)); |
392 | } |
393 | else if (membertype.basetype == SPIRType::Struct) |
394 | { |
395 | json_stream->emit_json_key_value(key: "type" , value: "_" + std::to_string(val: membertype.self)); |
396 | } |
397 | else |
398 | { |
399 | json_stream->emit_json_key_value(key: "type" , value: type_to_glsl(type: membertype)); |
400 | } |
401 | emit_type_member_qualifiers(type, index); |
402 | json_stream->end_json_object(); |
403 | } |
404 | |
405 | void CompilerReflection::emit_type_array(const SPIRType &type) |
406 | { |
407 | if (!type_is_top_level_physical_pointer(type) && !type.array.empty()) |
408 | { |
409 | json_stream->emit_json_key_array(key: "array" ); |
410 | // Note that we emit the zeros here as a means of identifying |
411 | // unbounded arrays. This is necessary as otherwise there would |
412 | // be no way of differentiating between float[4] and float[4][] |
413 | for (const auto &value : type.array) |
414 | json_stream->emit_json_array_value(value); |
415 | json_stream->end_json_array(); |
416 | |
417 | json_stream->emit_json_key_array(key: "array_size_is_literal" ); |
418 | for (const auto &value : type.array_size_literal) |
419 | json_stream->emit_json_array_value(value); |
420 | json_stream->end_json_array(); |
421 | } |
422 | } |
423 | |
424 | void CompilerReflection::emit_type_member_qualifiers(const SPIRType &type, uint32_t index) |
425 | { |
426 | auto &membertype = get<SPIRType>(id: type.member_types[index]); |
427 | emit_type_array(type: membertype); |
428 | auto &memb = ir.meta[type.self].members; |
429 | if (index < memb.size()) |
430 | { |
431 | auto &dec = memb[index]; |
432 | if (dec.decoration_flags.get(bit: DecorationLocation)) |
433 | json_stream->emit_json_key_value(key: "location" , value: dec.location); |
434 | if (dec.decoration_flags.get(bit: DecorationOffset)) |
435 | json_stream->emit_json_key_value(key: "offset" , value: dec.offset); |
436 | |
437 | // Array stride is a property of the array type, not the struct. |
438 | if (has_decoration(id: type.member_types[index], decoration: DecorationArrayStride)) |
439 | json_stream->emit_json_key_value(key: "array_stride" , |
440 | value: get_decoration(id: type.member_types[index], decoration: DecorationArrayStride)); |
441 | |
442 | if (dec.decoration_flags.get(bit: DecorationMatrixStride)) |
443 | json_stream->emit_json_key_value(key: "matrix_stride" , value: dec.matrix_stride); |
444 | if (dec.decoration_flags.get(bit: DecorationRowMajor)) |
445 | json_stream->emit_json_key_value(key: "row_major" , value: true); |
446 | |
447 | if (type_is_top_level_physical_pointer(type: membertype)) |
448 | json_stream->emit_json_key_value(key: "physical_pointer" , value: true); |
449 | } |
450 | } |
451 | |
452 | string CompilerReflection::execution_model_to_str(spv::ExecutionModel model) |
453 | { |
454 | switch (model) |
455 | { |
456 | case ExecutionModelVertex: |
457 | return "vert" ; |
458 | case ExecutionModelTessellationControl: |
459 | return "tesc" ; |
460 | case ExecutionModelTessellationEvaluation: |
461 | return "tese" ; |
462 | case ExecutionModelGeometry: |
463 | return "geom" ; |
464 | case ExecutionModelFragment: |
465 | return "frag" ; |
466 | case ExecutionModelGLCompute: |
467 | return "comp" ; |
468 | case ExecutionModelRayGenerationNV: |
469 | return "rgen" ; |
470 | case ExecutionModelIntersectionNV: |
471 | return "rint" ; |
472 | case ExecutionModelAnyHitNV: |
473 | return "rahit" ; |
474 | case ExecutionModelClosestHitNV: |
475 | return "rchit" ; |
476 | case ExecutionModelMissNV: |
477 | return "rmiss" ; |
478 | case ExecutionModelCallableNV: |
479 | return "rcall" ; |
480 | default: |
481 | return "???" ; |
482 | } |
483 | } |
484 | |
485 | // FIXME include things like the local_size dimensions, geometry output vertex count, etc |
486 | void CompilerReflection::emit_entry_points() |
487 | { |
488 | auto entries = get_entry_points_and_stages(); |
489 | if (!entries.empty()) |
490 | { |
491 | // Needed to make output deterministic. |
492 | sort(first: begin(cont&: entries), last: end(cont&: entries), comp: [](const EntryPoint &a, const EntryPoint &b) -> bool { |
493 | if (a.execution_model < b.execution_model) |
494 | return true; |
495 | else if (a.execution_model > b.execution_model) |
496 | return false; |
497 | else |
498 | return a.name < b.name; |
499 | }); |
500 | |
501 | json_stream->emit_json_key_array(key: "entryPoints" ); |
502 | for (auto &e : entries) |
503 | { |
504 | json_stream->begin_json_object(); |
505 | json_stream->emit_json_key_value(key: "name" , value: e.name); |
506 | json_stream->emit_json_key_value(key: "mode" , value: execution_model_to_str(model: e.execution_model)); |
507 | if (e.execution_model == ExecutionModelGLCompute) |
508 | { |
509 | const auto &spv_entry = get_entry_point(name: e.name, execution_model: e.execution_model); |
510 | |
511 | SpecializationConstant spec_x, spec_y, spec_z; |
512 | get_work_group_size_specialization_constants(x&: spec_x, y&: spec_y, z&: spec_z); |
513 | |
514 | json_stream->emit_json_key_array(key: "workgroup_size" ); |
515 | json_stream->emit_json_array_value(value: spec_x.id != ID(0) ? spec_x.constant_id : |
516 | spv_entry.workgroup_size.x); |
517 | json_stream->emit_json_array_value(value: spec_y.id != ID(0) ? spec_y.constant_id : |
518 | spv_entry.workgroup_size.y); |
519 | json_stream->emit_json_array_value(value: spec_z.id != ID(0) ? spec_z.constant_id : |
520 | spv_entry.workgroup_size.z); |
521 | json_stream->end_json_array(); |
522 | |
523 | json_stream->emit_json_key_array(key: "workgroup_size_is_spec_constant_id" ); |
524 | json_stream->emit_json_array_value(value: spec_x.id != ID(0)); |
525 | json_stream->emit_json_array_value(value: spec_y.id != ID(0)); |
526 | json_stream->emit_json_array_value(value: spec_z.id != ID(0)); |
527 | json_stream->end_json_array(); |
528 | } |
529 | json_stream->end_json_object(); |
530 | } |
531 | json_stream->end_json_array(); |
532 | } |
533 | } |
534 | |
535 | void CompilerReflection::emit_resources() |
536 | { |
537 | auto res = get_shader_resources(); |
538 | emit_resources(tag: "subpass_inputs" , resources: res.subpass_inputs); |
539 | emit_resources(tag: "inputs" , resources: res.stage_inputs); |
540 | emit_resources(tag: "outputs" , resources: res.stage_outputs); |
541 | emit_resources(tag: "textures" , resources: res.sampled_images); |
542 | emit_resources(tag: "separate_images" , resources: res.separate_images); |
543 | emit_resources(tag: "separate_samplers" , resources: res.separate_samplers); |
544 | emit_resources(tag: "images" , resources: res.storage_images); |
545 | emit_resources(tag: "ssbos" , resources: res.storage_buffers); |
546 | emit_resources(tag: "ubos" , resources: res.uniform_buffers); |
547 | emit_resources(tag: "push_constants" , resources: res.push_constant_buffers); |
548 | emit_resources(tag: "counters" , resources: res.atomic_counters); |
549 | emit_resources(tag: "acceleration_structures" , resources: res.acceleration_structures); |
550 | } |
551 | |
552 | void CompilerReflection::emit_resources(const char *tag, const SmallVector<Resource> &resources) |
553 | { |
554 | if (resources.empty()) |
555 | { |
556 | return; |
557 | } |
558 | |
559 | json_stream->emit_json_key_array(key: tag); |
560 | for (auto &res : resources) |
561 | { |
562 | auto &type = get_type(id: res.type_id); |
563 | auto typeflags = ir.meta[type.self].decoration.decoration_flags; |
564 | auto &mask = get_decoration_bitset(id: res.id); |
565 | |
566 | // If we don't have a name, use the fallback for the type instead of the variable |
567 | // for SSBOs and UBOs since those are the only meaningful names to use externally. |
568 | // Push constant blocks are still accessed by name and not block name, even though they are technically Blocks. |
569 | bool is_push_constant = get_storage_class(id: res.id) == StorageClassPushConstant; |
570 | bool is_block = get_decoration_bitset(id: type.self).get(bit: DecorationBlock) || |
571 | get_decoration_bitset(id: type.self).get(bit: DecorationBufferBlock); |
572 | |
573 | ID fallback_id = !is_push_constant && is_block ? ID(res.base_type_id) : ID(res.id); |
574 | |
575 | json_stream->begin_json_object(); |
576 | |
577 | if (type.basetype == SPIRType::Struct) |
578 | { |
579 | json_stream->emit_json_key_value(key: "type" , value: "_" + std::to_string(val: res.base_type_id)); |
580 | } |
581 | else |
582 | { |
583 | json_stream->emit_json_key_value(key: "type" , value: type_to_glsl(type)); |
584 | } |
585 | |
586 | json_stream->emit_json_key_value(key: "name" , value: !res.name.empty() ? res.name : get_fallback_name(id: fallback_id)); |
587 | { |
588 | bool ssbo_block = type.storage == StorageClassStorageBuffer || |
589 | (type.storage == StorageClassUniform && typeflags.get(bit: DecorationBufferBlock)); |
590 | Bitset qualifier_mask = ssbo_block ? get_buffer_block_flags(id: res.id) : mask; |
591 | |
592 | if (qualifier_mask.get(bit: DecorationNonReadable)) |
593 | json_stream->emit_json_key_value(key: "writeonly" , value: true); |
594 | if (qualifier_mask.get(bit: DecorationNonWritable)) |
595 | json_stream->emit_json_key_value(key: "readonly" , value: true); |
596 | if (qualifier_mask.get(bit: DecorationRestrict)) |
597 | json_stream->emit_json_key_value(key: "restrict" , value: true); |
598 | if (qualifier_mask.get(bit: DecorationCoherent)) |
599 | json_stream->emit_json_key_value(key: "coherent" , value: true); |
600 | if (qualifier_mask.get(bit: DecorationVolatile)) |
601 | json_stream->emit_json_key_value(key: "volatile" , value: true); |
602 | } |
603 | |
604 | emit_type_array(type); |
605 | |
606 | { |
607 | bool is_sized_block = is_block && (get_storage_class(id: res.id) == StorageClassUniform || |
608 | get_storage_class(id: res.id) == StorageClassUniformConstant || |
609 | get_storage_class(id: res.id) == StorageClassStorageBuffer); |
610 | if (is_sized_block) |
611 | { |
612 | uint32_t block_size = uint32_t(get_declared_struct_size(struct_type: get_type(id: res.base_type_id))); |
613 | json_stream->emit_json_key_value(key: "block_size" , value: block_size); |
614 | } |
615 | } |
616 | |
617 | if (type.storage == StorageClassPushConstant) |
618 | json_stream->emit_json_key_value(key: "push_constant" , value: true); |
619 | if (mask.get(bit: DecorationLocation)) |
620 | json_stream->emit_json_key_value(key: "location" , value: get_decoration(id: res.id, decoration: DecorationLocation)); |
621 | if (mask.get(bit: DecorationRowMajor)) |
622 | json_stream->emit_json_key_value(key: "row_major" , value: true); |
623 | if (mask.get(bit: DecorationColMajor)) |
624 | json_stream->emit_json_key_value(key: "column_major" , value: true); |
625 | if (mask.get(bit: DecorationIndex)) |
626 | json_stream->emit_json_key_value(key: "index" , value: get_decoration(id: res.id, decoration: DecorationIndex)); |
627 | if (type.storage != StorageClassPushConstant && mask.get(bit: DecorationDescriptorSet)) |
628 | json_stream->emit_json_key_value(key: "set" , value: get_decoration(id: res.id, decoration: DecorationDescriptorSet)); |
629 | if (mask.get(bit: DecorationBinding)) |
630 | json_stream->emit_json_key_value(key: "binding" , value: get_decoration(id: res.id, decoration: DecorationBinding)); |
631 | if (mask.get(bit: DecorationInputAttachmentIndex)) |
632 | json_stream->emit_json_key_value(key: "input_attachment_index" , |
633 | value: get_decoration(id: res.id, decoration: DecorationInputAttachmentIndex)); |
634 | if (mask.get(bit: DecorationOffset)) |
635 | json_stream->emit_json_key_value(key: "offset" , value: get_decoration(id: res.id, decoration: DecorationOffset)); |
636 | |
637 | // For images, the type itself adds a layout qualifer. |
638 | // Only emit the format for storage images. |
639 | if (type.basetype == SPIRType::Image && type.image.sampled == 2) |
640 | { |
641 | const char *fmt = format_to_glsl(format: type.image.format); |
642 | if (fmt != nullptr) |
643 | json_stream->emit_json_key_value(key: "format" , value: std::string(fmt)); |
644 | } |
645 | json_stream->end_json_object(); |
646 | } |
647 | json_stream->end_json_array(); |
648 | } |
649 | |
650 | void CompilerReflection::emit_specialization_constants() |
651 | { |
652 | auto specialization_constants = get_specialization_constants(); |
653 | if (specialization_constants.empty()) |
654 | return; |
655 | |
656 | json_stream->emit_json_key_array(key: "specialization_constants" ); |
657 | for (const auto &spec_const : specialization_constants) |
658 | { |
659 | auto &c = get<SPIRConstant>(id: spec_const.id); |
660 | auto type = get<SPIRType>(id: c.constant_type); |
661 | json_stream->begin_json_object(); |
662 | json_stream->emit_json_key_value(key: "name" , value: get_name(id: spec_const.id)); |
663 | json_stream->emit_json_key_value(key: "id" , value: spec_const.constant_id); |
664 | json_stream->emit_json_key_value(key: "type" , value: type_to_glsl(type)); |
665 | json_stream->emit_json_key_value(key: "variable_id" , value: spec_const.id); |
666 | switch (type.basetype) |
667 | { |
668 | case SPIRType::UInt: |
669 | json_stream->emit_json_key_value(key: "default_value" , value: c.scalar()); |
670 | break; |
671 | |
672 | case SPIRType::Int: |
673 | json_stream->emit_json_key_value(key: "default_value" , value: c.scalar_i32()); |
674 | break; |
675 | |
676 | case SPIRType::Float: |
677 | json_stream->emit_json_key_value(key: "default_value" , value: c.scalar_f32()); |
678 | break; |
679 | |
680 | case SPIRType::Boolean: |
681 | json_stream->emit_json_key_value(key: "default_value" , value: c.scalar() != 0); |
682 | break; |
683 | |
684 | default: |
685 | break; |
686 | } |
687 | json_stream->end_json_object(); |
688 | } |
689 | json_stream->end_json_array(); |
690 | } |
691 | |
692 | string CompilerReflection::to_member_name(const SPIRType &type, uint32_t index) const |
693 | { |
694 | auto *type_meta = ir.find_meta(id: type.self); |
695 | |
696 | if (type_meta) |
697 | { |
698 | auto &memb = type_meta->members; |
699 | if (index < memb.size() && !memb[index].alias.empty()) |
700 | return memb[index].alias; |
701 | else |
702 | return join(ts: "_m" , ts&: index); |
703 | } |
704 | else |
705 | return join(ts: "_m" , ts&: index); |
706 | } |
707 | |