diff --git a/doc/portability.dox b/doc/portability.dox index 955df7cab..432fd7f26 100644 --- a/doc/portability.dox +++ b/doc/portability.dox @@ -48,14 +48,7 @@ If you include @ref Magnum.h, you get these predefined macros: Example usage: -@code{.cpp} -#ifndef MAGNUM_TARGET_GLES -Renderer::setPolygonMode(Renderer::PolygonMode::Lines); -// draw mesh as wireframe... -#else -// use different mesh, as polygon mode is not supported in OpenGL ES... -#endif -@endcode +@snippet Magnum.cpp portability-targets Each feature is marked accordingly if it is not available in some targets. See also @ref requires-gl, @ref requires-gles20 and @ref requires-gles30. @@ -85,15 +78,7 @@ thus the decision cannot be made at compile time. Header @ref Extensions.h contains list of extensions, which you can pass to @ref Context::isExtensionSupported() and decide based on that: -@code{.cpp} -if(Context::current().isExtensionSupported()) { - // draw mesh with wireframe on top in one pass using geometry shader... -} else { - // draw underlying mesh... - Renderer::setPolygonMode(Renderer::PolygonMode::Lines); - // draw mesh as wirefreame in second pass... -} -@endcode +@snippet Magnum.cpp portability-extensions You can also decide on particular OpenGL version using @ref Context::isVersionSupported(), but remember that some features from that version might be available even if @@ -104,10 +89,7 @@ extensions, you can use macros @ref MAGNUM_ASSERT_EXTENSION_SUPPORTED() or @ref MAGNUM_ASSERT_VERSION_SUPPORTED() to add mandatory requirement of given extension or version: -@code{.cpp} -MAGNUM_ASSERT_EXTENSION_SUPPORTED(GL::ARB::geometry_shader4); -// just use geometry shader and don't care about old hardware -@endcode +@snippet Magnum.cpp portability-extension-assert Each class, function or enum value is marked accordingly if it needs specific extension or specific OpenGL version. Various classes in Magnum are taking @@ -130,13 +112,9 @@ you can decide on the syntax in your shader code. You can also use @ref Context::supportedVersion() to conveniently select the first supported version from a list: -@code{.cpp} -// MyShader.cpp -Version version = Context::current().supportedVersion({Version::GL430, - Version::GL330, - Version::GL210}); -attachShader(Shader::fromFile(version, "MyShader.vert")); -@endcode +

+@snippet Magnum.cpp portability-shaders +

