aorecomp

Anarchy Online classic in the browser

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


commit 67b28ca1f9831ab4134ea06077125698f6dd8acc
parent 1a3967db65d12885ad9b4e05a0e62bab413e3f27
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 01:47:52 +0000

Add native GPU rasterization and advance client to window setup

Diffstat:
Mdecompiled/Makefile.linux | 13+++++++++----
Adecompiled/runtime/gl_renderer.c | 187+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adecompiled/runtime/gl_renderer.h | 21+++++++++++++++++++++
Mdecompiled/runtime/guest_crt.c | 15+++++++++++++++
Mdecompiled/runtime/linux_graphics.c | 54+++++++++++++++++++++++++++++++++++++++++++++++++++---
Adecompiled/tests/atoi.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecompiled/tests/graphics.c | 139++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Atools/reconstruction/test_portable_atoi.py | 26++++++++++++++++++++++++++
Mtools/reconstruction/test_portable_graphics.py | 4++--
9 files changed, 529 insertions(+), 10 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_graphics.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_tls.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_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_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_tls.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_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)) @@ -100,6 +100,10 @@ build/linux/stricmp: tests/stricmp.c runtime/guest_crt.c runtime/guest_heap.c ru mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ +build/linux/atoi: tests/atoi.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + build/linux/platform-diagnostics: tests/platform_diagnostics.c runtime/linux_diagnostics.c runtime/linux_platform.c runtime/guest_crt.c runtime/guest_msvcp.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c src/ffr.dll/lifted-0002.c data/ffr_dll.c runtime/recomp_types.h runtime/platform_diagnostics.h runtime/linux_platform.h runtime/guest_crt.h runtime/guest_msvcp.h runtime/guest_memory.h runtime/guest_heap.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ @@ -124,7 +128,7 @@ 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_graphics.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.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_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_sync.c runtime/linux_tls.c runtime/linux_network.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_format.c runtime/guest_exception.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 $@ @@ -132,11 +136,11 @@ build/linux/create-directory: tests/create_directory.c runtime/linux_platform.c mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ -build/linux/graphics: tests/graphics.c runtime/linux_graphics.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h +build/linux/graphics: tests/graphics.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lSDL2 -lGL -o $@ -test: build/linux/graphics build/linux/stricmp build/linux/stdio-input build/linux/guest-stat build/linux/chmod build/linux/create-directory build/linux/dynamic-imports build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception build/linux/platform-diagnostics build/linux/new-handler +test: build/linux/graphics build/linux/atoi build/linux/stricmp build/linux/stdio-input build/linux/guest-stat build/linux/chmod build/linux/create-directory build/linux/dynamic-imports build/linux/linecrypt build/linux/guest-heap build/linux/guest-crt build/linux/linux-platform build/linux/msvcp-strings build/linux/ace-token build/linux/msvcp-stdio-data build/linux/guest-eh build/linux/guest-rand build/linux/semaphore build/linux/guest-scanf build/linux/network-startup build/linux/os-version build/linux/guest-format build/linux/event build/linux/tls build/linux/module-filename build/linux/delete-file build/linux/guest-stdio build/linux/guest-exception build/linux/platform-diagnostics build/linux/new-handler ./build/linux/linecrypt ./build/linux/guest-heap ./build/linux/guest-crt @@ -165,6 +169,7 @@ test: build/linux/graphics build/linux/stricmp build/linux/stdio-input build/lin ./build/linux/guest-stat ./build/linux/stdio-input ./build/linux/stricmp + ./build/linux/atoi ./build/linux/graphics -include $(OBJECTS:.o=.d) diff --git a/decompiled/runtime/gl_renderer.c b/decompiled/runtime/gl_renderer.c @@ -0,0 +1,187 @@ +#define GL_GLEXT_PROTOTYPES +#include "gl_renderer.h" +#include <SDL2/SDL_opengl.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static GLuint program, vertex_array, vertex_buffer, index_buffer; +struct gl_target { + unsigned width, height; + GLuint framebuffer, color, depth; +}; + +static void check_graphics(const char *operation) +{ + GLenum error = glGetError(); + if (error != GL_NO_ERROR) { + fprintf(stderr, "OpenGL %s failed: %04x\n", operation, error); + abort(); + } +} + +int gl_renderer_initialize(void) +{ + if (program) return 1; + const char *sources[] = { + "#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.xy,2.0*position.z-position.w,position.w); color=diffuse; }\n", + "#version 330 core\n" + "in vec4 color; out vec4 pixel;\n" + "void main() { pixel=color; }\n" + }; + program = glCreateProgram(); + for (unsigned i = 0; i < 2; ++i) { + GLuint shader = glCreateShader(i ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER); + glShaderSource(shader, 1, &sources[i], NULL); + glCompileShader(shader); + GLint success; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + char log[2048]; + glGetShaderInfoLog(shader, sizeof(log), NULL, log); + fprintf(stderr, "Native shader compilation failed: %s\n", log); + glDeleteShader(shader); + gl_renderer_shutdown(); + return 0; + } + glAttachShader(program, shader); + glDeleteShader(shader); + } + glLinkProgram(program); + GLint success; + glGetProgramiv(program, GL_LINK_STATUS, &success); + if (!success) { + char log[2048]; + glGetProgramInfoLog(program, sizeof(log), NULL, log); + fprintf(stderr, "Native shader link failed: %s\n", log); + gl_renderer_shutdown(); + return 0; + } + + /* One streamed vertex/index pair is shared by this single rendering context. */ + glGenVertexArrays(1, &vertex_array); + glGenBuffers(1, &vertex_buffer); + glGenBuffers(1, &index_buffer); + glBindVertexArray(vertex_array); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + 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)); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glBindVertexArray(0); + check_graphics("renderer initialization"); + return 1; +} + +void gl_renderer_shutdown(void) +{ + glDeleteBuffers(1, &index_buffer); + glDeleteBuffers(1, &vertex_buffer); + glDeleteVertexArrays(1, &vertex_array); + glDeleteProgram(program); + index_buffer = vertex_buffer = vertex_array = program = 0; +} + +struct gl_target *gl_target_create(unsigned width, unsigned height) +{ + struct gl_target *target = calloc(1, sizeof(*target)); + if (!target) return NULL; + target->width = width; + target->height = height; + + /* Color and depth belong to the target, so switching targets preserves both. */ + glGenTextures(1, &target->color); + glBindTexture(GL_TEXTURE_2D, target->color); + 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); + glGenRenderbuffers(1, &target->depth); + glBindRenderbuffer(GL_RENDERBUFFER, target->depth); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height); + glGenFramebuffers(1, &target->framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, target->color, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, target->depth); + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + if (status != GL_FRAMEBUFFER_COMPLETE) { + fprintf(stderr, "Native framebuffer creation failed: %04x\n", status); + gl_target_destroy(target); + return NULL; + } + check_graphics("target creation"); + return target; +} + +void gl_target_destroy(struct gl_target *target) +{ + glDeleteFramebuffers(1, &target->framebuffer); + glDeleteRenderbuffers(1, &target->depth); + glDeleteTextures(1, &target->color); + free(target); +} + +void gl_target_clear(struct gl_target *target, uint32_t argb, float depth) +{ + glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); + glDisable(GL_SCISSOR_TEST); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glDepthMask(GL_TRUE); + glClearColor(((argb >> 16) & 255) / 255.0f, ((argb >> 8) & 255) / 255.0f, + (argb & 255) / 255.0f, (argb >> 24) / 255.0f); + glClearDepth(depth); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + check_graphics("target clear"); +} + +void gl_target_draw(struct gl_target *target, const struct gl_vertex *vertices, + unsigned vertex_count, const uint16_t *indices, unsigned index_count) +{ + glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); + glViewport(0, 0, target->width, target->height); + glDisable(GL_SCISSOR_TEST); + glDisable(GL_BLEND); + glDisable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glDepthMask(GL_TRUE); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glUseProgram(program); + glBindVertexArray(vertex_array); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, vertex_count * sizeof(*vertices), vertices, GL_STREAM_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_count * sizeof(*indices), indices, GL_STREAM_DRAW); + glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_SHORT, NULL); + glBindVertexArray(0); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + check_graphics("indexed triangles"); +} + +void gl_target_read(struct gl_target *target, unsigned char *rgba) +{ + glBindFramebuffer(GL_FRAMEBUFFER, target->framebuffer); + glReadPixels(0, 0, target->width, target->height, GL_RGBA, GL_UNSIGNED_BYTE, rgba); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + check_graphics("target readback"); + + /* Guest images are top-down; OpenGL readback starts at the bottom row. */ + size_t pitch = target->width * 4; + unsigned char *row = malloc(pitch); + if (!row) abort(); + for (unsigned y = 0; y < target->height / 2; ++y) { + unsigned char *top = rgba + y * pitch; + unsigned char *bottom = rgba + (target->height - 1 - y) * pitch; + memcpy(row, top, pitch); + memcpy(top, bottom, pitch); + memcpy(bottom, row, pitch); + } + free(row); +} diff --git a/decompiled/runtime/gl_renderer.h b/decompiled/runtime/gl_renderer.h @@ -0,0 +1,21 @@ +#ifndef GL_RENDERER_H +#define GL_RENDERER_H +#include <stdint.h> + +/* These are host objects. Guest addresses are translated by the API adapter. */ +struct gl_target; +struct gl_vertex { + float position[4]; /* D3D clip coordinates: -w <= x,y <= w and 0 <= z <= w. */ + float color[4]; +}; + +int gl_renderer_initialize(void); +void gl_renderer_shutdown(void); +struct gl_target *gl_target_create(unsigned width, unsigned height); +void gl_target_destroy(struct gl_target *target); +void gl_target_clear(struct gl_target *target, uint32_t argb, float depth); +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); + +#endif diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -655,6 +655,20 @@ unsupported: abort(); } +static void crt_atoi(void) +{ + const unsigned char *input = (const unsigned char *)ADDR(MEM32(g_esp + 4)); + MEM32(guest_crt_errno_address()) = 0; + while (*input == ' ' || (*input >= '\t' && *input <= '\r')) ++input; + int negative = *input == '-'; + if (*input == '+' || *input == '-') ++input; + uint32_t value = 0; + /* Legacy MSVCRT wraps at 32 bits, including arbitrarily long inputs. */ + while (*input >= '0' && *input <= '9') value = value * 10u + (*input++ - '0'); + g_eax = negative ? 0u - value : value; + g_esp += 4; +} + recomp_func_t guest_crt_lookup(const char *name) { static const struct { const char *name; recomp_func_t function; } functions[] = { @@ -683,6 +697,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"strrchr", crt_strrchr}, {"_mbsrchr", crt_strrchr}, /* Initial single-byte locale. */ {"memcmp", crt_memcmp}, {"memchr", crt_memchr}, {"sscanf", crt_sscanf}, + {"atoi", crt_atoi}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) if (!strcmp(name, functions[i].name)) return functions[i].function; diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c @@ -1,5 +1,6 @@ #include "linux_graphics.h" #include "guest_heap.h" +#include "gl_renderer.h" #include <SDL2/SDL.h> #include <SDL2/SDL_opengl.h> @@ -16,6 +17,7 @@ static unsigned current_interface, current_slot; static void close_graphics(void) { + if (context) gl_renderer_shutdown(); SDL_GL_DeleteContext(context); SDL_DestroyWindow(window); SDL_QuitSubSystem(SDL_INIT_VIDEO); @@ -36,6 +38,7 @@ static int initialize_graphics(void) if (!window) goto failed; context = SDL_GL_CreateContext(window); if (!context) goto failed; + if (!gl_renderer_initialize()) goto failed; fprintf(stderr, "[linux-graphics] %s; OpenGL %s\n", glGetString(GL_RENDERER), glGetString(GL_VERSION)); atexit(close_graphics); return 1; @@ -159,9 +162,12 @@ static void get_caps(void) uint32_t size = MEM32(outputs[i]); memset((void *)ADDR(outputs[i]), 0, size); MEM32(outputs[i]) = size; - /* The SDL/OpenGL context supports windowed output. Surface operations - * and a software emulation layer are not advertised before implementation. */ - if (!i) MEM32(outputs[i] + 8) = 0x80000u; /* DDCAPS2_CANRENDERWINDOWED */ + /* Native triangles and windowed output are implemented. Guest surface + * operations and a software emulation layer are not advertised yet. */ + if (!i) { + MEM32(outputs[i] + 4) = 1; /* DDCAPS_3D */ + MEM32(outputs[i] + 8) = 0x80000u; /* DDCAPS2_CANRENDERWINDOWED */ + } } g_eax = 0; g_esp += 16; @@ -256,6 +262,47 @@ static void enumerate_adapters(void) g_esp += 16; } +static void enumerate_devices(void) +{ + find_object(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; } + const char *description = (const char *)glGetString(GL_RENDERER); + const char *name = "Native OpenGL"; + size_t description_size = strlen(description) + 1; + uint32_t record = guest_malloc(236 + description_size + strlen(name) + 1); + if (!record) abort(); + memset((void *)ADDR(record), 0, 236); + MEM32(record) = 0x80000; /* D3DDEVCAPS_HWRASTERIZATION */ + 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 + 88) = 8; /* Gouraud RGB interpolation */ + MEM32(record + 116) = 0x100; /* DDBD_32 color */ + MEM32(record + 120) = 0x200; /* DDBD_24 depth */ + /* HAL denotes rasterization without hardware transform-and-lighting. + * Texturing, 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); + memcpy((void *)ADDR(record + 236), description, description_size); + strcpy((char *)ADDR(record + 236 + description_size), name); + recomp_func_t function = recomp_lookup(callback); + if (!function) function = recomp_lookup_import(callback); + uint32_t stack = g_esp, caller = g_cur_func; + PUSH32(g_esp, data); + PUSH32(g_esp, record); + PUSH32(g_esp, record + 236 + description_size); + PUSH32(g_esp, record + 236); + PUSH32(g_esp, RECOMP_RETADDR); + function(); + if (g_esp != stack) { fprintf(stderr, "Direct3D device callback unbalanced the guest stack\n"); abort(); } + g_cur_func = caller; + guest_free(record); + g_eax = 0; + g_esp += 16; +} + static void query_interface(void) { struct draw_object *object = find_object(MEM32(g_esp + 4)); @@ -324,6 +371,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address) if (current_slot == 0) return query_interface; if (current_slot == 1) return add_reference; if (current_slot == 2) return release_reference; + if (current_interface == 2 && current_slot == 3) return enumerate_devices; if (current_interface == 1 && current_slot == 8) return enumerate_display_modes; if (current_slot == 11) return get_caps; if (current_slot == 20) return set_cooperative_level; diff --git a/decompiled/tests/atoi.c b/decompiled/tests/atoi.c @@ -0,0 +1,80 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +static int (__cdecl *convert)(const char *); +static int *error_number; +#else +#include "guest_crt.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { (void)address; abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); } +#endif + +static uint32_t call(const char *input) +{ +#ifdef _WIN32 + *error_number = 77; + uint32_t result = convert(input); + assert(*error_number == 0); + return result; +#else + strcpy((char *)ADDR(0x50000100u), input); + MEM32(guest_crt_errno_address()) = 77; + g_esp = 0x5000ff00u; + MEM32(g_esp + 4) = 0x50000100u; + g_ebx = 0x12345678u; g_esi = 0x23456789u; + g_edi = 0x3456789au; g_ebp = 0x456789abu; + guest_crt_lookup("atoi")(); + assert(g_esp == 0x5000ff04u && MEM32(guest_crt_errno_address()) == 0); + assert(g_ebx == 0x12345678u && g_esi == 0x23456789u); + assert(g_edi == 0x3456789au && g_ebp == 0x456789abu); + return g_eax; +#endif +} + +int main(void) +{ +#ifdef _WIN32 + HMODULE crt = LoadLibraryA("msvcrt.dll"); + convert = (void *)GetProcAddress(crt, "atoi"); + int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(crt, "_errno"); + assert(convert && get_errno); + error_number = get_errno(); +#else + assert(guest_map("atoi test", 0x50000000u, 0x10000u)); + g_fs_base = 0x50000000u; +#endif + const struct { const char *input; uint32_t expected; } cases[] = { + {"", 0}, {" -42junk", 0xffffffd6u}, {"0x10", 0}, + {"2147483647", 0x7fffffffu}, {"2147483648", 0x80000000u}, + {"4294967295", 0xffffffffu}, {"4294967296", 0}, + {"-2147483649", 0x7fffffffu}, {"9223372036854775808", 0}, + {"184467440737095516160", 0}, {"-999999999999999999999", 559939585}, + {"+", 0}, {"\v\f\r\n\t 7", 7}, {"\xa0" "42", 0} + }; + uint32_t results[14], digest = 2166136261u, random = 12345; + + for (unsigned i = 0; i < 14; ++i) results[i] = call(cases[i].input); + for (unsigned c = 0; c < 256; ++c) { + char input[] = {(char)c, '1', '2', '3', 0}; + digest = (digest ^ call(input)) * 16777619u; + } + for (unsigned i = 0; i < 4096; ++i) { + char input[68]; + input[0] = i & 1 ? '+' : '-'; + for (unsigned j = 1; j < 65; ++j) { + random = random * 1664525u + 1013904223u; + input[j] = '0' + random % 10; + } + strcpy(input + 65, "x"); + digest = (digest ^ call(input)) * 16777619u; + } + + for (unsigned i = 0; i < 14; ++i) assert(results[i] == cases[i].expected); + assert(digest == 0xc691d92du); + printf("atoi: 4366 cases, digest %08x, decimal prefixes, 32-bit wrap, errno and ABI passed\n", digest); + return 0; +} diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c @@ -5,6 +5,7 @@ #ifdef _WIN32 #include <windows.h> #include <ddraw.h> +#include <d3d.h> #include <stddef.h> _Static_assert(sizeof(DDDEVICEIDENTIFIER2) == 1072, "guest device identifier size"); _Static_assert(offsetof(DDDEVICEIDENTIFIER2, guidDeviceIdentifier) == 1048, "guest identifier offset"); @@ -12,18 +13,23 @@ _Static_assert(offsetof(DDDEVICEIDENTIFIER2, dwWHQLLevel) == 1064, "guest certif _Static_assert(sizeof(DDCAPS_DX7) == 380, "guest capabilities size"); _Static_assert(sizeof(DDSURFACEDESC2) == 124, "guest mode description size"); _Static_assert(offsetof(DDSURFACEDESC2, ddpfPixelFormat) == 72, "guest pixel format offset"); +_Static_assert(sizeof(D3DDEVICEDESC7) == 236, "guest Direct3D device description size"); +_Static_assert(offsetof(D3DDEVICEDESC7, deviceGUID) == 196, "guest device GUID offset"); static uint32_t scratch[512], create_address, create_ex_address, enumerate_address; #else #include "linux_graphics.h" +#include "gl_renderer.h" #include "guest_memory.h" #include <SDL2/SDL.h> #include <SDL2/SDL_opengl.h> static uint32_t enumerate_address = 1; static void enumeration_callback(void); static void mode_callback(void); +static void device_callback(void); recomp_func_t recomp_lookup(uint32_t address) { if (address == 0x11110004u) return mode_callback; + if (address == 0x11110008u) return device_callback; assert(address == 0x11110000u); return enumeration_callback; } @@ -195,9 +201,10 @@ static void capability_queries_respect_structure_sizes(void) assert(result == 0 && *memory(output) == sizes[i]); assert(*memory(output + sizes[i]) == 0xccccccccu); #ifndef _WIN32 + assert(*memory(output + 4) == 1); assert(*memory(output + 8) == 0x80000u); for (unsigned offset = 4; offset < sizes[i]; offset += 4) - if (offset != 8) assert(*memory(output + offset) == 0); + if (offset != 4 && offset != 8) assert(*memory(output + offset) == 0); #endif } assert(invoke(method, 3, object, 0, output) == 0); @@ -429,7 +436,135 @@ static void direct3d_shares_the_directdraw_seven_reference_count(void) assert(ref(draw, 2) == 1 && ref(d3d, 2) == 0); } +static unsigned device_count; +static int inspect_device(uint32_t description, uint32_t name, uint32_t caps, uint32_t data) +{ + assert(data == 0x12345678u); + assert(*(char *)memory(description) && *(char *)memory(name)); + assert(*memory(caps + 4) == 56 && *memory(caps + 60) == 56); + assert(*memory(caps + 116) & 0x100); /* 32-bit color */ +#ifndef _WIN32 + assert(*memory(caps + 120) == 0x200); /* Native target's 24-bit depth. */ + assert(!strcmp((char *)memory(description), (const char *)glGetString(GL_RENDERER))); + assert(!strcmp((char *)memory(name), "Native OpenGL")); + assert(*memory(caps) == 0x80000u); + 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. */ + const uint32_t hal[] = {0x84e63de0, 0x11cf46aa, 0x00006f81, 0x6e1520c0}; + assert(!memcmp(memory(caps + 196), hal, 16)); +#endif + ++device_count; + uint32_t object = create(); + assert(ref(object, 2) == 0); + return 1; +} + +#ifdef _WIN32 +static HRESULT WINAPI device_callback(char *description, char *name, D3DDEVICEDESC7 *caps, void *data) +{ + return inspect_device((uintptr_t)description, (uintptr_t)name, (uintptr_t)caps, (uintptr_t)data); +} +#else +static void device_callback(void) +{ + g_cur_func = 0x11110008u; + g_eax = inspect_device(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16)); + g_esp += 20; +} +#endif + +static void direct3d_enumerates_the_native_renderer(void) +{ + const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797}; + const uint32_t d3d7[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9}; + memcpy(memory(workspace() + 4), draw7, 16); + assert(create_ex(workspace() + 4, workspace(), 0) == 0); + uint32_t draw = *memory(workspace()); + assert(query(draw, d3d7) == 0); + uint32_t d3d = *memory(workspace()), method = memory(*memory(d3d))[3]; +#ifdef _WIN32 + uint32_t callback = (uintptr_t)device_callback; +#else + uint32_t callback = 0x11110008u; +#endif + device_count = 0; + + uint32_t result = invoke(method, 3, d3d, callback, 0x12345678u); + + assert(result == 0 && device_count > 0); #ifndef _WIN32 + assert(device_count == 1); +#endif + assert(invoke(method, 3, d3d, 0, 0) == 0x80070057u); + assert(ref(draw, 2) == 1 && ref(d3d, 2) == 0); +} + +#ifndef _WIN32 +static void indexed_triangles_preserve_depth_and_target_contents(void) +{ + struct gl_target *first = gl_target_create(32, 32), *second = gl_target_create(32, 32); + assert(first && second); + struct gl_vertex vertices[] = { + {{-0.75f, -0.75f, 0.25f, 1}, {0, 1, 0, 1}}, + {{0.75f, -0.75f, 0.25f, 1}, {0, 1, 0, 1}}, + {{0, 0.75f, 0.25f, 1}, {0, 1, 0, 1}} + }; + const uint16_t indices[] = {0, 1, 2}; + unsigned char pixels[32 * 32 * 4]; + + gl_target_clear(first, 0xff102030u, 1.0f); + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_clear(second, 0xff405060u, 1.0f); + for (unsigned i = 0; i < 3; ++i) { + vertices[i].position[2] = 0.75f; + vertices[i].color[0] = 1; + vertices[i].color[1] = 0; + } + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_read(first, pixels); + + const unsigned center = (16 * 32 + 16) * 4; + assert(pixels[center] == 0 && pixels[center + 1] == 255 && pixels[center + 2] == 0 && pixels[center + 3] == 255); + assert(pixels[0] == 16 && pixels[1] == 32 && pixels[2] == 48 && pixels[3] == 255); + /* Top-down readback places the narrow apex above the broad triangle base. */ + assert(pixels[(8 * 32 + 8) * 4] == 16); + assert(pixels[(24 * 32 + 8) * 4 + 1] == 255); + gl_target_read(second, pixels); + assert(pixels[center] == 64 && pixels[center + 1] == 80 && pixels[center + 2] == 96 && pixels[center + 3] == 255); + gl_target_clear(first, 0xff102030u, 0.0f); + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_read(first, pixels); + assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); + gl_target_clear(first, 0xff102030u, 1.0f); + for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = -0.25f; + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_read(first, pixels); + assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); + struct gl_vertex gradient[] = { + {{-1, -1, 0.25f, 1}, {1, 0, 0, 1}}, + {{1, -1, 0.25f, 1}, {0, 1, 0, 1}}, + {{-1, 1, 0.25f, 1}, {0, 0, 1, 1}} + }; + const uint16_t reversed[] = {0, 2, 1}; + gl_target_draw(first, gradient, 3, reversed, 3); + gl_target_read(first, pixels); + unsigned sample = (24 * 32 + 8) * 4; + assert((pixels[sample] == 127 || pixels[sample] == 128) && pixels[sample + 1] == 68 && pixels[sample + 2] == 60); + for (unsigned i = 0; i < 4; ++i) gradient[1].position[i] *= 2; + gl_target_draw(first, gradient, 3, reversed, 3); + gl_target_read(first, pixels); + assert(pixels[sample] == 147 && pixels[sample + 1] == 39 && pixels[sample + 2] == 69); + gl_target_clear(first, 0xff102030u, 1.0f); + for (unsigned i = 0; i < 3; ++i) vertices[i].position[2] = 1.25f; + gl_target_draw(first, vertices, 3, indices, 3); + gl_target_read(first, pixels); + assert(pixels[center] == 16 && pixels[center + 1] == 32 && pixels[center + 2] == 48); + gl_target_destroy(first); + gl_target_destroy(second); + fprintf(stderr, "Native renderer: indexed triangles, depth, clipping, independent targets and top-down readback passed\n"); +} + static void native_context_renders_and_reads_pixels(void) { assert(SDL_GL_GetCurrentContext()); @@ -464,7 +599,9 @@ int main(void) display_modes_describe_the_native_monitor(); independent_interface_counts_share_object_lifetime(); direct3d_shares_the_directdraw_seven_reference_count(); + direct3d_enumerates_the_native_renderer(); #ifndef _WIN32 + indexed_triangles_preserve_depth_and_target_contents(); native_context_renders_and_reads_pixels(); #endif puts("DirectDraw: adapter/mode callbacks, reentrancy, capabilities, DirectDrawCreateEx, windowed mode, device information, DirectDraw/Direct3D interface lifetime and ABI passed"); diff --git a/tools/reconstruction/test_portable_atoi.py b/tools/reconstruction/test_portable_atoi.py @@ -0,0 +1,26 @@ +"""Compare guest decimal parsing with Windows x86 MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-atoi" +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/atoi.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference)], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/translated" +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + [ + "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", + "tests/atoi.c", "runtime/guest_crt.c", "runtime/guest_heap.c", + "runtime/guest_memory.c", "runtime/recomp_state.c", "-lm", "-o", linux_output, +], check=True) +actual = subprocess.check_output(wsl + [linux_output], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip()) diff --git a/tools/reconstruction/test_portable_graphics.py b/tools/reconstruction/test_portable_graphics.py @@ -1,4 +1,4 @@ -"""Compare guest DirectDraw interface lifetime with Windows x86.""" +"""Compare graphics API contracts with Windows and test the native renderer.""" import pathlib import subprocess @@ -18,7 +18,7 @@ linux_output = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:] + "/transla wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] subprocess.run(wsl + [ "gcc", "-O2", "-DRECOMP_PORTABLE", "-Iruntime", "-fno-strict-aliasing", - "tests/graphics.c", "runtime/linux_graphics.c", "runtime/guest_heap.c", + "tests/graphics.c", "runtime/linux_graphics.c", "runtime/gl_renderer.c", "runtime/guest_heap.c", "runtime/guest_memory.c", "runtime/recomp_state.c", "-lSDL2", "-lGL", "-o", linux_output, ], check=True) actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_output], text=True, timeout=30)