From 64919ab55aea51e176c8370f4dc4671ce357ce8e Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Mon, 15 Mar 2021 19:01:42 -0700 Subject: [PATCH] Make interpolators available in python --- CMakeLists.txt | 2 + docs/reference/s3tc.rst | 76 +------------------ quicktex/s3tc/_bindings.cpp | 8 +- quicktex/s3tc/bc1/BC1Decoder.h | 8 +- quicktex/s3tc/bc1/BC1Encoder.cpp | 6 +- quicktex/s3tc/bc1/BC1Encoder.h | 4 +- quicktex/s3tc/bc1/SingleColorTable.h | 2 +- quicktex/s3tc/bc1/_bindings.cpp | 8 +- quicktex/s3tc/bc3/BC3Decoder.h | 5 +- quicktex/s3tc/bc3/BC3Encoder.h | 7 +- quicktex/s3tc/bc3/_bindings.cpp | 8 +- .../s3tc/{ => interpolator}/Interpolator.cpp | 2 +- .../s3tc/{ => interpolator}/Interpolator.h | 2 +- quicktex/s3tc/interpolator/__init__.py | 1 + quicktex/s3tc/interpolator/_bindings.cpp | 50 ++++++++++++ 15 files changed, 88 insertions(+), 101 deletions(-) rename quicktex/s3tc/{ => interpolator}/Interpolator.cpp (99%) rename quicktex/s3tc/{ => interpolator}/Interpolator.h (99%) create mode 100644 quicktex/s3tc/interpolator/__init__.py create mode 100644 quicktex/s3tc/interpolator/_bindings.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fcd4ba1..e42dd7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ file(GLOB SOURCE_FILES "quicktex/s3tc/bc3/*.cpp" "quicktex/s3tc/bc4/*.cpp" "quicktex/s3tc/bc5/*.cpp" + "quicktex/s3tc/interpolator/*.cpp" ) file(GLOB HEADER_FILES @@ -26,6 +27,7 @@ file(GLOB HEADER_FILES "quicktex/s3tc/bc3/*.h" "quicktex/s3tc/bc4/*.h" "quicktex/s3tc/bc5/*.h" + "quicktex/s3tc/interpolator/*.h" ) file(GLOB TEST_FILES "tests/*.cpp") diff --git a/docs/reference/s3tc.rst b/docs/reference/s3tc.rst index 9cb5898..6d9a8d3 100644 --- a/docs/reference/s3tc.rst +++ b/docs/reference/s3tc.rst @@ -1,81 +1,11 @@ -.. py:currentmodule:: quicktex.s3tc +.. py:module:: quicktex.s3tc s3tc module =========== -.. pybind handles enums in a really weird way that doesnt play nice with Sphinx, - so the docs are rewritten here. - -.. py:class:: InterpolatorType - - An enum representing various methods for interpolating colors, used by the BC1 and BC3 encoders/decoders. - Vendor-specific interpolation modes should only be used when the result will only be used on that type of GPU. - For most applications, :py:attr:`~quicktex.s3tc.InterpolatorType.Ideal` should be used. - - .. py:data:: Ideal - - The default mode, with no rounding for colors 2 and 3. This matches the D3D10 docs on BC1. - - .. py:data:: IdealRound - - Round colors 2 and 3. Matches the AMD Compressonator tool and the D3D9 docs on DXT1. - - .. py:data:: Nvidia - - Nvidia GPU mode. - - .. py:data:: AMD - - AMD GPU mode. - bc1 module ---------- .. automodule:: quicktex.s3tc.bc1 - - .. autoclass:: BC1Encoder - :members: - :undoc-members: - - .. autoclass:: BC1Decoder - :members: - :undoc-members: - -bc3 module ----------- - -.. automodule:: quicktex.s3tc.bc3 - - .. autoclass:: BC3Encoder - :members: - :undoc-members: - - .. autoclass:: BC3Decoder - :members: - :undoc-members: - -bc4 module ----------- - -.. automodule:: quicktex.s3tc.bc4 - - .. autoclass:: BC4Encoder - :members: - :undoc-members: - - .. autoclass:: BC4Decoder - :members: - :undoc-members: - -bc5 module ----------- - -.. automodule:: quicktex.s3tc.bc5 - - .. autoclass:: BC5Encoder - :members: - :undoc-members: - - .. autoclass:: BC5Decoder - :members: - :undoc-members: \ No newline at end of file + :members: + :undoc-members: diff --git a/quicktex/s3tc/_bindings.cpp b/quicktex/s3tc/_bindings.cpp index 55a8ffa..4e3c31a 100644 --- a/quicktex/s3tc/_bindings.cpp +++ b/quicktex/s3tc/_bindings.cpp @@ -19,7 +19,7 @@ #include -#include "Interpolator.h" +#include "interpolator/Interpolator.h" namespace py = pybind11; namespace quicktex::bindings { @@ -27,6 +27,7 @@ namespace quicktex::bindings { using namespace quicktex; using namespace quicktex::s3tc; +void InitInterpolator(py::module_ &s3tc); void InitBC1(py::module_ &s3tc); void InitBC3(py::module_ &s3tc); void InitBC4(py::module_ &s3tc); @@ -35,7 +36,7 @@ void InitBC5(py::module_ &s3tc); void InitS3TC(py::module_ &m) { py::module_ s3tc = m.def_submodule("_s3tc", "s3tc compression library based on rgbcx.h written by Richard Goldreich"); - using IType = Interpolator::Type; +/* using IType = Interpolator::Type; auto interpolator_type = py::enum_(s3tc, "InterpolatorType", R"pbdoc( An enum representing various methods for interpolating colors, used by the BC1 and BC3 encoders/decoders. Vendor-specific interpolation modes should only be used when the result will only be used on that type of GPU. @@ -45,8 +46,9 @@ For most applications, :py:attr:`~quicktex.s3tc.InterpolatorType.Ideal` should b interpolator_type.value("Ideal", IType::Ideal, "The default mode, with no rounding for colors 2 and 3. This matches the D3D10 docs on BC1."); interpolator_type.value("IdealRound", IType::IdealRound, "Round colors 2 and 3. Matches the AMD Compressonator tool and the D3D9 docs on DXT1."); interpolator_type.value("Nvidia", IType::Nvidia, "Nvidia GPU mode."); - interpolator_type.value("AMD", IType::AMD, "AMD GPU mode."); + interpolator_type.value("AMD", IType::AMD, "AMD GPU mode.");*/ + InitInterpolator(s3tc); InitBC1(s3tc); InitBC3(s3tc); InitBC4(s3tc); diff --git a/quicktex/s3tc/bc1/BC1Decoder.h b/quicktex/s3tc/bc1/BC1Decoder.h index a1701f3..f5951f1 100644 --- a/quicktex/s3tc/bc1/BC1Decoder.h +++ b/quicktex/s3tc/bc1/BC1Decoder.h @@ -25,15 +25,15 @@ #include "../../BlockDecoder.h" #include "../../BlockView.h" #include "../../ndebug.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" #include "BC1Block.h" -namespace quicktex::s3tc { +namespace quicktex::s3tc { class BC1Decoder final : public BlockDecoderTemplate { public: using InterpolatorPtr = std::shared_ptr; - BC1Decoder(Interpolator::Type type = Interpolator::Type::Ideal, bool write_alpha = false) - : write_alpha(write_alpha), _interpolator(Interpolator::MakeInterpolator(type)) {} + BC1Decoder(InterpolatorPtr interpolator = std::make_shared(), bool write_alpha = false) + : write_alpha(write_alpha), _interpolator(interpolator) {} void DecodeBlock(Color4x4 dest, BC1Block *const block) const noexcept(ndebug) override; diff --git a/quicktex/s3tc/bc1/BC1Encoder.cpp b/quicktex/s3tc/bc1/BC1Encoder.cpp index f1de5c1..de62257 100644 --- a/quicktex/s3tc/bc1/BC1Encoder.cpp +++ b/quicktex/s3tc/bc1/BC1Encoder.cpp @@ -34,7 +34,7 @@ #include "../../Vector4Int.h" #include "../../bitwiseEnums.h" #include "../../util.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" #include "Histogram.h" #include "OrderTable.h" #include "SingleColorTable.h" @@ -43,8 +43,8 @@ namespace quicktex::s3tc { // constructors -BC1Encoder::BC1Encoder(Interpolator::Type type, unsigned int level, bool allow_3color, bool allow_3color_black) - : _interpolator(Interpolator::MakeInterpolator(type)) { +BC1Encoder::BC1Encoder(InterpolatorPtr interpolator, unsigned int level, bool allow_3color, bool allow_3color_black) + : _interpolator(interpolator) { OrderTable<3>::Generate(); OrderTable<4>::Generate(); diff --git a/quicktex/s3tc/bc1/BC1Encoder.h b/quicktex/s3tc/bc1/BC1Encoder.h index 3ccb9d9..e34142f 100644 --- a/quicktex/s3tc/bc1/BC1Encoder.h +++ b/quicktex/s3tc/bc1/BC1Encoder.h @@ -29,7 +29,7 @@ #include "../../BlockEncoder.h" #include "../../BlockView.h" #include "../../Color.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" #include "BC1Block.h" #include "SingleColorTable.h" @@ -111,7 +111,7 @@ class BC1Encoder final : public BlockEncoderTemplate { PCA }; - BC1Encoder(Interpolator::Type type = Interpolator::Type::Ideal, unsigned level = 5, bool allow_3color = true, bool allow_3color_black = true); + BC1Encoder(InterpolatorPtr interpolator = std::make_shared(), unsigned level = 5, bool allow_3color = true, bool allow_3color_black = true); Interpolator::Type GetInterpolatorType() const { return _interpolator->GetType(); } diff --git a/quicktex/s3tc/bc1/SingleColorTable.h b/quicktex/s3tc/bc1/SingleColorTable.h index 6da31a1..f9424de 100644 --- a/quicktex/s3tc/bc1/SingleColorTable.h +++ b/quicktex/s3tc/bc1/SingleColorTable.h @@ -24,7 +24,7 @@ #include #include "../../util.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" namespace quicktex::s3tc { diff --git a/quicktex/s3tc/bc1/_bindings.cpp b/quicktex/s3tc/bc1/_bindings.cpp index 01278e3..02ccfab 100644 --- a/quicktex/s3tc/bc1/_bindings.cpp +++ b/quicktex/s3tc/bc1/_bindings.cpp @@ -27,7 +27,7 @@ #include "../../BlockDecoder.h" #include "../../BlockEncoder.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" #include "BC1Decoder.h" #include "BC1Encoder.h" @@ -35,7 +35,7 @@ namespace py = pybind11; namespace quicktex::bindings { using namespace quicktex::s3tc; -using namespace quicktex::s3tc ; +using InterpolatorPtr = std::shared_ptr; void InitBC1(py::module_ &s3tc) { auto bc1 = s3tc.def_submodule("_bc1", "BC1 encoding/decoding module"); @@ -45,7 +45,7 @@ void InitBC1(py::module_ &s3tc) { // BC1Encoder py::class_ bc1_encoder(bc1, "BC1Encoder", block_encoder); - bc1_encoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal, py::arg("level") = 5, + bc1_encoder.def(py::init(), py::arg("interpolator") = std::make_shared(), py::arg("level") = 5, py::arg("use_3color") = true, py::arg("use_3color_black") = true); bc1_encoder.def("set_level", &BC1Encoder::SetLevel); bc1_encoder.def_property_readonly("interpolator_type", &BC1Encoder::GetInterpolatorType); @@ -89,7 +89,7 @@ void InitBC1(py::module_ &s3tc) { // BC1Decoder py::class_ bc1_decoder(bc1, "BC1Decoder", block_decoder); - bc1_decoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal, py::arg("write_alpha") = false); + bc1_decoder.def(py::init(), py::arg("interpolator") = std::make_shared(), py::arg("write_alpha") = false); bc1_decoder.def_property_readonly("interpolator_type", &BC1Decoder::GetInterpolatorType); bc1_decoder.def_readwrite("write_alpha", &BC1Decoder::write_alpha); } diff --git a/quicktex/s3tc/bc3/BC3Decoder.h b/quicktex/s3tc/bc3/BC3Decoder.h index 2e29dad..4cfa918 100644 --- a/quicktex/s3tc/bc3/BC3Decoder.h +++ b/quicktex/s3tc/bc3/BC3Decoder.h @@ -24,9 +24,9 @@ #include "../../BlockDecoder.h" #include "../../BlockView.h" #include "../../ndebug.h" -#include "../Interpolator.h" #include "../bc1/BC1Decoder.h" #include "../bc4/BC4Decoder.h" +#include "../interpolator/Interpolator.h" #include "BC3Block.h" namespace quicktex::s3tc { @@ -35,8 +35,9 @@ class BC3Decoder : public BlockDecoderTemplate { public: using BC1DecoderPtr = std::shared_ptr; using BC4DecoderPtr = std::shared_ptr; + using InterpolatorPtr = std::shared_ptr; - BC3Decoder(Interpolator::Type type = Interpolator::Type::Ideal) : BC3Decoder(std::make_shared(type), std::make_shared(3)) {} + BC3Decoder(InterpolatorPtr interpolator = std::make_shared()) : BC3Decoder(std::make_shared(interpolator), std::make_shared(3)) {} BC3Decoder(BC1DecoderPtr bc1_decoder, BC4DecoderPtr bc4_decoder = std::make_shared()) : _bc1_decoder(bc1_decoder), _bc4_decoder(bc4_decoder) {} void DecodeBlock(Color4x4 dest, BC3Block *const block) const noexcept(ndebug) override; diff --git a/quicktex/s3tc/bc3/BC3Encoder.h b/quicktex/s3tc/bc3/BC3Encoder.h index dd14616..e2c2b8f 100644 --- a/quicktex/s3tc/bc3/BC3Encoder.h +++ b/quicktex/s3tc/bc3/BC3Encoder.h @@ -23,9 +23,9 @@ #include "../../BlockEncoder.h" #include "../../BlockView.h" -#include "../Interpolator.h" #include "../bc1/BC1Encoder.h" #include "../bc4/BC4Encoder.h" +#include "../interpolator/Interpolator.h" #include "BC3Block.h" namespace quicktex::s3tc { @@ -34,9 +34,10 @@ class BC3Encoder : public BlockEncoderTemplate { public: using BC1EncoderPtr = std::shared_ptr; using BC4EncoderPtr = std::shared_ptr; + using InterpolatorPtr = std::shared_ptr; - BC3Encoder(Interpolator::Type type = Interpolator::Type::Ideal, unsigned level = 5, bool allow_3color = true, bool allow_3color_black = true) - : _bc1_encoder(std::make_shared(type, level, allow_3color, allow_3color_black)), _bc4_encoder(std::make_shared(3)) {} + BC3Encoder(InterpolatorPtr interpolator = std::make_shared(), unsigned level = 5, bool allow_3color = true, bool allow_3color_black = true) + : _bc1_encoder(std::make_shared(interpolator, level, allow_3color, allow_3color_black)), _bc4_encoder(std::make_shared(3)) {} void EncodeBlock(Color4x4 pixels, BC3Block *dest) const override; diff --git a/quicktex/s3tc/bc3/_bindings.cpp b/quicktex/s3tc/bc3/_bindings.cpp index cee2b40..089c53a 100644 --- a/quicktex/s3tc/bc3/_bindings.cpp +++ b/quicktex/s3tc/bc3/_bindings.cpp @@ -27,7 +27,7 @@ #include "../../BlockDecoder.h" #include "../../BlockEncoder.h" -#include "../Interpolator.h" +#include "../interpolator/Interpolator.h" #include "BC3Decoder.h" #include "BC3Encoder.h" @@ -35,7 +35,7 @@ namespace py = pybind11; namespace quicktex::bindings { using namespace quicktex::s3tc; -using namespace quicktex::s3tc ; +using InterpolatorPtr = std::shared_ptr; void InitBC3(py::module_ &s3tc) { auto bc3 = s3tc.def_submodule("_bc3", "BC3 encoding/decoding module"); @@ -45,7 +45,7 @@ void InitBC3(py::module_ &s3tc) { // BC3Encoder py::class_ bc3_encoder(bc3, "BC3Encoder", block_encoder); - bc3_encoder.def(py::init(), py::arg("interpolator") = Interpolator::Type::Ideal, py::arg("level") = 5, + bc3_encoder.def(py::init(), py::arg("interpolator") = std::make_shared(), py::arg("level") = 5, py::arg("use_3color") = true, py::arg("use_3color_black") = true); bc3_encoder.def_property_readonly("bc1_encoder", &BC3Encoder::GetBC1Encoder); bc3_encoder.def_property_readonly("bc4_encoder", &BC3Encoder::GetBC4Encoder); @@ -53,7 +53,7 @@ void InitBC3(py::module_ &s3tc) { // BC3Decoder py::class_ bc3_decoder(bc3, "BC3Decoder", block_decoder); - bc3_decoder.def(py::init(), py::arg("type") = Interpolator::Type::Ideal); + bc3_decoder.def(py::init(), py::arg("interpolator") = std::make_shared()); bc3_decoder.def_property_readonly("bc1_decoder", &BC3Decoder::GetBC1Decoder); bc3_decoder.def_property_readonly("bc4_decoder", &BC3Decoder::GetBC4Decoder); }; diff --git a/quicktex/s3tc/Interpolator.cpp b/quicktex/s3tc/interpolator/Interpolator.cpp similarity index 99% rename from quicktex/s3tc/Interpolator.cpp rename to quicktex/s3tc/interpolator/Interpolator.cpp index 0923186..200f71e 100644 --- a/quicktex/s3tc/Interpolator.cpp +++ b/quicktex/s3tc/interpolator/Interpolator.cpp @@ -24,7 +24,7 @@ #include #include -#include "../util.h" +#include "../../util.h" namespace quicktex::s3tc { diff --git a/quicktex/s3tc/Interpolator.h b/quicktex/s3tc/interpolator/Interpolator.h similarity index 99% rename from quicktex/s3tc/Interpolator.h rename to quicktex/s3tc/interpolator/Interpolator.h index 9309a60..59ffe42 100644 --- a/quicktex/s3tc/Interpolator.h +++ b/quicktex/s3tc/interpolator/Interpolator.h @@ -22,7 +22,7 @@ #include // for uint8_t, uint16_t #include // for unique_ptr -#include "../Color.h" // for Color +#include "../../Color.h" // for Color namespace quicktex::s3tc { diff --git a/quicktex/s3tc/interpolator/__init__.py b/quicktex/s3tc/interpolator/__init__.py new file mode 100644 index 0000000..357d87a --- /dev/null +++ b/quicktex/s3tc/interpolator/__init__.py @@ -0,0 +1 @@ +# from _quicktex._s3tc._interpolator import * \ No newline at end of file diff --git a/quicktex/s3tc/interpolator/_bindings.cpp b/quicktex/s3tc/interpolator/_bindings.cpp new file mode 100644 index 0000000..7918431 --- /dev/null +++ b/quicktex/s3tc/interpolator/_bindings.cpp @@ -0,0 +1,50 @@ +/* Python-rgbcx Texture Compression Library + Copyright (C) 2021 Andrew Cassidy + Partially derived from rgbcx.h written by Richard Geldreich + and licenced under the public domain + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . + */ + +#include + +#include + +#include "Interpolator.h" + +namespace py = pybind11; +namespace quicktex::bindings { + +using namespace quicktex::s3tc; +using InterpolatorPtr = std::shared_ptr; + +void InitInterpolator(py::module_ &s3tc) { + auto interpolator = s3tc.def_submodule("_interpolator", "Classes defining various methods of interpolating colors in BC1 and BC3 textures"); + + // Interpolator + py::class_ ideal( + interpolator, "Interpolator", + "Interpolator base class representing the default mode, with no rounding for colors 2 and 3. This matches the D3D10 docs on BC1."); + + // InterpolatorRound + py::class_ round(interpolator, "InterpolatorRound", ideal, + "Round colors 2 and 3. Matches the AMD Compressonator tool and the D3D9 docs on DXT1."); + + // InterpolatorNvidia + py::class_ nvidia(interpolator, "InterpolatorNvidia", ideal, "Nvidia GPU mode."); + + // InterpolatorAMD + py::class_ amd(interpolator, "InterpolatorAMD", ideal, "AMD GPU mode."); +} +} // namespace quicktex::bindings \ No newline at end of file