@code{.vert} // MyShader.vert @@ -161,12 +139,7 @@ be available in given GLSL version (e.g. causing compilation errors). You can use @ref Context::isExtensionSupported(Version) const to check that the extension is present in given version: -@code{.cpp} -if(!Context::current().isExtensionSupported(version)) { - bindAttributeLocation(Position::Location, "position"); - // ... -} -@endcode +@snippet Magnum.cpp portability-shaders-bind See also @ref AbstractShaderProgram class documentation for information about specifying attribute location, uniform location and texture binding unit in @@ -198,27 +171,7 @@ and desktop (using SDL2 toolkit). Thanks to static polymorphism most of the functions will work on both without changes, the main difference might (or might not, depending what you use) be in particular event handlers: -@code{.cpp} -#ifndef MAGNUM_TARGET_GLES -#include -#else -#include -#endif - -class MyApplication: public Platform::Application { - public: - MyApplication(const Arguments& arguments); - - protected: - void viewportEvent(const Vector2i& size) override; - void drawEvent() override; - void keyPressEvent(KeyEvent& event) override; -}; - -// ... - -MAGNUM_APPLICATION_MAIN(MyApplication) -@endcode +@snippet MagnumPlatform-portability.cpp application And corresponding CMake code. Note that we need to call @cmake find_package() @ce twice, first to get the @ref MAGNUM_TARGET_GLES variable and then again to find diff --git a/doc/snippets/CMakeLists.txt b/doc/snippets/CMakeLists.txt index b828c4584..03527e3f3 100644 --- a/doc/snippets/CMakeLists.txt +++ b/doc/snippets/CMakeLists.txt @@ -93,6 +93,24 @@ if(WITH_SDL2APPLICATION) endif() endif() +if((NOT TARGET_GLES AND WITH_SDL2APPLICATION) OR (TARGET_GLES AND WITH_XEGLAPPLICATION)) + add_library(snippets-MagnumPlatform-portability STATIC MagnumPlatform-portability.cpp) + if(TARGET_GLES) + target_link_libraries(snippets-MagnumPlatform-portability PRIVATE MagnumXEglApplication) + else() + target_link_libraries(snippets-MagnumPlatform-portability PRIVATE MagnumSdl2Application) + endif() + + set_target_properties( + snippets-MagnumPlatform-portability + PROPERTIES FOLDER "Magnum/doc/snippets") + # Otherwise it's not linked correctly. I have no idea why, but whatever. + if(CMAKE_VERSION VERSION_LESS 3.0) + find_package(SDL2 REQUIRED) + target_link_libraries(snippets-MagnumPlatform-portability PRIVATE SDL2::SDL2) + endif() +endif() + if(WITH_WINDOWLESSEGLAPPLICATION) add_library(snippets-MagnumPlatform-custom STATIC MagnumPlatform-custom.cpp) add_library(snippets-MagnumPlatform-windowless STATIC MagnumPlatform-windowless.cpp) diff --git a/doc/snippets/Magnum.cpp b/doc/snippets/Magnum.cpp index 33da7d324..6e332eaaa 100644 --- a/doc/snippets/Magnum.cpp +++ b/doc/snippets/Magnum.cpp @@ -207,6 +207,49 @@ texture.setStorage(4, TextureFormat::RGBA8, {256, 256}); } #endif +{ +/* [portability-targets] */ +#ifndef MAGNUM_TARGET_GLES +Renderer::setPolygonMode(Renderer::PolygonMode::Line); +// draw mesh as wireframe... +#else +// use different mesh, as polygon mode is not supported in OpenGL ES... +#endif +/* [portability-targets] */ +} + +#ifndef MAGNUM_TARGET_GLES +{ +/* [portability-extensions] */ +if(Context::current().isExtensionSupported()) { + // draw mesh with wireframe on top in one pass using geometry shader... +} else { + // draw underlying mesh... + Renderer::setPolygonMode(Renderer::PolygonMode::Line); + // draw mesh as wirefreame in second pass... +} +/* [portability-extensions] */ +} + +{ +/* [portability-extension-assert] */ +MAGNUM_ASSERT_EXTENSION_SUPPORTED(Extensions::GL::ARB::geometry_shader4); +// just use geometry shader and don't care about old hardware +/* [portability-extension-assert] */ +} + +{ +/* [portability-shaders] */ +// MyShader.cpp +Version version = Context::current().supportedVersion({Version::GL430, + Version::GL330, + Version::GL210}); +Shader vert{version, Shader::Type::Vertex}; +vert.addFile("MyShader.vert"); +/* [portability-shaders] */ +} +#endif + #ifndef MAGNUM_TARGET_GLES struct MyShader: AbstractShaderProgram { /* [AbstractShaderProgram-input-attributes] */ @@ -282,6 +325,16 @@ MyShader& setTransformFeedback(TransformFeedback& feedback, Int totalCount, Buff /* [AbstractShaderProgram-xfb] */ void foo() { +{ +Version version{}; +/* [portability-shaders-bind] */ +if(!Context::current().isExtensionSupported(version)) { + bindAttributeLocation(Position::Location, "position"); + // ... +} +/* [portability-shaders-bind] */ +} + /* [AbstractShaderProgram-binding] */ // Shaders attached... diff --git a/doc/snippets/MagnumPlatform-portability.cpp b/doc/snippets/MagnumPlatform-portability.cpp new file mode 100644 index 000000000..f284e5617 --- /dev/null +++ b/doc/snippets/MagnumPlatform-portability.cpp @@ -0,0 +1,48 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 + 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. +*/ + +/* [application] */ +#ifndef MAGNUM_TARGET_GLES +#include +#else +#include +#endif + +using namespace Magnum; + +class MyApplication: public Platform::Application { + public: + MyApplication(const Arguments& arguments); + + protected: + void viewportEvent(const Vector2i& size) override; + void drawEvent() override; + void keyPressEvent(KeyEvent& event) override; +}; + +// ... + +MAGNUM_APPLICATION_MAIN(MyApplication) +/* [application] */