/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "AnyConverter.h" #include #include #include #include #include #include #include #include namespace Magnum { namespace ShaderTools { struct AnyConverter::State { Format inputFormat, outputFormat; Containers::String inputVersion, outputVersion; }; AnyConverter::AnyConverter(PluginManager::Manager& manager): AbstractConverter{manager} {} AnyConverter::AnyConverter(PluginManager::AbstractManager& manager, const std::string& plugin): AbstractConverter{manager, plugin}, _state{Containers::InPlaceInit} {} AnyConverter::~AnyConverter() = default; ConverterFeatures AnyConverter::doFeatures() const { /** @todo Preprocess, Optimize, DebugInfo, those also need checks that the plugin actually supports them */ return ConverterFeature::ValidateFile|ConverterFeature::ConvertFile; } void AnyConverter::doSetInputFormat(const Format format, const Containers::StringView version) { _state->inputFormat = format; _state->inputVersion = Containers::String::nullTerminatedGlobalView(version); } void AnyConverter::doSetOutputFormat(Format format, Containers::StringView version) { _state->outputFormat = format; _state->outputVersion = Containers::String::nullTerminatedGlobalView(version); } namespace { using namespace Containers::Literals; Containers::StringView formatForExtension(const char* prefix, const Containers::StringView filename) { /** @todo lowercase only the extension, once Directory::split() is done */ const std::string normalized = Utility::String::lowercase(filename); /* https://github.com/KhronosGroup/SPIRV-Tools/blob/a715b1b4053519ad0f2bdb2d22ace35d35867cff/README.md#command-line-tools "It's a convention to name SPIR-V assembly and binary files with suffix .spvasm and .spv, respectively." IT'S GREAT THAT I HAD TO SEARCH HALF THE INTERNET TO FIND THIS CONVENTION. Especially when tests in the SPIRV-Cross repo use `.asm.bla` instead, FFS. */ if(Utility::String::endsWith(normalized, ".spvasm") || /* Not official, used by https://github.com/KhronosGroup/SPIRV-Cross */ Utility::String::endsWith(normalized, ".asm.vert") || Utility::String::endsWith(normalized, ".asm.frag") || Utility::String::endsWith(normalized, ".asm.geom") || Utility::String::endsWith(normalized, ".asm.comp") || Utility::String::endsWith(normalized, ".asm.tesc") || Utility::String::endsWith(normalized, ".asm.tese") || Utility::String::endsWith(normalized, ".asm.rgen") || Utility::String::endsWith(normalized, ".asm.rint") || Utility::String::endsWith(normalized, ".asm.rahit") || Utility::String::endsWith(normalized, ".asm.rchit") || Utility::String::endsWith(normalized, ".asm.rmiss") || Utility::String::endsWith(normalized, ".asm.rcall") || Utility::String::endsWith(normalized, ".asm.mesh") || Utility::String::endsWith(normalized, ".asm.task")) return "SpirvAssembly"_s; /* https://github.com/KhronosGroup/glslang/blob/3ce148638bdc3807316e358dee4a5c9583189ae7/StandAlone/StandAlone.cpp#L260-L274 */ else if(Utility::String::endsWith(normalized, ".glsl") || Utility::String::endsWith(normalized, ".vert") || Utility::String::endsWith(normalized, ".frag") || Utility::String::endsWith(normalized, ".geom") || Utility::String::endsWith(normalized, ".comp") || Utility::String::endsWith(normalized, ".tesc") || Utility::String::endsWith(normalized, ".tese") || Utility::String::endsWith(normalized, ".rgen") || Utility::String::endsWith(normalized, ".rint") || Utility::String::endsWith(normalized, ".rahit") || Utility::String::endsWith(normalized, ".rchit") || Utility::String::endsWith(normalized, ".rmiss") || Utility::String::endsWith(normalized, ".rcall") || Utility::String::endsWith(normalized, ".mesh") || Utility::String::endsWith(normalized, ".task")) return "Glsl"_s; else if(Utility::String::endsWith(normalized, ".spv")) return "Spirv"_s; Error{} << prefix << "cannot determine the format of" << filename; return {}; } } std::pair AnyConverter::doValidateFile(const Stage stage, const Containers::StringView filename) { CORRADE_INTERNAL_ASSERT(manager()); /* Decide on a plugin name based on the extension */ const Containers::StringView format = formatForExtension("ShaderTools::AnyConverter::validateFile():", filename); if(format.isEmpty()) return {}; const std::string plugin = Utility::formatString("{}ShaderConverter", format); /* Try to load the plugin */ if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { Error{} << "ShaderTools::AnyConverter::validateFile(): cannot load the" << plugin << "plugin"; return {}; } PluginManager::PluginMetadata* metadata = manager()->metadata(plugin); if(flags() & ConverterFlag::Verbose) { Debug d; d << "ShaderTools::AnyConverter::validateFile(): using" << plugin; CORRADE_INTERNAL_ASSERT(metadata); if(plugin != metadata->name()) d << "(provided by" << metadata->name() << Debug::nospace << ")"; } /* Instantiate the plugin, check that it can actually validate */ Containers::Pointer converter = static_cast*>(manager())->instantiate(plugin); if(!(converter->features() & ConverterFeature::ValidateFile)) { Error{} << "ShaderTools::AnyConverter::validateFile():" << metadata->name() << "does not support validation"; return {}; } /* Propagate input/output version and flags */ converter->setFlags(flags()); converter->setInputFormat(_state->inputFormat, _state->inputVersion); converter->setOutputFormat(_state->outputFormat, _state->outputVersion); /* Try to validate the file (error output should be printed by the plugin itself) */ return converter->validateFile(stage, filename); } bool AnyConverter::doConvertFileToFile(const Stage stage, const Containers::StringView from, const Containers::StringView to) { CORRADE_INTERNAL_ASSERT(manager()); /* Decide on a plugin name based on the input and output extension. This might result in invalid combinations such as SpirvToGlslShaderConverter which can't be really handled yet but I think that's okay for now */ const Containers::StringView formatFrom = formatForExtension("ShaderTools::AnyConverter::convertFileToFile():", from); const Containers::StringView formatTo = formatForExtension("ShaderTools::AnyConverter::convertFileToFile():", to); if(formatFrom.isEmpty() || formatTo.isEmpty()) return {}; const std::string plugin = Utility::formatString( formatFrom == formatTo ? "{}ShaderConverter" : "{}To{}ShaderConverter", formatFrom, formatTo); /* Try to load the plugin */ if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { Error{} << "ShaderTools::AnyConverter::convertFileToFile(): cannot load the" << plugin << "plugin"; return {}; } PluginManager::PluginMetadata* metadata = manager()->metadata(plugin); if(flags() & ConverterFlag::Verbose) { Debug d; d << "ShaderTools::AnyConverter::convertFileToFile(): using" << plugin; CORRADE_INTERNAL_ASSERT(metadata); if(plugin != metadata->name()) d << "(provided by" << metadata->name() << Debug::nospace << ")"; } /* Instantiate the plugin, check that it can actually validate */ Containers::Pointer converter = static_cast*>(manager())->instantiate(plugin); if(!(converter->features() & ConverterFeature::ConvertFile)) { Error{} << "ShaderTools::AnyConverter::convertFileToFile():" << metadata->name() << "does not support conversion"; return {}; } /* Propagate input/output version and flags */ converter->setFlags(flags()); converter->setInputFormat(_state->inputFormat, _state->inputVersion); converter->setOutputFormat(_state->outputFormat, _state->outputVersion); /* Try to convert the file (error output should be printed by the plugin itself) */ return converter->convertFileToFile(stage, from, to); } }} CORRADE_PLUGIN_REGISTER(AnyShaderConverter, Magnum::ShaderTools::AnyConverter, "cz.mosra.magnum.ShaderTools.AbstractConverter/0.1")