mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 19:57:06 +01:00
7d3ccd6dc4
The game now reads .ktx2 texture files, for the OpenGL 4.6 and the Vulkan renderer alike (texture_source::load_KTX2(), parser in model/ktx2.*): - formats: BC1 (RGB and RGBA), BC2, BC3, BC4, BC5 (unsigned and signed), BC6H (unsigned and signed float), BC7, and plain R8G8B8A8 / B8G8R8A8; - the levels stored as they are or packed with zstd (the bundled zstd); with gfx.textures.streaming the worker threads read and unpack them; - the KTXorientation key is honoured: "rd" files are flipped to the bottom row first, BC1-BC5 block by block, plain texels row by row. BC6H and BC7 can't be flipped without decoding: an "rd" file of these is loaded as stored, with a warning in the log, as the BC7 dds files are. A file without the key is taken the way the dds files are (ddsupperorigin); - srgb or linear colour is chosen by the material, as for the dds files; - the alpha of BC7 is checked on a small level, as for the dds files. Not supported, refused with the reason in the log: Basis Universal (ETC1S / UASTC, BasisLZ), zlib supercompression, arrays, cube maps and 3d images. BC1 without alpha, BC4, BC5 and BC6H are new formats for the renderers: opengl_texture::mapping and the Vulkan texture_format_of() map them (no srgb variants for BC4/BC5/BC6H; their alpha reads 1). .ktx2 is looked for right after .dds (textures, materials, the editor's texture browsers). Existing data holds no .ktx2 files, so nothing changes for existing sceneries and vehicles, and no asset change in the SVN is needed. Unit tests: tests/unit/test_ktx2.cpp (header, levels, zstd, orientation, refused files, flipping of BC1-BC5 and plain texels).
106 lines
3.7 KiB
C++
106 lines
3.7 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <istream>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// the khronos texture 2.0 files (.ktx2) the game takes: a 2d image with its mipmaps, of the bc formats (bc1 to bc7) or of plain
|
|
// rgba / bgra bytes, its levels stored as they are or packed with zstd. the files hold the levels the way vulkan takes them; what
|
|
// the renderers make of them is the matter of texture_source::load_KTX2() (model/Texture.cpp). the code here knows nothing of
|
|
// opengl or vulkan, so that it can be tested on its own
|
|
namespace ktx2
|
|
{
|
|
|
|
// the formats of the texels the game takes from the files (vkFormat of the header)
|
|
enum class Format
|
|
{
|
|
bc1Rgb, // no alpha
|
|
bc1Rgba, // 1 bit alpha
|
|
bc2,
|
|
bc3,
|
|
bc4, // one channel (red)
|
|
bc4Signed,
|
|
bc5, // two channels (red, green): normal maps
|
|
bc5Signed,
|
|
bc6h, // hdr colour, no alpha
|
|
bc6hSigned,
|
|
bc7,
|
|
rgba8,
|
|
bgra8
|
|
};
|
|
|
|
// how the levels are stored in the file
|
|
enum class Supercompression
|
|
{
|
|
none,
|
|
zstd
|
|
};
|
|
|
|
// where the file says the first row of a level is (the KTXorientation key)
|
|
enum class Origin
|
|
{
|
|
unstated,
|
|
top, // "rd", what the format holds by default
|
|
bottom // "ru"
|
|
};
|
|
|
|
// a level of the image in the file: where its data is, how many bytes it takes there and how many unpacked
|
|
struct Level
|
|
{
|
|
std::uint64_t offset{0};
|
|
std::uint64_t length{0};
|
|
std::uint64_t uncompressedLength{0};
|
|
};
|
|
|
|
// what the header of a file tells of its image
|
|
struct Description
|
|
{
|
|
Format format{Format::rgba8};
|
|
bool srgb{false}; // the file states srgb colour (the material decides it in the game, as it does for the dds files)
|
|
std::uint32_t width{0};
|
|
std::uint32_t height{0};
|
|
Supercompression supercompression{Supercompression::none};
|
|
Origin origin{Origin::unstated};
|
|
std::vector<Level> levels; // the largest first
|
|
};
|
|
|
|
// the description of the image of a file, or nothing and the reason in Error when the game can't take it. reads the header, the
|
|
// index of the levels and the key/value data; the levels themselves aren't read
|
|
std::optional<Description> Parse(std::istream &Stream, std::string &Error);
|
|
|
|
// whether the format is made of the 4x4 blocks
|
|
bool IsCompressed(Format Value);
|
|
// the bytes of a 4x4 block of a compressed format, of a texel of the others
|
|
std::uint32_t BlockBytes(Format Value);
|
|
// the bytes the data of a level of the given size takes
|
|
std::uint64_t LevelBytes(Format Value, std::uint32_t Width, std::uint32_t Height);
|
|
// the size of a level
|
|
std::uint32_t LevelExtent(std::uint32_t Extent, std::size_t Level);
|
|
|
|
// appends the data of Count levels of the image, from the level First on, to Data: read from the file and unpacked. false and the
|
|
// reason in Error when the file doesn't hold them whole
|
|
bool ReadLevels(std::istream &Stream, Description const &Image, std::size_t First, std::size_t Count, std::vector<std::uint8_t> &Data, std::string &Error);
|
|
|
|
// whether the rows of the format can be turned upside down without decoding it: not those of bc6h and bc7, whose blocks are
|
|
// split into parts in many ways
|
|
bool CanFlip(Format Value);
|
|
// turns the rows of Count levels, one after another in Data and the first of the given size, upside down. of a compressed level the
|
|
// rows of its blocks swap places and the rows of texels inside each block too; a level lower than a block has only its rows of
|
|
// texels swapped, the rest of the block isn't drawn
|
|
void FlipLevels(Format Value, std::span<std::uint8_t> Data, std::uint32_t Width, std::uint32_t Height, std::size_t Count);
|
|
|
|
} // namespace ktx2
|