commit 200e72f39ffbc0cb5b803f44bc1ea607f7b58df7
parent 10ba94eca5cd778ccd247c7871f5b80fe8a6a898
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 15:15:55 +0000
Use the constant texture factor in native combinations
Diffstat:
5 files changed, 38 insertions(+), 22 deletions(-)
diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c
@@ -11,6 +11,7 @@ static GLuint samplers[2];
static GLint texture_enabled[2], texture_alpha[2], texture_color_op[2], texture_alpha_op[2];
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 int depth_write = 1;
static uint32_t depth_function = 4;
@@ -69,12 +70,13 @@ int gl_renderer_initialize(void)
"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"
"uniform int color_arg1[2], alpha_arg1[2];\n"
- "void combine(int s, vec4 t) { vec3 c=color_arg2[s]==0 ? color.rgb : pixel.rgb;"
- "float a=alpha_arg2[s]==0 ? color.a : pixel.a; if(!has_alpha[s]) t.a=1.0;"
- "t.rgb=color_arg1[s]==0 ? color.rgb : color_arg1[s]==1 ? pixel.rgb : t.rgb;"
- "t.a=alpha_arg1[s]==0 ? color.a : alpha_arg1[s]==1 ? pixel.a : t.a;"
- "pixel.rgb=color_op[s]==4 ? t.rgb*c : color_op[s]==7 ? min(vec3(1.0),t.rgb+c) : t.rgb;"
- "if(alpha_op[s]!=1) pixel.a=alpha_op[s]==3 ? a : alpha_op[s]==4 ? t.a*a : t.a; }\n"
+ "uniform vec4 texture_factor;\n"
+ "vec4 argument(int a, vec4 t) { return a==0 ? color : a==1 ? pixel : a==2 ? t : texture_factor; }\n"
+ "void combine(int s, vec4 t) { if(!has_alpha[s]) t.a=1.0;"
+ "vec3 c1=argument(color_arg1[s],t).rgb, c2=argument(color_arg2[s],t).rgb;"
+ "float a1=argument(alpha_arg1[s],t).a, a2=argument(alpha_arg2[s],t).a;"
+ "pixel.rgb=color_op[s]==4 ? c1*c2 : color_op[s]==7 ? min(vec3(1.0),c1+c2) : c1;"
+ "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)); }"
"if(fog_enabled) pixel.rgb=mix(fog_color,pixel.rgb,clamp(fog_factor,0.0,1.0)); }\n"
@@ -120,6 +122,7 @@ int gl_renderer_initialize(void)
}
fog_enabled = glGetUniformLocation(program, "fog_enabled");
fog_color = glGetUniformLocation(program, "fog_color");
+ texture_factor = glGetUniformLocation(program, "texture_factor");
glUseProgram(program);
glUniform1i(glGetUniformLocation(program, "image0"), 0);
glUniform1i(glGetUniformLocation(program, "image1"), 1);
@@ -420,6 +423,9 @@ void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *stat
glUniform1i(texture_alpha_arg2[stage], state->alpha_arg2);
glUniform1i(texture_color_arg1[stage], state->color_arg1);
glUniform1i(texture_alpha_arg1[stage], state->alpha_arg1);
+ glUniform4f(texture_factor, ((state->factor >> 16) & 255) / 255.0f,
+ ((state->factor >> 8) & 255) / 255.0f, (state->factor & 255) / 255.0f,
+ (state->factor >> 24) / 255.0f);
}
glActiveTexture(GL_TEXTURE0);
check_graphics("texture binding");
diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h
@@ -17,6 +17,7 @@ struct gl_texture_state {
int min_linear, mag_linear, clamp_u, clamp_v, alpha, color_op, alpha_op, mip_point;
int color_arg2, alpha_arg2;
int color_arg1, alpha_arg1;
+ uint32_t factor;
};
int gl_renderer_initialize(void);
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -61,6 +61,7 @@ static struct device_object {
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 texture_factor;
uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16];
int scene;
struct draw_object *owner;
@@ -1350,6 +1351,7 @@ static void create_device(void)
device->depth_function = 4;
device->clipping = 1;
device->lighting = 1;
+ device->texture_factor = UINT32_MAX;
device->fog_end = device->fog_density = 0x3f800000;
for (unsigned matrix = 0; matrix < 5; ++matrix)
for (unsigned i = 0; i < 16; i += 5) device->transforms[matrix][i] = 0x3f800000;
@@ -1548,7 +1550,8 @@ static uint32_t device_state(struct device_object *device, uint32_t state)
case 25: case 56: return 8; /* ALWAYS alpha/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: case 60: return UINT32_MAX; /* Stencil masks/texture factor. */
+ case 58: case 59: return UINT32_MAX; /* Stencil masks. */
+ case 60: return device->texture_factor;
case 128: case 129: case 130: case 131:
case 132: case 133: case 134: case 135: return 0; /* Coordinate wrapping. */
case 141: case 142: case 145: return 1;
@@ -1647,6 +1650,7 @@ static void device_set_state(void)
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 == 60) device->texture_factor = 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;
@@ -1830,12 +1834,13 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
}
for (unsigned i = 0; i < 2; ++i) {
struct texture_stage *stage = &device->stages[i];
- if (stage->color_op != 1 && (stage->texture || stage->color_arg1 != 2)) {
+ int needs_texture = stage->color_arg1 == 2 || (stage->color_op != 2 && stage->color_arg2 == 2);
+ if (stage->color_op != 1 && (stage->texture || !needs_texture)) {
struct gl_texture_state state = {stage->texture ? stage->texture->target : NULL,
stage->min_filter == 2, stage->mag_filter == 2,
stage->sampler_u == 3, stage->sampler_v == 3,
stage->texture && stage->texture->alpha, stage->color_op, stage->alpha_op, stage->mip_filter == 2,
- stage->color_arg2, stage->alpha_arg2, stage->color_arg1, stage->alpha_arg1};
+ stage->color_arg2, stage->alpha_arg2, stage->color_arg1, stage->alpha_arg1, device->texture_factor};
gl_renderer_set_texture(i, &state);
} else gl_renderer_set_texture(i, NULL);
}
@@ -1968,10 +1973,10 @@ static void device_set_texture_state(void)
else if (state == 1 && (value == 1 || value == 2 || value == 4 || value == 7)) device->stages[stage].color_op = value;
else if (state == 4 && (value >= 1 && value <= 4)) device->stages[stage].alpha_op = value;
/* CURRENT selects the preceding stage; DIFFUSE selects vertex color. */
- else if (state == 2 && value <= 2) device->stages[stage].color_arg1 = value;
- else if (state == 5 && value <= 2) device->stages[stage].alpha_arg1 = value;
- else if (state == 3 && value <= 1) device->stages[stage].color_arg2 = value;
- else if (state == 6 && value <= 1) device->stages[stage].alpha_arg2 = value;
+ else if (state == 2 && value <= 3) device->stages[stage].color_arg1 = value;
+ else if (state == 5 && value <= 3) device->stages[stage].alpha_arg1 = value;
+ else if (state == 3 && value <= 3) device->stages[stage].color_arg2 = value;
+ else if (state == 6 && value <= 3) device->stages[stage].alpha_arg2 = value;
else if (value != texture_stage_state(&device->stages[stage], state)) {
fprintf(stderr, "Unsupported texture-stage state %u value=%08x\n", state, value); exit(3);
}
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -1077,20 +1077,24 @@ 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 < 256; ++sample) {
+ for (unsigned sample = 0; sample < 320; ++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},
{0,32,.25f,2,0xc080c0a0, -.25f,1.875f, .875f,1.625f},
};
+ uint32_t factor = sample & 8 ? 0x804060c0u : 0x20c08040u;
+ 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) {
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);
}
+ const uint32_t arguments[] = {0, 1, 2, 0, 3};
for (unsigned stage = 0; stage < 2; ++stage) {
- uint32_t texture = sample >= 192 ? 0 : textures[sample & 16 ? 0 : stage];
+ uint32_t texture = sample >= 192 && sample < 256 ? 0 : textures[sample & 16 ? 0 : stage];
assert(!invoke(device, 35, 3, stage, texture));
assert(!invoke(device, 34, 3, stage, workspace()) && *memory(workspace()) == texture);
if (texture) assert(invoke(texture, 2, 1) > 0);
@@ -1105,14 +1109,14 @@ static void two_texture_stages_combine_independent_samples(uint32_t draw, uint32
assert(!invoke(device, 37, 4, stage, 12u, stage ? 3u : 1u));
assert(!invoke(device, 37, 4, stage, 13u, stage ? 3u : 1u));
assert(!invoke(device, 37, 4, stage, 14u, stage ? 3u : 1u));
- assert(!invoke(device, 37, 4, stage, 2u, (sample / 64) % 3));
- assert(!invoke(device, 37, 4, stage, 5u, sample & 16 ? ((sample / 64) % 3 + 1) % 3 : (sample / 64) % 3));
- assert(!invoke(device, 36, 4, stage, 2u, workspace()) && *memory(workspace()) == (sample / 64) % 3);
+ assert(!invoke(device, 37, 4, stage, 2u, arguments[sample / 64]));
+ assert(!invoke(device, 37, 4, stage, 5u, sample & 16 ? (arguments[sample / 64] + 1) % 4 : arguments[sample / 64]));
+ assert(!invoke(device, 36, 4, stage, 2u, workspace()) && *memory(workspace()) == arguments[sample / 64]);
const uint32_t operations[] = {4, 7, 2, 1};
assert(!invoke(device, 37, 4, stage, 1u, stage ? operations[(sample >> 2) & 3] : 4u));
assert(!invoke(device, 37, 4, stage, 4u, stage ? (sample & 3) + 1 : 4u));
- assert(!invoke(device, 37, 4, stage, 3u, sample & 16 ? 0u : 1u));
- assert(!invoke(device, 37, 4, stage, 6u, sample & 16 ? 0u : 1u));
+ assert(!invoke(device, 37, 4, stage, 3u, sample >= 256 ? sample & 3 : sample & 16 ? 0u : 1u));
+ assert(!invoke(device, 37, 4, stage, 6u, sample >= 256 ? 3u : sample & 16 ? 0u : 1u));
}
assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u));
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) == 4773888
+assert len(windows_pixels) == len(linux_pixels) == 4970496
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 1591296 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 1656832 pixels agree within one 8-bit channel step; maximum error:", error)