aorecomp

Anarchy Online classic in the browser

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


commit be53db09c6385b87cca1f04b691565a00a779c75
parent a0460eb12aadbb45573b20f9da856b6d232eb9b3
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 08:39:51 +0000

Implement Linux vertex buffers and GDI font rasterization

Diffstat:
Mdecompiled/Makefile.linux | 17+++++++++++++----
Adecompiled/runtime/linux_gdi.c | 334+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/linux_gdi.h | 7+++++++
Mdecompiled/runtime/linux_graphics.c | 147++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mdecompiled/runtime/linux_imports.c | 5+++++
Mdecompiled/tests/devices.c | 44++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/tests/fonts.c | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_fonts.py | 45+++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 724 insertions(+), 8 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Isrc -Idata -DRECOMP_PORTABLE CFLAGS := -O2 -fno-strict-aliasing GAME_SOURCES := $(wildcard src/*/*.c) src/dispatch.c DATA_SOURCES := $(wildcard data/*.c) -RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c +RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/source/*.c) OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES)) @@ -12,10 +12,12 @@ OBJECTS := $(patsubst %.c,build/linux/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNT all: build/linux/game build/linux/game: $(OBJECTS) - $(CC) $(OBJECTS) -lm -pthread -lSDL2 -lGL -o $@ + $(CC) $(OBJECTS) -lm -pthread -lSDL2 -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ objects: $(OBJECTS) +build/linux/runtime/linux_gdi.o: CPPFLAGS += $(shell pkg-config --cflags freetype2 fontconfig) + build/linux/%.o: %.c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@ @@ -128,9 +130,9 @@ build/linux/guest-stat: tests/guest_stat.c runtime/guest_stdio.c runtime/guest_c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ -build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h) +build/linux/dynamic-imports: tests/dynamic_imports.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c $(wildcard runtime/*.h) mkdir -p $(@D) - $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -lSDL2 -lGL -o $@ + $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags freetype2 fontconfig) $(filter %.c,$^) -pthread -lm -lSDL2 -lGL $(shell pkg-config --libs freetype2 fontconfig) -o $@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_platform.h runtime/guest_memory.h mkdir -p $(@D) @@ -169,6 +171,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/system-time ./build/linux/rtti ./build/linux/module-snapshot + ./build/linux/fonts build/linux/font-metrics.bin build/linux/font-pixels.bin ./build/linux/module-filename ./build/linux/delete-file ./build/linux/guest-stdio @@ -248,3 +251,9 @@ build/linux/module-snapshot: tests/module_snapshot.c runtime/linux_snapshot.c ru $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ test: build/linux/module-snapshot + +build/linux/fonts: tests/fonts.c runtime/linux_gdi.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_gdi.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(shell pkg-config --cflags freetype2 fontconfig) $(filter %.c,$^) -lm $(shell pkg-config --libs freetype2 fontconfig) -o $@ + +test: build/linux/fonts diff --git a/decompiled/runtime/linux_gdi.c b/decompiled/runtime/linux_gdi.c @@ -0,0 +1,334 @@ +#include "linux_gdi.h" +#include <fontconfig/fontconfig.h> +#include <ft2build.h> +#include FT_FREETYPE_H +#include FT_TRUETYPE_TABLES_H + +/* GDI objects contain only guest tokens; font faces and raster buffers stay native. */ +static struct font { + uint32_t handle; + FT_Face face; + int ascent, descent, weight, italic; + struct font *next; +} *fonts; +static struct bitmap { + uint32_t handle; + unsigned width, height, pitch; + unsigned char *pixels; + struct bitmap *next; +} *bitmaps; +static struct context { + uint32_t handle, font, bitmap; + struct context *next; +} *contexts; +static FT_Library library; +static uint32_t next_handle = 0xf5000002; + +static struct font *find_font(uint32_t handle) +{ + for (struct font *font = fonts; font; font = font->next) + if (font->handle == handle) return font; + fprintf(stderr, "Unknown GDI font %08x\n", handle); + abort(); +} + +static struct context *find_context(uint32_t handle) +{ + for (struct context *context = contexts; context; context = context->next) + if (context->handle == handle) return context; + fprintf(stderr, "Unknown GDI context %08x\n", handle); + abort(); +} + +static struct bitmap *find_bitmap(uint32_t handle) +{ + for (struct bitmap *bitmap = bitmaps; bitmap; bitmap = bitmap->next) + if (bitmap->handle == handle) return bitmap; + fprintf(stderr, "Unknown GDI bitmap %08x\n", handle); + abort(); +} + +static void check_font(FT_Error error) +{ + if (error) { fprintf(stderr, "FreeType operation failed: %d\n", error); abort(); } +} + +static unsigned big_endian_word(const unsigned char *data) +{ + return (unsigned)data[0] * 256 + data[1]; +} + +static void size_font(struct font *font, int height) +{ + TT_OS2 *os2 = FT_Get_Sfnt_Table(font->face, ft_sfnt_os2); + unsigned em = font->face->units_per_EM; + if (!os2 || !em || !height || height == INT32_MIN) { + fprintf(stderr, "Unsupported font metrics or default height\n"); abort(); + } + unsigned pixels = height < 0 ? -height : (uint64_t)height * em / (os2->usWinAscent + os2->usWinDescent); + if (!pixels) pixels = 1; + font->ascent = (os2->usWinAscent * pixels + em / 2) / em; + font->descent = (os2->usWinDescent * pixels + em / 2) / em; + + /* OpenType VDMX describes the hinted cell bounds used by GDI. Select the + * largest fitting cell for positive heights, or the exact em size for negative. */ + FT_ULong length = 0; + if (!FT_Load_Sfnt_Table(font->face, FT_MAKE_TAG('V','D','M','X'), 0, NULL, &length)) { + unsigned char *table = malloc(length); + if (!table) abort(); + check_font(FT_Load_Sfnt_Table(font->face, FT_MAKE_TAG('V','D','M','X'), 0, table, &length)); + unsigned ratios = length >= 6 ? big_endian_word(table + 4) : 0; + if (6 + ratios * 6 > length) abort(); + for (unsigned i = 0; i < ratios; ++i) { + const unsigned char *ratio = table + 6 + i * 4; + if (ratio[1] && (ratio[1] < ratio[2] || ratio[1] > ratio[3])) continue; + unsigned group = big_endian_word(table + 6 + ratios * 4 + i * 2); + if (group + 4 > length) abort(); + unsigned records = big_endian_word(table + group); + if (group + 4 + records * 6 > length) abort(); + for (unsigned j = 0; j < records; ++j) { + const unsigned char *record = table + group + 4 + j * 6; + unsigned ppem = big_endian_word(record); + int top = (int16_t)big_endian_word(record + 2), bottom = (int16_t)big_endian_word(record + 4); + if (height < 0 ? ppem == (unsigned)-height : top - bottom <= height) { + pixels = ppem; font->ascent = top; font->descent = -bottom; + if (height < 0 || top - bottom == height) break; + } else if (height > 0) break; + } + break; + } + free(table); + } + check_font(FT_Set_Pixel_Sizes(font->face, 0, pixels)); +} + +static void create_font(void) +{ + int height = MEM32(g_esp + 4), weight = MEM32(g_esp + 20); + uint32_t italic = MEM32(g_esp + 24), face = MEM32(g_esp + 56); + if (MEM32(g_esp + 8) || MEM32(g_esp + 12) || MEM32(g_esp + 16) || + MEM32(g_esp + 28) || MEM32(g_esp + 32) || MEM32(g_esp + 48) != 3 || + MEM32(g_esp + 36) > 1) { + fprintf(stderr, "Unsupported GDI font rotation, decoration, charset or quality\n"); abort(); + } + if (!library) check_font(FT_Init_FreeType(&library)); + FcPattern *pattern = FcPatternCreate(); + FcPatternAddString(pattern, FC_FAMILY, (const FcChar8 *)ADDR(face)); + FcPatternAddInteger(pattern, FC_WEIGHT, weight >= 700 ? FC_WEIGHT_BOLD : FC_WEIGHT_REGULAR); + FcPatternAddInteger(pattern, FC_SLANT, italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN); + FcConfigSubstitute(NULL, pattern, FcMatchPattern); + FcDefaultSubstitute(pattern); + FcResult result; + FcPattern *match = FcFontMatch(NULL, pattern, &result); + FcPatternDestroy(pattern); + FcChar8 *path; + int index = 0; + if (!match || FcPatternGetString(match, FC_FILE, 0, &path) != FcResultMatch) abort(); + FcPatternGetInteger(match, FC_INDEX, 0, &index); + struct font *font = calloc(1, sizeof(*font)); + if (!font) abort(); + check_font(FT_New_Face(library, (const char *)path, index, &font->face)); + FcPatternDestroy(match); + font->handle = next_handle++; + font->weight = weight ? weight : 400; + font->italic = italic != 0; + size_font(font, height); + font->next = fonts; fonts = font; + g_eax = font->handle; + g_esp += 60; +} + +static void create_context(unsigned arguments) +{ + struct context *context = calloc(1, sizeof(*context)); + if (!context) abort(); + context->handle = next_handle++; + context->font = 0xf5000000; + context->bitmap = 0xf5000001; + context->next = contexts; contexts = context; + g_eax = context->handle; + g_esp += (arguments + 1) * 4; +} + +static void create_ic(void) { create_context(4); } +static void create_compatible_dc(void) { create_context(1); } + +static void delete_dc(void) +{ + uint32_t handle = MEM32(g_esp + 4); + struct context **link = &contexts; + while (*link && (*link)->handle != handle) link = &(*link)->next; + g_eax = *link != NULL; + if (*link) { struct context *context = *link; *link = context->next; free(context); } + g_esp += 8; +} + +static void select_object(void) +{ + struct context *context = find_context(MEM32(g_esp + 4)); + uint32_t handle = MEM32(g_esp + 8); + g_eax = 0; + int is_font = handle == 0xf5000000, is_bitmap = handle == 0xf5000001; + for (struct font *font = fonts; font; font = font->next) if (font->handle == handle) is_font = 1; + for (struct bitmap *bitmap = bitmaps; bitmap; bitmap = bitmap->next) if (bitmap->handle == handle) is_bitmap = 1; + if (is_font) { g_eax = context->font; context->font = handle; } + else if (is_bitmap) { g_eax = context->bitmap; context->bitmap = handle; } + g_esp += 12; +} + +static void delete_object(void) +{ + uint32_t handle = MEM32(g_esp + 4); + struct font **font = &fonts; + while (*font && (*font)->handle != handle) font = &(*font)->next; + struct bitmap **bitmap = &bitmaps; + while (*bitmap && (*bitmap)->handle != handle) bitmap = &(*bitmap)->next; + g_eax = 0; + if (*font) { + struct font *object = *font; *font = object->next; + check_font(FT_Done_Face(object->face)); free(object); g_eax = 1; + } else if (*bitmap) { + struct bitmap *object = *bitmap; *bitmap = object->next; + free(object->pixels); free(object); g_eax = 1; + } + g_esp += 8; +} + +static void text_metrics(void) +{ + struct font *font = find_font(find_context(MEM32(g_esp + 4))->font); + uint32_t output = MEM32(g_esp + 8); + TT_OS2 *os2 = FT_Get_Sfnt_Table(font->face, ft_sfnt_os2); + unsigned ppem = font->face->size->metrics.y_ppem, em = font->face->units_per_EM; + int height = font->ascent + font->descent; + const uint32_t values[] = {height, font->ascent, font->descent, height > ppem ? height - ppem : 0, + 0, (os2->xAvgCharWidth * ppem + em / 2) / em, + ((font->face->bbox.xMax - font->face->bbox.xMin) * ppem + em / 2) / em, + font->weight, 0, 96, 96}; + memcpy((void *)ADDR(output), values, sizeof(values)); + const unsigned char chars[] = {30, 255, 31, 32, font->italic ? 255 : 0, 0, 0, 39, 0}; + memcpy((void *)ADDR(output + 44), chars, sizeof(chars)); + g_eax = 1; + g_esp += 12; +} + +static void text_extent(int wide) +{ + struct font *font = find_font(find_context(MEM32(g_esp + 4))->font); + uint32_t text = MEM32(g_esp + 8), count = MEM32(g_esp + 12), output = MEM32(g_esp + 16); + int width = 0; + for (uint32_t i = 0; i < count; ++i) { + uint32_t character = wide ? MEM16(text + i * 2) : MEM8(text + i); + check_font(FT_Load_Char(font->face, character, FT_LOAD_TARGET_MONO)); + width += font->face->glyph->advance.x / 64; + } + MEM32(output) = width; + MEM32(output + 4) = count ? font->ascent + font->descent : 0; + g_eax = 1; + g_esp += 20; +} + +static void text_extent_a(void) { text_extent(0); } +static void text_extent_w(void) { text_extent(1); } + +static void create_bitmap(void) +{ + int32_t width = MEM32(g_esp + 4), height = MEM32(g_esp + 8); + uint32_t planes = MEM32(g_esp + 12), bits = MEM32(g_esp + 16), input = MEM32(g_esp + 20); + g_esp += 24; + if (planes != 1 || bits != 1) { fprintf(stderr, "Only monochrome GDI bitmaps are implemented\n"); abort(); } + g_eax = 0; + if (width <= 0 || height <= 0) return; + struct bitmap *bitmap = calloc(1, sizeof(*bitmap)); + if (!bitmap) abort(); + bitmap->width = width; bitmap->height = height; + bitmap->pitch = ((uint64_t)width + 15) / 16 * 2; + bitmap->pixels = calloc(height, bitmap->pitch); + if (!bitmap->pixels) { free(bitmap); return; } + if (input) memcpy(bitmap->pixels, (void *)ADDR(input), (size_t)height * bitmap->pitch); + bitmap->handle = next_handle++; + bitmap->next = bitmaps; bitmaps = bitmap; + g_eax = bitmap->handle; +} + +static void text_out_w(void) +{ + struct context *context = find_context(MEM32(g_esp + 4)); + struct font *font = find_font(context->font); + struct bitmap *bitmap = find_bitmap(context->bitmap); + int32_t x = MEM32(g_esp + 8), y = MEM32(g_esp + 12); + uint32_t text = MEM32(g_esp + 16), count = MEM32(g_esp + 20); + int width = 0; + for (uint32_t i = 0; i < count; ++i) { + check_font(FT_Load_Char(font->face, MEM16(text + i * 2), FT_LOAD_TARGET_MONO)); + width += font->face->glyph->advance.x / 64; + } + + /* MM_TEXT defaults: black text, opaque white background, top-left alignment. */ + for (int row = y < 0 ? 0 : y; row < y + font->ascent + font->descent && row < bitmap->height; ++row) + for (int column = x < 0 ? 0 : x; column < x + width && column < bitmap->width; ++column) + bitmap->pixels[row * bitmap->pitch + column / 8] |= 0x80 >> (column & 7); + int pen = x; + for (uint32_t i = 0; i < count; ++i) { + check_font(FT_Load_Char(font->face, MEM16(text + i * 2), FT_LOAD_RENDER | FT_LOAD_TARGET_MONO)); + FT_GlyphSlot glyph = font->face->glyph; + for (unsigned row = 0; row < glyph->bitmap.rows; ++row) { + int target_y = y + font->ascent - glyph->bitmap_top + row; + if (target_y < 0 || target_y >= bitmap->height) continue; + const unsigned char *line = glyph->bitmap.buffer + row * glyph->bitmap.pitch; + for (unsigned column = 0; column < glyph->bitmap.width; ++column) { + int target_x = pen + glyph->bitmap_left + column; + if (target_x < 0 || target_x >= bitmap->width) continue; + if (line[column / 8] & (0x80 >> (column & 7))) + bitmap->pixels[target_y * bitmap->pitch + target_x / 8] &= ~(0x80 >> (target_x & 7)); + } + } + pen += glyph->advance.x / 64; + } + g_eax = 1; + g_esp += 24; +} + +static void get_di_bits(void) +{ + find_context(MEM32(g_esp + 4)); + struct bitmap *bitmap = find_bitmap(MEM32(g_esp + 8)); + uint32_t start = MEM32(g_esp + 12), count = MEM32(g_esp + 16), output = MEM32(g_esp + 20); + uint32_t info = MEM32(g_esp + 24), usage = MEM32(g_esp + 28); + g_esp += 32; + if (usage || MEM32(info) != 40 || MEM16(info + 12) != 1 || MEM16(info + 14) != 1 || + MEM32(info + 4) != bitmap->width || MEM32(info + 16)) { + fprintf(stderr, "Unsupported GDI DIB conversion\n"); abort(); + } + int32_t height = MEM32(info + 8); + if ((int64_t)height != bitmap->height && -(int64_t)height != bitmap->height) abort(); + unsigned pitch = (bitmap->width + 31) / 32 * 4; + MEM32(info + 20) = pitch * bitmap->height; + MEM32(info + 40) = 0; MEM32(info + 44) = 0xffffff; + g_eax = 0; + if (start >= bitmap->height) return; + if (count > bitmap->height - start) count = bitmap->height - start; + if (output) { + for (unsigned i = 0; i < count; ++i) { + unsigned row = height < 0 ? start + i : bitmap->height - start - i - 1; + memset((void *)ADDR(output + i * pitch), 0, pitch); + memcpy((void *)ADDR(output + i * pitch), bitmap->pixels + row * bitmap->pitch, (bitmap->width + 7) / 8); + } + } + g_eax = count; +} + +recomp_func_t linux_gdi_lookup(const char *name) +{ + static const struct { const char *name; recomp_func_t function; } functions[] = { + {"CreateFontA", create_font}, {"CreateICA", create_ic}, + {"CreateCompatibleDC", create_compatible_dc}, {"DeleteDC", delete_dc}, + {"SelectObject", select_object}, {"DeleteObject", delete_object}, + {"GetTextMetricsA", text_metrics}, {"GetTextExtentPoint32A", text_extent_a}, + {"GetTextExtentPoint32W", text_extent_w}, + {"CreateBitmap", create_bitmap}, {"TextOutW", text_out_w}, {"GetDIBits", get_di_bits}, + }; + for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) + if (!strcmp(name, functions[i].name)) return functions[i].function; + return NULL; +} diff --git a/decompiled/runtime/linux_gdi.h b/decompiled/runtime/linux_gdi.h @@ -0,0 +1,7 @@ +#ifndef LINUX_GDI_H +#define LINUX_GDI_H +#include "recomp_types.h" + +recomp_func_t linux_gdi_lookup(const char *name); + +#endif diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -50,8 +50,13 @@ static struct device_object { struct surface_object *texture; struct device_object *next; } *devices; -static uint32_t vtables[7]; -static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9, 49}; +static struct vertex_buffer { + uint32_t interface, references, caps, fvf, count, size, data; + struct draw_object *owner; + struct vertex_buffer *next; +} *vertex_buffers; +static uint32_t vtables[8]; +static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9, 49, 9}; static unsigned current_interface, current_slot; static void close_graphics(void) @@ -1527,9 +1532,133 @@ static void device_texture_formats(void) g_esp += 16; } +static struct vertex_buffer *find_vertex_buffer(uint32_t address) +{ + for (struct vertex_buffer *buffer = vertex_buffers; buffer; buffer = buffer->next) + if (buffer->interface == address) return buffer; + fprintf(stderr, "Invalid guest vertex buffer %08x\n", address); + abort(); +} + +static unsigned vertex_stride(uint32_t fvf) +{ + unsigned position = fvf & 14, textures = (fvf >> 8) & 15; + if (!position || textures > 8 || (fvf & 0xf001)) return 0; + unsigned size = position == 2 ? 12 : position == 4 ? 16 : (position / 2 + 1) * 4; + if (fvf & 16) size += 12; + if (fvf & 32) size += 4; + if (fvf & 64) size += 4; + if (fvf & 128) size += 4; + const unsigned dimensions[] = {2, 3, 4, 1}; + for (unsigned i = 0; i < textures; ++i) size += dimensions[(fvf >> (16 + i * 2)) & 3] * 4; + return size; +} + +static void create_vertex_buffer(void) +{ + struct draw_object *owner = find_object(MEM32(g_esp + 4)); + uint32_t desc = MEM32(g_esp + 8), output = MEM32(g_esp + 12), flags = MEM32(g_esp + 16); + g_esp += 20; + g_eax = 0x80070057u; + if (!output) return; + MEM32(output) = 0; + if (!desc || MEM32(desc) != 16 || flags) return; + uint32_t caps = MEM32(desc + 4), fvf = MEM32(desc + 8), count = MEM32(desc + 12); + unsigned stride = vertex_stride(fvf); + if (!stride || !count || count > UINT32_MAX / stride) return; + if (caps & ~0x10801u) { fprintf(stderr, "Unsupported vertex buffer caps %08x\n", caps); exit(3); } + struct vertex_buffer *buffer = calloc(1, sizeof(*buffer)); + if (!buffer) abort(); + buffer->size = stride * count; + buffer->data = guest_malloc(buffer->size); + if (!buffer->data) { free(buffer); g_eax = 0x8007000eu; return; } + buffer->interface = create_interface(7); + buffer->references = 1; + buffer->caps = caps; buffer->fvf = fvf; buffer->count = count; + buffer->owner = owner; + ++owner->references[1]; + buffer->next = vertex_buffers; + vertex_buffers = buffer; + MEM32(output) = buffer->interface; + g_eax = 0; +} + +static void vertex_buffer_query_interface(void) +{ + struct vertex_buffer *buffer = find_vertex_buffer(MEM32(g_esp + 4)); + uint32_t iid = MEM32(g_esp + 8), output = MEM32(g_esp + 12); + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + const uint32_t buffer7[] = {0xf5049e7d, 0x11d24861, 0xa00007a4, 0xa82906c9}; + g_esp += 16; + g_eax = 0x80070057u; + if (!output) return; + MEM32(output) = 0; + g_eax = 0x80004002u; + if (memcmp((void *)ADDR(iid), unknown, 16) && memcmp((void *)ADDR(iid), buffer7, 16)) return; + MEM32(output) = buffer->interface; + ++buffer->references; + g_eax = 0; +} + +static void vertex_buffer_add_reference(void) +{ + g_eax = ++find_vertex_buffer(MEM32(g_esp + 4))->references; + g_esp += 8; +} + +static void vertex_buffer_release_reference(void) +{ + struct vertex_buffer *buffer = find_vertex_buffer(MEM32(g_esp + 4)); + g_eax = --buffer->references; + if (!g_eax) { + struct vertex_buffer **link = &vertex_buffers; + while (*link != buffer) link = &(*link)->next; + *link = buffer->next; + release_draw(buffer->owner, 1); + guest_free(buffer->data); + guest_free(buffer->interface); + free(buffer); + } + g_esp += 8; +} + +static void vertex_buffer_lock(void) +{ + struct vertex_buffer *buffer = find_vertex_buffer(MEM32(g_esp + 4)); + uint32_t flags = MEM32(g_esp + 8), output = MEM32(g_esp + 12), size = MEM32(g_esp + 16); + g_esp += 20; + g_eax = 0x80070057u; + if (!output) return; + if (flags & ~0x2831u) { fprintf(stderr, "Unsupported vertex buffer lock flags %08x\n", flags); exit(3); } + MEM32(output) = buffer->data; + if (size) MEM32(size) = buffer->size; + g_eax = 0; +} + +static void vertex_buffer_unlock(void) +{ + find_vertex_buffer(MEM32(g_esp + 4)); + g_eax = 0; + g_esp += 8; +} + +static void vertex_buffer_description(void) +{ + struct vertex_buffer *buffer = find_vertex_buffer(MEM32(g_esp + 4)); + uint32_t output = MEM32(g_esp + 8); + g_eax = 0x80070057u; + if (output) { + MEM32(output + 4) = buffer->caps; + MEM32(output + 8) = buffer->fvf; + MEM32(output + 12) = buffer->count; + g_eax = 0; + } + g_esp += 12; +} + static void unavailable_method(void) { - const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7"}; + const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7", "IDirect3DVertexBuffer7"}; fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n", names[current_interface], current_slot, g_cur_func); exit(3); @@ -1537,10 +1666,19 @@ static void unavailable_method(void) recomp_func_t linux_graphics_virtual(uint32_t address) { - if (address < 0xf3000000u || address >= 0xf3000700u || (address & 3)) return NULL; + if (address < 0xf3000000u || address >= 0xf3000800u || (address & 3)) return NULL; current_interface = (address - 0xf3000000u) / 0x100; current_slot = (address & 0xff) / 4; if (current_slot >= interface_slots[current_interface]) return NULL; + if (current_interface == 7) { + if (current_slot == 0) return vertex_buffer_query_interface; + if (current_slot == 1) return vertex_buffer_add_reference; + if (current_slot == 2) return vertex_buffer_release_reference; + if (current_slot == 3) return vertex_buffer_lock; + if (current_slot == 4) return vertex_buffer_unlock; + if (current_slot == 6) return vertex_buffer_description; + return unavailable_method; + } if (current_interface == 6) { if (current_slot == 0) return device_query_interface; if (current_slot == 1) return device_add_reference; @@ -1601,6 +1739,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 2) return release_reference; if (current_interface == 2 && current_slot == 3) return enumerate_devices; if (current_interface == 2 && current_slot == 4) return create_device; + if (current_interface == 2 && current_slot == 5) return create_vertex_buffer; if (current_interface == 1 && current_slot == 4) return create_clipper; if (current_interface == 1 && current_slot == 6) return create_surface; if (current_interface == 1 && current_slot == 8) return enumerate_display_modes; diff --git a/decompiled/runtime/linux_imports.c b/decompiled/runtime/linux_imports.c @@ -16,6 +16,7 @@ #include "linux_network.h" #include "linux_graphics.h" #include "linux_window.h" +#include "linux_gdi.h" #include <strings.h> /* External calls receive stable guest identifiers rather than host code addresses. */ @@ -156,6 +157,10 @@ recomp_func_t recomp_lookup_import(uint32_t va) if (function) return function; } if (imports[current_import].name) { + if (!strcasecmp(imports[current_import].dll, "gdi32.dll")) { + recomp_func_t function = linux_gdi_lookup(imports[current_import].name); + if (function) return function; + } recomp_func_t function = linux_platform_lookup(imports[current_import].dll, imports[current_import].name); if (function) return function; function = linux_window_lookup(imports[current_import].dll, imports[current_import].name); diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c @@ -346,6 +346,49 @@ static void material_state_is_copied(uint32_t device) assert(invoke(device, 17, 2, 0u) == 0x80070057u); } +static void vertex_buffers_preserve_data_and_description(uint32_t d3d) +{ + const uint32_t formats[] = {0x112, 0x1c4, 0x44, 0x242, 0x00030112}; + const uint32_t strides[] = {32, 32, 20, 32, 28}; + const uint32_t buffer_iid[] = {0xf5049e7d, 0x11d24861, 0xa00007a4, 0xa82906c9}; + const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000}; + for (unsigned i = 0; i < 5; ++i) { + uint32_t desc = workspace() + 128; + uint32_t values[] = {16, 0x800, formats[i], 17}; + memcpy(memory(desc), values, 16); + assert(!invoke(d3d, 5, 4, desc, workspace(), 0u)); + uint32_t buffer = *memory(workspace()); + assert(buffer); + assert(!invoke(buffer, 4, 1)); + + assert(!invoke(buffer, 3, 4, 1u, workspace(), workspace() + 4)); + uint32_t data = *memory(workspace()), size = *memory(workspace() + 4); + assert(size == strides[i] * 17); + assert(!invoke(buffer, 3, 4, 1u, workspace(), 0u)); + assert(*memory(workspace()) == data); + for (unsigned n = 0; n < size; ++n) ((unsigned char *)memory(data))[n] = (n * 37) & 255; + assert(!invoke(buffer, 4, 1)); + assert(!invoke(buffer, 3, 4, 0x11u, workspace(), 0u)); + data = *memory(workspace()); + + for (unsigned n = 0; n < size; ++n) assert(((unsigned char *)memory(data))[n] == ((n * 37) & 255)); + assert(!invoke(buffer, 4, 1)); + assert(!invoke(buffer, 4, 1)); + memset(memory(desc), 0xa5, 20); + *memory(desc) = 0; + assert(!invoke(buffer, 6, 2, desc)); + values[0] = 0; /* The driver preserves the caller's size field. */ + assert(!memcmp(memory(desc), values, 16) && *memory(desc + 16) == 0xa5a5a5a5); + assert(query(buffer, buffer_iid) == buffer); + assert(query(buffer, unknown) == buffer); + assert(invoke(buffer, 2, 1) == 2); + assert(invoke(buffer, 2, 1) == 1); + assert(invoke(buffer, 1, 1) == 2); + assert(invoke(buffer, 2, 1) == 1); + assert(!invoke(buffer, 2, 1)); + } +} + static void device_lifetime_and_rendering(const char *pixels_path) { const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; @@ -367,6 +410,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) uint32_t draw = *memory(workspace()); assert(!invoke(draw, 20, 3, 0u, 0x808u)); uint32_t d3d = query(draw, d3d_iid), desc = workspace() + 128; + vertex_buffers_preserve_data_and_description(d3d); memset(memory(desc), 0, 124); *memory(desc) = 124; *memory(desc + 4) = 7; *memory(desc + 8) = 32; *memory(desc + 12) = 32; *memory(desc + 104) = 0x6040; diff --git a/decompiled/tests/fonts.c b/decompiled/tests/fonts.c @@ -0,0 +1,133 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +static unsigned char scratch[65536]; +#else +#include "linux_gdi.h" +#endif + +static uint32_t workspace(void) +{ +#ifdef _WIN32 + return (uintptr_t)scratch; +#else + return 0x50000000; +#endif +} + +static void *memory(uint32_t address) +{ +#ifdef _WIN32 + return (void *)(uintptr_t)address; +#else + return (void *)ADDR(address); +#endif +} + +static uint32_t call(const char *name, unsigned count, ...) +{ + uint32_t args[14]; + va_list list; + va_start(list, count); + for (unsigned i = 0; i < count; ++i) args[i] = va_arg(list, uint32_t); + va_end(list); +#ifdef _WIN32 + FARPROC function = GetProcAddress(GetModuleHandleA("gdi32.dll"), name); + assert(function); + if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))function)(args[0]); + if (count == 2) return ((uint32_t (WINAPI *)(uint32_t,uint32_t))function)(args[0],args[1]); + if (count == 4) return ((uint32_t (WINAPI *)(uint32_t,uint32_t,uint32_t,uint32_t))function)(args[0],args[1],args[2],args[3]); + if (count == 5) return ((uint32_t (WINAPI *)(uint32_t,uint32_t,uint32_t,uint32_t,uint32_t))function)(args[0],args[1],args[2],args[3],args[4]); + if (count == 7) return ((uint32_t (WINAPI *)(uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t))function)(args[0],args[1],args[2],args[3],args[4],args[5],args[6]); + assert(count == 14); + return ((uint32_t (WINAPI *)(uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t,uint32_t))function)(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13]); +#else + g_esp = 0x5000ff00; + for (unsigned i = 0; i < count; ++i) MEM32(g_esp + 4 + i * 4) = args[i]; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + recomp_func_t function = linux_gdi_lookup(name); + assert(function); + function(); + assert(g_esp == 0x5000ff00 + (count + 1) * 4); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); + return g_eax; +#endif +} + +static void font_metrics_and_monochrome_glyphs(FILE *metrics, FILE *pixels) +{ + uint32_t base = workspace(); + strcpy(memory(base), "Verdana"); + strcpy(memory(base + 64), "DISPLAY"); + uint32_t dc = call("CreateICA", 4, base + 64, 0u, 0u, 0u); + assert(dc); + const uint16_t characters[] = {'A', 'g', 'W', 'i', ' ', '@', 0xe9}; + for (unsigned style = 0; style < 4; ++style) { + for (int height = 10; height <= 24; ++height) { + uint32_t font = call("CreateFontA", 14, height, 0u, 0u, 0u, + style & 1 ? 700u : 400u, style >> 1, 0u, 0u, 1u, 7u, 0u, 3u, 0u, base); + assert(font); + uint32_t previous = call("SelectObject", 2, dc, font); + assert(previous); + memset(memory(base + 128), 0xa5, 60); + + assert(call("GetTextMetricsA", 2, dc, base + 132)); + uint32_t *values = memory(base + 132); + assert(fwrite(values, 1, 53, metrics) == 53); + + assert(*(uint32_t *)memory(base + 128) == 0xa5a5a5a5); + assert(*(unsigned char *)memory(base + 185) == 0xa5); + assert(values[0] == values[1] + values[2] && values[0] <= (unsigned)height); + if (height == 17 && !style) assert(values[0] == 17 && values[1] == 14 && values[2] == 3 && values[3] == 3); + + for (unsigned i = 0; i < 7; ++i) { + uint32_t bitmap_dc = call("CreateCompatibleDC", 1, 0u); + memset(memory(base + 1024), 0xff, 512); + uint32_t bitmap = call("CreateBitmap", 5, 64u, 64u, 1u, 1u, base + 1024); + assert(bitmap_dc && bitmap); + uint32_t old_bitmap = call("SelectObject", 2, bitmap_dc, bitmap); + uint32_t old_font = call("SelectObject", 2, bitmap_dc, font); + *(uint16_t *)memory(base + 200) = characters[i]; + uint32_t header[] = {40, 64, (uint32_t)-64, 0x10001, 0, 0, 0, 0, 0, 0, 0, 0}; + memcpy(memory(base + 256), header, sizeof(header)); + + assert(call("TextOutW", 5, bitmap_dc, 0u, 0u, base + 200, 1u)); + assert(call("GetTextExtentPoint32W", 4, bitmap_dc, base + 200, 1u, base + 208)); + assert(fwrite(memory(base + 208), 1, 8, metrics) == 8); + assert(call("SelectObject", 2, bitmap_dc, old_font) == font); + assert(call("SelectObject", 2, bitmap_dc, old_bitmap) == bitmap); + assert(call("GetDIBits", 7, bitmap_dc, bitmap, 0u, 64u, base + 1024, base + 256, 0u) == 64); + + unsigned dark = 0; + for (unsigned n = 0; n < 512; ++n) if (((unsigned char *)memory(base + 1024))[n] != 255) ++dark; + assert(characters[i] == ' ' ? dark == 0 : dark > 0); + assert(fwrite(memory(base + 1024), 1, 512, pixels) == 512); + assert(call("DeleteDC", 1, bitmap_dc)); + assert(call("DeleteObject", 1, bitmap)); + } + assert(call("SelectObject", 2, dc, previous) == font); + assert(call("DeleteObject", 1, font)); + } + } + assert(call("DeleteDC", 1, dc)); +} + +int main(int argc, char **argv) +{ + assert(argc == 3); +#ifdef _WIN32 + assert(LoadLibraryA("gdi32.dll")); +#else + assert(guest_map("font fixture", 0x50000000, 0x10000)); +#endif + FILE *metrics = fopen(argv[1], "wb"), *pixels = fopen(argv[2], "wb"); + assert(metrics && pixels); + font_metrics_and_monochrome_glyphs(metrics, pixels); + assert(!fclose(metrics) && !fclose(pixels)); + puts("Fonts: 60 Verdana sizes/styles, 420 monochrome glyphs, layout, ownership and ABI passed"); +} diff --git a/tools/reconstruction/test_portable_fonts.py b/tools/reconstruction/test_portable_fonts.py @@ -0,0 +1,45 @@ +"""Compare the AO monochrome font path against Windows GDI using local Verdana.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-fonts" +work.mkdir(parents=True, exist_ok=True) +reference = work / "reference.exe" +subprocess.run([ + str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), + "-O2", "tests/fonts.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference), str(work / "reference.metrics"), str(work / "reference.bits")], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +linux_work = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/fonts"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/fonts", linux_work + "/native.metrics", linux_work + "/native.bits"], text=True, timeout=30) +assert actual == expected, (expected, actual) +assert (work / "reference.metrics").read_bytes() == (work / "native.metrics").read_bytes(), "font metrics" +reference_data = (work / "reference.bits").read_bytes() +native_data = (work / "native.bits").read_bytes() +assert len(reference_data) == len(native_data) == 420 * 512 + +# GDI and FreeType can round an outline edge to adjacent pixels. Require every +# ink pixel to be within one pixel of reference ink, in both directions; isolated +# stray pixels or missing strokes fail. Measurements above remain byte-exact. +different_pixels = different_glyphs = maximum_difference = 0 +for glyph in range(420): + masks = [] + for data in [reference_data, native_data]: + masks.append({(x, y) for y in range(64) for x in range(64) + if not (data[glyph * 512 + y * 8 + x // 8] & (0x80 >> (x % 8)))}) + difference = len(masks[0] ^ masks[1]) + different_pixels += difference + different_glyphs += difference != 0 + maximum_difference = max(maximum_difference, difference) + for source, target in [(masks[0], masks[1]), (masks[1], masks[0])]: + for x, y in source - target: + assert any((x + dx, y + dy) in target for dx in [-1, 0, 1] for dy in [-1, 0, 1]), (glyph, x, y) +print(actual.strip()) +print("All metrics match Windows exactly; glyphs pass the one-pixel outline comparison.") +print("Different ink pixels:", different_pixels, "across", different_glyphs, "glyphs; maximum per glyph:", maximum_difference)