From 717433e2aab78c9804a634e824c4ca17574cdb5f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 27 Feb 2023 12:23:38 +0100 Subject: test: add bitmask benchmark --- src/anim-atlas.cpp | 14 ++++++++++---- src/anim-atlas.hpp | 1 + test/bitmask.cpp | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/anim-atlas.cpp b/src/anim-atlas.cpp index ffade2d6..c6193b5b 100644 --- a/src/anim-atlas.cpp +++ b/src/anim-atlas.cpp @@ -127,15 +127,14 @@ auto anim_atlas::frame_quad(const Vector3& center, rotation r, std::size_t i) co }}; } -BitArray anim_atlas::make_bitmask(const ImageView2D& tex) +void anim_atlas::make_bitmask_(const ImageView2D& tex, BitArray& array) { const auto pixels = tex.pixels(); const auto size = pixels.size(); - const auto width = size[1], height = size[0], dest_len = width*height, + const auto width = size[1], height = size[0], stride = (std::size_t)pixels.stride()[0], width0 = width & ~7u; const auto* const data = (const unsigned char*)pixels.data(); - auto* const dest = new unsigned char[(dest_len+7)>>3]; - auto array = BitArray{dest, 0, dest_len, {}}; + auto* const dest = (unsigned char*)array.data(); fm_assert(tex.pixelSize() == 4); fm_assert(pixels.stride()[1] == 4); @@ -165,6 +164,13 @@ BitArray anim_atlas::make_bitmask(const ImageView2D& tex) array.set((height-j-1)*width + i, alpha >= amin); } } +} + +BitArray anim_atlas::make_bitmask(const ImageView2D& tex) +{ + const auto size = tex.pixels().size(); + auto array = BitArray{NoInit, size[0]*size[1]}; + make_bitmask_(tex, array); return array; } diff --git a/src/anim-atlas.hpp b/src/anim-atlas.hpp index eaddef21..e5446c82 100644 --- a/src/anim-atlas.hpp +++ b/src/anim-atlas.hpp @@ -48,6 +48,7 @@ struct anim_atlas final fm_DECLARE_DELETED_COPY_ASSIGNMENT(anim_atlas); + static void make_bitmask_(const ImageView2D& tex, BitArray& array); static BitArray make_bitmask(const ImageView2D& tex); private: diff --git a/test/bitmask.cpp b/test/bitmask.cpp index f7d12dc2..4deeca6e 100644 --- a/test/bitmask.cpp +++ b/test/bitmask.cpp @@ -1,13 +1,15 @@ #include "app.hpp" #include "compat/assert.hpp" #include "src/anim-atlas.hpp" +#include "loader/loader.hpp" +#include +#include #include #include +#include #include -#include -#include #include -#include +#include namespace floormat { @@ -38,9 +40,26 @@ constexpr bool result[] = { }; -} // namespace +[[maybe_unused]] void bitmask_benchmark() +{ + std::chrono::high_resolution_clock clock; + auto img = loader.texture(loader.SCENERY_PATH, "door-close"_s); + auto bitmask = anim_atlas::make_bitmask(img); + constexpr int runs = 10, warmup = 100, cycles = 1000; + for (int i = 0; i < runs; i++) + { + for (int i = 0; i < warmup; i++) + anim_atlas::make_bitmask_(img, bitmask); + auto time0 = clock.now(); + for (int i = 0; i < cycles; i++) + (void)anim_atlas::make_bitmask_(img, bitmask); + std::chrono::duration time = clock.now() - time0; -void test_app::test_bitmask() + fm_log("[BENCH] bitmask %d/%d took %.1f ms", i, runs, time.count()); + } +} + +void bitmask_test() { constexpr auto size = std::size(img_data), width = 4_uz, height = size/4; static_assert(size % 4 == 0); @@ -53,4 +72,12 @@ void test_app::test_bitmask() fm_abort("wrong value at bit %zu, should be %s", i, result[i] ? "true" : "false"); } +} // namespace + +void test_app::test_bitmask() +{ + bitmask_test(); + //bitmask_benchmark(); +} + } // namespace floormat -- cgit v1.2.3