blob: 2e9ce372e74356945cbc84b33e99e6753e0aafb6 [file] [log] [blame]
// 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 "src/tint/cmd/common/helper.h"
#include <iostream>
#include <utility>
#include <vector>
#if TINT_BUILD_SPV_READER
#include "spirv-tools/libspirv.hpp"
#include "src/tint/lang/spirv/reader/reader.h"
#endif
#if TINT_BUILD_WGSL_WRITER
#include "src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.h"
#include "src/tint/lang/wgsl/writer/writer.h"
#endif
#if TINT_BUILD_WGSL_READER
#include "src/tint/lang/wgsl/reader/reader.h"
#endif
#include "src/tint/utils/diagnostic/formatter.h"
#include "src/tint/utils/text/string.h"
#include "src/tint/utils/text/styled_text.h"
#include "src/tint/utils/text/styled_text_printer.h"
#include "src/tint/utils/text/text_style.h"
#include "src/tint/utils/traits/traits.h"
namespace tint::cmd {
namespace {
enum class InputFormat {
kUnknown,
kWgsl,
kSpirvBin,
kSpirvAsm,
};
/// @param out the stream to write to
/// @param value the InputFormat
/// @returns @p out so calls can be chained
template <typename STREAM, typename = traits::EnableIfIsOStream<STREAM>>
auto& operator<<(STREAM& out, InputFormat value) {
switch (value) {
case InputFormat::kUnknown:
break;
case InputFormat::kWgsl:
return out << "wgsl";
case InputFormat::kSpirvBin:
return out << "spirv";
case InputFormat::kSpirvAsm:
return out << "spirv asm";
}
return out << "unknown";
}
InputFormat InputFormatFromFilename(const std::string& filename) {
auto input_format = InputFormat::kUnknown;
if (tint::HasSuffix(filename, ".wgsl")) {
input_format = InputFormat::kWgsl;
} else if (tint::HasSuffix(filename, ".spv")) {
input_format = InputFormat::kSpirvBin;
} else if (tint::HasSuffix(filename, ".spvasm")) {
input_format = InputFormat::kSpirvAsm;
}
return input_format;
}
void PrintBindings(tint::inspector::Inspector& inspector, const std::string& ep_name) {
auto bindings = inspector.GetResourceBindings(ep_name);
if (!inspector.error().empty()) {
std::cerr << "Failed to get bindings from Inspector: " << inspector.error() << std::endl;
exit(1);
}
for (auto& binding : bindings) {
std::cout << "\t[" << binding.bind_group << "][" << binding.binding << "]:" << std::endl;
std::cout << "\t\t resource_type = " << ResourceTypeToString(binding.resource_type)
<< std::endl;
std::cout << "\t\t dim = " << TextureDimensionToString(binding.dim) << std::endl;
std::cout << "\t\t sampled_kind = " << SampledKindToString(binding.sampled_kind)
<< std::endl;
std::cout << "\t\t image_format = " << TexelFormatToString(binding.image_format)
<< std::endl;
std::cout << std::endl;
}
}
#if TINT_BUILD_SPV_READER
tint::Program ReadSpirv(const std::vector<uint32_t>& data, const LoadProgramOptions& opts) {
if (opts.use_ir) {
#if TINT_BUILD_WGSL_WRITER
// Parse the SPIR-V binary to a core Tint IR module.
auto result = tint::spirv::reader::ReadIR(data);
if (result != Success) {
std::cerr << "Failed to parse SPIR-V: " << result.Failure() << "\n";
exit(1);
}
// Convert the IR module to a WGSL AST program.
tint::wgsl::writer::ProgramOptions options;
options.allow_non_uniform_derivatives =
opts.spirv_reader_options.allow_non_uniform_derivatives;
options.allowed_features = opts.spirv_reader_options.allowed_features;
auto ast = tint::wgsl::writer::IRToProgram(result.Get(), options);
if (!ast.IsValid() || ast.Diagnostics().ContainsErrors()) {
std::cerr << "Failed to convert IR to AST:\n\n" << ast.Diagnostics() << "\n";
exit(1);
}
return ast;
#else
std::cerr << "Tint not built with the WGSL writer enabled" << std::endl;
exit(1);
#endif // TINT_BUILD_WGSL_READER
} else {
return tint::spirv::reader::Read(data, opts.spirv_reader_options);
}
}
#endif // TINT_BUILD_SPV_READER
} // namespace
[[noreturn]] void TintInternalCompilerErrorReporter(const InternalCompilerError& err) {
auto printer = StyledTextPrinter::Create(stderr);
StyledText msg;
msg << (style::Error + style::Bold) << err.Error();
msg << R"(
********************************************************************
* The tint shader compiler has encountered an unexpected error. *
* *
* Please help us fix this issue by submitting a bug report at *
* crbug.com/tint with the source program that triggered the bug. *
********************************************************************
)";
printer->Print(msg);
exit(1);
}
void PrintWGSL(std::ostream& out, const tint::Program& program) {
#if TINT_BUILD_WGSL_WRITER
tint::wgsl::writer::Options options;
auto result = tint::wgsl::writer::Generate(program, options);
if (result == Success) {
out << std::endl << result->wgsl << std::endl;
} else {
out << result.Failure() << std::endl;
}
#else
(void)out;
(void)program;
#endif
}
ProgramInfo LoadProgramInfo(const LoadProgramOptions& opts) {
auto input_format = InputFormatFromFilename(opts.filename);
auto load = [&]() -> ProgramInfo {
switch (input_format) {
case InputFormat::kUnknown:
break;
case InputFormat::kWgsl: {
#if TINT_BUILD_WGSL_READER
std::vector<uint8_t> data;
if (!ReadFile<uint8_t>(opts.filename, &data)) {
exit(1);
}
tint::wgsl::reader::Options options;
options.allowed_features = tint::wgsl::AllowedFeatures::Everything();
auto file = std::make_unique<tint::Source::File>(
opts.filename, std::string(data.begin(), data.end()));
return ProgramInfo{
/* program */ tint::wgsl::reader::Parse(file.get(), options),
/* source_file */ std::move(file),
};
#else
std::cerr << "Tint not built with the WGSL reader enabled" << std::endl;
exit(1);
#endif // TINT_BUILD_WGSL_READER
}
case InputFormat::kSpirvBin: {
#if TINT_BUILD_SPV_READER
std::vector<uint32_t> data;
if (!ReadFile<uint32_t>(opts.filename, &data)) {
exit(1);
}
return ProgramInfo{
/* program */ ReadSpirv(data, opts),
/* source_file */ nullptr,
};
#else
std::cerr << "Tint not built with the SPIR-V reader enabled" << std::endl;
exit(1);
#endif // TINT_BUILD_SPV_READER
}
case InputFormat::kSpirvAsm: {
#if TINT_BUILD_SPV_READER
std::vector<char> text;
if (!ReadFile<char>(opts.filename, &text)) {
exit(1);
}
// Use Vulkan 1.1, since this is what Tint, internally, is expecting.
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_1);
tools.SetMessageConsumer([](spv_message_level_t, const char*,
const spv_position_t& pos, const char* msg) {
std::cerr << (pos.line + 1) << ":" << (pos.column + 1) << ": " << msg
<< std::endl;
});
std::vector<uint32_t> data;
if (!tools.Assemble(text.data(), text.size(), &data,
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS)) {
exit(1);
}
auto file = std::make_unique<tint::Source::File>(
opts.filename, std::string(text.begin(), text.end()));
return ProgramInfo{
/* program */ ReadSpirv(data, opts),
/* source_file */ std::move(file),
};
#else
std::cerr << "Tint not built with the SPIR-V reader enabled" << std::endl;
exit(1);
#endif // TINT_BUILD_SPV_READER
}
}
std::cerr << "Unknown input format: " << input_format << std::endl;
exit(1);
};
ProgramInfo info = load();
if (info.program.Diagnostics().Count() > 0) {
if (!info.program.IsValid() && input_format != InputFormat::kWgsl) {
// Invalid program from a non-wgsl source.
// Print the WGSL, to help understand the diagnostics.
PrintWGSL(std::cout, info.program);
}
auto printer = tint::StyledTextPrinter::Create(stderr);
tint::diag::Formatter formatter;
printer->Print(formatter.Format(info.program.Diagnostics()));
}
if (!info.program.IsValid()) {
exit(1);
}
return info;
}
void PrintInspectorData(tint::inspector::Inspector& inspector) {
auto entry_points = inspector.GetEntryPoints();
if (!inspector.error().empty()) {
std::cerr << "Failed to get entry points from Inspector: " << inspector.error()
<< std::endl;
exit(1);
}
for (auto& entry_point : entry_points) {
std::cout << "Entry Point = " << entry_point.name << " ("
<< EntryPointStageToString(entry_point.stage) << ")" << std::endl;
if (entry_point.workgroup_size) {
std::cout << " Workgroup Size (" << entry_point.workgroup_size->x << ", "
<< entry_point.workgroup_size->y << ", " << entry_point.workgroup_size->z
<< ")" << std::endl;
}
if (!entry_point.input_variables.empty()) {
std::cout << " Input Variables:" << std::endl;
for (const auto& var : entry_point.input_variables) {
std::cout << "\t";
if (auto location = var.attributes.location) {
std::cout << "@location(" << location.value() << ") ";
}
if (auto color = var.attributes.color) {
std::cout << "@color(" << color.value() << ") ";
}
std::cout << var.name << std::endl;
}
}
if (!entry_point.output_variables.empty()) {
std::cout << " Output Variables:" << std::endl;
for (const auto& var : entry_point.output_variables) {
std::cout << "\t";
if (auto location = var.attributes.location) {
std::cout << "@location(" << location.value() << ") ";
}
std::cout << var.name << std::endl;
}
}
if (!entry_point.overrides.empty()) {
std::cout << " Overrides:" << std::endl;
for (const auto& var : entry_point.overrides) {
std::cout << "\tname: " << var.name << std::endl;
std::cout << "\tid: " << var.id.value << std::endl;
}
}
auto bindings = inspector.GetResourceBindings(entry_point.name);
if (!inspector.error().empty()) {
std::cerr << "Failed to get bindings from Inspector: " << inspector.error()
<< std::endl;
exit(1);
}
if (!bindings.empty()) {
std::cout << " Bindings:" << std::endl;
PrintBindings(inspector, entry_point.name);
std::cout << std::endl;
}
std::cout << std::endl;
}
}
void PrintInspectorBindings(tint::inspector::Inspector& inspector) {
std::cout << std::string(80, '-') << std::endl;
auto entry_points = inspector.GetEntryPoints();
if (!inspector.error().empty()) {
std::cerr << "Failed to get entry points from Inspector: " << inspector.error()
<< std::endl;
exit(1);
}
for (auto& entry_point : entry_points) {
std::cout << "Entry Point = " << entry_point.name << std::endl;
PrintBindings(inspector, entry_point.name);
}
std::cout << std::string(80, '-') << std::endl;
}
std::string EntryPointStageToString(tint::inspector::PipelineStage stage) {
switch (stage) {
case tint::inspector::PipelineStage::kVertex:
return "vertex";
case tint::inspector::PipelineStage::kFragment:
return "fragment";
case tint::inspector::PipelineStage::kCompute:
return "compute";
}
return "Unknown";
}
std::string TextureDimensionToString(tint::inspector::ResourceBinding::TextureDimension dim) {
switch (dim) {
case tint::inspector::ResourceBinding::TextureDimension::kNone:
return "None";
case tint::inspector::ResourceBinding::TextureDimension::k1d:
return "1d";
case tint::inspector::ResourceBinding::TextureDimension::k2d:
return "2d";
case tint::inspector::ResourceBinding::TextureDimension::k2dArray:
return "2dArray";
case tint::inspector::ResourceBinding::TextureDimension::k3d:
return "3d";
case tint::inspector::ResourceBinding::TextureDimension::kCube:
return "Cube";
case tint::inspector::ResourceBinding::TextureDimension::kCubeArray:
return "CubeArray";
}
return "Unknown";
}
std::string SampledKindToString(tint::inspector::ResourceBinding::SampledKind kind) {
switch (kind) {
case tint::inspector::ResourceBinding::SampledKind::kFloat:
return "Float";
case tint::inspector::ResourceBinding::SampledKind::kUInt:
return "UInt";
case tint::inspector::ResourceBinding::SampledKind::kSInt:
return "SInt";
case tint::inspector::ResourceBinding::SampledKind::kUnknown:
break;
}
return "Unknown";
}
std::string TexelFormatToString(tint::inspector::ResourceBinding::TexelFormat format) {
switch (format) {
case tint::inspector::ResourceBinding::TexelFormat::kR32Uint:
return "R32Uint";
case tint::inspector::ResourceBinding::TexelFormat::kR32Sint:
return "R32Sint";
case tint::inspector::ResourceBinding::TexelFormat::kR32Float:
return "R32Float";
case tint::inspector::ResourceBinding::TexelFormat::kBgra8Unorm:
return "Bgra8Unorm";
case tint::inspector::ResourceBinding::TexelFormat::kRgba8Unorm:
return "Rgba8Unorm";
case tint::inspector::ResourceBinding::TexelFormat::kRgba8Snorm:
return "Rgba8Snorm";
case tint::inspector::ResourceBinding::TexelFormat::kRgba8Uint:
return "Rgba8Uint";
case tint::inspector::ResourceBinding::TexelFormat::kRgba8Sint:
return "Rgba8Sint";
case tint::inspector::ResourceBinding::TexelFormat::kRg32Uint:
return "Rg32Uint";
case tint::inspector::ResourceBinding::TexelFormat::kRg32Sint:
return "Rg32Sint";
case tint::inspector::ResourceBinding::TexelFormat::kRg32Float:
return "Rg32Float";
case tint::inspector::ResourceBinding::TexelFormat::kRgba16Uint:
return "Rgba16Uint";
case tint::inspector::ResourceBinding::TexelFormat::kRgba16Sint:
return "Rgba16Sint";
case tint::inspector::ResourceBinding::TexelFormat::kRgba16Float:
return "Rgba16Float";
case tint::inspector::ResourceBinding::TexelFormat::kRgba32Uint:
return "Rgba32Uint";
case tint::inspector::ResourceBinding::TexelFormat::kRgba32Sint:
return "Rgba32Sint";
case tint::inspector::ResourceBinding::TexelFormat::kRgba32Float:
return "Rgba32Float";
case tint::inspector::ResourceBinding::TexelFormat::kR8Unorm:
return "R8Unorm";
case tint::inspector::ResourceBinding::TexelFormat::kNone:
return "None";
}
return "Unknown";
}
std::string ResourceTypeToString(tint::inspector::ResourceBinding::ResourceType type) {
switch (type) {
case tint::inspector::ResourceBinding::ResourceType::kUniformBuffer:
return "UniformBuffer";
case tint::inspector::ResourceBinding::ResourceType::kStorageBuffer:
return "StorageBuffer";
case tint::inspector::ResourceBinding::ResourceType::kReadOnlyStorageBuffer:
return "ReadOnlyStorageBuffer";
case tint::inspector::ResourceBinding::ResourceType::kSampler:
return "Sampler";
case tint::inspector::ResourceBinding::ResourceType::kComparisonSampler:
return "ComparisonSampler";
case tint::inspector::ResourceBinding::ResourceType::kSampledTexture:
return "SampledTexture";
case tint::inspector::ResourceBinding::ResourceType::kMultisampledTexture:
return "MultisampledTexture";
case tint::inspector::ResourceBinding::ResourceType::kWriteOnlyStorageTexture:
return "WriteOnlyStorageTexture";
case tint::inspector::ResourceBinding::ResourceType::kReadOnlyStorageTexture:
return "ReadOnlyStorageTexture";
case tint::inspector::ResourceBinding::ResourceType::kReadWriteStorageTexture:
return "ReadWriteStorageTexture";
case tint::inspector::ResourceBinding::ResourceType::kDepthTexture:
return "DepthTexture";
case tint::inspector::ResourceBinding::ResourceType::kDepthMultisampledTexture:
return "DepthMultisampledTexture";
case tint::inspector::ResourceBinding::ResourceType::kExternalTexture:
return "ExternalTexture";
}
return "Unknown";
}
std::string ComponentTypeToString(tint::inspector::ComponentType type) {
switch (type) {
case tint::inspector::ComponentType::kUnknown:
return "unknown";
case tint::inspector::ComponentType::kF32:
return "f32";
case tint::inspector::ComponentType::kU32:
return "u32";
case tint::inspector::ComponentType::kI32:
return "i32";
case tint::inspector::ComponentType::kF16:
return "f16";
}
return "unknown";
}
std::string CompositionTypeToString(tint::inspector::CompositionType type) {
switch (type) {
case tint::inspector::CompositionType::kUnknown:
return "unknown";
case tint::inspector::CompositionType::kScalar:
return "scalar";
case tint::inspector::CompositionType::kVec2:
return "vec2";
case tint::inspector::CompositionType::kVec3:
return "vec3";
case tint::inspector::CompositionType::kVec4:
return "vec4";
}
return "unknown";
}
std::string InterpolationTypeToString(tint::inspector::InterpolationType type) {
switch (type) {
case tint::inspector::InterpolationType::kUnknown:
return "unknown";
case tint::inspector::InterpolationType::kPerspective:
return "perspective";
case tint::inspector::InterpolationType::kLinear:
return "linear";
case tint::inspector::InterpolationType::kFlat:
return "flat";
}
return "unknown";
}
std::string InterpolationSamplingToString(tint::inspector::InterpolationSampling type) {
switch (type) {
case tint::inspector::InterpolationSampling::kUnknown:
return "unknown";
case tint::inspector::InterpolationSampling::kNone:
return "none";
case tint::inspector::InterpolationSampling::kCenter:
return "center";
case tint::inspector::InterpolationSampling::kCentroid:
return "centroid";
case tint::inspector::InterpolationSampling::kSample:
return "sample";
}
return "unknown";
}
std::string OverrideTypeToString(tint::inspector::Override::Type type) {
switch (type) {
case tint::inspector::Override::Type::kBool:
return "bool";
case tint::inspector::Override::Type::kFloat32:
return "f32";
case tint::inspector::Override::Type::kFloat16:
return "f16";
case tint::inspector::Override::Type::kUint32:
return "u32";
case tint::inspector::Override::Type::kInt32:
return "i32";
}
return "unknown";
}
} // namespace tint::cmd