aorecomp

Anarchy Online classic in the browser

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


commit 1128c5346906824c7c3fd950e5cf620652c678b0
parent e94df371874a8667e5c8ffc8fa95f9764064e420
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 03:25:21 +0000

Implement native textures and verify alpha blending against Windows

Diffstat:
Mdecompiled/runtime/gl_renderer.c | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mdecompiled/runtime/gl_renderer.h | 7+++++++
Mdecompiled/runtime/linux_graphics.c | 192+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mdecompiled/tests/devices.c | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mdecompiled/tests/graphics.c | 6++++--
Mtools/reconstruction/test_portable_devices.py | 4++--
6 files changed, 384 insertions(+), 20 deletions(-)

diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -7,6 +7,7 @@ #include <assert.h> static GLuint program, vertex_array, vertex_buffer, index_buffer; +static GLint texture_enabled, texture_alpha, texture_modulate, texture_modulate_alpha; struct gl_depth { unsigned width, height, references; GLuint buffer; @@ -33,11 +34,16 @@ int gl_renderer_initialize(void) "#version 330 core\n" "layout(location=0) in vec4 position;\n" "layout(location=1) in vec4 diffuse;\n" - "out vec4 color;\n" - "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; }\n", + "layout(location=2) in vec2 texcoord;\n" + "out vec4 color; out vec2 uv;\n" + "void main() { gl_Position=vec4(position.x,-position.y,2.0*position.z-position.w,position.w); color=diffuse; uv=texcoord; }\n", "#version 330 core\n" - "in vec4 color; out vec4 pixel;\n" - "void main() { pixel=color; }\n" + "in vec4 color; in vec2 uv; out vec4 pixel;\n" + "uniform sampler2D image;\n" + "uniform bool textured, has_alpha, modulate, modulate_alpha;\n" + "void main() { pixel=color; if(textured) { vec4 t=texture(image,uv);" + "pixel.rgb=modulate ? t.rgb*color.rgb : t.rgb;" + "pixel.a=has_alpha ? t.a : 1.0; if(modulate_alpha) pixel.a*=color.a; } }\n" }; program = glCreateProgram(); for (unsigned i = 0; i < 2; ++i) { @@ -68,6 +74,13 @@ int gl_renderer_initialize(void) return 0; } + texture_enabled = glGetUniformLocation(program, "textured"); + texture_alpha = glGetUniformLocation(program, "has_alpha"); + texture_modulate = glGetUniformLocation(program, "modulate"); + texture_modulate_alpha = glGetUniformLocation(program, "modulate_alpha"); + glUseProgram(program); + glUniform1i(glGetUniformLocation(program, "image"), 0); + /* One streamed vertex/index pair is shared by this single rendering context. */ glGenVertexArrays(1, &vertex_array); glGenBuffers(1, &vertex_buffer); @@ -77,8 +90,10 @@ int gl_renderer_initialize(void) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(struct gl_vertex), (void *)offsetof(struct gl_vertex, position)); 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)); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); glBindVertexArray(0); check_graphics("renderer initialization"); return 1; @@ -141,6 +156,7 @@ struct gl_target *gl_target_create(unsigned width, unsigned height) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); target->depth = gl_depth_create(width, height, 24); if (!target->depth) { gl_target_destroy(target); return NULL; } glGenFramebuffers(1, &target->framebuffer); @@ -186,7 +202,6 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); glViewport(0, 0, target->width, target->height); glDisable(GL_SCISSOR_TEST); - glDisable(GL_BLEND); glDisable(GL_CULL_FACE); if (target->depth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); @@ -223,3 +238,44 @@ void gl_target_write(struct gl_target *target, const unsigned char *rgba) GL_RGBA, GL_UNSIGNED_BYTE, rgba); check_graphics("target upload"); } + +void gl_renderer_set_texture(const struct gl_texture_state *state) +{ + glUseProgram(program); + glUniform1i(texture_enabled, state != NULL); + glActiveTexture(GL_TEXTURE0); + if (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_modulate, state->modulate); + glUniform1i(texture_modulate_alpha, state->modulate_alpha); + } else glBindTexture(GL_TEXTURE_2D, 0); + check_graphics("texture binding"); +} + +static GLenum blend_factor(uint32_t factor) +{ + /* Direct3D blend factors supported by the platform adapter. */ + switch (factor) { + case 1: return GL_ZERO; + case 2: return GL_ONE; + case 5: return GL_SRC_ALPHA; + case 6: return GL_ONE_MINUS_SRC_ALPHA; + default: abort(); + } +} + +void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination) +{ + if (enabled) { + glEnable(GL_BLEND); + glBlendFunc(blend_factor(source), blend_factor(destination)); + } else glDisable(GL_BLEND); + check_graphics("blend state"); +} diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -8,6 +8,11 @@ struct gl_depth; struct gl_vertex { float position[4]; /* D3D clip coordinates: -w <= x,y <= w and 0 <= z <= w. */ float color[4]; + float uv[2]; +}; +struct gl_texture_state { + struct gl_target *image; + int min_linear, mag_linear, clamp_u, clamp_v, alpha, modulate, modulate_alpha, mip_point; }; int gl_renderer_initialize(void); @@ -22,5 +27,7 @@ void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, unsigned vertex_count, const uint16_t *indices, unsigned index_count); 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_renderer_set_texture(const struct gl_texture_state *state); +void gl_renderer_set_blend(int enabled, uint32_t source, uint32_t destination); #endif diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -19,7 +19,7 @@ static struct clipper_object { } *clippers; static struct surface_object { uint32_t interfaces[2], references[2]; - uint32_t width, height, caps, pixels, lock_flags, depth_bits; + uint32_t width, height, caps, pixels, lock_flags, depth_bits, alpha; int locked; struct draw_object *owner; struct clipper_object *clipper; @@ -30,9 +30,13 @@ static struct surface_object { } *surfaces; static struct device_object { uint32_t interface, references, cull; + uint32_t min_filter, mag_filter, mip_filter, address_u, address_v, color_op, alpha_op; + uint32_t alpha_arg2; + uint32_t blend, source_blend, destination_blend; int scene; struct draw_object *owner; struct surface_object *target; + struct surface_object *texture; struct device_object *next; } *devices; static uint32_t vtables[7]; @@ -298,16 +302,30 @@ static void enumerate_adapters(void) static void describe_device(uint32_t record) { memset((void *)ADDR(record), 0, 236); - MEM32(record) = 0x80000; /* D3DDEVCAPS_HWRASTERIZATION */ + MEM32(record) = 0x80200; /* HWRASTERIZATION | TEXTUREVIDEOMEMORY */ MEM32(record + 4) = MEM32(record + 60) = 56; /* D3DPRIMCAPS sizes */ MEM32(record + 64) = 0x10; /* Triangle culling: NONE */ MEM32(record + 68) = 0x10; /* Triangle rasterization: ZTEST */ MEM32(record + 72) = 8; /* Depth comparison: LESSEQUAL */ + MEM32(record + 76) = MEM32(record + 80) = 0x33; /* ZERO, ONE, SRCALPHA, INVSRCALPHA. */ MEM32(record + 88) = 8; /* Gouraud RGB interpolation */ + MEM32(record + 92) = 0x45; /* Perspective, alpha, normalized repeat range. */ + MEM32(record + 96) = 0x03000303; /* Point/linear minification and magnification. */ + MEM32(record + 104) = 0x15; /* Independent U/V, wrap and clamp. */ MEM32(record + 116) = 0x100; /* DDBD_32 color */ MEM32(record + 120) = 0x600; /* DDBD_16 | DDBD_24 depth */ + GLint maximum; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maximum); + MEM32(record + 124) = MEM32(record + 128) = 1; + MEM32(record + 132) = MEM32(record + 136) = maximum; + 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; /* HAL denotes rasterization without hardware transform-and-lighting. - * Texturing, lighting, W buffering and other unimplemented features stay zero. */ + * Lighting, W buffering and other unimplemented features stay zero. */ static const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; memcpy((void *)ADDR(record + 196), hal, 16); } @@ -549,11 +567,20 @@ static void create_surface(void) uint32_t flags = MEM32(desc + 4), caps = MEM32(desc + 104); uint32_t width = MEM32(desc + 12), height = MEM32(desc + 8); int depth = flags == 0x1007 && caps == 0x24000; - if (outer || !((flags == 1 && caps == 0x200) || depth || + int texture = flags == 0x1007 && (caps == 0x1000 || caps == 0x5000); + if (outer || !((flags == 1 && caps == 0x200) || depth || texture || (flags == 7 && (caps == 0x40 || caps == 0x2040 || caps == 0x6040)))) { fprintf(stderr, "Unsupported DirectDraw surface flags=%08x caps=%08x outer=%08x\n", flags, caps, outer); exit(3); } + if (texture) { + const uint32_t rgb[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0}; + const uint32_t argb[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000}; + if ((memcmp((void *)ADDR(desc + 72), rgb, 32) && memcmp((void *)ADDR(desc + 72), argb, 32)) || + MEM32(desc + 108) || MEM32(desc + 112) || MEM32(desc + 116)) { + fprintf(stderr, "Unsupported texture format or extended caps\n"); exit(3); + } + } if (depth) { const uint32_t format16[] = {32, 0x400, 0, 16, 0, 0xffff, 0, 0}; const uint32_t format24[] = {32, 0x400, 0, 24, 0, 0xffffff, 0, 0}; @@ -586,6 +613,10 @@ static void create_surface(void) } else surface->target = gl_target_create(width, height); if (!surface->target && !surface->depth) { free(surface); g_eax = 0x8007000eu; return; } + if (texture) { + surface->alpha = MEM32(desc + 76) & 1; + gl_target_set_depth(surface->target, NULL); + } surface->width = width; surface->height = height; surface->caps = caps | 0x10004000u; /* Local video memory. */ @@ -658,7 +689,8 @@ static void surface_release_reference(void) static void surface_pixel_format(struct surface_object *surface, uint32_t output) { - const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0}; + const uint32_t format[] = {32, 0x40 | surface->alpha, 0, 32, 0xff0000, 0xff00, 0xff, + surface->alpha ? 0xff000000u : 0}; const uint32_t depth[] = {32, 0x400, 0, surface->depth_bits, 0, surface->depth_bits == 16 ? 0xffff : 0xffffff, 0, 0}; memcpy((void *)ADDR(output), surface->depth ? depth : format, sizeof(format)); @@ -866,6 +898,13 @@ static void create_device(void) device->interface = create_interface(6); device->references = 1; device->cull = 3; /* D3DCULL_CCW default; drawing currently requires CULL_NONE. */ + device->min_filter = device->mag_filter = device->address_u = device->address_v = 1; + device->mip_filter = 1; + device->color_op = 4; + device->alpha_op = 2; + device->alpha_arg2 = 1; + device->source_blend = 2; + device->destination_blend = 1; device->owner = owner; device->target = surface; ++owner->references[1]; @@ -909,6 +948,7 @@ 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); release_draw(device->owner, 1); guest_free(device->interface); free(device); @@ -1012,9 +1052,12 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 7: case 14: case 137: return 1; /* Z enable/write and lighting. */ case 8: return 3; /* Solid fill. */ case 9: return 2; /* Gouraud shading. */ + case 19: return device->source_blend; + case 20: return device->destination_blend; case 22: return device->cull; case 23: return 4; /* LESSEQUAL depth comparison. */ - case 26: case 27: case 28: return 0; /* Dither, blending and fog. */ + case 27: return device->blend; + case 26: case 28: return 0; /* Dither and fog. */ default: fprintf(stderr, "Unsupported Direct3D render state %u\n", state); exit(3); } } @@ -1033,6 +1076,9 @@ static void device_set_state(void) struct device_object *device = find_device(MEM32(g_esp + 4)); uint32_t state = MEM32(g_esp + 8), value = MEM32(g_esp + 12); if (state == 22 && (value == 1 || value == 3)) device->cull = value; + else if (state == 27 && value <= 1) device->blend = value; + else if (state == 19 && (value == 1 || value == 2 || value == 5 || value == 6)) device->source_blend = value; + else if (state == 20 && (value == 1 || value == 2 || value == 5 || value == 6)) device->destination_blend = value; else if (value != device_state(device, state)) { fprintf(stderr, "Unsupported Direct3D render state %u value=%08x\n", state, value); exit(3); } @@ -1046,7 +1092,7 @@ static void device_draw_primitive(void) uint32_t primitive = MEM32(g_esp + 8), fvf = MEM32(g_esp + 12), input = MEM32(g_esp + 16); uint32_t count = MEM32(g_esp + 20), flags = MEM32(g_esp + 24); g_esp += 28; - if (primitive != 4 || fvf != 0x44 || flags || device->cull != 1) { + if (primitive != 4 || (fvf != 0x44 && fvf != 0x144) || flags || device->cull != 1) { fprintf(stderr, "Unsupported Direct3D draw primitive=%u fvf=%08x flags=%08x cull=%u\n", primitive, fvf, flags, device->cull); exit(3); } g_eax = 0x80070057u; @@ -1054,28 +1100,151 @@ static void device_draw_primitive(void) struct gl_vertex *vertices = malloc(count * sizeof(*vertices)); uint16_t *indices = malloc(count * sizeof(*indices)); if (!vertices || !indices) abort(); + uint32_t stride = fvf == 0x144 ? 28 : 20; for (uint32_t i = 0; i < count; ++i) { float position[4]; - memcpy(position, (void *)ADDR(input + i * 20), 16); + memcpy(position, (void *)ADDR(input + i * stride), 16); float w = 1.0f / position[3]; /* D3D transformed coordinates put integer positions at pixel centers. */ vertices[i].position[0] = (2 * (position[0] + 0.5f) / device->target->width - 1) * w; vertices[i].position[1] = (1 - 2 * (position[1] + 0.5f) / device->target->height) * w; vertices[i].position[2] = position[2] * w; vertices[i].position[3] = w; - uint32_t color = MEM32(input + i * 20 + 16); + uint32_t color = MEM32(input + i * stride + 16); vertices[i].color[0] = ((color >> 16) & 255) / 255.0f; vertices[i].color[1] = ((color >> 8) & 255) / 255.0f; vertices[i].color[2] = (color & 255) / 255.0f; vertices[i].color[3] = (color >> 24) / 255.0f; + vertices[i].uv[0] = vertices[i].uv[1] = 0; + if (fvf == 0x144) memcpy(vertices[i].uv, (void *)ADDR(input + i * stride + 20), 8); indices[i] = i; } + if (device->texture && device->color_op != 1) { + struct gl_texture_state state = {device->texture->target, + device->min_filter == 2, device->mag_filter == 2, + device->address_u == 3, device->address_v == 3, + device->texture->alpha, device->color_op == 4, device->alpha_op == 4, device->mip_filter == 2}; + gl_renderer_set_texture(&state); + } else gl_renderer_set_texture(NULL); + gl_renderer_set_blend(device->blend, device->source_blend, device->destination_blend); gl_target_draw(device->target->target, vertices, count, indices, count); + gl_renderer_set_blend(0, 2, 1); + gl_renderer_set_texture(NULL); free(indices); free(vertices); g_eax = 0; } +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); } + 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 (texture) ++texture->references[0]; + if (device->texture) release_surface(device->texture, 0); + device->texture = texture; + } + g_eax = 0; +} + +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); } + g_eax = 0x80070057u; + if (output) { + MEM32(output) = device->texture ? device->texture->interfaces[0] : 0; + if (device->texture) ++device->texture->references[0]; + g_eax = 0; + } + g_esp += 16; +} + +static uint32_t texture_stage_state(struct device_object *device, uint32_t state) +{ + switch (state) { + case 1: return device->color_op; + case 2: case 5: return 2; /* Texture argument. */ + case 3: return 1; /* Current/diffuse argument. */ + case 6: return device->alpha_arg2; + case 4: return device->alpha_op; + case 11: case 15: case 19: case 20: case 24: return 0; + case 12: return 1; /* Legacy ADDRESS is separate from ADDRESSU/V. */ + 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 21: return 1; /* No anisotropic filtering. */ + default: fprintf(stderr, "Unsupported texture-stage state %u\n", state); exit(3); + } +} + +static void device_get_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), output = MEM32(g_esp + 16); + if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); } + g_eax = 0x80070057u; + if (output) { MEM32(output) = texture_stage_state(device, state); g_eax = 0; } + g_esp += 20; +} + +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 == 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 == 13 && (value == 1 || value == 3)) { + device->address_u = value; + } else if (state == 14 && (value == 1 || value == 3)) device->address_v = value; + else if (state == 1 && (value == 1 || value == 2 || value == 4)) device->color_op = value; + else if (state == 4 && (value == 2 || value == 4)) device->alpha_op = value; + /* Stage zero's CURRENT and DIFFUSE both select interpolated vertex color. */ + else if (state == 6 && value <= 1) device->alpha_arg2 = value; + else if (value != texture_stage_state(device, state)) { + fprintf(stderr, "Unsupported texture-stage state %u value=%08x\n", state, value); exit(3); + } + g_eax = 0; + g_esp += 20; +} + +static void device_texture_formats(void) +{ + find_device(MEM32(g_esp + 4)); + uint32_t callback = MEM32(g_esp + 8), data = MEM32(g_esp + 12); + if (!callback) { g_eax = 0x80070057u; g_esp += 16; return; } + uint32_t record = guest_malloc(32); + if (!record) abort(); + recomp_func_t function = recomp_lookup(callback); + if (!function) function = recomp_lookup_import(callback); + uint32_t stack = g_esp, caller = g_cur_func; + for (unsigned alpha = 0; alpha < 2; ++alpha) { + const uint32_t format[] = {32, 0x40 | alpha, 0, 32, 0xff0000, 0xff00, 0xff, + alpha ? 0xff000000u : 0}; + memcpy((void *)ADDR(record), format, 32); + PUSH32(g_esp, data); + PUSH32(g_esp, record); + PUSH32(g_esp, RECOMP_RETADDR); + function(); + if (g_esp != stack) { fprintf(stderr, "Texture-format callback unbalanced the guest stack\n"); abort(); } + g_cur_func = caller; + if (!g_eax) break; + } + guest_free(record); + g_eax = 0; + g_esp += 16; +} + static void unavailable_method(void) { const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7"}; @@ -1095,6 +1264,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 1) return device_add_reference; if (current_slot == 2) return device_release_reference; if (current_slot == 3) return device_caps; + if (current_slot == 4) return device_texture_formats; if (current_slot == 5) return device_begin_scene; if (current_slot == 6) return device_end_scene; if (current_slot == 7) return device_parent; @@ -1105,6 +1275,10 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 20) return device_set_state; if (current_slot == 21) return device_get_state; if (current_slot == 25) return device_draw_primitive; + if (current_slot == 34) return device_get_texture; + if (current_slot == 35) return device_set_texture; + if (current_slot == 36) return device_get_texture_state; + if (current_slot == 37) return device_set_texture_state; return unavailable_method; } if (current_interface == 5) { diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -11,7 +11,8 @@ static uint32_t scratch[1024]; #else #include "linux_graphics.h" #include "guest_memory.h" -recomp_func_t recomp_lookup(uint32_t address) { (void)address; assert(0); return NULL; } +static void format_callback(void); +recomp_func_t recomp_lookup(uint32_t address) { assert(address == 0x11110000u); return format_callback; } recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } #endif @@ -71,6 +72,129 @@ static uint32_t query(uint32_t object, const uint32_t *iid) return *memory(workspace()); } +static unsigned format_count, format_mask; +static uint32_t inspect_format(uint32_t record, uint32_t data) +{ + assert(*memory(record) == 32 && (data == 0 || data == 1)); + if ((*memory(record + 4) & 0x40) && *memory(record + 12) == 32 && + *memory(record + 16) == 0xff0000 && *memory(record + 20) == 0xff00 && *memory(record + 24) == 0xff) { + if (*memory(record + 28) == 0xff000000) format_mask |= 2; + else if (!*memory(record + 28)) format_mask |= 1; + } + ++format_count; + return data ? 0 : 1; +} + +#ifdef _WIN32 +static HRESULT WINAPI format_callback(DDPIXELFORMAT *format, void *data) +{ + return inspect_format((uintptr_t)format, (uintptr_t)data); +} +#else +static void format_callback(void) +{ + g_eax = inspect_format(MEM32(g_esp + 4), MEM32(g_esp + 8)); + g_esp += 12; +} +#endif + +static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target, FILE *file) +{ + uint32_t desc = workspace() + 128, input = workspace() + 512; +#ifdef _WIN32 + uint32_t callback = (uintptr_t)format_callback; +#else + uint32_t callback = 0x11110000u; +#endif + assert(!invoke(device, 4, 3, callback, 0u) && format_mask == 3); + format_count = 0; + assert(!invoke(device, 4, 3, callback, 1u) && format_count == 1); + const uint32_t states[] = {1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 16, 17, 18, 21}; + const uint32_t defaults[] = {4, 2, 1, 2, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1}; + for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) { + assert(!invoke(device, 36, 4, 0u, states[i], workspace())); + assert(*memory(workspace()) == defaults[i]); + } + assert(!invoke(device, 34, 3, 0u, workspace()) && !*memory(workspace())); + assert(!invoke(device, 21, 3, 19u, workspace()) && *memory(workspace()) == 2); + assert(!invoke(device, 21, 3, 20u, workspace()) && *memory(workspace()) == 1); + assert(!invoke(device, 21, 3, 27u, workspace()) && !*memory(workspace())); + const uint32_t texels[] = { + 0xffff0000, 0x8000ff00, 0x400000ff, 0x00ffffff, + 0xff203040, 0x8090a0b0, 0x40ff00ff, 0x0000ffff, + 0xff804020, 0x80108030, 0x40204080, 0x00ffff00, + 0xff000000, 0x80777777, 0x40bbbbbb, 0x00336699 + }; + + for (unsigned alpha = 0; alpha < 2; ++alpha) { + memset(memory(desc), 0, 124); + *memory(desc) = 124; *memory(desc + 4) = 0x1007; + *memory(desc + 8) = 4; *memory(desc + 12) = 4; *memory(desc + 104) = 0x1000; + const uint32_t format[] = {32, 0x40 | alpha, 0, 32, 0xff0000, 0xff00, 0xff, alpha ? 0xff000000 : 0}; + 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)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert(!memcmp(memory(desc + 72), format, 32)); + for (unsigned y = 0; y < 4; ++y) memcpy(memory(pixels + y * pitch), texels + y * 4, 16); + assert(!invoke(texture, 32, 2, 0u)); + assert(!invoke(device, 35, 3, 0u, texture)); + assert(!invoke(device, 35, 3, 0u, texture)); + assert(!invoke(device, 34, 3, 0u, workspace()) && *memory(workspace()) == texture); + assert(invoke(texture, 2, 1) == 2); + assert(invoke(texture, 2, 1) == 1); + + const uint32_t blends[][3] = {{0, 2, 1}, {1, 5, 6}, {1, 6, 5}, {1, 1, 2}, {1, 2, 1}}; + for (unsigned scenario = 0; scenario < 40; ++scenario) { + uint32_t filter = scenario & 1 ? 2 : 1, address = scenario & 2 ? 3 : 1; + float span = scenario & 4 ? 16.25f : 1.75f; + struct vertex { float x, y, z, rhw; uint32_t color; float u, v; } vertices[] = { + {0, 0, .25f, 1, 0x4080c0ff, -.25f, -.375f}, + {32, 0, .25f, .5f, 0x8080c0ff, span, -.375f}, + {0, 32, .25f, 2, 0xc080c0ff, -.25f, span} + }; + memcpy(memory(input), vertices, sizeof(vertices)); + assert(!invoke(device, 37, 4, 0u, 16u, filter)); + assert(!invoke(device, 37, 4, 0u, 17u, filter)); + assert(!invoke(device, 37, 4, 0u, 18u, (scenario & 4) ? 2u : 1u)); + assert(!invoke(device, 37, 4, 0u, 1u, (scenario & 3) == 3 ? 2u : 4u)); + assert(!invoke(device, 37, 4, 0u, 4u, scenario & 2 ? 4u : 2u)); + assert(!invoke(device, 37, 4, 0u, 6u, scenario & 4 ? 0u : 1u)); + assert(!invoke(device, 36, 4, 0u, 6u, workspace()) && *memory(workspace()) == (scenario & 4 ? 0u : 1u)); + assert(!invoke(device, 20, 3, 27u, blends[scenario / 8][0])); + assert(!invoke(device, 20, 3, 19u, blends[scenario / 8][1])); + assert(!invoke(device, 20, 3, 20u, blends[scenario / 8][2])); + assert(!invoke(device, 21, 3, 19u, workspace()) && *memory(workspace()) == blends[scenario / 8][1]); + assert(!invoke(device, 21, 3, 20u, workspace()) && *memory(workspace()) == blends[scenario / 8][2]); + assert(!invoke(device, 37, 4, 0u, 13u, address)); + assert(!invoke(device, 37, 4, 0u, 14u, address)); + assert(!invoke(device, 36, 4, 0u, 13u, workspace()) && *memory(workspace()) == address); + assert(!invoke(device, 36, 4, 0u, 14u, workspace()) && *memory(workspace()) == address); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, 0x144u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + pixels = *memory(desc + 36); pitch = *memory(desc + 16); + + 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); + 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(device, 34, 3, 0u, workspace()) && !*memory(workspace())); + } +} + static void device_lifetime_and_rendering(const char *pixels_path) { const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; @@ -192,6 +316,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) } assert(!invoke(color, 32, 2, 0u)); assert(fwrite(rgb, 1, sizeof(rgb), file) == sizeof(rgb)); + textured_triangles(draw, device, color, file); assert(!fclose(file)); assert(!invoke(device, 2, 1)); assert(!invoke(draw, 2, 1)); @@ -205,6 +330,6 @@ int main(int argc, char **argv) g_esp = 0x5000f000; #endif device_lifetime_and_rendering(argv[1]); - puts("device: ownership, viewport, scenes, GPU clear, transformed triangle and depth rejection passed"); + puts("device: ownership, viewport, scenes, clear, triangles, depth, textures, filtering and blending passed"); return 0; } diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -459,10 +459,12 @@ static int inspect_device(uint32_t description, uint32_t name, uint32_t caps, ui assert(*memory(caps + 120) == 0x600); /* Native 16/24-bit depth buffers. */ assert(!strcmp((char *)memory(description), (const char *)glGetString(GL_RENDERER))); assert(!strcmp((char *)memory(name), "Native OpenGL")); - assert(*memory(caps) == 0x80000u); + 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) == 0); /* No texture stages advertised. */ + assert(*memory(caps + 184) == 0x00010001); /* One texture and one blend stage. */ + assert(*memory(caps + 92) == 0x45 && *memory(caps + 96) == 0x03000303); + assert(*memory(caps + 104) == 0x15 && *memory(caps + 180) == 0xb); 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) == 6144 +assert len(windows_pixels) == len(linux_pixels) == 251904 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 2048 pixels agree within one 8-bit channel step; maximum error:", error) +print("All 83968 pixels agree within one 8-bit channel step; maximum error:", error)