commit 892bed9e0ee9cf1009b4d298a915315361045f1e
parent d1239f7976cac42c39a7c590379f849d90958f88
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 13:38:51 +0000
Implement native camera matrices and device light state
Diffstat:
2 files changed, 138 insertions(+), 1 deletion(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -44,17 +44,22 @@ static struct surface_object {
struct surface_data *data;
struct surface_object *next;
} *surfaces;
+struct device_light {
+ uint32_t index, enabled, data[26];
+ struct device_light *next;
+};
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 alpha_arg2;
uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting;
- uint32_t material[17], viewport[6];
+ uint32_t material[17], viewport[6], transforms[3][16];
int scene;
struct draw_object *owner;
struct surface_object *target;
struct surface_object *texture;
+ struct device_light *lights;
struct device_object *next;
} *devices;
static struct vertex_buffer {
@@ -1336,6 +1341,8 @@ static void create_device(void)
device->depth_function = 4;
device->clipping = 1;
device->lighting = 1;
+ for (unsigned matrix = 0; matrix < 3; ++matrix)
+ for (unsigned i = 0; i < 16; i += 5) device->transforms[matrix][i] = 0x3f800000;
device->owner = owner;
device->target = surface;
device->viewport[2] = surface->width;
@@ -1385,6 +1392,11 @@ static void device_release_reference(void)
if (device->texture) release_surface(device->texture, 0);
release_draw(device->owner, 1);
guest_free(device->interface);
+ while (device->lights) {
+ struct device_light *light = device->lights;
+ device->lights = light->next;
+ free(light);
+ }
free(device);
}
g_esp += 8;
@@ -1545,6 +1557,53 @@ static uint32_t device_state(struct device_object *device, uint32_t state)
}
}
+static void device_light_state(void)
+{
+ struct device_object *device = find_device(MEM32(g_esp + 4));
+ uint32_t index = MEM32(g_esp + 8), data = MEM32(g_esp + 12);
+ g_esp += 16;
+ g_eax = 0x80070057u;
+ if (current_slot != 44 && !data) return;
+ if (current_slot == 18) {
+ uint32_t type = MEM32(data);
+ if (type < 1 || type > 3) return;
+ if (type != 1 && !MEMF(data + 64) && !MEMF(data + 68) && !MEMF(data + 72)) return;
+ }
+ struct device_light *light = device->lights;
+ while (light && light->index != index) light = light->next;
+ if (!light) {
+ if (current_slot == 19 || current_slot == 45) return;
+ light = calloc(1, sizeof(*light));
+ if (!light) abort();
+ light->index = index;
+ /* LightEnable creates a white directional light pointing along +Z. */
+ light->data[0] = 3;
+ light->data[1] = light->data[2] = light->data[3] = light->data[18] = 0x3f800000;
+ light->next = device->lights;
+ device->lights = light;
+ }
+ if (current_slot == 18) memcpy(light->data, (void *)ADDR(data), 104);
+ else if (current_slot == 19) memcpy((void *)ADDR(data), light->data, 104);
+ else if (current_slot == 44) light->enabled = data ? 128 : 0;
+ else MEM32(data) = light->enabled;
+ g_eax = 0;
+}
+
+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) {
+ 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);
+ g_eax = 0;
+}
+
static void device_get_state(void)
{
struct device_object *device = find_device(MEM32(g_esp + 4));
@@ -1942,9 +2001,11 @@ recomp_func_t linux_graphics_virtual(uint32_t address)
if (current_slot == 7) return device_parent;
if (current_slot == 9) return device_get_target;
if (current_slot == 10) return device_clear;
+ if (current_slot == 11 || current_slot == 12) return device_transform;
if (current_slot == 13) return device_set_viewport;
if (current_slot == 15) return device_viewport;
if (current_slot == 16 || current_slot == 17) return device_material;
+ if (current_slot == 18 || current_slot == 19 || current_slot == 44 || current_slot == 45) return device_light_state;
if (current_slot == 20) return device_set_state;
if (current_slot == 21) return device_get_state;
if (current_slot == 25) return device_draw_primitive;
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -666,6 +666,80 @@ static void vertex_buffers_preserve_data_and_description(uint32_t d3d)
}
}
+static void light_definitions_and_enable_state_are_owned(uint32_t device)
+{
+ uint32_t input = workspace() + 512, output = workspace() + 768;
+ const uint32_t default_light[] = {3, 0x3f800000, 0x3f800000, 0x3f800000,
+ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0x3f800000, 0,0,0,0,0,0,0};
+ memset(memory(output), 0xa5, 108);
+ assert(invoke(device, 19, 3, 12345u, output) == 0x80070057u);
+ assert(invoke(device, 45, 3, 12345u, output) == 0x80070057u);
+ assert(*memory(output) == 0xa5a5a5a5);
+
+ assert(!invoke(device, 44, 3, 12345u, 0u));
+ assert(!invoke(device, 19, 3, 12345u, output));
+ assert(!memcmp(memory(output), default_light, 104));
+ assert(!invoke(device, 45, 3, 12345u, output) && !*memory(output));
+ assert(!invoke(device, 44, 3, 12345u, 17u));
+ assert(!invoke(device, 45, 3, 12345u, output) && *memory(output) == 128);
+ for (unsigned type = 1; type <= 3; ++type) {
+ memcpy(memory(input), default_light, 104);
+ *memory(input) = type;
+ *memory(input + 64) = 0x3f800000; /* Unnormalized direction is retained. */
+ *memory(input + 76) = 0x42c80000; /* Range 100. */
+ *memory(input + 84) = 0x3f800000; /* Constant attenuation 1. */
+ *memory(input + 96) = 0x3f000000; /* Theta 0.5. */
+ *memory(input + 100) = 0x3f800000; /* Phi 1. */
+ assert(!invoke(device, 18, 3, type, input));
+ memset(memory(output), 0xcc, 108);
+
+ assert(!invoke(device, 19, 3, type, output));
+ assert(!memcmp(memory(output), memory(input), 104));
+ assert(*memory(output + 104) == 0xcccccccc);
+ assert(!invoke(device, 45, 3, type, output) && !*memory(output));
+ assert(!invoke(device, 44, 3, type, 1u));
+ assert(!invoke(device, 18, 3, type, input));
+ memset(memory(input), 0, 104);
+ assert(!invoke(device, 45, 3, type, output) && *memory(output) == 128);
+ assert(!invoke(device, 44, 3, type, 0u));
+ }
+ assert(!invoke(device, 44, 3, 12345u, 0u));
+ assert(invoke(device, 18, 3, 99u, input) == 0x80070057u);
+ *memory(input) = 3;
+ assert(invoke(device, 18, 3, 99u, input) == 0x80070057u);
+ assert(invoke(device, 19, 3, 99u, output) == 0x80070057u);
+}
+
+static void transforms_copy_world_view_and_projection(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},
+ };
+ uint32_t input = workspace() + 512, output = workspace() + 768;
+ for (unsigned state = 1; state <= 3; ++state) {
+ assert(!invoke(device, 12, 3, state, 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));
+ memset(memory(input), 0xa5, 64);
+ }
+
+ for (unsigned state = 1; state <= 3; ++state) {
+ memset(memory(output), 0xcc, 68);
+ assert(!invoke(device, 12, 3, state, output));
+ assert(!memcmp(memory(output), matrices[state - 1], 64));
+ assert(*memory(output + 64) == 0xcccccccc);
+ memcpy(memory(input), identity, 64);
+ assert(!invoke(device, 11, 3, state, input));
+ }
+}
+
static void device_lifetime_and_rendering(const char *pixels_path)
{
const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
@@ -704,6 +778,8 @@ 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);
+ light_definitions_and_enable_state_are_owned(device);
uint32_t identity = query(device, unknown);
assert(!invoke(device, 9, 2, workspace()));