From 4de30ab058150342ef82efdd760d1499f6183e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 6 May 2019 23:57:33 +0200 Subject: [PATCH] python: initial bits of the Shaders library. --- package/ci/travis-desktop.sh | 2 +- src/python/magnum/CMakeLists.txt | 15 +++- src/python/magnum/shaders.cpp | 87 +++++++++++++++++++++++ src/python/magnum/test/test_shaders_gl.py | 42 +++++++++++ src/python/setup.py.cmake | 1 + 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/python/magnum/shaders.cpp create mode 100644 src/python/magnum/test/test_shaders_gl.py diff --git a/package/ci/travis-desktop.sh b/package/ci/travis-desktop.sh index b71064d..ffe26ed 100755 --- a/package/ci/travis-desktop.sh +++ b/package/ci/travis-desktop.sh @@ -31,7 +31,7 @@ cmake .. \ -DWITH_MESHTOOLS=OFF \ -DWITH_PRIMITIVES=OFF \ -DWITH_SCENEGRAPH=OFF \ - -DWITH_SHADERS=OFF \ + -DWITH_SHADERS=ON \ -DWITH_TEXT=OFF \ -DWITH_TEXTURETOOLS=OFF \ -DWITH_TRADE=OFF \ diff --git a/src/python/magnum/CMakeLists.txt b/src/python/magnum/CMakeLists.txt index 9d51b62..bbf2a9b 100644 --- a/src/python/magnum/CMakeLists.txt +++ b/src/python/magnum/CMakeLists.txt @@ -24,7 +24,7 @@ # # *Not* REQUIRED -find_package(Magnum COMPONENTS GL) +find_package(Magnum COMPONENTS GL Shaders) set(magnum_SRCS magnum.cpp @@ -55,6 +55,19 @@ if(Magnum_GL_FOUND) LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) endif() +if(Magnum_Shaders_FOUND) + set(magnum_shaders_SRCS + shaders.cpp) + + pybind11_add_module(magnum_shaders ${magnum_shaders_SRCS}) + target_include_directories(magnum_shaders PRIVATE ${PROJECT_SOURCE_DIR}/src/python) + target_link_libraries(magnum_shaders PRIVATE Magnum::Shaders) + set_target_properties(magnum_shaders PROPERTIES + FOLDER "python" + OUTPUT_NAME "shaders" + LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) +endif() + file(GENERATE OUTPUT ${output_dir}/magnum/__init__.py INPUT ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py) diff --git a/src/python/magnum/shaders.cpp b/src/python/magnum/shaders.cpp new file mode 100644 index 0000000..9bc8a60 --- /dev/null +++ b/src/python/magnum/shaders.cpp @@ -0,0 +1,87 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + 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 +#include + +#include "magnum/bootstrap.h" +#include "magnum/NonDestructible.h" + +namespace magnum { namespace { + +template void vertexColor(NonDestructibleBase, GL::AbstractShaderProgram>& c) { + /* Attributes */ + c.attr("COLOR3") = GL::DynamicAttribute{ + GL::DynamicAttribute::Kind::Generic, 3, + GL::DynamicAttribute::Components::Three, + GL::DynamicAttribute::DataType::Float}; + c.attr("COLOR4") = GL::DynamicAttribute{ + GL::DynamicAttribute::Kind::Generic, 3, + GL::DynamicAttribute::Components::Four, + GL::DynamicAttribute::DataType::Float}; + + /* Methods */ + c + .def(py::init(), "Constructor") + + /* Using lambdas to avoid method chaining getting into signatures */ + + // TODO: make writeonly once https://github.com/pybind/pybind11/pull/1144 + // is released + .def_property("transformation_projection_matrix", [](Shaders::VertexColor&) { + return MatrixTypeFor{}; + }, &Shaders::VertexColor::setTransformationProjectionMatrix, + "Transformation and projection matrix"); +} + +void shaders(py::module& m) { + m.import("magnum.gl"); + + /* 2D/3D vertex color shader */ + { + NonDestructibleBase vertexColor2D{m, + "VertexColor2D", "2D vertex color shader"}; + NonDestructibleBase vertexColor3D{m, + "VertexColor3D", "3D vertex color shader"}; + vertexColor2D.attr("POSITION") = GL::DynamicAttribute{ + GL::DynamicAttribute::Kind::Generic, 0, + GL::DynamicAttribute::Components::Two, + GL::DynamicAttribute::DataType::Float}; + vertexColor3D.attr("POSITION") = GL::DynamicAttribute{ + GL::DynamicAttribute::Kind::Generic, 0, + GL::DynamicAttribute::Components::Three, + GL::DynamicAttribute::DataType::Float}; + vertexColor(vertexColor2D); + vertexColor(vertexColor3D); + } +} + +}} + +PYBIND11_MODULE(shaders, m) { + m.doc() = "Builtin shaders"; + + magnum::shaders(m); +} diff --git a/src/python/magnum/test/test_shaders_gl.py b/src/python/magnum/test/test_shaders_gl.py new file mode 100644 index 0000000..acdf0ab --- /dev/null +++ b/src/python/magnum/test/test_shaders_gl.py @@ -0,0 +1,42 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 +# 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. +# + +import unittest + +# setUpModule gets called before everything else, skipping if GL tests can't +# be run +from . import GLTestCase, setUpModule + +from magnum import * +from magnum import shaders + +class VertexColor(GLTestCase): + def test_init(self): + a = shaders.VertexColor2D() + b = shaders.VertexColor3D() + + def test_uniforms(self): + a = shaders.VertexColor2D() + a.transformation_projection_matrix = Matrix3.translation(Vector2.x_axis()) diff --git a/src/python/setup.py.cmake b/src/python/setup.py.cmake index 0cdfb13..ca90573 100644 --- a/src/python/setup.py.cmake +++ b/src/python/setup.py.cmake @@ -34,6 +34,7 @@ extension_paths = { 'corrade.containers': '$', 'magnum._magnum': '$', 'magnum.gl': '$<$:$>', + 'magnum.shaders': '$<$:$>', 'magnum.platform.egl': '$<$:$>', 'magnum.platform.glx': '$<$:$>', }