commit ad191b93e2413ffdfa2660986302c4336e4cd6de
parent 65c60893100cdb3212a5f96f000b2f942034bbff
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 14:29:11 +0000
Apply native stage-zero texture transforms
Diffstat:
3 files changed, 50 insertions(+), 22 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -52,11 +52,11 @@ struct device_light {
static struct device_object {
uint32_t interface, references, cull;
uint32_t min_filter, mag_filter, mip_filter, address, address_u, address_v, color_op, alpha_op;
- uint32_t sampler_u, sampler_v;
+ uint32_t sampler_u, sampler_v, texture_transform;
uint32_t alpha_arg2;
uint32_t fog_enabled, fog_color, fog_start, fog_end, fog_density, fog_vertex_mode;
uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting;
- uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[3][16];
+ uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[4][16];
int scene;
struct draw_object *owner;
struct surface_object *target;
@@ -1344,7 +1344,7 @@ static void create_device(void)
device->clipping = 1;
device->lighting = 1;
device->fog_end = device->fog_density = 0x3f800000;
- for (unsigned matrix = 0; matrix < 3; ++matrix)
+ for (unsigned matrix = 0; matrix < 4; ++matrix)
for (unsigned i = 0; i < 16; i += 5) device->transforms[matrix][i] = 0x3f800000;
device->owner = owner;
device->target = surface;
@@ -1604,13 +1604,14 @@ static void device_transform(void)
struct device_object *device = find_device(MEM32(g_esp + 4));
uint32_t state = MEM32(g_esp + 8), matrix = MEM32(g_esp + 12);
g_esp += 16;
- if (state < 1 || state > 3) {
+ if ((state < 1 || state > 3) && state != 16) {
fprintf(stderr, "Unsupported Direct3D transform state %u\n", state); exit(3);
}
g_eax = 0x80070057u;
if (!matrix) return;
- if (current_slot == 11) memcpy(device->transforms[state - 1], (void *)ADDR(matrix), 64);
- else memcpy((void *)ADDR(matrix), device->transforms[state - 1], 64);
+ unsigned index = state == 16 ? 3 : state - 1;
+ if (current_slot == 11) memcpy(device->transforms[index], (void *)ADDR(matrix), 64);
+ else memcpy((void *)ADDR(matrix), device->transforms[index], 64);
g_eax = 0;
}
@@ -1774,6 +1775,11 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
vertices[i].fog = fminf(1, fmaxf(0, (end - fabsf(camera[2])) / (end - start)));
}
memcpy(vertices[i].uv, (const unsigned char *)input_vertex + stride - 8, 8);
+ if (device->texture_transform == 2) {
+ float coordinate[] = {vertices[i].uv[0], vertices[i].uv[1], 1, 0}, transformed[4];
+ transform_vector(coordinate, (const float *)device->transforms[3], transformed);
+ memcpy(vertices[i].uv, transformed, 8);
+ }
continue;
}
float position[4];
@@ -1870,7 +1876,8 @@ static uint32_t texture_stage_state(struct device_object *device, uint32_t state
case 6: return device->alpha_arg2;
case 4: return device->alpha_op;
case 7: case 8: case 9: case 10: /* Inactive bump matrix. */
- case 11: case 15: case 19: case 20: case 22: case 23: case 24: return 0;
+ case 11: case 15: case 19: case 20: case 22: case 23: return 0;
+ case 24: return device->texture_transform;
case 12: return device->address;
case 13: return device->address_u;
case 14: return device->address_v;
@@ -1905,6 +1912,7 @@ static void device_set_texture_state(void)
if (state == 16 && (value == 1 || value == 2)) device->mag_filter = value;
else if (state == 17 && (value == 1 || value == 2)) device->min_filter = value;
else if (state == 18 && (value == 1 || value == 2)) device->mip_filter = value;
+ else if (state == 24 && (value == 0 || value == 2)) device->texture_transform = value;
/* ADDRESS has an independent cache; only changed states update the sampler. */
else if (state == 12 && (value == 1 || value == 3)) {
if (device->address != value) device->sampler_u = device->sampler_v = value;
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -160,7 +160,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
assert(invoke(texture, 2, 1) == 1);
const uint32_t blends[][3] = {{0, 2, 1}, {1, 5, 6}, {1, 6, 5}, {1, 1, 2}, {1, 2, 1}};
- for (unsigned legacy = 0; legacy <= 3; ++legacy)
+ for (unsigned legacy = 0; legacy <= 5; ++legacy)
for (unsigned scenario = 0; scenario < (legacy < 2 ? 40 : 4); ++scenario) {
uint32_t filter = scenario & 1 ? 2 : 1, address = scenario & 2 ? 3 : 1;
float span = scenario & 4 ? 16.25f : 1.75f;
@@ -170,6 +170,23 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
{0, 32, .25f, 2, 0xc080c0ff, -.25f, span}
};
memcpy(memory(input), vertices, sizeof(vertices));
+ const float matrices[][16] = {
+ {2,0,0,0, 0,3,0,0, 0,0,1,0, .3f,.7f,0,1},
+ {1,0,0,0, 0,1,0,0, .125f,.375f,1,0, 3,7,0,1},
+ {0,1,0,0, -1,0,0,0, 1,0,1,0, 3,7,0,1},
+ {1,.5f,0,0, .25f,1,0,0, .125f,-.25f,1,0, 3,7,0,1},
+ };
+ memcpy(memory(workspace() + 1024), matrices[scenario & 3], 64);
+ assert(!invoke(device, 11, 3, 16u, workspace() + 1024));
+ assert(!invoke(device, 37, 4, 0u, 24u, legacy == 3 || legacy == 5 ? 2u : 0u));
+ assert(!invoke(device, 36, 4, 0u, 24u, workspace()) && *memory(workspace()) == (legacy == 3 || legacy == 5 ? 2u : 0u));
+ assert(!invoke(device, 20, 3, 137u, legacy < 4 ? 1u : 0u));
+ if (legacy >= 4) for (unsigned i = 0; i < 3; ++i) {
+ float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f};
+ memcpy(memory(input + i * 24), xyz, 12);
+ *memory(input + i * 24 + 12) = vertices[i].color;
+ memcpy(memory(input + i * 24 + 16), &vertices[i].u, 8);
+ }
assert(!invoke(device, 37, 4, 0u, 16u, filter));
assert(!invoke(device, 37, 4, 0u, 17u, filter));
assert(!invoke(device, 37, 4, 0u, 18u, (scenario & 4) ? 2u : 1u));
@@ -208,7 +225,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
assert(!invoke(d3d, 7, 1));
assert(!invoke(device, 5, 1));
- assert(!invoke(device, 25, 6, 4u, 0x144u, input, 3u, 0u));
+ assert(!invoke(device, 25, 6, 4u, legacy >= 4 ? 0x142u : 0x144u, input, 3u, 0u));
assert(!invoke(device, 6, 1));
*memory(desc) = 124;
assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
@@ -227,6 +244,7 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
assert(!invoke(device, 35, 3, 0u, 0u));
assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace()));
}
+ assert(!invoke(device, 37, 4, 0u, 24u, 0u));
invoke(d3d, 2, 1);
}
@@ -899,33 +917,35 @@ static void light_definitions_and_enable_state_are_owned(uint32_t device)
assert(invoke(device, 19, 3, 99u, output) == 0x80070057u);
}
-static void transforms_copy_world_view_and_projection(uint32_t device)
+static void transforms_copy_geometry_and_texture_matrices(uint32_t device)
{
const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
const float matrices[][16] = {
{2,0,0,0, 0,3,0,0, 0,0,4,0, 5,6,7,1},
{0,1,0,0, -1,0,0,0, 0,0,1,0, -2,3,-4,1},
{1.5f,0,0,0, 0,2,0,0, 0,0,1,1, 0,0,-.5f,0},
+ {2,0,0,0, 0,3,0,0, .25f,.5f,1,0, 0,0,0,1},
};
+ const unsigned states[] = {1, 2, 3, 16};
uint32_t input = workspace() + 512, output = workspace() + 768;
- for (unsigned state = 1; state <= 3; ++state) {
- assert(!invoke(device, 12, 3, state, output));
+ for (unsigned index = 0; index < 4; ++index) {
+ assert(!invoke(device, 12, 3, states[index], output));
assert(!memcmp(memory(output), identity, 64));
}
- for (unsigned state = 1; state <= 3; ++state) {
- memcpy(memory(input), matrices[state - 1], 64);
- assert(!invoke(device, 11, 3, state, input));
+ for (unsigned index = 0; index < 4; ++index) {
+ memcpy(memory(input), matrices[index], 64);
+ assert(!invoke(device, 11, 3, states[index], input));
memset(memory(input), 0xa5, 64);
}
- for (unsigned state = 1; state <= 3; ++state) {
+ for (unsigned index = 0; index < 4; ++index) {
memset(memory(output), 0xcc, 68);
- assert(!invoke(device, 12, 3, state, output));
- assert(!memcmp(memory(output), matrices[state - 1], 64));
+ assert(!invoke(device, 12, 3, states[index], output));
+ assert(!memcmp(memory(output), matrices[index], 64));
assert(*memory(output + 64) == 0xcccccccc);
memcpy(memory(input), identity, 64);
- assert(!invoke(device, 11, 3, state, input));
+ assert(!invoke(device, 11, 3, states[index], input));
}
}
@@ -967,7 +987,7 @@ static void device_lifetime_and_rendering(const char *pixels_path)
uint32_t device = *memory(workspace());
initial_device_states(device);
material_state_is_copied(device);
- transforms_copy_world_view_and_projection(device);
+ transforms_copy_geometry_and_texture_matrices(device);
light_definitions_and_enable_state_are_owned(device);
uint32_t identity = query(device, unknown);
diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py
@@ -25,8 +25,8 @@ actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_wor
assert actual == expected, (expected, actual)
windows_pixels = (work / "reference.rgb").read_bytes()
linux_pixels = (work / "native.rgb").read_bytes()
-assert len(windows_pixels) == len(linux_pixels) == 1775616
+assert len(windows_pixels) == len(linux_pixels) == 1873920
error = max(abs(a - b) for a, b in zip(windows_pixels, linux_pixels))
assert error <= 1, error
print("Windows == Linux: " + actual.strip())
-print("All 591872 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 624640 pixels agree within one 8-bit channel step; maximum error:", error)