135 lines
No EOL
5 KiB
C++
135 lines
No EOL
5 KiB
C++
/*
|
|
Crafter®.Asset
|
|
Copyright (C) 2026 Catcrafts®
|
|
catcrafts.net
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License version 3.0 as published by the Free Software Foundation;
|
|
This library 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 library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
import Crafter.Asset;
|
|
import Crafter.Math;
|
|
import std;
|
|
using namespace Crafter;
|
|
namespace fs = std::filesystem;
|
|
|
|
// CPU GDeflate roundtrip sanity test across the size boundaries from the
|
|
// implementation plan. Returns 0 on pass, 1 on first byte-mismatch.
|
|
static int RunCompressionRoundtrip() {
|
|
const std::array<std::size_t, 5> sizes = { 1, 65535, 65536, 65537, 16 * 1024 * 1024 };
|
|
std::mt19937_64 rng(0xC0FFEEu);
|
|
for (std::size_t n : sizes) {
|
|
std::vector<std::byte> input(n);
|
|
for (std::size_t i = 0; i < n; ++i) {
|
|
// Mix random bytes with a deterministic pattern so the codec is
|
|
// exercised on both compressible and noisy regions.
|
|
input[i] = static_cast<std::byte>((i * 0x9E3779B97F4A7C15ULL ^ rng()) & 0xFF);
|
|
}
|
|
std::array<std::span<const std::byte>, 1> streams = { std::span(input) };
|
|
Compression::CompressedBlob blob = Compression::CompressStreams(streams);
|
|
std::vector<std::byte> output(n);
|
|
std::array<std::span<std::byte>, 1> outputs = { std::span(output) };
|
|
Compression::DecompressCPU(blob, outputs);
|
|
if (output != input) {
|
|
std::cerr << "[FAIL] roundtrip size=" << n << "\n";
|
|
return 1;
|
|
}
|
|
std::cout << "[ok] size=" << n
|
|
<< " compressed=" << blob.bytes.size()
|
|
<< " ratio=" << (double(blob.bytes.size()) / double(n)) << "\n";
|
|
}
|
|
std::cout << "All roundtrips passed.\n";
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
// Parse arguments: crafter-asset <input_file> [output_file] [--format u8|f16]
|
|
fs::path inputPath;
|
|
fs::path outputPath;
|
|
std::string textureFormat = "f16"; // default
|
|
bool hasOutputPath = false;
|
|
|
|
std::vector<std::string> positional;
|
|
for (int i = 1; i < argc; ++i) {
|
|
std::string arg = argv[i];
|
|
if (arg == "--test-compression") {
|
|
return RunCompressionRoundtrip();
|
|
}
|
|
if (arg == "--format" || arg == "-f") {
|
|
if (i + 1 >= argc) {
|
|
std::cerr << "Error: --format requires a value (u8 or f16)\n";
|
|
return 1;
|
|
}
|
|
textureFormat = argv[++i];
|
|
if (textureFormat != "u8" && textureFormat != "f16") {
|
|
std::cerr << "Error: Invalid format '" << textureFormat
|
|
<< "'. Must be 'u8' or 'f16'.\n";
|
|
return 1;
|
|
}
|
|
} else {
|
|
positional.push_back(arg);
|
|
}
|
|
}
|
|
|
|
if (positional.empty() || positional.size() > 2) {
|
|
std::cout << "Usage: crafter-asset <input_file> [output_file] [--format u8|f16]\n";
|
|
return 1;
|
|
}
|
|
|
|
inputPath = positional[0];
|
|
if (positional.size() == 2) {
|
|
outputPath = positional[1];
|
|
hasOutputPath = true;
|
|
}
|
|
|
|
// Check if output path is provided; if not, use input path with a modified extension
|
|
if (!hasOutputPath) {
|
|
std::string extension = inputPath.extension().string();
|
|
if (extension == ".obj") {
|
|
outputPath = inputPath;
|
|
outputPath.replace_extension(".mesh");
|
|
} else if (extension == ".png") {
|
|
outputPath = inputPath;
|
|
outputPath.replace_extension(".tex");
|
|
} else {
|
|
std::cerr << "Unsupported file type: " << extension << "\n";
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (!fs::exists(inputPath)) {
|
|
std::cerr << "Error: Input file does not exist.\n";
|
|
return 1;
|
|
}
|
|
|
|
std::string extension = inputPath.extension().string();
|
|
try {
|
|
if (extension == ".obj") {
|
|
auto mesh = MeshAsset<VertexNormalTangentUVPacked>::LoadOBJ(inputPath);
|
|
mesh.Save(outputPath);
|
|
}
|
|
else if (extension == ".png") {
|
|
if (textureFormat == "f16") {
|
|
auto texture = TextureAsset<Vector<_Float16, 4, 4>>::LoadPNG<_Float16>(inputPath);
|
|
texture.Save(outputPath);
|
|
} else { // u8
|
|
auto texture = TextureAsset<Vector<std::uint8_t, 4, 4>>::LoadPNG<std::uint8_t>(inputPath);
|
|
texture.Save(outputPath);
|
|
}
|
|
}
|
|
else {
|
|
std::cerr << "Unsupported file type: " << extension << "\n";
|
|
return 1;
|
|
}
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "Error: " << e.what() << "\n";
|
|
return 1;
|
|
}
|
|
return 0;
|
|
} |