commit 19f9afd25f068e00b1c8b67e29dad2f10552aa5e
parent 70f6dbe4fb0c6b6e6a45226c97051e89cffabb6b
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 14:14:07 +0000
Use scene vertex diffuse colors in native lighting
Diffstat:
3 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -1664,12 +1664,12 @@ static float normalize_vector(float vector[3])
return length;
}
-static void light_vertex(struct device_object *device, const float position[4], const float normal[3], float color[4])
+static void light_vertex(struct device_object *device, const float position[4], const float normal[3], const float diffuse_color[4], float color[4])
{
const float *material = (const float *)device->material;
for (unsigned j = 0; j < 3; ++j)
color[j] = material[12 + j] + material[4 + j] * ((device->ambient >> (16 - j * 8)) & 255) / 255.0f;
- color[3] = material[3];
+ color[3] = diffuse_color[3];
for (struct device_light *light = device->lights; light; light = light->next) {
if (!light->enabled) continue;
const float *data = (const float *)light->data;
@@ -1699,7 +1699,7 @@ static void light_vertex(struct device_object *device, const float position[4],
}
float diffuse = fmaxf(0, normal[0] * direction[0] + normal[1] * direction[1] + normal[2] * direction[2]);
for (unsigned j = 0; j < 3; ++j)
- color[j] += attenuation * (material[j] * data[1 + j] * diffuse + material[4 + j] * data[9 + j]);
+ color[j] += attenuation * (diffuse_color[j] * data[1 + j] * diffuse + material[4 + j] * data[9 + j]);
}
for (unsigned j = 0; j < 4; ++j) color[j] = fminf(1, fmaxf(0, color[j]));
}
@@ -1708,8 +1708,9 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
uint32_t input, uint32_t count, uint32_t flags,
uint32_t input_indices, uint32_t element_count)
{
+ int untransformed = (fvf & ~0x40u) == 0x112;
if ((primitive != 4 && primitive != 5) ||
- ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144 && fvf != 0x112) || flags) {
+ ((fvf & ~0x80u) != 0x44 && (fvf & ~0x80u) != 0x144 && !untransformed) || flags) {
fprintf(stderr, "Unsupported Direct3D draw primitive=%u fvf=%08x flags=%08x cull=%u\n", primitive, fvf, flags, device->cull); exit(3);
}
g_eax = 0x80070057u;
@@ -1723,7 +1724,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
if (!vertices || !indices) abort();
/* Affine world/view transforms also determine the inverse-transpose normal matrix. */
float world_view[16], normal_matrix[9];
- if (fvf == 0x112) {
+ if (untransformed) {
const float *world = (const float *)device->transforms[0], *view = (const float *)device->transforms[1];
for (unsigned i = 0; i < 4; ++i) transform_vector(world + i * 4, view, world_view + i * 4);
if (world_view[3] || world_view[7] || world_view[11] || world_view[15] != 1) {
@@ -1739,11 +1740,11 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
}
/* Specular input is inactive while D3DRENDERSTATE_SPECULARENABLE is false. */
uint32_t uv_offset = 20 + (fvf & 0x80 ? 4 : 0), stride = uv_offset + (fvf & 0x100 ? 8 : 0);
- if (fvf == 0x112) stride = 32;
+ if (untransformed) stride = fvf & 0x40 ? 36 : 32;
for (uint32_t i = 0; i < count; ++i) {
- if (fvf == 0x112) {
- float input_vertex[8], position[4], camera[4], projected[4], normal[3];
- memcpy(input_vertex, (void *)ADDR(input + i * stride), 32);
+ if (untransformed) {
+ float input_vertex[9], position[4], camera[4], projected[4], normal[3], diffuse[4];
+ memcpy(input_vertex, (void *)ADDR(input + i * stride), stride);
memcpy(position, input_vertex, 12); position[3] = 1;
transform_vector(position, world_view, camera);
transform_vector(camera, (const float *)device->transforms[2], projected);
@@ -1756,15 +1757,23 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
for (unsigned j = 0; j < 3; ++j)
normal[j] = input_vertex[3] * normal_matrix[j] + input_vertex[4] * normal_matrix[3+j] + input_vertex[5] * normal_matrix[6+j];
if (device->normalize_normals) normalize_vector(normal);
- if (device->lighting) light_vertex(device, camera, normal, vertices[i].color);
- else for (unsigned j = 0; j < 4; ++j) vertices[i].color[j] = 1;
+ if (fvf & 0x40) {
+ uint32_t color = MEM32(input + i * stride + 24);
+ diffuse[0] = ((color >> 16) & 255) / 255.0f;
+ diffuse[1] = ((color >> 8) & 255) / 255.0f;
+ diffuse[2] = (color & 255) / 255.0f;
+ diffuse[3] = (color >> 24) / 255.0f;
+ } else if (device->lighting) memcpy(diffuse, device->material, 16);
+ else for (unsigned j = 0; j < 4; ++j) diffuse[j] = 1;
+ if (device->lighting) light_vertex(device, camera, normal, diffuse, vertices[i].color);
+ else memcpy(vertices[i].color, diffuse, 16);
vertices[i].fog = 0;
if (device->fog_vertex_mode == 3) {
float start, end;
memcpy(&start, &device->fog_start, 4); memcpy(&end, &device->fog_end, 4);
vertices[i].fog = fminf(1, fmaxf(0, (end - fabsf(camera[2])) / (end - start)));
}
- memcpy(vertices[i].uv, input_vertex + 6, 8);
+ memcpy(vertices[i].uv, input_vertex + (fvf & 0x40 ? 7 : 6), 8);
continue;
}
float position[4];
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -486,7 +486,7 @@ static void indexed_vertex_buffers_use_relative_indices(uint32_t draw, uint32_t
static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, uint32_t target, FILE *file)
{
- for (unsigned sample = 0; sample < 17; ++sample) {
+ for (unsigned colored = 0; colored <= 1; ++colored) for (unsigned sample = 0; sample < 17; ++sample) {
uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128;
float vertices[3][8] = {{-.75f,.75f,.5f,0,0,-1,0,0}, {.75f,.75f,.5f,0,0,-1,1,0}, {-.75f,-.75f,.5f,0,0,-1,0,1}};
float world[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
@@ -524,17 +524,21 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui
assert(!invoke(device, 20, 3, 27u, sample == 16 ? 1u : 0u));
assert(!invoke(device, 20, 3, 19u, 5u));
assert(!invoke(device, 20, 3, 20u, 6u));
- memcpy(memory(input), vertices, sizeof(vertices));
+ if (colored) for (unsigned i = 0; i < 3; ++i) {
+ memcpy(memory(input + i * 36), vertices[i], 24);
+ *memory(input + i * 36 + 24) = sample == 6 ? (0xff204060u + i * 0x203020u) : 0x804060a0u;
+ memcpy(memory(input + i * 36 + 28), vertices[i] + 6, 8);
+ } else memcpy(memory(input), vertices, sizeof(vertices));
assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
assert(!invoke(device, 5, 1));
- assert(!invoke(device, 25, 6, 4u, 0x112u, input, 3u, 0u));
+ assert(!invoke(device, 25, 6, 4u, colored ? 0x152u : 0x112u, input, 3u, 0u));
assert(!invoke(device, 6, 1));
*memory(desc) = 124;
assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
- if (sample == 0) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0xffffff);
+ if (sample == 0) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (colored ? 0x4060a0 : 0xffffff));
if (sample == 1) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0x336699);
unsigned char rgb[32 * 32 * 3];
for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
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) == 1671168
+assert len(windows_pixels) == len(linux_pixels) == 1723392
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 557056 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 574464 pixels agree within one 8-bit channel step; maximum error:", error)