mirror of https://github.com/mosra/magnum.git
50 changed files with 1022 additions and 1 deletions
@ -0,0 +1,208 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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 <Corrade/PluginManager/Manager.h> |
||||||
|
#include <Corrade/PluginManager/PluginMetadata.h> |
||||||
|
#include <Corrade/Containers/String.h> |
||||||
|
#include <Corrade/Containers/StringStl.h> |
||||||
|
#include <Corrade/Utility/Assert.h> |
||||||
|
#include <Corrade/Utility/DebugStl.h> |
||||||
|
#include <Corrade/Utility/FormatStl.h> |
||||||
|
#include <Corrade/Utility/String.h> |
||||||
|
|
||||||
|
namespace Magnum { namespace ShaderTools { |
||||||
|
|
||||||
|
struct AnyConverter::State { |
||||||
|
Format inputFormat, outputFormat; |
||||||
|
Containers::String inputVersion, outputVersion; |
||||||
|
}; |
||||||
|
|
||||||
|
AnyConverter::AnyConverter(PluginManager::Manager<AbstractConverter>& 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<bool, Containers::String> 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<AbstractConverter> converter = static_cast<PluginManager::Manager<AbstractConverter>*>(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<AbstractConverter> converter = static_cast<PluginManager::Manager<AbstractConverter>*>(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") |
||||||
@ -0,0 +1,144 @@ |
|||||||
|
#ifndef Magnum_ShaderTools_AnyConverter_h |
||||||
|
#define Magnum_ShaderTools_AnyConverter_h |
||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class @ref Magnum::ShaderTools::AnyConverter |
||||||
|
* @m_since_latest |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Magnum/ShaderTools/AbstractConverter.h" |
||||||
|
#include "MagnumPlugins/AnyShaderConverter/configure.h" |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_ANYSHADERCONVERTER_BUILD_STATIC |
||||||
|
#ifdef AnyShaderConverter_EXPORTS |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_EXPORT CORRADE_VISIBILITY_EXPORT |
||||||
|
#else |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_EXPORT CORRADE_VISIBILITY_IMPORT |
||||||
|
#endif |
||||||
|
#else |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_EXPORT CORRADE_VISIBILITY_STATIC |
||||||
|
#endif |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_LOCAL CORRADE_VISIBILITY_LOCAL |
||||||
|
#else |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_EXPORT |
||||||
|
#define MAGNUM_ANYSHADERCONVERTER_LOCAL |
||||||
|
#endif |
||||||
|
|
||||||
|
namespace Magnum { namespace ShaderTools { |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Any shader converter plugin |
||||||
|
@m_since_latest |
||||||
|
|
||||||
|
@m_keywords{AnyShaderConverter} |
||||||
|
|
||||||
|
Detects file type based on file extension, loads corresponding plugin and then |
||||||
|
tries to either validate or convert the file with it. Detected file formats: |
||||||
|
|
||||||
|
- GLSL (`*.glsl`, `*.vert`, `*.frag`, `*.geom`, `*.comp`, `*.tesc`, `*.tese`, |
||||||
|
`*.rgen`, `*.rint`, `*.rahit`, `*.rchit`, `*.rmiss`, `*.rcall`, `*.mesh`, |
||||||
|
`*.task`) |
||||||
|
- SPIR-V (`*.spv`) |
||||||
|
- SPIR-V Assembly (`*.spvasm`, `*.asm.vert`, ..., `*.asm.task`) |
||||||
|
|
||||||
|
Supported validation scenarios: |
||||||
|
|
||||||
|
- GLSL, validated with any plugin that provides `GlslShaderConverter` |
||||||
|
- SPIR-V, validated with any plugin that provides `SpirvShaderConverter` |
||||||
|
- SPIR-V Assembly, validated with any plugin that provides |
||||||
|
`SpirvAssemblyShaderConverter` |
||||||
|
|
||||||
|
Supported conversion paths: |
||||||
|
|
||||||
|
- GLSL to SPIR-V, converted with any plugin that provides |
||||||
|
`GlslToSpirvShaderConverter` |
||||||
|
- SPIR-V to SPIR-V, converted with any plugin that provides |
||||||
|
`SpirvShaderConverter` |
||||||
|
- SPIR-V to SPIR-V Assembly, converted with any plugin that provides |
||||||
|
`SpirvToSpirvAssemblyShaderConverter` |
||||||
|
- SPIR-V Assembly to SPIR-V, converted with any plugin that provides |
||||||
|
`SpirvAssemblyToSpirvShaderConverter` |
||||||
|
- SPIR-V Assembly to SPIR-V Assembly, converted with any plugin that provides |
||||||
|
`SpirvAssemblyShaderConverter` |
||||||
|
|
||||||
|
Only validating and converting files is supported. |
||||||
|
|
||||||
|
@section ShaderTools-AnyConverter-usage Usage |
||||||
|
|
||||||
|
This plugin depends on the @ref ShaderTools library and is built if |
||||||
|
`WITH_ANYSHADERCONVERTER` is enabled when building Magnum. To use as a dynamic |
||||||
|
plugin, load @cpp "AnyShaderConverter" @ce via |
||||||
|
@ref Corrade::PluginManager::Manager. |
||||||
|
|
||||||
|
Additionally, if you're using Magnum as a CMake subproject, do the following: |
||||||
|
|
||||||
|
@code{.cmake} |
||||||
|
set(WITH_ANYSHADERCONVERTER ON CACHE BOOL "" FORCE) |
||||||
|
add_subdirectory(magnum EXCLUDE_FROM_ALL) |
||||||
|
|
||||||
|
# So the dynamically loaded plugin gets built implicitly |
||||||
|
add_dependencies(your-app Magnum::AnyShaderConverter) |
||||||
|
@endcode |
||||||
|
|
||||||
|
To use as a static plugin or as a dependency of another plugin with CMake, you |
||||||
|
need to request the `AnyShaderConverter` component of the `Magnum` package and |
||||||
|
link to the `Magnum::AnyShaderConverter` target: |
||||||
|
|
||||||
|
@code{.cmake} |
||||||
|
find_package(Magnum REQUIRED AnyShaderConverter) |
||||||
|
|
||||||
|
# ... |
||||||
|
target_link_libraries(your-app PRIVATE Magnum::AnyShaderConverter) |
||||||
|
@endcode |
||||||
|
|
||||||
|
See @ref building, @ref cmake and @ref plugins for more information. |
||||||
|
*/ |
||||||
|
class MAGNUM_ANYSHADERCONVERTER_EXPORT AnyConverter: public AbstractConverter { |
||||||
|
public: |
||||||
|
/** @brief Constructor with access to plugin manager */ |
||||||
|
explicit AnyConverter(PluginManager::Manager<AbstractConverter>& manager); |
||||||
|
|
||||||
|
/** @brief Plugin manager constructor */ |
||||||
|
explicit AnyConverter(PluginManager::AbstractManager& manager, const std::string& plugin); |
||||||
|
|
||||||
|
~AnyConverter(); |
||||||
|
|
||||||
|
private: |
||||||
|
MAGNUM_ANYSHADERCONVERTER_LOCAL ConverterFeatures doFeatures() const override; |
||||||
|
MAGNUM_ANYSHADERCONVERTER_LOCAL void doSetInputFormat(Format, Containers::StringView version) override; |
||||||
|
MAGNUM_ANYSHADERCONVERTER_LOCAL void doSetOutputFormat(Format, Containers::StringView version) override; |
||||||
|
MAGNUM_ANYSHADERCONVERTER_LOCAL std::pair<bool, Containers::String> doValidateFile(Stage stage, Containers::StringView filename) override; |
||||||
|
MAGNUM_ANYSHADERCONVERTER_LOCAL bool doConvertFileToFile(Stage stage, Containers::StringView from, Containers::StringView to) override; |
||||||
|
|
||||||
|
struct State; |
||||||
|
Containers::Pointer<State> _state; |
||||||
|
}; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,68 @@ |
|||||||
|
# |
||||||
|
# This file is part of Magnum. |
||||||
|
# |
||||||
|
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
# 2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
# |
||||||
|
# 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. |
||||||
|
# |
||||||
|
|
||||||
|
find_package(Corrade REQUIRED PluginManager) |
||||||
|
|
||||||
|
if(BUILD_PLUGINS_STATIC) |
||||||
|
set(MAGNUM_ANYSHADERCONVERTER_BUILD_STATIC 1) |
||||||
|
endif() |
||||||
|
|
||||||
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake |
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/configure.h) |
||||||
|
|
||||||
|
# AnyShaderConverter plugin |
||||||
|
add_plugin(AnyShaderConverter |
||||||
|
"${MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_BINARY_INSTALL_DIR};${MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_LIBRARY_INSTALL_DIR}" |
||||||
|
"${MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_BINARY_INSTALL_DIR};${MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_LIBRARY_INSTALL_DIR}" |
||||||
|
AnyShaderConverter.conf |
||||||
|
AnyConverter.cpp |
||||||
|
AnyConverter.h) |
||||||
|
if(BUILD_PLUGINS_STATIC AND BUILD_STATIC_PIC) |
||||||
|
set_target_properties(AnyShaderConverter PROPERTIES POSITION_INDEPENDENT_CODE ON) |
||||||
|
endif() |
||||||
|
target_link_libraries(AnyShaderConverter PUBLIC MagnumShaderTools) |
||||||
|
# Modify output location only if all are set, otherwise it makes no sense |
||||||
|
if(CMAKE_RUNTIME_OUTPUT_DIRECTORY AND CMAKE_LIBRARY_OUTPUT_DIRECTORY AND CMAKE_ARCHIVE_OUTPUT_DIRECTORY) |
||||||
|
set_target_properties(AnyShaderConverter PROPERTIES |
||||||
|
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/magnum$<$<CONFIG:Debug>:-d>/imageconverters |
||||||
|
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/magnum$<$<CONFIG:Debug>:-d>/imageconverters |
||||||
|
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/magnum$<$<CONFIG:Debug>:-d>/imageconverters) |
||||||
|
endif() |
||||||
|
|
||||||
|
install(FILES AnyConverter.h ${CMAKE_CURRENT_BINARY_DIR}/configure.h |
||||||
|
DESTINATION ${MAGNUM_PLUGINS_INCLUDE_INSTALL_DIR}/AnyShaderConverter) |
||||||
|
|
||||||
|
# Automatic static plugin import |
||||||
|
if(BUILD_PLUGINS_STATIC) |
||||||
|
install(FILES importStaticPlugin.cpp DESTINATION ${MAGNUM_PLUGINS_INCLUDE_INSTALL_DIR}/AnyShaderConverter) |
||||||
|
target_sources(AnyShaderConverter INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/importStaticPlugin.cpp) |
||||||
|
endif() |
||||||
|
|
||||||
|
if(BUILD_TESTS) |
||||||
|
add_subdirectory(Test) |
||||||
|
endif() |
||||||
|
|
||||||
|
# Magnum AnyShaderConverter target alias for superprojects |
||||||
|
add_library(Magnum::AnyShaderConverter ALIAS AnyShaderConverter) |
||||||
@ -0,0 +1,377 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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 <sstream> |
||||||
|
#include <Corrade/Containers/ArrayView.h> |
||||||
|
#include <Corrade/Containers/String.h> |
||||||
|
#include <Corrade/Containers/StringStl.h> |
||||||
|
#include <Corrade/PluginManager/Manager.h> |
||||||
|
#include <Corrade/TestSuite/Tester.h> |
||||||
|
#include <Corrade/Utility/DebugStl.h> |
||||||
|
#include <Corrade/Utility/Directory.h> |
||||||
|
#include <Corrade/Utility/FormatStl.h> |
||||||
|
|
||||||
|
#include "Magnum/ShaderTools/AbstractConverter.h" |
||||||
|
|
||||||
|
#include "configure.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace ShaderTools { namespace Test { namespace { |
||||||
|
|
||||||
|
struct AnyConverterTest: TestSuite::Tester { |
||||||
|
explicit AnyConverterTest(); |
||||||
|
|
||||||
|
void validate(); |
||||||
|
void validateNotSupported(); |
||||||
|
void validatePropagateFlags(); |
||||||
|
void validatePropagateInputVersion(); |
||||||
|
void validatePropagateOutputVersion(); |
||||||
|
|
||||||
|
void convert(); |
||||||
|
void convertNotSupported(); |
||||||
|
void convertPropagateFlags(); |
||||||
|
void convertPropagateInputVersion(); |
||||||
|
void convertPropagateOutputVersion(); |
||||||
|
|
||||||
|
void detectValidate(); |
||||||
|
void detectConvert(); |
||||||
|
|
||||||
|
void unknown(); |
||||||
|
|
||||||
|
/* Explicitly forbid system-wide plugin dependencies. Tests that need those
|
||||||
|
have their own manager. */ |
||||||
|
PluginManager::Manager<AbstractConverter> _manager{"nonexistent"}; |
||||||
|
}; |
||||||
|
|
||||||
|
constexpr struct { |
||||||
|
const char* name; |
||||||
|
const char* filename; |
||||||
|
const char* plugin; |
||||||
|
} DetectValidateData[]{ |
||||||
|
{"SPIR-V", "flat.spv", "SpirvShaderConverter"}, |
||||||
|
{"SPIR-V assembly uppercase", "DOOM.SPVASM", "SpirvAssemblyShaderConverter"}, |
||||||
|
{"SPIR-V assembly weird", "test.asm.rahit", "SpirvAssemblyShaderConverter"}, |
||||||
|
{"GLSL explicit", "phong.glsl", "GlslShaderConverter"}, |
||||||
|
{"GLSL implicit", "phong.frag", "GlslShaderConverter"}, |
||||||
|
}; |
||||||
|
|
||||||
|
constexpr struct { |
||||||
|
const char* name; |
||||||
|
const char* from; |
||||||
|
const char* to; |
||||||
|
const char* plugin; |
||||||
|
} DetectConvertData[]{ |
||||||
|
{"SPIR-V to SPIR-V", "flat.spv", "optimized.spv", "SpirvShaderConverter"}, |
||||||
|
{"SPIR-V assembly to SPIR-V", "a.spvasm", "b.spv", "SpirvAssemblyToSpirvShaderConverter"}, |
||||||
|
{"SPIR-V to GLSL", "phong.frag.spv", "phong.glsl", "SpirvToGlslShaderConverter"} |
||||||
|
}; |
||||||
|
|
||||||
|
AnyConverterTest::AnyConverterTest() { |
||||||
|
addTests({&AnyConverterTest::validate, |
||||||
|
&AnyConverterTest::validateNotSupported, |
||||||
|
&AnyConverterTest::validatePropagateFlags, |
||||||
|
&AnyConverterTest::validatePropagateInputVersion, |
||||||
|
&AnyConverterTest::validatePropagateOutputVersion, |
||||||
|
|
||||||
|
&AnyConverterTest::convert, |
||||||
|
&AnyConverterTest::convertNotSupported, |
||||||
|
&AnyConverterTest::convertPropagateFlags, |
||||||
|
&AnyConverterTest::convertPropagateInputVersion, |
||||||
|
&AnyConverterTest::convertPropagateOutputVersion}); |
||||||
|
|
||||||
|
addInstancedTests({&AnyConverterTest::detectValidate}, |
||||||
|
Containers::arraySize(DetectValidateData)); |
||||||
|
|
||||||
|
addInstancedTests({&AnyConverterTest::detectConvert}, |
||||||
|
Containers::arraySize(DetectConvertData)); |
||||||
|
|
||||||
|
addTests({&AnyConverterTest::unknown}); |
||||||
|
|
||||||
|
/* Load the plugin directly from the build tree. Otherwise it's static and
|
||||||
|
already loaded. */ |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_INTERNAL_ASSERT_OUTPUT(_manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Create the output directory if it doesn't exist yet */ |
||||||
|
CORRADE_INTERNAL_ASSERT_OUTPUT(Utility::Directory::mkpath(ANYSHADERCONVERTER_TEST_OUTPUT_DIR)); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::validate() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
const std::string filename = Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"); |
||||||
|
|
||||||
|
/* Make it print a warning so we know it's doing something */ |
||||||
|
CORRADE_COMPARE(converter->validateFile(Stage::Fragment, filename), |
||||||
|
std::make_pair(true, Utility::formatString("WARNING: {}:4: 'reserved__identifier' : identifiers containing consecutive underscores (\"__\") are reserved", filename))); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::validateNotSupported() { |
||||||
|
CORRADE_SKIP("No plugin that would support just validation exists."); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::validatePropagateFlags() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
const std::string filename = Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"); |
||||||
|
|
||||||
|
/* With this, the warning should turn into an error. The converter should
|
||||||
|
also print the verbose info. */ |
||||||
|
converter->setFlags(ConverterFlag::Verbose|ConverterFlag::WarningAsError); |
||||||
|
|
||||||
|
std::ostringstream out; |
||||||
|
Debug redirectDebug{&out}; |
||||||
|
CORRADE_COMPARE(converter->validateFile(Stage::Fragment, filename), |
||||||
|
std::make_pair(false, Utility::formatString("WARNING: {}:4: 'reserved__identifier' : identifiers containing consecutive underscores (\"__\") are reserved", filename))); |
||||||
|
CORRADE_COMPARE(out.str(), |
||||||
|
"ShaderTools::AnyConverter::validateFile(): using GlslShaderConverter (provided by GlslangShaderConverter)\n"); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::validatePropagateInputVersion() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
/* This is an invalid version. We have to supply a valid file path because
|
||||||
|
the version gets checked in doValidateData(), called from |
||||||
|
AbstractConverter::doValidateFile() with the file contents. */ |
||||||
|
converter->setInputFormat(Format::Glsl, "100"); |
||||||
|
|
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_COMPARE(converter->validateFile(Stage::Fragment, Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl")), |
||||||
|
std::make_pair(false, "")); |
||||||
|
CORRADE_COMPARE(out.str(), |
||||||
|
"ShaderTools::GlslangConverter::validateData(): input format version should be one of supported GLSL #version strings but got 100\n"); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::validatePropagateOutputVersion() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
/* This is an invalid version. We have to supply a valid file path because
|
||||||
|
the version gets checked in doValidateData(), called from |
||||||
|
AbstractConverter::doValidateFile() with the file contents. */ |
||||||
|
converter->setOutputFormat(Format::Spirv, "opengl4.0"); |
||||||
|
|
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_COMPARE(converter->validateFile(Stage::Fragment, Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl")), |
||||||
|
std::make_pair(false, "")); |
||||||
|
CORRADE_COMPARE(out.str(), |
||||||
|
"ShaderTools::GlslangConverter::validateData(): output format should be Unspecified but got ShaderTools::Format::Spirv\n"); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::convert() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
const std::string inputFilename = Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"); |
||||||
|
const std::string outputFilename = Utility::Directory::join(ANYSHADERCONVERTER_TEST_OUTPUT_DIR, "file.spv"); |
||||||
|
Utility::Directory::rm(outputFilename); |
||||||
|
CORRADE_VERIFY(!Utility::Directory::exists(outputFilename)); |
||||||
|
|
||||||
|
/* Make it print a warning so we know it's doing something */ |
||||||
|
std::ostringstream out; |
||||||
|
Warning redirectWarning{&out}; |
||||||
|
CORRADE_VERIFY(converter->convertFileToFile(Stage::Fragment, inputFilename, outputFilename)); |
||||||
|
CORRADE_VERIFY(Utility::Directory::exists(outputFilename)); |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"ShaderTools::GlslangConverter::convertDataToData(): compilation succeeded with the following message:\n" |
||||||
|
"WARNING: {}:4: 'reserved__identifier' : identifiers containing consecutive underscores (\"__\") are reserved\n", inputFilename)); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::convertNotSupported() { |
||||||
|
CORRADE_SKIP("No plugin that would support just validation exists."); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::convertPropagateFlags() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
const std::string filename = Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"); |
||||||
|
|
||||||
|
/* With this, the warning should turn into an error. The converter should
|
||||||
|
also print the verbose info. */ |
||||||
|
converter->setFlags(ConverterFlag::Verbose|ConverterFlag::WarningAsError); |
||||||
|
|
||||||
|
/* We have to supply a valid file path because the version gets checked in
|
||||||
|
doConvertDataToData(), called from AbstractConverter::doConvertFileToFile() |
||||||
|
with the file contents. */ |
||||||
|
std::ostringstream out; |
||||||
|
Debug redirectDebug{&out}; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_VERIFY(!converter->convertFileToFile(Stage::Fragment, Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"), Utility::Directory::join(ANYSHADERCONVERTER_TEST_OUTPUT_DIR, "file.spv"))); |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"ShaderTools::AnyConverter::convertFileToFile(): using GlslToSpirvShaderConverter (provided by GlslangShaderConverter)\n" |
||||||
|
"ShaderTools::GlslangConverter::convertDataToData(): compilation failed:\n" |
||||||
|
"WARNING: {}:4: 'reserved__identifier' : identifiers containing consecutive underscores (\"__\") are reserved\n", filename)); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::convertPropagateInputVersion() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
/* This is an invalid version */ |
||||||
|
converter->setInputFormat(Format::Glsl, "100"); |
||||||
|
|
||||||
|
/* We have to supply a valid file path because the version gets checked in
|
||||||
|
doConvertDataToData(), called from AbstractConverter::doConvertFileToFile() |
||||||
|
with the file contents. */ |
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_VERIFY(!converter->convertFileToFile(Stage::Fragment, Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"), Utility::Directory::join(ANYSHADERCONVERTER_TEST_OUTPUT_DIR, "file.spv"))); |
||||||
|
CORRADE_COMPARE(out.str(), |
||||||
|
"ShaderTools::GlslangConverter::convertDataToData(): input format version should be one of supported GLSL #version strings but got 100\n"); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::convertPropagateOutputVersion() { |
||||||
|
PluginManager::Manager<AbstractConverter> manager{MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR}; |
||||||
|
#ifdef ANYSHADERCONVERTER_PLUGIN_FILENAME |
||||||
|
CORRADE_VERIFY(manager.load(ANYSHADERCONVERTER_PLUGIN_FILENAME) & PluginManager::LoadState::Loaded); |
||||||
|
#endif |
||||||
|
|
||||||
|
if(manager.load("GlslangShaderConverter") < PluginManager::LoadState::Loaded) |
||||||
|
CORRADE_SKIP("GlslangShaderConverter plugin can't be loaded."); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
/* This is an invalid version */ |
||||||
|
converter->setOutputFormat(Format::Spirv, "opengl4.0"); |
||||||
|
|
||||||
|
/* We have to supply a valid file path because the version gets checked in
|
||||||
|
doConvertDataToData(), called from AbstractConverter::doConvertFileToFile() |
||||||
|
with the file contents. */ |
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_VERIFY(!converter->convertFileToFile(Stage::Fragment, Utility::Directory::join(ANYSHADERCONVERTER_TEST_DIR, "file.glsl"), Utility::Directory::join(ANYSHADERCONVERTER_TEST_OUTPUT_DIR, "file.spv"))); |
||||||
|
CORRADE_COMPARE(out.str(), |
||||||
|
"ShaderTools::GlslangConverter::convertDataToData(): output format version target should be opengl4.5 or vulkanX.Y but got opengl4.0\n"); |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::detectValidate() { |
||||||
|
auto&& data = DetectValidateData[testCaseInstanceId()]; |
||||||
|
setTestCaseDescription(data.name); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = _manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_COMPARE(converter->validateFile({}, data.filename), |
||||||
|
std::make_pair(false, "")); |
||||||
|
#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"PluginManager::Manager::load(): plugin {0} is not static and was not found in nonexistent\n" |
||||||
|
"ShaderTools::AnyConverter::validateFile(): cannot load the {0} plugin\n", data.plugin)); |
||||||
|
#else |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"PluginManager::Manager::load(): plugin {0} was not found\n" |
||||||
|
"ShaderTools::AnyConverter::validateFile(): cannot load the {0} plugin\n", data.plugin)); |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::detectConvert() { |
||||||
|
auto&& data = DetectConvertData[testCaseInstanceId()]; |
||||||
|
setTestCaseDescription(data.name); |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = _manager.instantiate("AnyShaderConverter"); |
||||||
|
|
||||||
|
std::ostringstream out; |
||||||
|
Error redirectError{&out}; |
||||||
|
CORRADE_VERIFY(!converter->convertFileToFile({}, data.from, Utility::Directory::join(ANYSHADERCONVERTER_TEST_OUTPUT_DIR, data.to))); |
||||||
|
#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"PluginManager::Manager::load(): plugin {0} is not static and was not found in nonexistent\n" |
||||||
|
"ShaderTools::AnyConverter::convertFileToFile(): cannot load the {0} plugin\n", data.plugin)); |
||||||
|
#else |
||||||
|
CORRADE_COMPARE(out.str(), Utility::formatString( |
||||||
|
"PluginManager::Manager::load(): plugin {0} was not found\n" |
||||||
|
"ShaderTools::AnyConverter::convertFileToFile(): cannot load the {0} plugin\n", data.plugin)); |
||||||
|
#endif |
||||||
|
} |
||||||
|
|
||||||
|
void AnyConverterTest::unknown() { |
||||||
|
std::ostringstream output; |
||||||
|
Error redirectError{&output}; |
||||||
|
|
||||||
|
Containers::Pointer<AbstractConverter> converter = _manager.instantiate("AnyShaderConverter"); |
||||||
|
CORRADE_COMPARE(converter->validateFile({}, "dead.cg"), |
||||||
|
std::make_pair(false, "")); |
||||||
|
CORRADE_COMPARE(output.str(), "ShaderTools::AnyConverter::validateFile(): cannot determine the format of dead.cg\n"); |
||||||
|
} |
||||||
|
|
||||||
|
}}}} |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::ShaderTools::Test::AnyConverterTest) |
||||||
@ -0,0 +1,65 @@ |
|||||||
|
# |
||||||
|
# This file is part of Magnum. |
||||||
|
# |
||||||
|
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
# 2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
# |
||||||
|
# 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. |
||||||
|
# |
||||||
|
|
||||||
|
if(CORRADE_TARGET_EMSCRIPTEN OR CORRADE_TARGET_ANDROID) |
||||||
|
set(ANYSHADERCONVERTER_TEST_DIR ".") |
||||||
|
set(ANYSHADERCONVERTER_TEST_OUTPUT_DIR "write") |
||||||
|
else() |
||||||
|
set(ANYSHADERCONVERTER_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
||||||
|
set(ANYSHADERCONVERTER_TEST_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
||||||
|
endif() |
||||||
|
|
||||||
|
# CMake before 3.8 has broken $<TARGET_FILE*> expressions for iOS (see |
||||||
|
# https://gitlab.kitware.com/cmake/cmake/merge_requests/404) and since Corrade |
||||||
|
# doesn't support dynamic plugins on iOS, this sorta works around that. Should |
||||||
|
# be revisited when updating Travis to newer Xcode (xcode7.3 has CMake 3.6). |
||||||
|
if(NOT BUILD_PLUGINS_STATIC) |
||||||
|
set(ANYSHADERCONVERTER_PLUGIN_FILENAME $<TARGET_FILE:AnyShaderConverter>) |
||||||
|
endif() |
||||||
|
|
||||||
|
# First replace ${} variables, then $<> generator expressions |
||||||
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake |
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/configure.h.in) |
||||||
|
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/configure.h |
||||||
|
INPUT ${CMAKE_CURRENT_BINARY_DIR}/configure.h.in) |
||||||
|
|
||||||
|
corrade_add_test(AnyShaderConverterTest AnyConverterTest.cpp |
||||||
|
LIBRARIES MagnumShaderTools |
||||||
|
FILES file.glsl) |
||||||
|
target_include_directories(AnyShaderConverterTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>) |
||||||
|
if(BUILD_PLUGINS_STATIC) |
||||||
|
target_link_libraries(AnyShaderConverterTest PRIVATE AnyShaderConverter) |
||||||
|
else() |
||||||
|
# So the plugins get properly built when building the test |
||||||
|
add_dependencies(AnyShaderConverterTest AnyShaderConverter) |
||||||
|
endif() |
||||||
|
set_target_properties(AnyShaderConverterTest PROPERTIES FOLDER "MagnumPlugins/AnyShaderConverter/Test") |
||||||
|
if(CORRADE_BUILD_STATIC AND NOT BUILD_PLUGINS_STATIC) |
||||||
|
# CMake < 3.4 does this implicitly, but 3.4+ not anymore (see CMP0065). |
||||||
|
# That's generally okay, *except if* the build is static, the executable |
||||||
|
# uses a plugin manager and needs to share globals with the plugins (such |
||||||
|
# as output redirection and so on). |
||||||
|
set_target_properties(AnyShaderConverterTest PROPERTIES ENABLE_EXPORTS ON) |
||||||
|
endif() |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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. |
||||||
|
*/ |
||||||
|
|
||||||
|
#cmakedefine ANYSHADERCONVERTER_PLUGIN_FILENAME "${ANYSHADERCONVERTER_PLUGIN_FILENAME}" |
||||||
|
#define ANYSHADERCONVERTER_TEST_DIR "${ANYSHADERCONVERTER_TEST_DIR}" |
||||||
|
#define ANYSHADERCONVERTER_TEST_OUTPUT_DIR "${ANYSHADERCONVERTER_TEST_OUTPUT_DIR}" |
||||||
|
|
||||||
|
#ifdef CORRADE_TARGET_WINDOWS |
||||||
|
#ifdef CORRADE_IS_DEBUG_BUILD |
||||||
|
#define MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_BINARY_INSTALL_DIR}" |
||||||
|
#else |
||||||
|
#define MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_BINARY_INSTALL_DIR}" |
||||||
|
#endif |
||||||
|
#else |
||||||
|
#ifdef CORRADE_IS_DEBUG_BUILD |
||||||
|
#define MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_LIBRARY_INSTALL_DIR}" |
||||||
|
#else |
||||||
|
#define MAGNUM_PLUGINS_SHADERCONVERTER_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_LIBRARY_INSTALL_DIR}" |
||||||
|
#endif |
||||||
|
#endif |
||||||
@ -0,0 +1,6 @@ |
|||||||
|
#version 140 |
||||||
|
|
||||||
|
void main() { |
||||||
|
float reserved__identifier = 3.0; |
||||||
|
gl_FragColor = vec4(reserved__identifier); |
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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. |
||||||
|
*/ |
||||||
|
|
||||||
|
#cmakedefine MAGNUM_ANYSHADERCONVERTER_BUILD_STATIC |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
/*
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||||
|
2020 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
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 "MagnumPlugins/AnyShaderConverter/configure.h" |
||||||
|
|
||||||
|
#ifdef MAGNUM_ANYSHADERCONVERTER_BUILD_STATIC |
||||||
|
#include <Corrade/PluginManager/AbstractManager.h> |
||||||
|
|
||||||
|
static int magnumAnyShaderConverterStaticImporter() { |
||||||
|
CORRADE_PLUGIN_IMPORT(AnyShaderConverter) |
||||||
|
return 1; |
||||||
|
} CORRADE_AUTOMATIC_INITIALIZER(magnumAnyShaderConverterStaticImporter) |
||||||
|
#endif |
||||||
Loading…
Reference in new issue