Skip to content

Commit

Permalink
ShaderTools: bootstrap new library.
Browse files Browse the repository at this point in the history
Currently there's just a plugin interface for shader converters. More
stuff (probably something like SPIR-V reflection) will come later.
  • Loading branch information
mosra committed Oct 29, 2020
1 parent 0f026cd commit 13dfb64
Show file tree
Hide file tree
Showing 24 changed files with 3,122 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ option(WITH_DEBUGTOOLS "Build DebugTools library" ON)
cmake_dependent_option(WITH_MESHTOOLS "Build MeshTools library" ON "NOT WITH_OBJIMPORTER;NOT WITH_SCENECONVERTER" ON)
option(WITH_SCENEGRAPH "Build SceneGraph library" ON)
option(WITH_SHADERS "Build Shaders library" ON)
option(WITH_SHADERTOOLS "Build ShaderTools library" ON)
cmake_dependent_option(WITH_TEXT "Build Text library" ON "NOT WITH_FONTCONVERTER;NOT WITH_MAGNUMFONT;NOT WITH_MAGNUMFONTCONVERTER" ON)
cmake_dependent_option(WITH_TEXTURETOOLS "Build TextureTools library" ON "NOT WITH_TEXT;NOT WITH_DISTANCEFIELDCONVERTER" ON)
cmake_dependent_option(WITH_TRADE "Build Trade library" ON "NOT WITH_MESHTOOLS;NOT WITH_PRIMITIVES;NOT WITH_IMAGECONVERTER;NOT WITH_ANYIMAGEIMPORTER;NOT WITH_ANYIMAGECONVERTER;NOT WITH_ANYSCENEIMPORTER;NOT WITH_OBJIMPORTER;NOT WITH_TGAIMAGECONVERTER;NOT WITH_TGAIMPORTER" ON)
Expand Down Expand Up @@ -362,6 +363,10 @@ set(MAGNUM_PLUGINS_SCENECONVERTER_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBU
set(MAGNUM_PLUGINS_SCENECONVERTER_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR}/sceneconverters)
set(MAGNUM_PLUGINS_SCENECONVERTER_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR}/sceneconverters)
set(MAGNUM_PLUGINS_SCENECONVERTER_RELEASE_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_LIBRARY_INSTALL_DIR}/sceneconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_BINARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_LIBRARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_AUDIOIMPORTER_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_BINARY_INSTALL_DIR}/audioimporters)
set(MAGNUM_PLUGINS_AUDIOIMPORTER_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR}/audioimporters)
set(MAGNUM_PLUGINS_AUDIOIMPORTER_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR}/audioimporters)
Expand Down Expand Up @@ -396,6 +401,7 @@ set(MAGNUM_PLUGINS_DIR ""
# Plugin directories. Set only if the above are non-empty. otherwise empty as
# well.
if(MAGNUM_PLUGINS_DIR)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DIR ${MAGNUM_PLUGINS_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_FONT_DIR ${MAGNUM_PLUGINS_DIR}/fonts)
set(MAGNUM_PLUGINS_FONTCONVERTER_DIR ${MAGNUM_PLUGINS_DIR}/fontconverters)
set(MAGNUM_PLUGINS_IMAGECONVERTER_DIR ${MAGNUM_PLUGINS_DIR}/imageconverters)
Expand All @@ -404,6 +410,7 @@ if(MAGNUM_PLUGINS_DIR)
set(MAGNUM_PLUGINS_AUDIOIMPORTER_DIR ${MAGNUM_PLUGINS_DIR}/audioimporters)
endif()
if(MAGNUM_PLUGINS_DEBUG_DIR)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_DIR ${MAGNUM_PLUGINS_DEBUG_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_FONT_DEBUG_DIR ${MAGNUM_PLUGINS_DEBUG_DIR}/fonts)
set(MAGNUM_PLUGINS_FONTCONVERTER_DEBUG_DIR ${MAGNUM_PLUGINS_DEBUG_DIR}/fontconverters)
set(MAGNUM_PLUGINS_IMAGECONVERTER_DEBUG_DIR ${MAGNUM_PLUGINS_DEBUG_DIR}/imageconverters)
Expand All @@ -413,6 +420,7 @@ if(MAGNUM_PLUGINS_DEBUG_DIR)
set(MAGNUM_PLUGINS_AUDIOIMPORTER_DEBUG_DIR ${MAGNUM_PLUGINS_DEBUG_DIR}/audioimporters)
endif()
if(MAGNUM_PLUGINS_RELEASE_DIR)
set(MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_FONTCONVERTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/fontconverters)
set(MAGNUM_PLUGINS_IMAGECONVERTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/imageconverters)
set(MAGNUM_PLUGINS_IMPORTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/importers)
Expand Down
1 change: 1 addition & 0 deletions doc/building.dox
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ specify which parts will be built and which not:
- `WITH_SCENEGRAPH` --- Build the @ref SceneGraph library
- `WITH_SHADERS` --- Build the @ref Shaders library. Enables also building of
the GL library.
- `WITH_SHADERTOOLS` --- Build the @ref ShaderTools library
- `WITH_TEXT` --- Build the @ref Text library. Enables also building of
the TextureTools library.
- `WITH_TEXTURETOOLS` --- Build the @ref TextureTools library. Enabled
Expand Down
6 changes: 6 additions & 0 deletions doc/changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ See also:
- Added @ref Shaders::Phong::setLightSpecularColors() for better control over
speculat highlights

@subsubsection changelog-latest-new-shadertools ShaderTools library

- New @ref ShaderTools library that provides a
@ref ShaderTools::AbstractConverter plugin interface for shader validation,
conversion, compilation and optimization

@subsubsection changelog-latest-new-scenegraph SceneGraph library

- Added @ref SceneGraph::Object::move()
Expand Down
1 change: 1 addition & 0 deletions doc/cmake.dox
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ the components. The optional components are:
- `Primitives` --- @ref Primitives library
- `SceneGraph` --- @ref SceneGraph library
- `Shaders` --- @ref Shaders library
- `ShaderTools` --- @ref ShaderTools library
- `Text` --- @ref Text library
- `TextureTools` --- @ref TextureTools library
- `Trade` --- @ref Trade library
Expand Down
1 change: 1 addition & 0 deletions doc/compilation-speedup.dox
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ available, each namespace has its own:
- @ref Magnum/OvrIntegration/OvrIntegration.h
- @ref Magnum/Platform/Platform.h
- @ref Magnum/SceneGraph/SceneGraph.h
- @ref Magnum/ShaderTools/ShaderTools.h
- @ref Magnum/Shaders/Shaders.h
- @ref Magnum/Text/Text.h
- @ref Magnum/Trade/Trade.h
Expand Down
2 changes: 2 additions & 0 deletions doc/custom-buildsystems-order.dot
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ digraph "Magnum library dependency order" {
MagnumPrimitives [label="Magnum\nPrimitives" class="m-info"]
MagnumSceneGraph [label="Magnum\nSceneGraph" class="m-info"]
MagnumShaders [label="Magnum\nShaders" class="m-info"]
MagnumShaderTools [label="Magnum\nShaderTools" class="m-info"]
MagnumText [label="Magnum\nText" class="m-info"]
MagnumTextureTools [label="Magnum\nTextureTools" class="m-info"]
MagnumTrade [label="Magnum\nTrade" class="m-info"]
Expand Down Expand Up @@ -85,6 +86,7 @@ digraph "Magnum library dependency order" {
MagnumSceneGraph -> Magnum

MagnumShaders -> MagnumGL
MagnumShaderTools -> Magnum

MagnumText -> MagnumTextureTools
MagnumText -> MagnumGL [style=dotted]
Expand Down
26 changes: 26 additions & 0 deletions doc/namespaces.dox
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,32 @@ See @ref building, @ref cmake and @ref shaders for more information.

*/

/** @dir Magnum/ShaderTools
* @brief Namespace @ref Magnum::ShaderTools
* @m_since_latest
*/
/** @namespace Magnum::ShaderTools
@brief Shader tools
@m_since_latest

Shader validation, conversion, compilation and reflection.

This library is built if `WITH_SHADERTOOLS` is enabled when building Magnum. To
use this library with CMake, request the `ShaderTools` component of the
`Magnum` package and link to the `Magnum::ShaderTools` target:

@code{.cmake}
find_package(Magnum REQUIRED ShaderTools)

# ...
target_link_libraries(your-app PRIVATE Magnum::ShaderTools)
@endcode

Additional plugins and utilities are built separately. See particular
`*Converter` class documentation, @ref building, @ref building-plugins,
@ref cmake, @ref cmake-plugins and @ref plugins for more information.
*/

/** @dir Magnum/Text
* @brief Namespace @ref Magnum::Text
*/
Expand Down
3 changes: 3 additions & 0 deletions doc/plugins.dox
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ of given type. Magnum provides these plugin interfaces:
- @ref Audio::AbstractImporter --- importers for audio formats. See
`*Importer` classes in the @ref Audio namespace for available audio
importer plugins.
- @ref ShaderTools::AbstractConverter --- shader conversion, compilation,
optimization and validation. See `*Converter` classes in the
@ref ShaderTools namespace for available shader converter plugins.

@section plugins-loading Loading and instantiating plugins

Expand Down
7 changes: 7 additions & 0 deletions doc/snippets/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ if(WITH_MESHTOOLS)
set_target_properties(snippets-MagnumMeshTools PROPERTIES FOLDER "Magnum/doc/snippets")
endif()

if(WITH_SHADERTOOLS)
add_library(snippets-MagnumShaderTools STATIC
MagnumShaderTools.cpp)
target_link_libraries(snippets-MagnumShaderTools PRIVATE MagnumShaderTools)
set_target_properties(snippets-MagnumShaderTools PROPERTIES FOLDER "Magnum/doc/snippets")
endif()

if(WITH_TRADE)
add_library(snippets-MagnumTrade STATIC
plugins.cpp
Expand Down
95 changes: 95 additions & 0 deletions doc/snippets/MagnumShaderTools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020 Vladimír Vondruš <[email protected]>
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 <unordered_map>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Utility/Resource.h>

#include "Magnum/FileCallback.h"
#include "Magnum/ShaderTools/AbstractConverter.h"

#define DOXYGEN_IGNORE(...) __VA_ARGS__

using namespace Magnum;

int main() {
{
Containers::Pointer<ShaderTools::AbstractConverter> converter;
Containers::Array<const char> extract(const std::string&, const std::string&);
/* [AbstractConverter-usage-callbacks] */
struct Data {
std::unordered_map<std::string,
Containers::Array<const char>> files;
} data;

converter->setInputFileCallback([](const std::string& filename,
InputFileCallbackPolicy policy, Data& data)
-> Containers::Optional<Containers::ArrayView<const char>>
{
auto found = data.files.find(filename);

/* Discard the loaded file, if not needed anymore */
if(policy == InputFileCallbackPolicy::Close) {
if(found != data.files.end()) data.files.erase(found);
return {};
}

/* Extract from an archive if not there yet */
if(found == data.files.end()) found = data.files.emplace(
filename, extract("shaders.zip", filename)).first;

return Containers::arrayView(found->second);
}, data);

/* extracted from a ZIP */
auto result = converter->validateFile(ShaderTools::Stage::Fragment, "ssao.frag");
/* [AbstractConverter-usage-callbacks] */
}

{
Containers::Pointer<ShaderTools::AbstractConverter> converter;
/* [AbstractConverter-setInputFileCallback] */
converter->setInputFileCallback([](const std::string& filename,
InputFileCallbackPolicy, void*) {
Utility::Resource rs("data");
return Containers::optional(rs.getRaw(filename));
});
/* [AbstractConverter-setInputFileCallback] */
}

{
Containers::Pointer<ShaderTools::AbstractConverter> converter;
/* [AbstractConverter-setInputFileCallback-template] */
const Utility::Resource rs{"data"};
converter->setInputFileCallback([](const std::string& filename,
InputFileCallbackPolicy, const Utility::Resource& rs) {
return Containers::optional(rs.getRaw(filename));
}, rs);
/* [AbstractConverter-setInputFileCallback-template] */
}

}
27 changes: 24 additions & 3 deletions modules/FindMagnum.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
# Primitives - Primitives library
# SceneGraph - SceneGraph library
# Shaders - Shaders library
# ShaderTools - ShaderTools library
# Text - Text library
# TextureTools - TextureTools library
# Trade - Trade library
Expand Down Expand Up @@ -164,6 +165,10 @@
# installation directory
# MAGNUM_PLUGINS_[DEBUG|RELEASE]_LIBRARY_INSTALL_DIR - Plugin library
# installation directory
# MAGNUM_PLUGINS_SHADERCONVERTER_[DEBUG|RELEASE]_BINARY_INSTALL_DIR - Shader
# converter plugin binary installation directory
# MAGNUM_PLUGINS_SHADERCONVERTER_[DEBUG|RELEASE]_LIBRARY_INSTALL_DIR - Shader
# converter plugin library installation directory
# MAGNUM_PLUGINS_FONT_[DEBUG|RELEASE]_BINARY_INSTALL_DIR - Font plugin binary
# installation directory
# MAGNUM_PLUGINS_FONT_[DEBUG|RELEASE]_LIBRARY_INSTALL_DIR - Font plugin
Expand Down Expand Up @@ -224,7 +229,7 @@ foreach(_component ${Magnum_FIND_COMPONENTS})

# Unrolling the transitive dependencies here so this doesn't need to be
# after resolving inter-component dependencies. Listing also all plugins.
if(_component MATCHES "^(Audio|DebugTools|MeshTools|Primitives|Text|TextureTools|Trade|.+Importer|.+ImageConverter|.+Font)$")
if(_component MATCHES "^(Audio|DebugTools|MeshTools|Primitives|ShaderTools|Text|TextureTools|Trade|.+Importer|.+ImageConverter|.+Font|.+ShaderConverter)$")
set(_MAGNUM_${_COMPONENT}_CORRADE_DEPENDENCIES PluginManager)
endif()

Expand Down Expand Up @@ -355,8 +360,8 @@ endif()
# Component distinction (listing them explicitly to avoid mistakes with finding
# components from other repositories)
set(_MAGNUM_LIBRARY_COMPONENT_LIST
Audio DebugTools GL MeshTools Primitives SceneGraph Shaders Text
TextureTools Trade Vk
Audio DebugTools GL MeshTools Primitives SceneGraph Shaders ShaderTools
Text TextureTools Trade Vk
AndroidApplication EmscriptenApplication GlfwApplication GlxApplication
Sdl2Application XEglApplication WindowlessCglApplication
WindowlessEglApplication WindowlessGlxApplication WindowlessIosApplication
Expand Down Expand Up @@ -470,6 +475,8 @@ set(_MAGNUM_ObjImporter_DEPENDENCIES MeshTools) # and below
foreach(_component ${_MAGNUM_PLUGIN_COMPONENT_LIST})
if(_component MATCHES ".+AudioImporter")
list(APPEND _MAGNUM_${_component}_DEPENDENCIES Audio)
elseif(_component MATCHES ".+ShaderConverter")
list(APPEND _MAGNUM_${_component}_DEPENDENCIES ShaderTools)
elseif(_component MATCHES ".+(Importer|ImageConverter|SceneConverter)")
list(APPEND _MAGNUM_${_component}_DEPENDENCIES Trade)
elseif(_component MATCHES ".+(Font|FontConverter)")
Expand Down Expand Up @@ -549,6 +556,10 @@ foreach(_component ${Magnum_FIND_COMPONENTS})
string(REPLACE "AudioImporter" "Importer" _MAGNUM_${_COMPONENT}_HEADER_NAME "${_component}")
set(_MAGNUM_${_COMPONENT}_INCLUDE_PATH_NAMES ${_MAGNUM_${_COMPONENT}_HEADER_NAME}.h)

# ShaderConverter plugin specific name suffixes
elseif(_component MATCHES ".+ShaderConverter$")
set(_MAGNUM_${_COMPONENT}_PATH_SUFFIX shaderconverters)

# Importer plugin specific name suffixes
elseif(_component MATCHES ".+Importer$")
set(_MAGNUM_${_COMPONENT}_PATH_SUFFIX importers)
Expand Down Expand Up @@ -862,6 +873,12 @@ foreach(_component ${Magnum_FIND_COMPONENTS})
set(_MAGNUM_${_COMPONENT}_INCLUDE_PATH_NAMES Cube.h)

# No special setup for SceneGraph library

# ShaderTools library
elseif(_component STREQUAL ShaderTools)
set_property(TARGET Magnum::${_component} APPEND PROPERTY
INTERFACE_LINK_LIBRARIES Corrade::PluginManager)

# No special setup for Shaders library

# Text library
Expand Down Expand Up @@ -1116,6 +1133,10 @@ set(MAGNUM_PLUGINS_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_BINARY_INSTALL_DIR}/magnum-
set(MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_LIBRARY_INSTALL_DIR}/magnum-d)
set(MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_BINARY_INSTALL_DIR}/magnum)
set(MAGNUM_PLUGINS_RELEASE_LIBRARY_INSTALL_DIR ${MAGNUM_LIBRARY_INSTALL_DIR}/magnum)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_BINARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_LIBRARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_SHADERCONVERTER_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR}/shaderconverters)
set(MAGNUM_PLUGINS_FONT_DEBUG_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_BINARY_INSTALL_DIR}/fonts)
set(MAGNUM_PLUGINS_FONT_DEBUG_LIBRARY_INSTALL_DIR ${MAGNUM_PLUGINS_DEBUG_LIBRARY_INSTALL_DIR}/fonts)
set(MAGNUM_PLUGINS_FONT_RELEASE_BINARY_INSTALL_DIR ${MAGNUM_PLUGINS_RELEASE_BINARY_INSTALL_DIR}/fonts)
Expand Down
4 changes: 4 additions & 0 deletions src/Magnum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ if(WITH_SHADERS)
add_subdirectory(Shaders)
endif()

if(WITH_SHADERTOOLS)
add_subdirectory(ShaderTools)
endif()

if(WITH_TEXT)
add_subdirectory(Text)
endif()
Expand Down
Loading

0 comments on commit 13dfb64

Please sign in to comment.