commit 9a98fa1ec142195caec93d6d11b85776ada44c93
parent 55005cffeb7eb2bb25081dc2afd9a77e68c67365
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 05:32:59 +0000
Implement native specular lighting with Windows differential coverage
Diffstat:
5 files changed, 71 insertions(+), 13 deletions(-)
diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c
@@ -62,10 +62,12 @@ int gl_renderer_initialize(void)
"layout(location=2) in vec2 texcoord;\n"
"layout(location=3) in float fog;\n"
"layout(location=4) in vec2 texcoord_second;\n"
+ "layout(location=5) in vec3 specular; out vec3 highlight;\n"
"out vec4 color; out vec2 uv, uv_second; out float fog_factor;\n"
- "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; uv=texcoord; uv_second=texcoord_second; fog_factor=fog; }\n",
+ "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; uv=texcoord; uv_second=texcoord_second; fog_factor=fog; highlight=specular; }\n",
"#version 330 core\n"
"in vec4 color; in vec2 uv, uv_second; in float fog_factor; out vec4 pixel;\n"
+ "in vec3 highlight;\n"
"uniform bool fog_enabled; uniform vec3 fog_color;\n"
"uniform sampler2D image0, image1;\n"
"uniform bool textured[2], has_alpha[2]; uniform int color_op[2], alpha_op[2], color_arg2[2], alpha_arg2[2];\n"
@@ -80,6 +82,7 @@ int gl_renderer_initialize(void)
"if(alpha_op[s]!=1) pixel.a=alpha_op[s]==3 ? a2 : alpha_op[s]==4 ? a1*a2 : a1; }\n"
"void main() { pixel=color; if(textured[0]) { combine(0,texture(image0,uv));"
"if(textured[1]) combine(1,texture(image1,uv_second)); }"
+ "pixel.rgb=min(vec3(1.0),pixel.rgb+highlight);"
"if(fog_enabled) pixel.rgb=mix(fog_color,pixel.rgb,clamp(fog_factor,0.0,1.0)); }\n"
};
program = glCreateProgram();
@@ -142,6 +145,8 @@ int gl_renderer_initialize(void)
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, fog));
glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, uv_second));
glEnableVertexAttribArray(4);
+ glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, specular));
+ glEnableVertexAttribArray(5);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h
@@ -11,6 +11,7 @@ struct gl_vertex {
float uv[2];
float fog;
float uv_second[2];
+ float specular[3];
};
struct gl_texture_state {
struct gl_target *image;
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -62,6 +62,8 @@ static struct device_object {
uint32_t blend, source_blend, destination_blend, depth_write, depth_function, clipping, lighting;
uint32_t depth_bias;
uint32_t texture_factor;
+ uint32_t specular;
+ float specular_power;
uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16];
int scene;
struct draw_object *owner;
@@ -1502,8 +1504,13 @@ static void device_material(void)
g_esp += 12;
g_eax = 0x80070057u;
if (!address) return;
- /* XYZRHW vertices are already lit. XYZ drawing remains unsupported. */
- if (current_slot == 16) memcpy(device->material, (void *)ADDR(address), sizeof(device->material));
+ /* Material colors and power are copied into device-owned storage. */
+ if (current_slot == 16) {
+ memcpy(device->material, (void *)ADDR(address), sizeof(device->material));
+ float power;
+ memcpy(&power, device->material + 16, 4);
+ if (power) device->specular_power = power;
+ }
else memcpy((void *)ADDR(address), device->material, sizeof(device->material));
g_eax = 0;
}
@@ -1541,7 +1548,8 @@ static uint32_t device_state(struct device_object *device, uint32_t state)
/* Inactive legacy states retain the values returned by Direct3D7. */
case 2: case 40: return UINT32_MAX; /* Retired antialias state sentinels. */
case 4: case 16: return 1; /* Perspective textures and last line pixel. */
- case 10: case 15: case 24: case 29: case 30: case 33: return 0;
+ case 10: case 15: case 24: case 30: case 33: return 0;
+ case 29: return device->specular;
case 35: case 48: return 0; /* Table/range fog. */
case 47: return device->depth_bias;
case 36: return device->fog_start;
@@ -1643,6 +1651,7 @@ static void device_set_state(void)
if (state == 22 && value >= 1 && value <= 3) device->cull = value;
else if (state == 27 && value <= 1) device->blend = value;
else if (state == 28 && value <= 1) device->fog_enabled = value;
+ else if (state == 29 && value <= 1) device->specular = value;
else if (state == 34) device->fog_color = value;
else if (state == 36) device->fog_start = value;
else if (state == 37) device->fog_end = value;
@@ -1679,7 +1688,7 @@ 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], const float diffuse_color[4], 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], float specular[3])
{
const float *material = (const float *)device->material;
for (unsigned j = 0; j < 3; ++j)
@@ -1715,8 +1724,18 @@ 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 * (diffuse_color[j] * data[1 + j] * diffuse + material[4 + j] * data[9 + j]);
+ if (device->specular && diffuse > 0) {
+ float viewer[] = {-position[0], -position[1], -position[2]};
+ normalize_vector(viewer);
+ for (unsigned j = 0; j < 3; ++j) viewer[j] += direction[j];
+ normalize_vector(viewer);
+ float reflection = fminf(1, fmaxf(0, normal[0] * viewer[0] + normal[1] * viewer[1] + normal[2] * viewer[2]));
+ float highlight = powf(reflection, device->specular_power) * attenuation;
+ for (unsigned j = 0; j < 3; ++j) specular[j] += material[8 + j] * data[5 + j] * highlight;
+ }
}
for (unsigned j = 0; j < 4; ++j) color[j] = fminf(1, fmaxf(0, color[j]));
+ for (unsigned j = 0; j < 3; ++j) specular[j] = fminf(1, fmaxf(0, specular[j]));
}
static void draw_primitive(struct device_object *device, uint32_t primitive, uint32_t fvf,
@@ -1754,10 +1773,11 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
if (!determinant) { fprintf(stderr, "Unsupported singular world/view transform\n"); exit(3); }
for (unsigned i = 0; i < 9; ++i) normal_matrix[i] = cofactors[i] / determinant;
}
- /* Specular input is inactive while D3DRENDERSTATE_SPECULARENABLE is false. */
+ /* Pretransformed vertices carry diffuse and optional specular colors. */
uint32_t uv_offset = 20 + (fvf & 0x80 ? 4 : 0), stride = uv_offset + texture_count * 8;
if (untransformed) stride = 12 + (fvf & 0x10 ? 12 : 0) + (fvf & 0x40 ? 4 : 0) + texture_count * 8;
for (uint32_t i = 0; i < count; ++i) {
+ memset(vertices[i].specular, 0, sizeof(vertices[i].specular));
if (untransformed) {
float input_vertex[11], position[4], camera[4], projected[4], normal[3], diffuse[4];
memcpy(input_vertex, (void *)ADDR(input + i * stride), stride);
@@ -1781,7 +1801,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
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);
+ if (device->lighting) light_vertex(device, camera, normal, diffuse, vertices[i].color, vertices[i].specular);
else memcpy(vertices[i].color, diffuse, 16);
vertices[i].fog = 0;
if (device->fog_vertex_mode == 3) {
@@ -1818,6 +1838,10 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
vertices[i].color[2] = (color & 255) / 255.0f;
vertices[i].color[3] = (color >> 24) / 255.0f;
vertices[i].fog = fvf & 0x80 ? (MEM32(input + i * stride + 20) >> 24) / 255.0f : 0;
+ if (device->specular && (fvf & 0x80)) {
+ uint32_t specular = MEM32(input + i * stride + 20);
+ for (unsigned j = 0; j < 3; ++j) vertices[i].specular[j] = ((specular >> (16 - j * 8)) & 255) / 255.0f;
+ }
for (unsigned stage = 0; stage < 2; ++stage) {
float *uv = stage ? vertices[i].uv_second : vertices[i].uv;
unsigned coordinate = device->stages[stage].texture_coordinate;
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -389,7 +389,10 @@ static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target,
assert(!invoke(device, 21, 3, 36u, workspace()) && *memory(workspace()) == 0x3e4ccccd);
assert(!invoke(device, 21, 3, 37u, workspace()) && *memory(workspace()) == 0x3f4ccccd);
assert(!invoke(device, 21, 3, 38u, workspace()) && *memory(workspace()) == 0x3f000000);
+ for (unsigned specular = 0; specular < 2; ++specular)
for (unsigned mode = 0; mode <= 3; mode += 3) for (unsigned sample = 0; sample < 7; ++sample) {
+ assert(!invoke(device, 20, 3, 29u, specular));
+ assert(!invoke(device, 21, 3, 29u, workspace()) && *memory(workspace()) == specular);
assert(!invoke(device, 20, 3, 140u, mode));
assert(!invoke(device, 21, 3, 140u, workspace()) && *memory(workspace()) == mode);
struct { float xyzw[4]; uint32_t diffuse, specular; } vertices[] = {
@@ -397,6 +400,11 @@ static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target,
{{28, 4, .25f, 1}, 0xff6080a0, (factors[sample][1] << 24) | 0xabcdef},
{{4, 28, .25f, 1}, 0xff6080a0, (factors[sample][2] << 24) | 0xabcdef},
};
+ if (sample == 4) {
+ vertices[0].specular = (vertices[0].specular & 0xff000000) | 0x102030;
+ vertices[1].specular = (vertices[1].specular & 0xff000000) | 0x406080;
+ vertices[2].specular = (vertices[2].specular & 0xff000000) | 0x80c0ff;
+ }
if (sample >= 4) { vertices[1].xyzw[3] = 2; vertices[2].xyzw[3] = .5f; }
if (sample == 6) {
for (unsigned i = 0; i < 3; ++i) memcpy(memory(input + i * 20), &vertices[i], 20);
@@ -412,7 +420,7 @@ static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target,
uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
if (sample == 0 || sample == 6) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0xc02040);
- if (sample == 2 || sample == 5) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == 0x6080a0);
+ if (sample == 2 || sample == 5) assert((*memory(pixels + 8 * pitch + 32) & 0xffffff) == (specular ? 0xffffffu : 0x6080a0u));
unsigned char rgb[32 * 32 * 3];
for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
uint32_t pixel = *memory(pixels + y * pitch + x * 4);
@@ -425,6 +433,7 @@ static void transformed_vertex_fog_blends_rgb(uint32_t device, uint32_t target,
}
assert(!invoke(device, 20, 3, 28u, 0u));
assert(!invoke(device, 20, 3, 140u, 0u));
+ assert(!invoke(device, 20, 3, 29u, 0u));
}
static void vertex_buffer_draws_match_memory_draws(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
@@ -556,6 +565,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 specular = 0; specular < 5; ++specular)
for (unsigned colored = 0; colored <= 2; ++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}};
@@ -564,6 +574,10 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui
float projection[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
float material[] = {.4f,.6f,.8f,.5f, .5f,.25f,.75f,1, 0,0,0,0, 0,0,0,0, 0};
float light[26] = {0, .5f,.75f,1,0, 0,0,0,0, .1f,.2f,.3f,0, 0,0,-1, 0,0,1, 10,2, 1,0,0, .5f,1.5f};
+ material[8] = .4f; material[9] = .5f; material[10] = .6f;
+ material[16] = specular == 2 || specular == 4 ? 0 : specular == 3 ? 2 : 8;
+ light[5] = .5f; light[6] = .6f; light[7] = .7f;
+ assert(!invoke(device, 20, 3, 29u, specular ? 1u : 0u));
uint32_t type = sample >= 9 && sample <= 10 ? 1 : sample >= 11 && sample <= 12 ? 2 : 3;
if (sample == 1) { material[12] = .2f; material[13] = .4f; material[14] = .6f; }
if (sample == 4 || sample == 5) for (unsigned i = 0; i < 3; ++i) vertices[i][5] = -2;
@@ -580,6 +594,8 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui
memcpy(memory(data), view, 64); assert(!invoke(device, 11, 3, 2u, data));
memcpy(memory(data), projection, 64); assert(!invoke(device, 11, 3, 3u, data));
memcpy(memory(data), material, 68); assert(!invoke(device, 16, 2, data));
+ assert(!invoke(device, 17, 2, data));
+ assert(!memcmp(memory(data), material, 68));
memcpy(memory(data), light, 104); *memory(data) = type;
assert(!invoke(device, 18, 3, 0u, data));
assert(!invoke(device, 44, 3, 0u, sample >= 3 ? 1u : 0u));
@@ -628,6 +644,7 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui
assert(!invoke(device, 20, 3, 28u, 0u));
assert(!invoke(device, 20, 3, 140u, 0u));
assert(!invoke(device, 20, 3, 27u, 0u));
+ assert(!invoke(device, 20, 3, 29u, 0u));
}
static void initial_device_states(uint32_t device)
@@ -1077,7 +1094,7 @@ static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32
assert(!invoke(device, 20, 3, 27u, 1u));
assert(!invoke(device, 20, 3, 19u, 5u));
assert(!invoke(device, 20, 3, 20u, 6u));
- for (unsigned sample = 0; sample < 512; ++sample) {
+ for (unsigned sample = 0; sample < 576; ++sample) {
struct vertex { float x, y, z, rhw; uint32_t color; float u, v, u1, v1; } vertices[] = {
{0,0,.25f,1,0x4080c0a0, -.25f,.125f, .875f,-.375f},
{32,0,.25f,.5f,0x8080c0a0, 1.75f,.125f, -.125f,-.375f},
@@ -1095,11 +1112,22 @@ static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32
assert(!invoke(device, 20, 3, 60u, factor));
assert(!invoke(device, 21, 3, 60u, workspace()) && *memory(workspace()) == factor);
memcpy(memory(input), vertices, sizeof(vertices));
- if (sample & 32) for (unsigned i = 0; i < 3; ++i) {
+ if ((sample & 32) && sample < 512) for (unsigned i = 0; i < 3; ++i) {
float xyz[] = {vertices[i].x / 16 - 1, 1 - vertices[i].y / 16, .25f};
memcpy(memory(input + i * 32), xyz, 12);
memcpy(memory(input + i * 32 + 12), &vertices[i].color, 20);
}
+ assert(!invoke(device, 20, 3, 29u, sample >= 512 ? 1u : 0u));
+ if (sample >= 512) {
+ const uint32_t specular[] = {0x00102040, 0x80307050, 0xff602030};
+ for (unsigned i = 0; i < 3; ++i) {
+ memcpy(memory(input + i * 40), &vertices[i], 20);
+ *memory(input + i * 40 + 20) = specular[i];
+ memcpy(memory(input + i * 40 + 24), &vertices[i].u, 16);
+ }
+ assert(!invoke(device, 20, 3, 28u, sample & 4 ? 1u : 0u));
+ assert(!invoke(device, 20, 3, 34u, 0x00406080u));
+ }
const uint32_t arguments[] = {0, 1, 2, 0, 3, 2, 2};
unsigned first_argument = sample >= 448 ? (sample >> 2) & 3 : arguments[sample / 64];
for (unsigned stage = 0; stage < 2; ++stage) {
@@ -1134,7 +1162,7 @@ static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32
*memory(workspace() + 4) = 0xcccccccc;
assert(!invoke(device, 38, 2, workspace()));
assert(*memory(workspace()) == 1 && *memory(workspace() + 4) == 0xcccccccc);
- assert(!invoke(device, 25, 6, 4u, sample & 32 ? 0x242u : 0x244u, input, 3u, 0u));
+ assert(!invoke(device, 25, 6, 4u, sample >= 512 ? 0x2c4u : sample & 32 ? 0x242u : 0x244u, input, 3u, 0u));
assert(!invoke(device, 6, 1));
*memory(desc) = 124;
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) == 5560320
+assert len(windows_pixels) == len(linux_pixels) == 6426624
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 1853440 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 2142208 pixels agree within one 8-bit channel step; maximum error:", error)