diff --git a/MobileGL/MG_Test/Backend/DirectGLES/CMakeLists.txt b/MobileGL/MG_Test/Backend/DirectGLES/CMakeLists.txt index 1990d78e..c74fa74f 100644 --- a/MobileGL/MG_Test/Backend/DirectGLES/CMakeLists.txt +++ b/MobileGL/MG_Test/Backend/DirectGLES/CMakeLists.txt @@ -67,6 +67,12 @@ target_include_directories(ContextEpochTest PRIVATE ${MGL_ROOT}/include ${MGL_RO target_link_libraries(ContextEpochTest PRIVATE MobileGLTestMain ${LINK_LIBRARIES}) gtest_discover_tests(ContextEpochTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) +# The record of texture storage sampled in the current offscreen draw batch. +add_executable(DrawTextureReadsTest DrawTextureReadsTest.cpp) +target_include_directories(DrawTextureReadsTest PRIVATE ${MGL_ROOT}/include ${MGL_ROOT}/MobileGL) +target_link_libraries(DrawTextureReadsTest PRIVATE MobileGLTestMain ${LINK_LIBRARIES}) +gtest_discover_tests(DrawTextureReadsTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) + # P3b/P4b R-5: D-K2's dependency rule had six statements and nothing compared them. This binary # is where the comparison can live, because it is the one that links BOTH the client check # (MG_Impl/Pipe, through MG_Pipe) and the server gate (MG_Backend/DirectGLES's resolvers). diff --git a/MobileGL/MG_Test/Backend/DirectGLES/DrawTextureReadsTest.cpp b/MobileGL/MG_Test/Backend/DirectGLES/DrawTextureReadsTest.cpp new file mode 100644 index 00000000..e6c60482 --- /dev/null +++ b/MobileGL/MG_Test/Backend/DirectGLES/DrawTextureReadsTest.cpp @@ -0,0 +1,61 @@ +// MobileGL - MobileGL/MG_Test/Backend/DirectGLES/DrawTextureReadsTest.cpp +// Copyright (c) 2025-2026 MobileGL-Dev +// Licensed under the GNU Lesser General Public License v3.0: +// https://www.gnu.org/licenses/gpl-3.0.txt +// https://www.gnu.org/licenses/lgpl-3.0.txt +// SPDX-License-Identifier: LGPL-3.0-only +// End of Source File Header + +// The per-context record of texture storage sampled in the current offscreen draw batch. +#include +#include + +namespace { + using MobileGL::MG_Backend::DirectGLES::TextureImpl::DrawTextureReads; + + TEST(DrawTextureReads, RecordsOnlyOffscreenBatches) { + DrawTextureReads reads; + EXPECT_FALSE(reads.NeedsSamplerWalk(1, 1)) << "the window surface is not tracked"; + reads.NoteRead(3, 7); + EXPECT_FALSE(reads.WasRead(3, 7)); + + reads.BindFramebuffer(5); + EXPECT_TRUE(reads.NeedsSamplerWalk(1, 1)); + reads.NoteRead(3, 7); + EXPECT_TRUE(reads.WasRead(3, 7)); + EXPECT_FALSE(reads.WasRead(3, 8)) << "a recycled slot has a new generation"; + EXPECT_FALSE(reads.WasRead(4, 7)); + } + + TEST(DrawTextureReads, FramebufferChangesAndSwapsEndTheBatch) { + DrawTextureReads reads; + reads.BindFramebuffer(5); + reads.NoteRead(2, 1); + reads.BindFramebuffer(5); + EXPECT_TRUE(reads.WasRead(2, 1)) << "rebinding the same framebuffer keeps the batch"; + + reads.BindFramebuffer(6); + EXPECT_FALSE(reads.WasRead(2, 1)); + reads.NoteRead(2, 1); + reads.EndBatch(); + EXPECT_FALSE(reads.WasRead(2, 1)); + + reads.NoteRead(9, 4); + reads.DeleteFramebuffer(7); + EXPECT_TRUE(reads.WasRead(9, 4)) << "deleting another framebuffer changes nothing"; + reads.DeleteFramebuffer(6); + EXPECT_FALSE(reads.WasRead(9, 4)); + EXPECT_FALSE(reads.NeedsSamplerWalk(1, 1)); + } + + TEST(DrawTextureReads, SamplerWalkRepeatsOnlyWhenTheViewsChange) { + DrawTextureReads reads; + reads.BindFramebuffer(5); + reads.RecordSamplerWalk(1, 10); + EXPECT_FALSE(reads.NeedsSamplerWalk(1, 10)); + EXPECT_TRUE(reads.NeedsSamplerWalk(1, 11)); + EXPECT_TRUE(reads.NeedsSamplerWalk(2, 10)); + reads.EndBatch(); + EXPECT_TRUE(reads.NeedsSamplerWalk(1, 10)); + } +} // namespace diff --git a/MobileGL/MG_Test/Util/CMakeLists.txt b/MobileGL/MG_Test/Util/CMakeLists.txt index 0fd253cc..0b6fb635 100644 --- a/MobileGL/MG_Test/Util/CMakeLists.txt +++ b/MobileGL/MG_Test/Util/CMakeLists.txt @@ -142,3 +142,9 @@ gtest_discover_tests(LogLevelTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) gtest_discover_tests(PipeStatsTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) gtest_discover_tests(DamageTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) gtest_discover_tests(ConfigFileTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) + +# Sparse texture uploads: the packed staging page and the two-box split. +add_executable(TextureUploadPackingTest TextureUploadPackingTest.cpp) +target_include_directories(TextureUploadPackingTest PRIVATE ${MGL_ROOT}/include ${MGL_ROOT}/MobileGL) +target_link_libraries(TextureUploadPackingTest PRIVATE MobileGLTestMain ${LINK_LIBRARIES}) +gtest_discover_tests(TextureUploadPackingTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit) diff --git a/MobileGL/MG_Test/Util/TextureUploadPackingTest.cpp b/MobileGL/MG_Test/Util/TextureUploadPackingTest.cpp new file mode 100644 index 00000000..2674b855 --- /dev/null +++ b/MobileGL/MG_Test/Util/TextureUploadPackingTest.cpp @@ -0,0 +1,201 @@ +// MobileGL - MobileGL/MG_Test/Util/TextureUploadPackingTest.cpp +// Copyright (c) 2025-2026 MobileGL-Dev +// Licensed under the GNU Lesser General Public License v3.0: +// https://www.gnu.org/licenses/gpl-3.0.txt +// https://www.gnu.org/licenses/lgpl-3.0.txt +// SPDX-License-Identifier: LGPL-3.0-only +// End of Source File Header + +// The sparse texture upload helpers: the packed staging page and the two-box split. +#include +#include +#include + +#include +#include +#include +#include + +namespace { + namespace Packed = MobileGL::MG_Util::PackedTextureUpload; + namespace Boxes = MobileGL::MG_Util::TextureUploadBoxes; + + // A level shadow whose every texel names its own coordinates, so a copied row shows where + // it came from. + struct Level { + int width, height; + std::vector texels; + + Level(int w, int h) : width(w), height(h), texels(static_cast(w) * h * Packed::BytesPerTexel) { + for (int y = 0; y < h; ++y) { + for (int x = 0; x < w; ++x) { + std::uint8_t* texel = Texel(x, y); + texel[0] = static_cast(x); + texel[1] = static_cast(y); + texel[2] = static_cast(x >> 8); + texel[3] = static_cast(y >> 8); + } + } + } + + std::uint8_t* Texel(int x, int y) { + return texels.data() + (static_cast(y) * width + x) * Packed::BytesPerTexel; + } + + Packed::Region Region(int level, int x, int y, int w, int h) { + return {Texel(x, y), static_cast(width) * Packed::BytesPerTexel, level, x, y, w, h}; + } + }; + + void AddRegion(Packed::Batch& batch, const Packed::Region& region) { + batch.regions[batch.count++] = region; + } + + bool Overlap(const Packed::Point& a, const Packed::Region& ra, const Packed::Point& b, const Packed::Region& rb) { + return a.x < b.x + rb.width && b.x < a.x + ra.width && a.y < b.y + rb.height && b.y < a.y + ra.height; + } + + void ExpectValidLayout(const Packed::Batch& batch) { + ASSERT_GT(batch.width, 0); + ASSERT_GT(batch.height, 0); + EXPECT_LE(batch.width, Packed::PageSide); + EXPECT_LE(batch.height, Packed::PageSide); + for (std::size_t i = 0; i < batch.count; ++i) { + const auto& region = batch.regions[i]; + const auto& offset = batch.offsets[i]; + EXPECT_GE(offset.x, 0); + EXPECT_GE(offset.y, 0); + EXPECT_LE(offset.x + region.width, batch.width) << "region " << i; + EXPECT_LE(offset.y + region.height, batch.height) << "region " << i; + for (std::size_t j = i + 1; j < batch.count; ++j) { + EXPECT_FALSE(Overlap(offset, region, batch.offsets[j], batch.regions[j])) + << "regions " << i << " and " << j; + } + } + } + + TEST(PackedTextureUpload, PacksScatteredSpritesWithoutOverlap) { + Level mip0(512, 512); + Level mip1(256, 256); + Packed::Batch batch; + AddRegion(batch, mip0.Region(0, 0, 0, 16, 16)); + AddRegion(batch, mip0.Region(0, 480, 32, 32, 16)); + AddRegion(batch, mip0.Region(0, 200, 400, 16, 32)); + AddRegion(batch, mip0.Region(0, 300, 300, 64, 64)); + AddRegion(batch, mip1.Region(1, 8, 8, 8, 8)); + AddRegion(batch, mip1.Region(1, 240, 240, 16, 16)); + + ASSERT_TRUE(Packed::Pack(batch, 512ull * 512 + 256ull * 256)); + ExpectValidLayout(batch); + // Largest first, so the 64x64 sprite takes the page origin. + EXPECT_EQ(batch.regions[0].width * batch.regions[0].height, 64 * 64); + EXPECT_EQ(batch.offsets[0].x, 0); + EXPECT_EQ(batch.offsets[0].y, 0); + } + + TEST(PackedTextureUpload, WritePageCopiesEachRegionToItsOffset) { + Level mip0(512, 512); + Packed::Batch batch; + AddRegion(batch, mip0.Region(0, 3, 5, 7, 9)); + AddRegion(batch, mip0.Region(0, 400, 300, 20, 11)); + AddRegion(batch, mip0.Region(0, 260, 17, 13, 13)); + ASSERT_TRUE(Packed::Pack(batch, 512ull * 512)); + ExpectValidLayout(batch); + + std::vector page(batch.ByteSize(), 0xCD); + Packed::WritePage(batch, page.data()); + const std::size_t pitch = static_cast(batch.width) * Packed::BytesPerTexel; + for (std::size_t i = 0; i < batch.count; ++i) { + const auto& region = batch.regions[i]; + const auto& offset = batch.offsets[i]; + for (int y = 0; y < region.height; ++y) { + for (int x = 0; x < region.width; ++x) { + const std::uint8_t* texel = + page.data() + (offset.y + y) * pitch + (offset.x + x) * Packed::BytesPerTexel; + ASSERT_EQ(texel[0], static_cast(region.x + x)) << "region " << i; + ASSERT_EQ(texel[1], static_cast(region.y + y)) << "region " << i; + } + } + } + } + + TEST(PackedTextureUpload, DeclinesBatchesThatSaveTooLittle) { + Level mip0(512, 512); + Packed::Batch single; + AddRegion(single, mip0.Region(0, 0, 0, 16, 16)); + EXPECT_FALSE(Packed::Pack(single, 512ull * 512)) << "one region already uploads exactly"; + + Packed::Batch dense; + AddRegion(dense, mip0.Region(0, 0, 0, 16, 16)); + AddRegion(dense, mip0.Region(0, 16, 0, 16, 16)); + EXPECT_FALSE(Packed::Pack(dense, 32ull * 16)) << "the union box is already small"; + } + + TEST(PackedTextureUpload, DeclinesRegionsThatCannotFitThePage) { + Level mip0(512, 512); + Packed::Batch wide; + AddRegion(wide, mip0.Region(0, 0, 0, Packed::PageSide + 1, 4)); + AddRegion(wide, mip0.Region(0, 300, 300, 4, 4)); + EXPECT_FALSE(Packed::Pack(wide, 512ull * 512)); + + Packed::Batch full; + AddRegion(full, mip0.Region(0, 0, 0, Packed::PageSide, Packed::PageSide)); + AddRegion(full, mip0.Region(0, 256, 256, Packed::PageSide, Packed::PageSide)); + EXPECT_FALSE(Packed::Pack(full, 512ull * 512)) << "more dirty texels than one page holds"; + } + + TEST(PackedTextureUpload, PacksTheMaximumRegionCount) { + Level mip0(512, 512); + Packed::Batch batch; + for (std::size_t i = 0; i < Packed::MaxRegions; ++i) { + const int size = 8 + static_cast(i % 4) * 4; + AddRegion(batch, mip0.Region(0, static_cast(i) * 15, static_cast(i) * 15, size, size)); + } + ASSERT_TRUE(Packed::Pack(batch, 512ull * 512)); + ExpectValidLayout(batch); + } + + std::array Regions(std::initializer_list boxes) { + std::array regions{}; + std::size_t i = 0; + for (const auto& box : boxes) + regions[i++] = box; + return regions; + } + + bool Contains(const Boxes::Box& outer, const Boxes::Box& inner) { + return outer.left <= inner.left && outer.top <= inner.top && outer.right >= inner.right && + outer.bottom >= inner.bottom; + } + + TEST(TextureUploadBoxes, SplitsTwoSeparatedClusters) { + const Boxes::Box a{0, 0, 16, 16}, b{20, 4, 36, 20}, c{400, 300, 416, 316}, d{420, 330, 436, 346}; + auto regions = Regions({c, a, d, b}); + const Boxes::Box bounds{0, 0, 436, 346}; + const auto split = Boxes::Split(bounds, regions, 4); + ASSERT_TRUE(split.has_value()); + const auto& first = (*split)[0]; + const auto& second = (*split)[1]; + const bool firstHoldsLeft = Contains(first, a); + const auto& left = firstHoldsLeft ? first : second; + const auto& right = firstHoldsLeft ? second : first; + EXPECT_TRUE(Contains(left, a) && Contains(left, b)); + EXPECT_TRUE(Contains(right, c) && Contains(right, d)); + EXPECT_LE(first.Texels() + second.Texels(), bounds.Texels() / 2); + const bool disjoint = left.right <= right.left || left.bottom <= right.top; + EXPECT_TRUE(disjoint); + } + + TEST(TextureUploadBoxes, KeepsOneBoxWhenNoCutSeparatesTheRegions) { + // Overlapping regions along the diagonal: no cut on either axis separates two groups. + auto regions = Regions({{0, 0, 100, 100}, {90, 90, 190, 190}, {180, 180, 280, 280}}); + EXPECT_FALSE(Boxes::Split({0, 0, 280, 280}, regions, 3).has_value()); + } + + TEST(TextureUploadBoxes, SkipsSmallOrSingleRegionLevels) { + EXPECT_FALSE(Boxes::WorthSearching({0, 0, 32, 32}, 4)) << "a small level keeps one job"; + EXPECT_FALSE(Boxes::WorthSearching({0, 0, 512, 512}, 1)); + EXPECT_FALSE(Boxes::WorthSearching({0, 0, 512, 512}, Boxes::MaxRegions + 1)); + EXPECT_TRUE(Boxes::WorthSearching({0, 0, 512, 512}, 2)); + } +} // namespace