#include "stdafx.h" #include "doctest.h" #include "model/ktx2.h" #include "zstd.h" #include #include #include #include #include #include // the khronos texture 2.0 files the game reads: what the header tells, the levels read and unpacked from zstd, and the rows of the // bc formats turned upside down without decoding them namespace { using Bytes = std::vector; // a file made for a test: the levels are stored the smallest first, after the header, the index and the key/value data, as the // tools write them struct TestFile { std::uint32_t vkFormat{145}; // VK_FORMAT_BC7_UNORM_BLOCK std::uint32_t width{16}; std::uint32_t height{16}; std::uint32_t depth{0}; std::uint32_t faces{1}; std::uint32_t scheme{0}; std::string orientation; // the value of KTXorientation, no key when empty std::vector levels; // the data stored of each level, the largest first std::vector unpacked; // the sizes of the levels packed with zstd, unpacked std::uint64_t levelOffsetShift{0}; // moves the data of the levels in the index, for a broken file }; void put32(std::string &Out, std::uint32_t const Value) { for (int shift = 0; shift < 32; shift += 8) { Out.push_back(static_cast((Value >> shift) & 0xff)); } } void put64(std::string &Out, std::uint64_t const Value) { put32(Out, static_cast(Value & 0xffffffffu)); put32(Out, static_cast(Value >> 32)); } std::string keyValues(std::string const &Orientation) { std::string out; if (Orientation.empty()) { return out; } std::string entry{"KTXorientation"}; entry.push_back('\0'); entry += Orientation; entry.push_back('\0'); put32(out, static_cast(entry.size())); out += entry; out.resize((out.size() + 3) & ~std::size_t{3}, '\0'); return out; } std::string write(TestFile const &File) { auto const kvd{keyValues(File.orientation)}; auto const count{File.levels.size()}; auto const kvdOffset{80 + 24 * count}; // the data of the levels, the smallest first std::vector offsets(count); auto position{kvdOffset + kvd.size()}; for (auto level = count; level-- > 0;) { offsets[level] = position; position += File.levels[level].size(); } std::string out{"\xAB\x4B\x54\x58\x20\x32\x30\xBB\x0D\x0A\x1A\x0A", 12}; put32(out, File.vkFormat); put32(out, 1); // typeSize put32(out, File.width); put32(out, File.height); put32(out, File.depth); put32(out, 0); // layerCount put32(out, File.faces); put32(out, static_cast(count)); put32(out, File.scheme); put32(out, 0); // dfdByteOffset: the descriptor isn't read put32(out, 0); put32(out, kvd.empty() ? 0 : static_cast(kvdOffset)); put32(out, static_cast(kvd.size())); put64(out, 0); // sgdByteOffset put64(out, 0); for (std::size_t level = 0; level < count; ++level) { put64(out, offsets[level] + File.levelOffsetShift); put64(out, File.levels[level].size()); put64(out, File.unpacked.empty() ? File.levels[level].size() : File.unpacked[level]); } out += kvd; for (auto level = count; level-- > 0;) { out.append(reinterpret_cast(File.levels[level].data()), File.levels[level].size()); } return out; } // data of the given size made of a running count, different in each level Bytes counting(std::size_t const Size, std::uint8_t const Start) { Bytes out(Size); std::iota(out.begin(), out.end(), Start); return out; } Bytes pack(Bytes const &Data) { Bytes out(ZSTD_compressBound(Data.size())); auto const size{ZSTD_compress(out.data(), out.size(), Data.data(), Data.size(), 3)}; REQUIRE(ZSTD_isError(size) == 0); out.resize(size); return out; } std::optional parse(std::string const &Content, std::string &Error) { std::istringstream stream{Content}; return ktx2::Parse(stream, Error); } // the bc7 file of a 16x16 image with its three levels down to a block TestFile bc7File() { TestFile file; file.levels = {counting(16 * 16, 0), counting(4 * 16, 1), counting(16, 2)}; return file; } } // namespace TEST_CASE("ktx2: the header of a bc7 file describes its image and levels") { auto file{bc7File()}; file.vkFormat = 146; // VK_FORMAT_BC7_SRGB_BLOCK std::string error; auto const image{parse(write(file), error)}; REQUIRE_MESSAGE(image.has_value(), error); CHECK(image->format == ktx2::Format::bc7); CHECK(image->srgb); CHECK(image->width == 16); CHECK(image->height == 16); CHECK(image->supercompression == ktx2::Supercompression::none); CHECK(image->origin == ktx2::Origin::unstated); REQUIRE(image->levels.size() == 3); CHECK(image->levels[0].length == 256); CHECK(image->levels[2].length == 16); } TEST_CASE("ktx2: the levels are read the largest first, from where the index puts them") { auto const file{bc7File()}; auto const content{write(file)}; std::string error; auto const image{parse(content, error)}; REQUIRE(image.has_value()); std::istringstream stream{content}; Bytes data; REQUIRE_MESSAGE(ktx2::ReadLevels(stream, *image, 0, 3, data, error), error); Bytes expected; for (auto const &level : file.levels) { expected.insert(expected.end(), level.begin(), level.end()); } CHECK(data == expected); // the levels past those too large for the game, appended to what's there Bytes tail{9}; REQUIRE(ktx2::ReadLevels(stream, *image, 1, 2, tail, error)); CHECK(tail.size() == 1 + 64 + 16); CHECK(tail[0] == 9); CHECK(tail[1] == file.levels[1][0]); CHECK_FALSE(ktx2::ReadLevels(stream, *image, 2, 2, tail, error)); } TEST_CASE("ktx2: levels packed with zstd are unpacked") { TestFile file; file.vkFormat = 133; // VK_FORMAT_BC1_RGBA_UNORM_BLOCK file.width = 8; file.height = 8; file.scheme = 2; std::vector const plain{counting(4 * 8, 10), counting(8, 20), counting(8, 30), counting(8, 40)}; for (auto const &level : plain) { file.levels.push_back(pack(level)); file.unpacked.push_back(level.size()); } auto const content{write(file)}; std::string error; auto const image{parse(content, error)}; REQUIRE_MESSAGE(image.has_value(), error); CHECK(image->format == ktx2::Format::bc1Rgba); CHECK_FALSE(image->srgb); CHECK(image->supercompression == ktx2::Supercompression::zstd); std::istringstream stream{content}; Bytes data; REQUIRE_MESSAGE(ktx2::ReadLevels(stream, *image, 0, 4, data, error), error); Bytes expected; for (auto const &level : plain) { expected.insert(expected.end(), level.begin(), level.end()); } CHECK(data == expected); } TEST_CASE("ktx2: a level which doesn't unpack fails to be read and leaves the data as it was") { TestFile file; file.vkFormat = 141; // VK_FORMAT_BC5_UNORM_BLOCK file.width = 4; file.height = 4; file.scheme = 2; file.levels = {Bytes(16, 0x55)}; // not zstd file.unpacked = {16}; auto const content{write(file)}; std::string error; auto const image{parse(content, error)}; REQUIRE(image.has_value()); std::istringstream stream{content}; Bytes data{1, 2}; CHECK_FALSE(ktx2::ReadLevels(stream, *image, 0, 1, data, error)); CHECK(data == Bytes{1, 2}); CHECK_FALSE(error.empty()); } TEST_CASE("ktx2: the orientation key tells where the first row is") { auto file{bc7File()}; std::string error; file.orientation = "rd"; CHECK(parse(write(file), error)->origin == ktx2::Origin::top); file.orientation = "ru"; CHECK(parse(write(file), error)->origin == ktx2::Origin::bottom); file.orientation = "r"; CHECK(parse(write(file), error)->origin == ktx2::Origin::unstated); } TEST_CASE("ktx2: the formats of the bc family and plain bytes are known") { struct Case { std::uint32_t vkFormat; ktx2::Format format; bool srgb; }; std::array const cases{Case{37, ktx2::Format::rgba8, false}, Case{50, ktx2::Format::bgra8, true}, Case{132, ktx2::Format::bc1Rgb, true}, Case{135, ktx2::Format::bc2, false}, Case{138, ktx2::Format::bc3, true}, Case{139, ktx2::Format::bc4, false}, Case{140, ktx2::Format::bc4Signed, false}, Case{142, ktx2::Format::bc5Signed, false}, Case{143, ktx2::Format::bc6h, false}, Case{144, ktx2::Format::bc6hSigned, false}}; for (auto const &item : cases) { CAPTURE(item.vkFormat); TestFile file; file.vkFormat = item.vkFormat; file.width = 4; file.height = 4; file.levels = {Bytes(ktx2::LevelBytes(item.format, 4, 4))}; std::string error; auto const image{parse(write(file), error)}; REQUIRE_MESSAGE(image.has_value(), error); CHECK(image->format == item.format); CHECK(image->srgb == item.srgb); } } TEST_CASE("ktx2: files the game can't take are refused with the reason") { std::string error; SUBCASE("not a ktx2 file") { auto content{write(bc7File())}; content[5] = '1'; // "KTX 10" CHECK_FALSE(parse(content, error).has_value()); } SUBCASE("cut short in the header") { CHECK_FALSE(parse(write(bc7File()).substr(0, 40), error).has_value()); } SUBCASE("basis universal") { auto file{bc7File()}; file.vkFormat = 0; file.scheme = 1; CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("basislz supercompression") { auto file{bc7File()}; file.scheme = 1; CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("an unknown format") { auto file{bc7File()}; file.vkFormat = 147; // VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("a cube map") { auto file{bc7File()}; file.faces = 6; CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("a 3d image") { auto file{bc7File()}; file.depth = 4; CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("a level past the end of the file") { auto file{bc7File()}; file.levelOffsetShift = 1000; CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("a level shorter than its texels") { auto file{bc7File()}; file.levels[1].resize(32); CHECK_FALSE(parse(write(file), error).has_value()); } SUBCASE("a packed level which unpacks to another size") { auto file{bc7File()}; file.scheme = 2; file.unpacked = {256, 64, 32}; CHECK_FALSE(parse(write(file), error).has_value()); } CHECK_FALSE(error.empty()); } TEST_CASE("ktx2: the sizes of the levels") { CHECK(ktx2::LevelExtent(16, 0) == 16); CHECK(ktx2::LevelExtent(16, 3) == 2); CHECK(ktx2::LevelExtent(16, 7) == 1); CHECK(ktx2::LevelExtent(16, 40) == 1); CHECK(ktx2::LevelBytes(ktx2::Format::bc1Rgb, 8, 8) == 32); CHECK(ktx2::LevelBytes(ktx2::Format::bc1Rgb, 2, 1) == 8); CHECK(ktx2::LevelBytes(ktx2::Format::bc5, 8, 4) == 32); CHECK(ktx2::LevelBytes(ktx2::Format::bc7, 1, 1) == 16); CHECK(ktx2::LevelBytes(ktx2::Format::rgba8, 3, 2) == 24); } TEST_CASE("ktx2: the rows of a bc1 image are turned upside down block by block") { // a 4x8 image: two blocks one above the other, each with its colours and a row of indices per texel row Bytes data{0x11, 0x11, 0x22, 0x22, 0x00, 0x01, 0x02, 0x03, 0x33, 0x33, 0x44, 0x44, 0x10, 0x11, 0x12, 0x13}; ktx2::FlipLevels(ktx2::Format::bc1Rgb, data, 4, 8, 1); CHECK(data == Bytes{0x33, 0x33, 0x44, 0x44, 0x13, 0x12, 0x11, 0x10, 0x11, 0x11, 0x22, 0x22, 0x03, 0x02, 0x01, 0x00}); // a level of 2 rows has only those swapped, the rest of the block isn't drawn Bytes lowLevel{0x11, 0x11, 0x22, 0x22, 0x00, 0x01, 0x02, 0x03}; ktx2::FlipLevels(ktx2::Format::bc1Rgba, lowLevel, 2, 2, 1); CHECK(lowLevel == Bytes{0x11, 0x11, 0x22, 0x22, 0x01, 0x00, 0x02, 0x03}); } TEST_CASE("ktx2: the rows of the alpha and of the channels of bc2 to bc5 are swapped") { // a bc4 block of 4 rows of 12 bit indices: 0x001, 0x002, 0x003, 0x004 Bytes const bc4{0xff, 0x00, 0x01, 0x20, 0x00, 0x03, 0x40, 0x00}; auto flipped{bc4}; ktx2::FlipLevels(ktx2::Format::bc4, flipped, 4, 4, 1); CHECK(flipped == Bytes{0xff, 0x00, 0x04, 0x30, 0x00, 0x02, 0x10, 0x00}); // the alpha of bc3 is a bc4 block, its colour a bc1 one Bytes bc3{bc4}; bc3.insert(bc3.end(), {0x11, 0x11, 0x22, 0x22, 0x00, 0x01, 0x02, 0x03}); ktx2::FlipLevels(ktx2::Format::bc3, bc3, 4, 4, 1); CHECK(Bytes(bc3.begin(), bc3.begin() + 8) == flipped); CHECK(Bytes(bc3.begin() + 12, bc3.end()) == Bytes{0x03, 0x02, 0x01, 0x00}); // the explicit alpha of bc2: 16 bits a row Bytes bc2{0x01, 0x10, 0x02, 0x20, 0x03, 0x30, 0x04, 0x40, 0x11, 0x11, 0x22, 0x22, 0x00, 0x01, 0x02, 0x03}; ktx2::FlipLevels(ktx2::Format::bc2, bc2, 4, 4, 1); CHECK(Bytes(bc2.begin(), bc2.begin() + 8) == Bytes{0x04, 0x40, 0x03, 0x30, 0x02, 0x20, 0x01, 0x10}); // flipped twice, the levels of an image come back as they were for (auto const format : {ktx2::Format::bc2, ktx2::Format::bc3, ktx2::Format::bc4Signed, ktx2::Format::bc5}) { auto const original{counting(static_cast(ktx2::LevelBytes(format, 16, 8) + ktx2::LevelBytes(format, 8, 4) + ktx2::LevelBytes(format, 4, 2)), 7)}; auto data{original}; ktx2::FlipLevels(format, data, 16, 8, 3); CHECK(data != original); ktx2::FlipLevels(format, data, 16, 8, 3); CHECK(data == original); } } TEST_CASE("ktx2: plain texels have their rows swapped, bc6h and bc7 are left as they are") { Bytes rgba{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6}; ktx2::FlipLevels(ktx2::Format::rgba8, rgba, 2, 3, 1); CHECK(rgba == Bytes{5, 5, 5, 5, 6, 6, 6, 6, 3, 3, 3, 3, 4, 4, 4, 4, 1, 1, 1, 1, 2, 2, 2, 2}); CHECK_FALSE(ktx2::CanFlip(ktx2::Format::bc7)); CHECK_FALSE(ktx2::CanFlip(ktx2::Format::bc6h)); CHECK(ktx2::CanFlip(ktx2::Format::bc5Signed)); auto const original{counting(32, 0)}; auto data{original}; ktx2::FlipLevels(ktx2::Format::bc7, data, 4, 8, 1); CHECK(data == original); }