Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
#include <gr2/types.h>
#include <cmath>
#include <cstdio>
#include <vector>
int main() {
// ScaleShearCurve uses 9 values per control point. A quadratic span reads
// three control points, so this is the regression case for the former 3*4
// stack buffer in Curve::eval().
gr2::Curve curve;
curve.degree = 2;
curve.dim = 9;
curve.knots = {0.0f, 1.0f, 2.0f};
curve.controls.resize(27);
for (size_t i = 0; i < curve.controls.size(); ++i) {
curve.controls[i] = 1.0f + static_cast<float>(i) * 0.01f;
}
float out[9] = {};
curve.eval(0.5f, out);
for (float value : out) {
if (!std::isfinite(value)) {
std::fprintf(stderr, "non-finite 9D curve output\n");
return 1;
}
}
// Degree 0 with several knots (AVA2 DaKeyframes32f) holds the last knot <= t.
gr2::Curve steps;
steps.degree = 0;
steps.dim = 1;
steps.knots = {0.0f, 0.5f, 1.0f};
steps.controls = {10.0f, 20.0f, 30.0f};
const float step_t[] = {-1.0f, 0.0f, 0.49f, 0.5f, 0.99f, 1.0f, 2.0f};
const float step_v[] = {10.0f, 10.0f, 10.0f, 20.0f, 20.0f, 30.0f, 30.0f};
for (size_t i = 0; i < sizeof step_t / sizeof step_t[0]; ++i) {
float v = 0.0f;
steps.eval(step_t[i], &v);
if (v != step_v[i]) {
std::fprintf(stderr, "degree-0 step t=%f got %f want %f\n", step_t[i], v, step_v[i]);
return 1;
}
}
// BoneTrack::sample fills SRT components and flags; sample_local = compose(sample).
gr2::BoneTrack track;
track.position.degree = 1;
track.position.dim = 3;
track.position.knots = {0.0f, 1.0f};
track.position.controls = {0.0f, 0.0f, 0.0f, 2.0f, 4.0f, 6.0f};
track.orientation.degree = 0;
track.orientation.dim = 4;
track.orientation.knots = {0.0f};
track.orientation.controls = {0.0f, 0.0f, 0.70710678f, 0.70710678f}; // 90 deg about Z
gr2::Transform x;
track.sample(0.5f, x);
if (x.flags != 3 || std::fabs(x.position[0] - 1.0f) > 1e-5f || std::fabs(x.position[2] - 3.0f) > 1e-5f) {
std::fprintf(stderr, "BoneTrack::sample flags=%u pos=%f,%f,%f\n", x.flags, x.position[0], x.position[1], x.position[2]);
return 1;
}
gr2::Animation anim;
anim.tracks.push_back(track);
std::vector<gr2::Mat4> local;
anim.sample_local(0.5f, local);
const gr2::Mat4 m = gr2::compose(x);
// Row-major composite: row 0 is the rotated X axis (0,1,0); translation in 12..14.
if (local.size() != 1 || local[0] != m || std::fabs(m[1] - 1.0f) > 1e-5f || std::fabs(m[12] - 1.0f) > 1e-5f) {
std::fprintf(stderr, "compose mismatch m[1]=%f m[12]=%f\n", m[1], m[12]);
return 1;
}
return 0;
}
+41
View File
@@ -0,0 +1,41 @@
#include <gr2_render/gr2_render.h>
#include <cassert>
#include <iostream>
#include <vector>
int main() {
std::cout << "[Test] Running Gr2Render unit tests..." << std::endl;
// Test 1: Null/empty load
{
auto inst = gr2_render::Gr2ModelInstance::Load("non_existent_file.gr2");
assert(!inst && "Expected nullptr for non-existent file");
auto inst_mem = gr2_render::Gr2ModelInstance::LoadFromMemory(nullptr, 0);
assert(!inst_mem && "Expected nullptr for empty memory buffer");
std::cout << " PASS: Null/invalid input handles gracefully." << std::endl;
}
// Test 2: Gr2MeshDraw layout and defaults
{
gr2_render::Gr2MeshDraw draw;
assert(draw.world[0] == 1.0f && draw.world[5] == 1.0f && draw.world[10] == 1.0f && draw.world[15] == 1.0f);
assert(draw.alpha_ref == 70);
assert(!draw.alpha_test);
assert(!draw.double_sided);
assert(draw.z_write);
assert(draw.lighting);
std::cout << " PASS: Gr2MeshDraw default values valid." << std::endl;
}
// Test 3: Load corrupt GR2 header into LoadFromMemory
{
std::vector<uint8_t> bogus(128, 0xAA);
auto inst = gr2_render::Gr2ModelInstance::LoadFromMemory(bogus.data(), bogus.size(), "bogus");
assert(!inst && "Expected failed load for corrupt data");
std::cout << " PASS: Corrupt GR2 data safely rejected by LoadFromMemory." << std::endl;
}
std::cout << "[Test] All Gr2Render tests passed successfully!" << std::endl;
return 0;
}
+105
View File
@@ -0,0 +1,105 @@
#include <gr2/gr2.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
namespace {
void wr32(std::vector<uint8_t> &bytes, size_t off, uint32_t value) {
bytes[off + 0] = uint8_t(value);
bytes[off + 1] = uint8_t(value >> 8);
bytes[off + 2] = uint8_t(value >> 16);
bytes[off + 3] = uint8_t(value >> 24);
}
constexpr uint32_t kMagic32LE[] = { 0xCAB067B8, 0x0FB16DF8, 0x7E8C7284, 0x1E00195E };
constexpr uint32_t kMagic64LE[] = { 0x5E499BE5, 0x141F636F, 0xA9EB131E, 0xC4EDBE90 };
constexpr uint32_t kMagic32BE[] = { 0xB595110E, 0x4BB5A56A, 0x502828EB, 0x04B37825 };
std::vector<uint8_t> granny_header(size_t size, uint32_t section_count, const uint32_t *magic = kMagic32LE) {
std::vector<uint8_t> bytes(size, 0);
for (int i = 0; i < 4; ++i) {
wr32(bytes, size_t(i) * 4, magic[i]);
}
constexpr size_t h = 32;
wr32(bytes, h + 0, 6); // Version
wr32(bytes, h + 4, uint32_t(size));
wr32(bytes, h + 12, 40); // section table starts at byte 72
wr32(bytes, h + 16, section_count);
// Root type/object remain section 0, offset 0. With no section this must be
// rejected by the FileInfo stage rather than returned as an empty File.
return bytes;
}
bool expect_failure(const std::vector<uint8_t> &bytes, const char *stage) {
gr2::LoadError err;
auto file = gr2::File::load(bytes.data(), bytes.size(), &err);
if (file || err.stage != stage) {
std::fprintf(stderr, "expected failure stage '%s', got file=%d stage='%s' message='%s'\n",
stage, file.has_value(), err.stage.c_str(), err.message.c_str());
return false;
}
return true;
}
} // namespace
int main() {
bool ok = true;
ok &= expect_failure(std::vector<uint8_t>(80, 0), "header");
ok &= expect_failure(granny_header(80, 0), "fileinfo");
// 64-bit little-endian Granny (AVA2 plechi_* assets) passes the header stage;
// big-endian is still rejected there.
ok &= expect_failure(granny_header(80, 0, kMagic64LE), "fileinfo");
ok &= expect_failure(granny_header(80, 0, kMagic32BE), "header");
auto unsupported = granny_header(160, 1);
constexpr size_t section = 72;
wr32(unsupported, section + 0, 3); // BitKnit v1 is not implemented
wr32(unsupported, section + 4, uint32_t(unsupported.size()));
wr32(unsupported, section + 12, 1); // non-empty expanded section
ok &= expect_failure(unsupported, "section");
// BitKnit2 (Format 4) with an empty stream: no 0x75B1 magic -> clean section failure.
auto bitknit_empty = unsupported;
wr32(bitknit_empty, section + 0, 4);
ok &= expect_failure(bitknit_empty, "section");
// BitKnit2 stored quantum: magic 0x75B1, u16 0 marker, 8 raw bytes. The section
// decodes, so loading proceeds to the (empty) type tree instead of failing in "section".
auto bitknit_stored = granny_header(160, 1);
const uint8_t stream[] = { 0xB1, 0x75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (size_t i = 0; i < sizeof(stream); ++i) bitknit_stored[120 + i] = stream[i];
wr32(bitknit_stored, section + 0, 4);
wr32(bitknit_stored, section + 4, 120);
wr32(bitknit_stored, section + 8, uint32_t(sizeof(stream)));
wr32(bitknit_stored, section + 12, 8);
{
gr2::LoadError err;
auto file = gr2::File::load(bitknit_stored.data(), bitknit_stored.size(), &err);
if (!file && err.stage == "section") {
std::fprintf(stderr, "BitKnit2 stored quantum rejected: %s\n", err.message.c_str());
ok = false;
}
}
// Truncated stored quantum (claims 8 bytes, carries 4) must fail cleanly.
auto bitknit_short = bitknit_stored;
wr32(bitknit_short, section + 8, 8);
ok &= expect_failure(bitknit_short, "section");
// BitKnit2 fixup table: u32 compressed size pointing past EOF -> clean "fixup" failure.
auto bitknit_fixup = bitknit_stored;
wr32(bitknit_fixup, section + 28, 150); // PointerFixupArrayOffset
wr32(bitknit_fixup, section + 32, 1); // PointerFixupArrayCount
wr32(bitknit_fixup, 150, 1000);
ok &= expect_failure(bitknit_fixup, "fixup");
auto bad_offset = granny_header(160, 1);
wr32(bad_offset, section + 4, UINT32_MAX);
ok &= expect_failure(bad_offset, "section");
return ok ? 0 : 1;
}