commit 9507c764c0d10de210e48b9bb9d716dc77673f56
parent 40d907db11f502912f9d42c2b25233019d8a9168
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 15:08:32 +0000
Apply native polygon depth bias with Windows comparisons
Diffstat:
5 files changed, 89 insertions(+), 6 deletions(-)
diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c
@@ -13,6 +13,7 @@ static GLint texture_color_arg2[2], texture_alpha_arg2[2];
static GLint fog_enabled, fog_color;
static int depth_write = 1;
static uint32_t depth_function = 4;
+static uint32_t depth_bias;
static uint32_t cull_mode = 1;
struct gl_depth {
unsigned width, height, references;
@@ -307,6 +308,11 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices,
else glDisable(GL_DEPTH_TEST);
glDepthFunc(GL_NEVER + depth_function - 1);
glDepthMask(depth_write ? GL_TRUE : GL_FALSE);
+ if (depth_bias) {
+ glEnable(GL_POLYGON_OFFSET_FILL);
+ /* D3D7 bias scale, checked on flat/sloped 16/24-bit depth surfaces. */
+ glPolygonOffset(-(float)depth_bias * .25f, -(float)depth_bias * .5f);
+ } else glDisable(GL_POLYGON_OFFSET_FILL);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glUseProgram(program);
glBindVertexArray(vertex_array);
@@ -409,10 +415,11 @@ void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *stat
check_graphics("texture binding");
}
-void gl_renderer_set_depth(int write, uint32_t function)
+void gl_renderer_set_depth(int write, uint32_t function, uint32_t bias)
{
depth_write = write;
depth_function = function;
+ depth_bias = bias;
}
void gl_renderer_set_cull(uint32_t mode)
diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h
@@ -39,7 +39,7 @@ void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int
void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *state);
void gl_renderer_set_fog(int enabled, uint32_t color);
void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination);
-void gl_renderer_set_depth(int write, uint32_t function);
+void gl_renderer_set_depth(int write, uint32_t function, uint32_t bias);
void gl_renderer_set_cull(uint32_t mode);
#endif
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -60,6 +60,7 @@ static struct device_object {
struct texture_stage stages[2];
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 depth_bias;
uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16];
int scene;
struct draw_object *owner;
@@ -1538,7 +1539,8 @@ static uint32_t device_state(struct device_object *device, uint32_t state)
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 35: case 47: case 48: return 0; /* Fog/depth bias. */
+ case 35: case 48: return 0; /* Table/range fog. */
+ case 47: return device->depth_bias;
case 36: return device->fog_start;
case 37: return device->fog_end;
case 38: return device->fog_density;
@@ -1643,6 +1645,7 @@ static void device_set_state(void)
else if (state == 38) device->fog_density = value;
else if (state == 14 && value <= 1) device->depth_write = value;
else if (state == 23 && value >= 1 && value <= 8) device->depth_function = value;
+ else if (state == 47 && value <= 16) device->depth_bias = value;
else if (state == 136 && value <= 1) device->clipping = value;
else if (state == 137 && value <= 1) device->lighting = value;
else if (state == 139) device->ambient = value;
@@ -1837,7 +1840,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
}
gl_renderer_set_fog(device->fog_enabled, device->fog_color);
gl_renderer_set_blend(device->blend, device->source_blend, device->destination_blend);
- gl_renderer_set_depth(device->depth_write, device->depth_function);
+ gl_renderer_set_depth(device->depth_write, device->depth_function, device->depth_bias);
gl_renderer_set_cull(device->cull);
const int32_t clip[] = {device->viewport[0], device->viewport[1], device->viewport[2], device->viewport[3]};
gl_target_draw(device->target->target, vertices, count, indices, index_count, clip);
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -1002,6 +1002,58 @@ static void transforms_copy_geometry_and_texture_matrices(uint32_t device)
}
}
+static void depth_bias_offsets_overlapping_geometry(uint32_t device, uint32_t target, unsigned bits, FILE *file)
+{
+ uint32_t desc = workspace() + 128, input = workspace() + 512;
+ assert(!invoke(device, 35, 3, 0u, 0u));
+ assert(!invoke(device, 20, 3, 137u, 0u));
+ assert(!invoke(device, 20, 3, 22u, 1u));
+ assert(!invoke(device, 20, 3, 14u, 1u));
+ assert(!invoke(device, 20, 3, 23u, 2u));
+ const uint32_t biases[] = {0, 1, 2, 16};
+ for (unsigned slope = 0; slope < 5; ++slope)
+ for (unsigned bias = 0; bias < 4; ++bias)
+ for (unsigned distance = 0; distance < 12; ++distance) {
+ struct vertex { float x, y, z, rhw; uint32_t color; } vertices[] = {
+ {0,0,.5f,1,0xffff0000}, {32,0,.5f,1,0xffff0000},
+ {0,32,.5f,1,0xffff0000},
+ };
+ const float depths[] = {.5f, .125f, .875f, .5f, .5f};
+ const float slopes[] = {0, 0, 0, .03125f, -.25f};
+ for (unsigned i = 0; i < 3; ++i) vertices[i].z = depths[slope] + (i == 1 ? slopes[slope] : 0);
+ assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
+ assert(!invoke(device, 20, 3, 47u, 0u));
+ memcpy(memory(input), vertices, sizeof(vertices));
+ assert(!invoke(device, 5, 1));
+ assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
+ const float offsets[] = {0, .5f, 1, 1.5f, 2, 8, 16, 32, 128, 256, 512, 1024};
+ for (unsigned i = 0; i < 3; ++i) {
+ vertices[i].z += offsets[distance] / (bits == 16 ? 65535 : 16777215);
+ vertices[i].color = 0xff0000ff;
+ }
+ memcpy(memory(input), vertices, sizeof(vertices));
+
+ assert(!invoke(device, 20, 3, 47u, biases[bias]));
+ assert(!invoke(device, 25, 6, 4u, 0x44u, input, 3u, 0u));
+ assert(!invoke(device, 6, 1));
+
+ assert(!invoke(device, 21, 3, 47u, workspace()) && *memory(workspace()) == biases[bias]);
+ *memory(desc) = 124;
+ assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u));
+ unsigned char rgb[32 * 32 * 3];
+ for (unsigned y = 0; y < 32; ++y) for (unsigned x = 0; x < 32; ++x) {
+ uint32_t pixel = *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4);
+ rgb[(y * 32 + x) * 3] = pixel >> 16;
+ rgb[(y * 32 + x) * 3 + 1] = pixel >> 8;
+ rgb[(y * 32 + x) * 3 + 2] = pixel;
+ }
+ assert(!invoke(target, 32, 2, 0u));
+ assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb));
+ }
+ assert(!invoke(device, 20, 3, 47u, 0u));
+ assert(!invoke(device, 20, 3, 23u, 4u));
+}
+
static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
{
uint32_t desc = workspace() + 128, input = workspace() + 512, textures[2];
@@ -1224,6 +1276,27 @@ static void device_lifetime_and_rendering(const char *pixels_path)
transformed_render_states_preserve_rasterization(device, color);
viewport_limits_clearing_and_drawing(device, color, file);
untransformed_vertices_apply_camera_and_lighting(device, color, file);
+ depth_bias_offsets_overlapping_geometry(device, color, 16, file);
+ assert(!invoke(color, 8, 3, 0u, depth));
+ memset(memory(desc), 0, 124);
+ *memory(desc) = 124; *memory(desc + 4) = 0x1007;
+ *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x24000;
+ const uint32_t depth24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0};
+ memcpy(memory(desc + 72), depth24, 32);
+ assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
+ depth = *memory(workspace());
+ assert(!invoke(color, 3, 2, depth));
+ assert(invoke(depth, 2, 1) == 1);
+ /* A new device selects the newly attached depth buffer on both platforms. */
+ assert(invoke(color, 1, 1) == 2);
+ assert(!invoke(device, 2, 1));
+ d3d = query(draw, d3d_iid);
+ memcpy(memory(workspace() + 16), hal_iid, 16);
+ assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace()));
+ device = *memory(workspace());
+ assert(invoke(color, 2, 1) == 1);
+ assert(invoke(d3d, 2, 1) > 0);
+ depth_bias_offsets_overlapping_geometry(device, color, 24, file);
two_texture_stages_combine_independent_samples(draw, device, color, file);
assert(!fclose(file));
assert(!invoke(device, 2, 1));
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) == 2709504
+assert len(windows_pixels) == len(linux_pixels) == 4184064
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 903168 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 1394688 pixels agree within one 8-bit channel step; maximum error:", error)