| |
| // Copyright 2023 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #include <iostream> |
| |
| #if TINT_BUILD_SPV_READER |
| #include "spirv-tools/libspirv.hpp" |
| #endif // TINT_BUILD_SPV_READER |
| |
| #include "src/tint/cmd/common/helper.h" |
| #include "src/tint/lang/core/type/struct.h" |
| #include "src/tint/lang/wgsl/ast/module.h" |
| #include "src/tint/lang/wgsl/inspector/entry_point.h" |
| #include "src/tint/utils/command/command.h" |
| #include "src/tint/utils/containers/transform.h" |
| #include "src/tint/utils/text/string.h" |
| |
| namespace { |
| |
| struct Options { |
| bool show_help = false; |
| |
| #if TINT_BUILD_SPV_READER |
| tint::spirv::reader::Options spirv_reader_options; |
| #endif |
| |
| std::string input_filename; |
| bool emit_json = false; |
| }; |
| |
| const char kUsage[] = R"(Usage: tint [options] <input-file> |
| |
| options: |
| --json -- Emit JSON |
| -h -- This help text |
| |
| )"; |
| |
| bool ParseArgs(const std::vector<std::string>& args, Options* opts) { |
| for (size_t i = 1; i < args.size(); ++i) { |
| const std::string& arg = args[i]; |
| if (arg == "-h" || arg == "--help") { |
| opts->show_help = true; |
| } else if (arg == "--json") { |
| opts->emit_json = true; |
| } else if (!arg.empty()) { |
| if (arg[0] == '-') { |
| std::cerr << "Unrecognized option: " << arg << std::endl; |
| return false; |
| } |
| if (!opts->input_filename.empty()) { |
| std::cerr << "More than one input file specified: '" << opts->input_filename |
| << "' and '" << arg << "'" << std::endl; |
| return false; |
| } |
| opts->input_filename = arg; |
| } |
| } |
| return true; |
| } |
| |
| void EmitJson(const tint::Program& program) { |
| tint::inspector::Inspector inspector(program); |
| |
| std::cout << "{" << std::endl; |
| std::cout << "\"extensions\": [" << std::endl; |
| |
| if (!inspector.GetUsedExtensionNames().empty()) { |
| bool first = true; |
| for (const auto& name : inspector.GetUsedExtensionNames()) { |
| if (!first) { |
| std::cout << ","; |
| } |
| first = false; |
| std::cout << "\"" << name << "\"" << std::endl; |
| } |
| } |
| std::cout << "]," << std::endl; |
| |
| std::cout << "\"entry_points\": ["; |
| |
| auto stage_var = [&](const tint::inspector::StageVariable& var) { |
| std::cout << std::endl; |
| std::cout << "{" << std::endl; |
| std::cout << "\"name\": \"" << var.name << "\""; |
| std::cout << "," << std::endl; |
| if (auto location = var.attributes.location) { |
| std::cout << "\"location\": " << location.value() << "," << std::endl; |
| std::cout << "\"interpolation\": {" << std::endl; |
| std::cout << "\"type\": \"" |
| << tint::cmd::InterpolationTypeToString(var.interpolation_type) << "\"," |
| << std::endl; |
| std::cout << "\"sampling\": \"" |
| << tint::cmd::InterpolationSamplingToString(var.interpolation_sampling) |
| << "\"" << std::endl; |
| std::cout << "}," << std::endl; |
| } |
| if (auto color = var.attributes.color) { |
| std::cout << "\"color\": " << color.value() << "," << std::endl; |
| } |
| std::cout << "\"component_type\": \"" |
| << tint::cmd::ComponentTypeToString(var.component_type) << "\"," << std::endl; |
| std::cout << "\"composition_type\": \"" |
| << tint::cmd::CompositionTypeToString(var.composition_type) << "\"" << std::endl; |
| std::cout << std::endl; |
| std::cout << "}"; |
| }; |
| |
| auto entry_points = inspector.GetEntryPoints(); |
| bool first = true; |
| for (auto& entry_point : entry_points) { |
| if (!first) { |
| std::cout << ","; |
| } |
| first = false; |
| |
| std::cout << std::endl; |
| std::cout << "{" << std::endl; |
| |
| std::cout << "\"name\": \"" << entry_point.name << "\"" |
| << "," << std::endl; |
| std::cout << "\"stage\": \"" << tint::cmd::EntryPointStageToString(entry_point.stage) |
| << "\"" |
| << "," << std::endl; |
| |
| if (entry_point.workgroup_size) { |
| std::cout << "\"workgroup_size\": ["; |
| std::cout << entry_point.workgroup_size->x << ", " << entry_point.workgroup_size->y |
| << ", " << entry_point.workgroup_size->z << "]" |
| << "," << std::endl; |
| } |
| |
| std::cout << "\"input_variables\": ["; |
| bool input_first = true; |
| for (const auto& var : entry_point.input_variables) { |
| if (!input_first) { |
| std::cout << ","; |
| } |
| input_first = false; |
| stage_var(var); |
| } |
| std::cout << std::endl |
| << "]" |
| << "," << std::endl; |
| |
| std::cout << "\"output_variables\": ["; |
| bool output_first = true; |
| for (const auto& var : entry_point.output_variables) { |
| if (!output_first) { |
| std::cout << ","; |
| } |
| output_first = false; |
| stage_var(var); |
| } |
| std::cout << std::endl |
| << "]" |
| << "," << std::endl; |
| |
| std::cout << "\"overrides\": ["; |
| |
| bool override_first = true; |
| for (const auto& var : entry_point.overrides) { |
| if (!override_first) { |
| std::cout << ","; |
| } |
| override_first = false; |
| |
| std::cout << std::endl; |
| std::cout << "{" << std::endl; |
| std::cout << "\"name\": \"" << var.name << "\"," << std::endl; |
| std::cout << "\"id\": " << var.id.value << "," << std::endl; |
| std::cout << "\"type\": \"" << tint::cmd::OverrideTypeToString(var.type) << "\"," |
| << std::endl; |
| std::cout << "\"is_initialized\": " << (var.is_initialized ? "true" : "false") << "," |
| << std::endl; |
| std::cout << "\"is_id_specified\": " << (var.is_id_specified ? "true" : "false") |
| << std::endl; |
| std::cout << "}"; |
| } |
| std::cout << std::endl |
| << "]" |
| << "," << std::endl; |
| |
| std::cout << "\"bindings\": ["; |
| auto bindings = inspector.GetResourceBindings(entry_point.name); |
| bool ep_first = true; |
| for (auto& binding : bindings) { |
| if (!ep_first) { |
| std::cout << ","; |
| } |
| ep_first = false; |
| |
| std::cout << std::endl; |
| std::cout << "{" << std::endl; |
| std::cout << "\"binding\": " << binding.binding << "," << std::endl; |
| std::cout << "\"group\": " << binding.bind_group << "," << std::endl; |
| std::cout << "\"size\": " << binding.size << "," << std::endl; |
| std::cout << "\"resource_type\": \"" |
| << tint::cmd::ResourceTypeToString(binding.resource_type) << "\"," |
| << std::endl; |
| std::cout << "\"dimemsions\": \"" << tint::cmd::TextureDimensionToString(binding.dim) |
| << "\"," << std::endl; |
| std::cout << "\"sampled_kind\": \"" |
| << tint::cmd::SampledKindToString(binding.sampled_kind) << "\"," << std::endl; |
| std::cout << "\"image_format\": \"" |
| << tint::cmd::TexelFormatToString(binding.image_format) << "\"" << std::endl; |
| std::cout << "}"; |
| } |
| std::cout << std::endl << "]" << std::endl; |
| std::cout << "}"; |
| } |
| std::cout << std::endl << "]," << std::endl; |
| std::cout << "\"structures\": ["; |
| |
| bool struct_first = true; |
| for (const auto* ty : program.Types()) { |
| if (!ty->Is<tint::core::type::Struct>()) { |
| continue; |
| } |
| const auto* s = ty->As<tint::core::type::Struct>(); |
| |
| if (!struct_first) { |
| std::cout << ","; |
| } |
| struct_first = false; |
| |
| std::cout << std::endl; |
| std::cout << "{" << std::endl; |
| std::cout << "\"name\": \"" << s->FriendlyName() << "\"," << std::endl; |
| std::cout << "\"align\": " << s->Align() << "," << std::endl; |
| std::cout << "\"size\": " << s->Size() << "," << std::endl; |
| std::cout << "\"members\": ["; |
| for (size_t i = 0; i < s->Members().Length(); ++i) { |
| auto* const m = s->Members()[i]; |
| |
| if (i != 0) { |
| std::cout << ","; |
| } |
| std::cout << std::endl; |
| |
| // Output field alignment padding, if any |
| auto* const prev_member = (i == 0) ? nullptr : s->Members()[i - 1]; |
| if (prev_member) { |
| uint32_t padding = m->Offset() - (prev_member->Offset() + prev_member->Size()); |
| if (padding > 0) { |
| size_t padding_offset = m->Offset() - padding; |
| std::cout << "{" << std::endl; |
| std::cout << "\"name\": \"implicit_padding\"," << std::endl; |
| std::cout << "\"offset\": " << padding_offset << "," << std::endl; |
| std::cout << "\"align\": 1," << std::endl; |
| std::cout << "\"size\": " << padding << std::endl; |
| std::cout << "}," << std::endl; |
| } |
| } |
| |
| std::cout << "{" << std::endl; |
| std::cout << "\"name\": \"" << m->Name().Name() << "\"," << std::endl; |
| std::cout << "\"offset\": " << m->Offset() << "," << std::endl; |
| std::cout << "\"align\": " << m->Align() << "," << std::endl; |
| std::cout << "\"size\": " << m->Size() << std::endl; |
| std::cout << "}"; |
| } |
| std::cout << std::endl << "]" << std::endl; |
| std::cout << "}"; |
| } |
| std::cout << std::endl << "]" << std::endl; |
| std::cout << "}" << std::endl; |
| } |
| |
| void EmitText(const tint::Program& program) { |
| tint::inspector::Inspector inspector(program); |
| if (!inspector.GetUsedExtensionNames().empty()) { |
| std::cout << "Extensions:" << std::endl; |
| for (const auto& name : inspector.GetUsedExtensionNames()) { |
| std::cout << "\t" << name << std::endl; |
| } |
| } |
| std::cout << std::endl; |
| |
| tint::cmd::PrintInspectorData(inspector); |
| |
| bool has_struct = false; |
| for (const auto* ty : program.Types()) { |
| if (!ty->Is<tint::core::type::Struct>()) { |
| continue; |
| } |
| has_struct = true; |
| break; |
| } |
| |
| if (has_struct) { |
| std::cout << "Structures" << std::endl; |
| for (const auto* ty : program.Types()) { |
| if (!ty->Is<tint::core::type::Struct>()) { |
| continue; |
| } |
| const auto* s = ty->As<tint::core::type::Struct>(); |
| std::cout << s->Layout() << std::endl << std::endl; |
| } |
| } |
| } |
| |
| } // namespace |
| |
| int main(int argc, const char** argv) { |
| std::vector<std::string> args(argv, argv + argc); |
| Options options; |
| |
| tint::SetInternalCompilerErrorReporter(&tint::cmd::TintInternalCompilerErrorReporter); |
| |
| if (!ParseArgs(args, &options)) { |
| std::cerr << "Failed to parse arguments." << std::endl; |
| return 1; |
| } |
| |
| if (options.show_help) { |
| std::cout << kUsage << std::endl; |
| return 0; |
| } |
| |
| tint::cmd::LoadProgramOptions opts; |
| opts.filename = options.input_filename; |
| #if TINT_BUILD_SPV_READER |
| opts.spirv_reader_options = options.spirv_reader_options; |
| #endif |
| |
| auto info = tint::cmd::LoadProgramInfo(opts); |
| |
| if (options.emit_json) { |
| EmitJson(info.program); |
| } else { |
| EmitText(info.program); |
| } |
| |
| return 0; |
| } |