commit ec64577b6247a42bf96d2d38d64bf7bddeead72e
parent 02b1790402add209e50fc36c2ac19a7cfaf3585a
Author: anon <anon@example.com>
Date: Fri, 16 Oct 2026 06:57:38 +0000
Implement alpha testing and verify cutoff and depth behavior
Diffstat:
5 files changed, 124 insertions(+), 5 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 texture_color_arg1[2], texture_alpha_arg1[2];
static GLint texture_factor;
static GLint fog_enabled, fog_color;
+static GLint alpha_function, alpha_reference;
static int depth_write = 1;
static uint32_t depth_function = 4;
static uint32_t depth_bias;
@@ -73,6 +74,7 @@ int gl_renderer_initialize(void)
"uniform bool textured[2], has_alpha[2]; uniform int color_op[2], alpha_op[2], color_arg2[2], alpha_arg2[2];\n"
"uniform int color_arg1[2], alpha_arg1[2];\n"
"uniform vec4 texture_factor;\n"
+ "uniform int alpha_function; uniform float alpha_reference;\n"
"vec4 argument(int a, vec4 t) { int source=a&15;"
"vec4 value=source==0 ? color : source==1 ? pixel : source==2 ? t : texture_factor;"
"return (a&32)!=0 ? vec4(value.a) : value; }\n"
@@ -85,7 +87,13 @@ int gl_renderer_initialize(void)
"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"
+ "if(fog_enabled) pixel.rgb=mix(fog_color,pixel.rgb,clamp(fog_factor,0.0,1.0));"
+ /* Compare quantized eight-bit alpha; exact halfway values round down. */
+ "float alpha=ceil(clamp(pixel.a,0.0,1.0)*255.0-0.5);"
+ "bool pass=alpha_function==1 ? false : alpha_function==2 ? alpha<alpha_reference :"
+ "alpha_function==3 ? alpha==alpha_reference : alpha_function==4 ? alpha<=alpha_reference :"
+ "alpha_function==5 ? alpha>alpha_reference : alpha_function==6 ? alpha!=alpha_reference :"
+ "alpha_function==7 ? alpha>=alpha_reference : true; if(!pass) discard; }\n"
};
program = glCreateProgram();
for (unsigned i = 0; i < 2; ++i) {
@@ -129,6 +137,8 @@ int gl_renderer_initialize(void)
fog_enabled = glGetUniformLocation(program, "fog_enabled");
fog_color = glGetUniformLocation(program, "fog_color");
texture_factor = glGetUniformLocation(program, "texture_factor");
+ alpha_function = glGetUniformLocation(program, "alpha_function");
+ alpha_reference = glGetUniformLocation(program, "alpha_reference");
glUseProgram(program);
glUniform1i(glGetUniformLocation(program, "image0"), 0);
glUniform1i(glGetUniformLocation(program, "image1"), 1);
@@ -481,3 +491,11 @@ void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination)
} else glDisable(GL_BLEND);
check_graphics("blend state");
}
+
+void gl_renderer_set_alpha_test(int enabled, uint32_t function, uint32_t reference)
+{
+ glUseProgram(program);
+ glUniform1i(alpha_function, enabled ? function : 8);
+ glUniform1f(alpha_reference, reference);
+ check_graphics("alpha test state");
+}
diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h
@@ -41,6 +41,7 @@ void gl_target_blit(struct gl_target *source, struct gl_target *target, const in
void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height);
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_alpha_test(int enabled, uint32_t function, uint32_t reference);
void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination);
void gl_renderer_set_depth(int write, uint32_t function, uint32_t bias);
void gl_renderer_set_cull(uint32_t mode);
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -63,6 +63,7 @@ static struct device_object {
uint32_t depth_bias;
uint32_t texture_factor;
uint32_t specular;
+ uint32_t alpha_test, alpha_reference, alpha_function;
float specular_power;
uint32_t emissive_source;
uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16];
@@ -407,6 +408,7 @@ static void describe_device(uint32_t record)
MEM32(record + 68) = 0x10; /* Triangle rasterization: ZTEST */
MEM32(record + 72) = 8; /* Depth comparison: LESSEQUAL */
MEM32(record + 76) = MEM32(record + 80) = 0x37; /* ZERO, ONE, SRCCOLOR, SRCALPHA, INVSRCALPHA. */
+ MEM32(record + 84) = 0xff; /* All eight alpha comparison functions. */
MEM32(record + 88) = 8; /* Gouraud RGB interpolation */
MEM32(record + 92) = 0x45; /* Perspective, alpha, normalized repeat range. */
MEM32(record + 96) = 0x03010317; /* Point/linear filtering with nearest mip selection. */
@@ -1349,6 +1351,7 @@ static void create_device(void)
stage->color_arg1 = stage->alpha_arg1 = 2;
}
device->source_blend = 2;
+ device->alpha_function = 8;
device->destination_blend = 1;
device->depth_write = 1;
device->depth_function = 4;
@@ -1549,14 +1552,17 @@ 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 30: case 33: return 0;
+ case 10: case 30: case 33: return 0;
+ case 15: return device->alpha_test;
+ case 24: return device->alpha_reference;
+ case 25: return device->alpha_function;
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;
case 37: return device->fog_end;
case 38: return device->fog_density;
- case 25: case 56: return 8; /* ALWAYS alpha/stencil comparison. */
+ case 56: return 8; /* ALWAYS stencil comparison. */
case 41: case 52: case 57: return 0; /* Color key and stencil disabled. */
case 53: case 54: case 55: return 1; /* KEEP stencil operations. */
case 58: case 59: return UINT32_MAX; /* Stencil masks. */
@@ -1659,6 +1665,9 @@ static void device_set_state(void)
else if (state == 37) device->fog_end = value;
else if (state == 38) device->fog_density = value;
else if (state == 14 && value <= 1) device->depth_write = value;
+ else if (state == 15 && value <= 1) device->alpha_test = value;
+ else if (state == 24 && value <= 255) device->alpha_reference = value;
+ else if (state == 25 && value >= 1 && value <= 8) device->alpha_function = 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 == 60) device->texture_factor = value;
@@ -1879,6 +1888,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
} else gl_renderer_set_texture(i, NULL);
}
gl_renderer_set_fog(device->fog_enabled, device->fog_color);
+ gl_renderer_set_alpha_test(device->alpha_test, device->alpha_function, device->alpha_reference);
gl_renderer_set_blend(device->blend, device->source_blend, device->destination_blend);
gl_renderer_set_depth(device->depth_write, device->depth_function, device->depth_bias);
gl_renderer_set_cull(device->cull);
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -1196,6 +1196,94 @@ static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32
assert(!invoke(textures[1], 2, 1));
}
+static void alpha_tests_preserve_rejected_color_and_depth(uint32_t draw, uint32_t device, uint32_t target, FILE *file)
+{
+ uint32_t desc = workspace() + 128, input = workspace() + 512;
+ memset(memory(desc), 0, 124);
+ *memory(desc) = 124; *memory(desc + 4) = 0x1007;
+ *memory(desc + 8) = 1; *memory(desc + 12) = 4; *memory(desc + 104) = 0x1000;
+ const uint32_t format[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
+ memcpy(memory(desc + 72), format, 32);
+ assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
+ uint32_t texture = *memory(workspace());
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
+ const uint32_t texels[] = {0x00ffffff, 0x7fffffff, 0x80ffffff, 0xffffffff};
+ memcpy(memory(*memory(desc + 36)), texels, 16);
+ assert(!invoke(texture, 32, 2, 0u));
+ assert(!invoke(device, 20, 3, 22u, 1u));
+ assert(!invoke(device, 20, 3, 14u, 1u));
+ assert(!invoke(device, 20, 3, 23u, 4u));
+ assert(!invoke(device, 20, 3, 27u, 0u));
+ assert(!invoke(device, 20, 3, 28u, 0u));
+ assert(!invoke(device, 20, 3, 29u, 0u));
+ assert(!invoke(device, 37, 4, 1u, 1u, 1u));
+ assert(!invoke(device, 37, 4, 0u, 1u, 2u));
+ assert(!invoke(device, 37, 4, 0u, 2u, 0u));
+ assert(!invoke(device, 37, 4, 0u, 4u, 2u));
+ assert(!invoke(device, 37, 4, 0u, 11u, 0u));
+ assert(!invoke(device, 37, 4, 0u, 13u, 3u));
+ assert(!invoke(device, 37, 4, 0u, 14u, 3u));
+ const uint32_t references[] = {0,1,7,8,15,16,31,32,63,64,95,96,127,128,159,160,191,192,223,224,239,240,247,248,254,255};
+ for (unsigned mode = 0; mode < 4; ++mode) for (unsigned enabled = 0; enabled < 2; ++enabled)
+ for (unsigned function = 1; function <= 8; ++function) for (unsigned ref = 0; ref < 26; ++ref) {
+ struct vertex { float x, y, z, rhw; uint32_t color; float u, v; } vertices[] = {
+ {0,0,.25f,1,0x80ff0000,0,.5f}, {32,0,.25f,1,0x80ff0000,1,.5f},
+ {32,32,.25f,1,0x80ff0000,1,.5f}, {0,32,.25f,1,0x80ff0000,0,.5f},
+ };
+ if (mode == 1) {
+ vertices[0].color = vertices[3].color = 0x00ff0000;
+ vertices[1].color = vertices[2].color = 0xffff0000;
+ }
+ assert(!invoke(device, 35, 3, 0u, mode >= 2 ? texture : 0u));
+ assert(!invoke(device, 37, 4, 0u, 5u, mode >= 2 ? 2u : 0u));
+ assert(!invoke(device, 37, 4, 0u, 16u, mode == 3 ? 2u : 1u));
+ assert(!invoke(device, 37, 4, 0u, 17u, mode == 3 ? 2u : 1u));
+ assert(!invoke(device, 20, 3, 15u, enabled));
+ assert(!invoke(device, 20, 3, 24u, references[ref]));
+ assert(!invoke(device, 20, 3, 25u, function));
+ assert(!invoke(device, 21, 3, 15u, workspace()) && *memory(workspace()) == enabled);
+ assert(!invoke(device, 21, 3, 24u, workspace()) && *memory(workspace()) == references[ref]);
+ assert(!invoke(device, 21, 3, 25u, workspace()) && *memory(workspace()) == function);
+ assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
+
+ memcpy(memory(input), vertices, sizeof(vertices));
+ assert(!invoke(device, 5, 1));
+ assert(!invoke(device, 25, 6, 6u, 0x144u, input, 4u, 0u));
+ assert(!invoke(device, 20, 3, 15u, 0u));
+ for (unsigned i = 0; i < 4; ++i) { vertices[i].z = .75f; vertices[i].color = 0xff0000ff; }
+ memcpy(memory(input), vertices, sizeof(vertices));
+ assert(!invoke(device, 25, 6, 6u, 0x144u, input, 4u, 0u));
+ assert(!invoke(device, 6, 1));
+
+ *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);
+ if (!enabled || function == 8) assert((pixel & 0xffffff) == 0xff0000);
+ if (enabled && function == 1) assert((pixel & 0xffffff) == 0x0000ff);
+ /* Filter precision can put exact half-byte alpha on either side of a cutoff.
+ * Check either visibility result only at those 1152 boundary pixels. */
+ if (mode == 3 && enabled && function >= 2 && function <= 7 &&
+ ((x == 8 && (references[ref] == 63 || references[ref] == 64)) ||
+ (x == 16 && (references[ref] == 127 || references[ref] == 128)) ||
+ (x == 24 && (references[ref] == 191 || references[ref] == 192)))) {
+ assert((pixel & 0xffffff) == 0xff0000 || (pixel & 0xffffff) == 0x0000ff);
+ pixel = 0;
+ }
+ 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, 35, 3, 0u, 0u));
+ assert(!invoke(texture, 2, 1));
+ assert(!invoke(device, 20, 3, 24u, 0u));
+ assert(!invoke(device, 20, 3, 25u, 8u));
+}
+
static void device_lifetime_and_rendering(const char *pixels_path)
{
const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
@@ -1354,6 +1442,7 @@ static void device_lifetime_and_rendering(const char *pixels_path)
assert(invoke(color, 2, 1) == 1);
assert(invoke(d3d, 2, 1) > 0);
depth_bias_offsets_overlapping_geometry(device, color, 24, file);
+ alpha_tests_preserve_rejected_color_and_depth(draw, device, color, 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,9 @@ 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) == 13522944
+assert len(windows_pixels) == len(linux_pixels) == 18634752
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 4507648 pixels agree within one 8-bit channel step; maximum error:", error)
+print("6210432 pixels agree within one 8-bit channel step; maximum error:", error)
+print("1152 filtered half-byte alpha cutoffs allow either visibility result; all other pixels are compared.")