aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 187f01d3d9afb8aa1471b4707ea4de7a2cc99761
parent 11d5ddb1c62ee6801465b8b9fff0bb9c61f59e0c
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 14:54:35 +0000

Render two independent native texture stages

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 68++++++++++++++++++++++++++++++++++++++++++++------------------------
Mdecompiled/runtime/gl_renderer.h | 4+++-
Mdecompiled/runtime/linux_graphics.c | 168++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mdecompiled/tests/devices.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mdecompiled/tests/graphics.c | 4++--
Mtools/reconstruction/test_portable_devices.py | 4++--
6 files changed, 231 insertions(+), 108 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -7,7 +7,9 @@ #include <assert.h> static GLuint program, vertex_array, vertex_buffer, index_buffer; -static GLint texture_enabled, texture_alpha, texture_color_op, texture_alpha_op; +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 fog_enabled, fog_color; static int depth_write = 1; static uint32_t depth_function = 4; @@ -56,16 +58,20 @@ int gl_renderer_initialize(void) "layout(location=1) in vec4 diffuse;\n" "layout(location=2) in vec2 texcoord;\n" "layout(location=3) in float fog;\n" - "out vec4 color; out vec2 uv; 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; fog_factor=fog; }\n", + "layout(location=4) in vec2 texcoord_second;\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", "#version 330 core\n" - "in vec4 color; in vec2 uv; in float fog_factor; out vec4 pixel;\n" + "in vec4 color; in vec2 uv, uv_second; in float fog_factor; out vec4 pixel;\n" "uniform bool fog_enabled; uniform vec3 fog_color;\n" - "uniform sampler2D image;\n" - "uniform bool textured, has_alpha; uniform int color_op, alpha_op;\n" - "void main() { pixel=color; if(textured) { vec4 t=texture(image,uv);" - "pixel.rgb=color_op==4 ? t.rgb*color.rgb : color_op==7 ? min(vec3(1.0),t.rgb+color.rgb) : t.rgb;" - "pixel.a=alpha_op==3 ? color.a : has_alpha ? t.a : 1.0; if(alpha_op==4) pixel.a*=color.a; }" + "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" + "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;" + "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" + "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" }; program = glCreateProgram(); @@ -97,14 +103,20 @@ int gl_renderer_initialize(void) return 0; } - texture_enabled = glGetUniformLocation(program, "textured"); - texture_alpha = glGetUniformLocation(program, "has_alpha"); - texture_color_op = glGetUniformLocation(program, "color_op"); - texture_alpha_op = glGetUniformLocation(program, "alpha_op"); + for (unsigned i = 0; i < 2; ++i) { + texture_enabled[i] = glGetUniformLocation(program, i ? "textured[1]" : "textured[0]"); + texture_alpha[i] = glGetUniformLocation(program, i ? "has_alpha[1]" : "has_alpha[0]"); + texture_color_op[i] = glGetUniformLocation(program, i ? "color_op[1]" : "color_op[0]"); + texture_alpha_op[i] = glGetUniformLocation(program, i ? "alpha_op[1]" : "alpha_op[0]"); + texture_color_arg2[i] = glGetUniformLocation(program, i ? "color_arg2[1]" : "color_arg2[0]"); + texture_alpha_arg2[i] = glGetUniformLocation(program, i ? "alpha_arg2[1]" : "alpha_arg2[0]"); + } fog_enabled = glGetUniformLocation(program, "fog_enabled"); fog_color = glGetUniformLocation(program, "fog_color"); glUseProgram(program); - glUniform1i(glGetUniformLocation(program, "image"), 0); + glUniform1i(glGetUniformLocation(program, "image0"), 0); + glUniform1i(glGetUniformLocation(program, "image1"), 1); + glGenSamplers(2, samplers); /* One streamed vertex/index pair is shared by this single rendering context. */ glGenVertexArrays(1, &vertex_array); @@ -117,6 +129,8 @@ int gl_renderer_initialize(void) glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, color)); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, uv)); 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); glEnableVertexAttribArray(3); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); @@ -128,6 +142,7 @@ int gl_renderer_initialize(void) void gl_renderer_shutdown(void) { + glDeleteSamplers(2, samplers); glDeleteBuffers(1, &index_buffer); glDeleteBuffers(1, &vertex_buffer); glDeleteVertexArrays(1, &vertex_array); @@ -355,11 +370,13 @@ void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int check_graphics("window presentation"); } -void gl_renderer_set_texture(const struct gl_texture_state *state) +void gl_renderer_set_texture(unsigned stage, const struct gl_texture_state *state) { + assert(stage < 2); glUseProgram(program); - glUniform1i(texture_enabled, state != NULL); - glActiveTexture(GL_TEXTURE0); + glUniform1i(texture_enabled[stage], state != NULL); + glActiveTexture(GL_TEXTURE0 + stage); + glBindSampler(stage, samplers[stage]); if (state) { make_resident(state->image); if (state->image->mip_dirty) { @@ -378,14 +395,17 @@ void gl_renderer_set_texture(const struct gl_texture_state *state) glBindTexture(GL_TEXTURE_2D, state->image->color); GLint min_filter = state->min_linear ? GL_LINEAR : GL_NEAREST; if (state->mip_point) min_filter = state->min_linear ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST; - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, state->mag_linear ? GL_LINEAR : GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state->clamp_u ? GL_CLAMP_TO_EDGE : GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state->clamp_v ? GL_CLAMP_TO_EDGE : GL_REPEAT); - glUniform1i(texture_alpha, state->alpha); - glUniform1i(texture_color_op, state->color_op); - glUniform1i(texture_alpha_op, state->alpha_op); + glSamplerParameteri(samplers[stage], GL_TEXTURE_MIN_FILTER, min_filter); + glSamplerParameteri(samplers[stage], GL_TEXTURE_MAG_FILTER, state->mag_linear ? GL_LINEAR : GL_NEAREST); + glSamplerParameteri(samplers[stage], GL_TEXTURE_WRAP_S, state->clamp_u ? GL_CLAMP_TO_EDGE : GL_REPEAT); + glSamplerParameteri(samplers[stage], GL_TEXTURE_WRAP_T, state->clamp_v ? GL_CLAMP_TO_EDGE : GL_REPEAT); + glUniform1i(texture_alpha[stage], state->alpha); + glUniform1i(texture_color_op[stage], state->color_op); + glUniform1i(texture_alpha_op[stage], state->alpha_op); + glUniform1i(texture_color_arg2[stage], state->color_arg2); + glUniform1i(texture_alpha_arg2[stage], state->alpha_arg2); } else glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE0); check_graphics("texture binding"); } diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -10,10 +10,12 @@ struct gl_vertex { float color[4]; float uv[2]; float fog; + float uv_second[2]; }; struct gl_texture_state { struct gl_target *image; int min_linear, mag_linear, clamp_u, clamp_v, alpha, color_op, alpha_op, mip_point; + int color_arg2, alpha_arg2; }; int gl_renderer_initialize(void); @@ -34,7 +36,7 @@ void gl_target_read(struct gl_target *target, unsigned char *rgba); void gl_target_write(struct gl_target *target, const unsigned char *rgba); void gl_target_blit(struct gl_target *source, struct gl_target *target, const int32_t source_rect[4], const int32_t target_rect[4]); void gl_target_present(struct gl_target *target, const int32_t rectangle[4], int width, int height); -void gl_renderer_set_texture(const struct gl_texture_state *state); +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); diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -49,18 +49,21 @@ struct device_light { uint32_t index, enabled, data[26]; struct device_light *next; }; -static struct device_object { - uint32_t interface, references, cull; +struct texture_stage { uint32_t min_filter, mag_filter, mip_filter, address, address_u, address_v, color_op, alpha_op; uint32_t sampler_u, sampler_v, texture_transform, texture_coordinate; uint32_t color_arg2, alpha_arg2; + struct surface_object *texture; +}; +static struct device_object { + uint32_t interface, references, cull; + 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 ambient, normalize_normals, material[17], viewport[6], transforms[4][16]; + uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16]; int scene; struct draw_object *owner; struct surface_object *target; - struct surface_object *texture; struct device_light *lights; struct device_object *next; } *devices; @@ -412,9 +415,9 @@ static void describe_device(uint32_t record) MEM32(record + 140) = UINT32_MAX; /* Float UV range exceeds this DWORD limit. */ MEM32(record + 144) = maximum; /* A 1-by-maximum texture is supported. */ MEM32(record + 148) = 1; /* No anisotropic filtering. */ - MEM32(record + 176) = 1; /* One texture-coordinate set. */ - MEM32(record + 180) = 0xb; /* Disable, select argument 1 and modulate. */ - MEM16(record + 184) = MEM16(record + 186) = 1; + MEM32(record + 176) = 2; /* Two texture-coordinate sets. */ + MEM32(record + 180) = 0x4b; /* Disable, select argument 1, modulate and add. */ + MEM16(record + 184) = MEM16(record + 186) = 2; /* HAL denotes rasterization without hardware transform-and-lighting. * Lighting, W buffering and other unimplemented features stay zero. */ static const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; @@ -1331,12 +1334,14 @@ static void create_device(void) device->interface = create_interface(6); device->references = 1; device->cull = 3; /* D3DCULL_CCW */ - device->min_filter = device->mag_filter = device->address = device->address_u = device->address_v = 1; - device->sampler_u = device->sampler_v = 1; - device->mip_filter = 1; - device->color_op = 4; - device->alpha_op = 2; - device->color_arg2 = device->alpha_arg2 = 1; + for (unsigned i = 0; i < 2; ++i) { + struct texture_stage *stage = &device->stages[i]; + stage->min_filter = stage->mag_filter = stage->address = stage->address_u = stage->address_v = 1; + stage->sampler_u = stage->sampler_v = stage->mip_filter = 1; + stage->color_op = i ? 1 : 4; + stage->alpha_op = i ? 1 : 2; + stage->color_arg2 = stage->alpha_arg2 = 1; + } device->source_blend = 2; device->destination_blend = 1; device->depth_write = 1; @@ -1344,7 +1349,7 @@ static void create_device(void) device->clipping = 1; device->lighting = 1; device->fog_end = device->fog_density = 0x3f800000; - for (unsigned matrix = 0; matrix < 4; ++matrix) + for (unsigned matrix = 0; matrix < 5; ++matrix) for (unsigned i = 0; i < 16; i += 5) device->transforms[matrix][i] = 0x3f800000; device->owner = owner; device->target = surface; @@ -1392,7 +1397,8 @@ static void device_release_reference(void) while (*link != device) link = &(*link)->next; *link = device->next; release_surface(device->target, 0); - if (device->texture) release_surface(device->texture, 0); + for (unsigned i = 0; i < 2; ++i) + if (device->stages[i].texture) release_surface(device->stages[i].texture, 0); release_draw(device->owner, 1); guest_free(device->interface); while (device->lights) { @@ -1604,12 +1610,12 @@ static void device_transform(void) struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t state = MEM32(g_esp + 8), matrix = MEM32(g_esp + 12); g_esp += 16; - if ((state < 1 || state > 3) && state != 16) { + if ((state < 1 || state > 3) && state != 16 && state != 17) { fprintf(stderr, "Unsupported Direct3D transform state %u\n", state); exit(3); } g_eax = 0x80070057u; if (!matrix) return; - unsigned index = state == 16 ? 3 : state - 1; + unsigned index = state >= 16 ? state - 13 : state - 1; if (current_slot == 11) memcpy(device->transforms[index], (void *)ADDR(matrix), 64); else memcpy((void *)ADDR(matrix), device->transforms[index], 64); g_eax = 0; @@ -1775,13 +1781,17 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin 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))); } - vertices[i].uv[0] = vertices[i].uv[1] = 0; - if (device->texture_coordinate < texture_count) - memcpy(vertices[i].uv, (const unsigned char *)input_vertex + stride - texture_count * 8 + device->texture_coordinate * 8, 8); - if (device->texture_transform == 2) { - float coordinate[] = {vertices[i].uv[0], vertices[i].uv[1], 1, 0}, transformed[4]; - transform_vector(coordinate, (const float *)device->transforms[3], transformed); - memcpy(vertices[i].uv, transformed, 8); + for (unsigned stage = 0; stage < 2; ++stage) { + float *uv = stage ? vertices[i].uv_second : vertices[i].uv; + unsigned coordinate = device->stages[stage].texture_coordinate; + uv[0] = uv[1] = 0; + if (coordinate < texture_count) + memcpy(uv, (const unsigned char *)input_vertex + stride - texture_count * 8 + coordinate * 8, 8); + if (device->stages[stage].texture_transform == 2) { + float input_uv[] = {uv[0], uv[1], 1, 0}, transformed[4]; + transform_vector(input_uv, (const float *)device->transforms[3 + stage], transformed); + memcpy(uv, transformed, 8); + } } continue; } @@ -1799,9 +1809,13 @@ 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; - vertices[i].uv[0] = vertices[i].uv[1] = 0; - if (device->texture_coordinate < texture_count) - memcpy(vertices[i].uv, (void *)ADDR(input + i * stride + uv_offset + device->texture_coordinate * 8), 8); + for (unsigned stage = 0; stage < 2; ++stage) { + float *uv = stage ? vertices[i].uv_second : vertices[i].uv; + unsigned coordinate = device->stages[stage].texture_coordinate; + uv[0] = uv[1] = 0; + if (coordinate < texture_count) + memcpy(uv, (void *)ADDR(input + i * stride + uv_offset + coordinate * 8), 8); + } } if (primitive == 4) for (uint32_t i = 0; i < element_count; ++i) indices[i] = input_indices ? MEM16(input_indices + i * 2) : i; else for (uint32_t i = 0; i < element_count - 2; ++i) { @@ -1810,13 +1824,17 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin indices[i * 3 + 1] = input_indices ? MEM16(input_indices + b * 2) : b; indices[i * 3 + 2] = input_indices ? MEM16(input_indices + c * 2) : c; } - if (device->texture && device->color_op != 1) { - struct gl_texture_state state = {device->texture->target, - device->min_filter == 2, device->mag_filter == 2, - device->sampler_u == 3, device->sampler_v == 3, - device->texture->alpha, device->color_op, device->alpha_op, device->mip_filter == 2}; - gl_renderer_set_texture(&state); - } else gl_renderer_set_texture(NULL); + for (unsigned i = 0; i < 2; ++i) { + struct texture_stage *stage = &device->stages[i]; + if (stage->texture && stage->color_op != 1) { + struct gl_texture_state state = {stage->texture->target, + stage->min_filter == 2, stage->mag_filter == 2, + stage->sampler_u == 3, stage->sampler_v == 3, + stage->texture->alpha, stage->color_op, stage->alpha_op, stage->mip_filter == 2, + stage->color_arg2, stage->alpha_arg2}; + gl_renderer_set_texture(i, &state); + } else gl_renderer_set_texture(i, NULL); + } 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); @@ -1825,7 +1843,7 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin gl_target_draw(device->target->target, vertices, count, indices, index_count, clip); gl_renderer_set_blend(0, 2, 1); gl_renderer_set_fog(0, 0); - gl_renderer_set_texture(NULL); + for (unsigned stage = 0; stage < 2; ++stage) gl_renderer_set_texture(stage, NULL); free(indices); free(vertices); g_eax = 0; @@ -1844,15 +1862,15 @@ static void device_set_texture(void) { struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t stage = MEM32(g_esp + 8), address = MEM32(g_esp + 12); - if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } + if (stage >= 2) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } struct surface_object *texture = address ? find_surface(address) : NULL; g_eax = 0x80070057u; g_esp += 16; if (texture && !(texture->caps & 0x1000)) return; - if (device->texture != texture) { + if (device->stages[stage].texture != texture) { if (texture) ++texture->references[0]; - if (device->texture) release_surface(device->texture, 0); - device->texture = texture; + if (device->stages[stage].texture) release_surface(device->stages[stage].texture, 0); + device->stages[stage].texture = texture; } g_eax = 0; } @@ -1861,34 +1879,34 @@ static void device_get_texture(void) { struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t stage = MEM32(g_esp + 8), output = MEM32(g_esp + 12); - if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } + if (stage >= 2) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } g_eax = 0x80070057u; if (output) { - MEM32(output) = device->texture ? device->texture->interfaces[0] : 0; - if (device->texture) ++device->texture->references[0]; + MEM32(output) = device->stages[stage].texture ? device->stages[stage].texture->interfaces[0] : 0; + if (device->stages[stage].texture) ++device->stages[stage].texture->references[0]; g_eax = 0; } g_esp += 16; } -static uint32_t texture_stage_state(struct device_object *device, uint32_t state) +static uint32_t texture_stage_state(struct texture_stage *stage, uint32_t state) { switch (state) { - case 1: return device->color_op; + case 1: return stage->color_op; case 2: case 5: return 2; /* Texture argument. */ - case 3: return device->color_arg2; - case 6: return device->alpha_arg2; - case 4: return device->alpha_op; + case 3: return stage->color_arg2; + case 6: return stage->alpha_arg2; + case 4: return stage->alpha_op; case 7: case 8: case 9: case 10: /* Inactive bump matrix. */ case 15: case 19: case 20: case 22: case 23: return 0; - case 11: return device->texture_coordinate; - case 24: return device->texture_transform; - case 12: return device->address; - case 13: return device->address_u; - case 14: return device->address_v; - case 16: return device->mag_filter; - case 17: return device->min_filter; - case 18: return device->mip_filter; + case 11: return stage->texture_coordinate; + case 24: return stage->texture_transform; + case 12: return stage->address; + case 13: return stage->address_u; + case 14: return stage->address_v; + case 16: return stage->mag_filter; + case 17: return stage->min_filter; + case 18: return stage->mip_filter; case 21: return 1; /* No anisotropic filtering. */ default: fprintf(stderr, "Unsupported texture-stage state %u\n", state); exit(3); } @@ -1903,7 +1921,7 @@ static void device_get_texture_state(void) /* Disabled stages still have queryable defaults for game state caches. */ const uint32_t inactive[] = {1, 2, 1, 1, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0}; - MEM32(output) = stage ? inactive[state - 1] : texture_stage_state(device, state); + MEM32(output) = stage >= 2 ? inactive[state - 1] : texture_stage_state(&device->stages[stage], state); g_eax = 0; } g_esp += 20; @@ -1913,29 +1931,29 @@ static void device_set_texture_state(void) { struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t stage = MEM32(g_esp + 8), state = MEM32(g_esp + 12), value = MEM32(g_esp + 16); - if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } - if (state == 11 && value <= 1) device->texture_coordinate = value; - else if (state == 16 && (value == 1 || value == 2)) device->mag_filter = value; - else if (state == 17 && (value == 1 || value == 2)) device->min_filter = value; - else if (state == 18 && (value == 1 || value == 2)) device->mip_filter = value; - else if (state == 24 && (value == 0 || value == 2)) device->texture_transform = value; + if (stage >= 2) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } + if (state == 11 && value <= 1) device->stages[stage].texture_coordinate = value; + else if (state == 16 && (value == 1 || value == 2)) device->stages[stage].mag_filter = value; + else if (state == 17 && (value == 1 || value == 2)) device->stages[stage].min_filter = value; + else if (state == 18 && (value == 1 || value == 2)) device->stages[stage].mip_filter = value; + else if (state == 24 && (value == 0 || value == 2)) device->stages[stage].texture_transform = value; /* ADDRESS has an independent cache; only changed states update the sampler. */ else if (state == 12 && (value == 1 || value == 3)) { - if (device->address != value) device->sampler_u = device->sampler_v = value; - device->address = value; + if (device->stages[stage].address != value) device->stages[stage].sampler_u = device->stages[stage].sampler_v = value; + device->stages[stage].address = value; } else if (state == 13 && (value == 1 || value == 3)) { - if (device->address_u != value) device->sampler_u = value; - device->address_u = value; + if (device->stages[stage].address_u != value) device->stages[stage].sampler_u = value; + device->stages[stage].address_u = value; } else if (state == 14 && (value == 1 || value == 3)) { - if (device->address_v != value) device->sampler_v = value; - device->address_v = value; - } - else if (state == 1 && (value == 1 || value == 2 || value == 4 || value == 7)) device->color_op = value; - else if (state == 4 && (value == 2 || value == 3 || value == 4)) device->alpha_op = value; - /* Stage zero's CURRENT and DIFFUSE both select interpolated vertex color. */ - else if (state == 3 && value <= 1) device->color_arg2 = value; - else if (state == 6 && value <= 1) device->alpha_arg2 = value; - else if (value != texture_stage_state(device, state)) { + if (device->stages[stage].address_v != value) device->stages[stage].sampler_v = value; + device->stages[stage].address_v = value; + } + 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 == 3 && value <= 1) device->stages[stage].color_arg2 = value; + else if (state == 6 && value <= 1) 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); } g_eax = 0; diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -977,21 +977,22 @@ static void transforms_copy_geometry_and_texture_matrices(uint32_t device) {0,1,0,0, -1,0,0,0, 0,0,1,0, -2,3,-4,1}, {1.5f,0,0,0, 0,2,0,0, 0,0,1,1, 0,0,-.5f,0}, {2,0,0,0, 0,3,0,0, .25f,.5f,1,0, 0,0,0,1}, + {0,1,0,0, -1,0,0,0, .75f,.25f,1,0, 0,0,0,1}, }; - const unsigned states[] = {1, 2, 3, 16}; + const unsigned states[] = {1, 2, 3, 16, 17}; uint32_t input = workspace() + 512, output = workspace() + 768; - for (unsigned index = 0; index < 4; ++index) { + for (unsigned index = 0; index < 5; ++index) { assert(!invoke(device, 12, 3, states[index], output)); assert(!memcmp(memory(output), identity, 64)); } - for (unsigned index = 0; index < 4; ++index) { + for (unsigned index = 0; index < 5; ++index) { memcpy(memory(input), matrices[index], 64); assert(!invoke(device, 11, 3, states[index], input)); memset(memory(input), 0xa5, 64); } - for (unsigned index = 0; index < 4; ++index) { + for (unsigned index = 0; index < 5; ++index) { memset(memory(output), 0xcc, 68); assert(!invoke(device, 12, 3, states[index], output)); assert(!memcmp(memory(output), matrices[index], 64)); @@ -1001,6 +1002,87 @@ static void transforms_copy_geometry_and_texture_matrices(uint32_t device) } } +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]; + const uint32_t texels[] = {0x40802040, 0x80c08020, 0xc02040c0, 0xff60a080}; + for (unsigned i = 0; i < 2; ++i) { + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 0x1007; + *memory(desc + 8) = 2; *memory(desc + 12) = 2; *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)); + textures[i] = *memory(workspace()); + assert(!invoke(textures[i], 25, 5, 0u, desc, 0x21u, 0u)); + for (unsigned y = 0; y < 2; ++y) for (unsigned x = 0; x < 2; ++x) + *memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4) = texels[(y * 2 + x + i) % 4]; + assert(!invoke(textures[i], 32, 2, 0u)); + } + assert(!invoke(device, 20, 3, 137u, 0u)); + assert(!invoke(device, 20, 3, 28u, 0u)); + assert(!invoke(device, 20, 3, 22u, 1u)); + 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 < 64; ++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}, + }; + 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); + } + for (unsigned stage = 0; stage < 2; ++stage) { + uint32_t texture = textures[sample & 16 ? 0 : stage]; + assert(!invoke(device, 35, 3, stage, texture)); + assert(!invoke(device, 34, 3, stage, workspace()) && *memory(workspace()) == texture); + assert(invoke(texture, 2, 1) > 0); + assert(!invoke(device, 37, 4, stage, 11u, stage)); + assert(!invoke(device, 37, 4, stage, 24u, sample & 32 ? 2u : 0u)); + const float matrix[] = {2,0,0,0, 0,1,0,0, .125f,.375f,1,0, 0,0,0,1}; + memcpy(memory(workspace() + 1024), matrix, 64); + assert(!invoke(device, 11, 3, 16u + stage, workspace() + 1024)); + assert(!invoke(device, 37, 4, stage, 18u, 1u)); + assert(!invoke(device, 37, 4, stage, 16u, stage + 1)); + assert(!invoke(device, 37, 4, stage, 17u, stage + 1)); + 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)); + 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, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, sample & 32 ? 0x242u : 0x244u, input, 3u, 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); + 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(textures[0], 2, 1) == 1); + assert(!invoke(device, 35, 3, 1u, 0u)); + assert(!invoke(textures[1], 2, 1)); +} + static void device_lifetime_and_rendering(const char *pixels_path) { const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; @@ -1138,6 +1220,7 @@ 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); + two_texture_stages_combine_independent_samples(draw, device, color, file); assert(!fclose(file)); assert(!invoke(device, 2, 1)); assert(!invoke(draw, 2, 1)); diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -535,9 +535,9 @@ static int inspect_device(uint32_t description, uint32_t name, uint32_t caps, ui assert(*memory(caps) == 0x80200u); assert(*memory(caps + 64) == 0x10 && *memory(caps + 68) == 0x10); assert(*memory(caps + 72) == 8 && *memory(caps + 88) == 8); - assert(*memory(caps + 184) == 0x00010001); /* One texture and one blend stage. */ + assert(*memory(caps + 184) == 0x00020002); /* Two textures and two blend stages. */ assert(*memory(caps + 92) == 0x45 && *memory(caps + 96) == 0x03010317); - assert(*memory(caps + 104) == 0x15 && *memory(caps + 180) == 0xb); + assert(*memory(caps + 104) == 0x15 && *memory(caps + 180) == 0x4b); const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; assert(!memcmp(memory(caps + 196), hal, 16)); #endif 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) == 2119680 +assert len(windows_pixels) == len(linux_pixels) == 2316288 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 706560 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 772096 pixels agree within one 8-bit channel step; maximum error:", error)