Fix model track-group movement and RGB destination-alpha blending

This commit is contained in:
shen
2026-09-30 08:13:29 -07:00
parent f345bd5d3b
commit 905f23f135
16 changed files with 367 additions and 72 deletions
+36
View File
@@ -0,0 +1,36 @@
#include "../../src/host/blend_state.h"
#include "../../src/host/draw_capture.h"
#include <cstdio>
int main(int argc, char** argv) {
int failures = 0;
auto check = [&](bool ok, const char* message) {
if (!ok) { std::fprintf(stderr, "FAIL %s\n", message); ++failures; }
};
using mt_host::d3d_blend_for_rgb_target;
check(d3d_blend_for_rgb_target(7) == 2, "gungsin DESTALPHA preserves the background on XRGB");
check(d3d_blend_for_rgb_target(8) == 1, "INVDESTALPHA contributes zero on XRGB");
check(d3d_blend_for_rgb_target(11) == 1, "SRCALPHASAT contributes zero RGB on XRGB");
for (std::uint8_t factor = 1; factor <= 10; ++factor)
if (factor != 7 && factor != 8)
check(d3d_blend_for_rgb_target(factor) == factor, "ordinary alpha/additive/color factors preserved");
if (argc == 2) {
std::vector<Render3DDraw> draws;
// An opaque scene draw can output alpha 0 into Vulkan's RGBA storage.
// Gungsin's SRCALPHA / DESTALPHA must still preserve that XRGB background.
for (auto color : {0x00804020u, 0x800080ffu}) {
Render3DDraw d{};
for (auto* matrix : {d.world, d.view, d.proj})
for (int i = 0; i < 16; ++i) matrix[i] = (i % 5 == 0) ? 1 : 0;
d.positions = {-1,-1,0.5f, 1,-1,0.5f, 1,1,0.5f, -1,1,0.5f};
d.indices = {0,1,2,0,2,3};
d.diffuse = {color,color,color,color};
d.color_op[0] = d.alpha_op[0] = 2; // SELECTARG1 / DIFFUSE
d.color_op[1] = d.alpha_op[1] = 1; // DISABLE
if (!draws.empty()) { d.alpha_blend = 1; d.src_blend = 5; d.dest_blend = 7; }
draws.push_back(std::move(d));
}
native_draw_capture::write(argv[1], draws, {}, 960, 640, {});
}
return failures ? 1 : 0;
}
+25
View File
@@ -0,0 +1,25 @@
"""GPU regression: python3 rgb_target_blend_test.py <host_blend_state_test> <mt_native_render>."""
import pathlib
import struct
import subprocess
import sys
import tempfile
with tempfile.TemporaryDirectory(prefix="metin2-rgb-blend-") as directory:
capture = pathlib.Path(directory) / "destalpha.capture"
screenshot = pathlib.Path(directory) / "destalpha.bmp"
subprocess.run([sys.argv[1], str(capture)], check=True)
subprocess.run([sys.argv[2], "--capture", str(capture), "--frames", "1",
"--width", "320", "--height", "240", "--screenshot-out", str(screenshot)],
check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
data = screenshot.read_bytes()
offset = struct.unpack_from("<I", data, 10)[0]
width, height = struct.unpack_from("<ii", data, 18)
bits = struct.unpack_from("<H", data, 28)[0]
assert bits in (24, 32), f"unsupported screenshot format: {bits}"
stride = ((width * bits + 31) // 32) * 4
pixel = offset + (abs(height) // 2) * stride + (width // 2) * (bits // 8)
actual = tuple(data[pixel:pixel + 3][::-1])
expected = (128, 128, 160) # source * 128/255 + background * 1
assert all(abs(a - b) <= 1 for a, b in zip(actual, expected)), (actual, expected)
print(f"PASS XRGB SRCALPHA/DESTALPHA GPU pixel: {actual}")
@@ -0,0 +1,90 @@
// Real packed bear assets: the auxiliary Hedra group precedes the moving Bip01 group.
#include "granny.h"
#include "../../src/platform/PackBackend.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
namespace {
int failures = 0;
void check(bool ok, const char* message) {
if (!ok) { std::fprintf(stderr, "FAIL %s\n", message); ++failures; }
}
granny_file* load(const std::string& path) {
std::vector<uint8_t> bytes;
if (!mtpack40250::read(path, bytes)) return nullptr;
return GrannyReadEntireFileFromMemory(bytes.size(), bytes.data());
}
void test_motion(granny_model* model, const char* name) {
const std::string base = "d:/ymir work/monster/bear/";
granny_file* file = load(base + name + ".gr2");
check(file != nullptr, "bear animation loads from the live pack path");
if (!file) return;
auto* info = GrannyGetFileInfo(file);
check(info && info->AnimationCount == 1, "one animation");
if (!info || info->AnimationCount != 1) { GrannyFreeFile(file); return; }
auto* animation = info->Animations[0];
check(animation->TrackGroupCount == 2, "both track groups exposed safely");
const bool auxiliaryFirst = animation->TrackGroupCount > 0 &&
!std::strcmp(animation->TrackGroups[0]->Name, "Hedra");
check(auxiliaryFirst, "auxiliary group first");
if (animation->TrackGroupCount == 2 && auxiliaryFirst) {
check(!std::strcmp(animation->TrackGroups[1]->Name, "Bip01"), "model group second");
}
std::vector<uint8_t> msa;
check(mtpack40250::read(base + name + ".msa", msa), "motion specification loads");
float accumulation[3] = {};
std::istringstream lines(std::string(msa.begin(), msa.end()));
for (std::string line; std::getline(lines, line);)
if (line.rfind("Accumulation", 0) == 0)
std::sscanf(line.c_str(), "Accumulation %f %f %f", accumulation, accumulation + 1, accumulation + 2);
check(accumulation[1] < 0, "moving resource has forward accumulation");
for (bool inverse : {false, true}) {
auto* instance = GrannyInstantiateModel(model);
auto* control = GrannyPlayControlledAnimation(0, animation, instance);
check(control != nullptr, "animation binds to named model");
if (control) {
GrannySetControlLoopCount(control, 0);
const float dt = 0.1f;
const float clock = animation->Duration + dt; // across the loop boundary
GrannySetModelClock(instance, clock);
float matrix[16] = {0,1,0,0, -1,0,0,0, 0,0,1,0, 100,200,0,1};
GrannyUpdateModelMatrix(instance, dt, matrix, matrix, inverse);
const float expectedX = 100 - (inverse ? -1.0f : 1.0f) * accumulation[1] * dt / animation->Duration;
check(std::abs(matrix[12] - expectedX) < 0.02f && std::abs(matrix[13] - 200) < 0.02f,
"model group root displacement, loop, rotation and inverse");
}
GrannyFreeModelInstance(instance);
}
// Hedra has no root displacement even though the other group moves.
granny_model decoration = *model;
decoration.Name = "Hedra";
auto* instance = GrannyInstantiateModel(&decoration);
auto* control = GrannyPlayControlledAnimation(0, animation, instance);
check(control != nullptr, "auxiliary object binds to its own group");
GrannySetModelClock(instance, 0.1f);
float matrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
GrannyUpdateModelMatrix(instance, 0.1f, matrix, matrix, false);
check(matrix[12] == 0 && matrix[13] == 0, "auxiliary group does not inherit bear movement");
GrannyFreeModelInstance(instance);
GrannyFreeFile(file);
}
}
int main(int argc, char** argv) {
if (argc < 2 || !mtpack40250::initialize(argv[1])) return std::getenv("MT_ASSETS_STRICT") ? 1 : 77;
auto* file = load("d:/ymir work/monster/bear/bear.gr2");
check(file != nullptr, "bear model loads");
if (!file) return 1;
auto* info = GrannyGetFileInfo(file);
check(info && info->ModelCount == 1, "one bear model");
if (info && info->ModelCount == 1) {
test_motion(info->Models[0], "02");
test_motion(info->Models[0], "03");
}
GrannyFreeFile(file);
return failures ? 1 : 0;
}