commit e6f66c2c1b1b5633e76e53cb0eca3c6f40d9ff63
parent 45224309d4d819fd9df5a12033a5d1fd3ddab5c9
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 01:25:30 +0000
Implement native primary graphics adapter discovery
Diffstat:
2 files changed, 258 insertions(+), 11 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -71,9 +71,8 @@ static uint32_t interface_pointer(struct draw_object *object, unsigned index)
return object->interfaces[index];
}
-static void direct_draw_create(void)
+static void create_draw(uint32_t adapter, uint32_t output, uint32_t outer, unsigned index)
{
- uint32_t adapter = MEM32(g_esp + 4), output = MEM32(g_esp + 8), outer = MEM32(g_esp + 12);
if (!output || outer) g_eax = 0x80070057u; /* DDERR_INVALIDPARAMS */
else if (adapter) {
fprintf(stderr, "Selecting a non-default DirectDraw adapter is not implemented\n");
@@ -84,9 +83,96 @@ static void direct_draw_create(void)
if (!object) abort();
object->next = objects;
objects = object;
- MEM32(output) = interface_pointer(object, 0);
+ MEM32(output) = interface_pointer(object, index);
g_eax = 0;
}
+}
+
+static void direct_draw_create(void)
+{
+ create_draw(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), 0);
+ g_esp += 16;
+}
+
+static void direct_draw_create_ex(void)
+{
+ uint32_t iid = MEM32(g_esp + 12);
+ static const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ if (!iid || memcmp((const void *)ADDR(iid), draw7, 16)) g_eax = 0x80070057u;
+ else if (MEM32(g_esp + 16)) g_eax = 0x80040110u; /* CLASS_E_NOAGGREGATION */
+ else create_draw(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 16), 1);
+ g_esp += 20;
+}
+
+static void set_cooperative_level(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t handle = MEM32(g_esp + 8), flags = MEM32(g_esp + 12);
+ /* Adapter discovery uses DDSCL_NORMAL without an application window. */
+ if (handle || flags != 8) {
+ fprintf(stderr, "Unsupported DirectDraw cooperative level: window=%08x flags=%08x\n", handle, flags);
+ exit(3);
+ }
+ g_eax = SDL_SetWindowFullscreen(window, 0) ? 0x80004005u : 0;
+ g_esp += 16;
+}
+
+static void get_device_identifier(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t output = MEM32(g_esp + 8), flags = MEM32(g_esp + 12);
+ g_eax = 0x80070057u;
+ if (output) {
+ char *information = (char *)ADDR(output);
+ /* DDDEVICEIDENTIFIER2 has 1068 bytes of fields, then four untouched
+ * alignment bytes. Unknown PCI/driver-version fields and WHQL are zero. */
+ memset(information, 0, 1068);
+ if (!(flags & ~1u)) {
+ strcpy(information, "ao-sdl-opengl");
+ snprintf(information + 512, 512, "%s", (const char *)glGetString(GL_RENDERER));
+ /* Stable identity of our single virtual adapter, not a physical GPU
+ * GUID. Legacy hardware-specific driver workarounds must not apply. */
+ static const uint32_t identifier[] = {0xc5cef85a, 0x4fae871b, 0x6942f28a, 0x0d55ec3f};
+ memcpy(information + 1048, identifier, 16);
+ g_eax = 0;
+ }
+ }
+ g_esp += 16;
+}
+
+static void enumerate_adapters(void)
+{
+ uint32_t callback = MEM32(g_esp + 4), data = MEM32(g_esp + 8), flags = MEM32(g_esp + 12);
+ if (!callback || (flags & ~7u)) { g_eax = 0x80070057u; g_esp += 16; return; }
+ if (!initialize_graphics()) { g_eax = 0x80004005u; g_esp += 16; return; }
+
+ /* This backend exposes its active primary adapter. Secondary, detached and
+ * non-display adapters are not implemented. Names come from the real backend. */
+ int display = SDL_GetWindowDisplayIndex(window);
+ const char *name = display < 0 ? NULL : SDL_GetDisplayName(display);
+ if (!name) { fprintf(stderr, "SDL display query failed: %s\n", SDL_GetError()); abort(); }
+ const char *description = (const char *)glGetString(GL_RENDERER);
+ size_t description_size = strlen(description) + 1, name_size = strlen(name) + 1;
+ uint32_t text = guest_malloc(description_size + name_size);
+ if (!text) abort();
+ memcpy((void *)ADDR(text), description, description_size);
+ memcpy((void *)ADDR(text + description_size), name, name_size);
+
+ 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, 0); /* The primary adapter has a null monitor and GUID. */
+ PUSH32(g_esp, data);
+ PUSH32(g_esp, text + description_size);
+ PUSH32(g_esp, text);
+ PUSH32(g_esp, 0);
+ PUSH32(g_esp, RECOMP_RETADDR);
+ function();
+ if (g_esp != stack) { fprintf(stderr, "DirectDraw enumeration callback unbalanced the guest stack\n"); abort(); }
+ g_cur_func = caller;
+ guest_free(text);
+ /* There is only one supported adapter, whether the callback continues or stops. */
+ g_eax = 0;
g_esp += 16;
}
@@ -151,10 +237,15 @@ 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_slot == 20) return set_cooperative_level;
+ if (current_interface == 1 && current_slot == 27) return get_device_identifier;
return unavailable_method;
}
recomp_func_t linux_graphics_lookup(const char *name)
{
- return !strcmp(name, "DirectDrawCreate") ? direct_draw_create : NULL;
+ if (!strcmp(name, "DirectDrawCreate")) return direct_draw_create;
+ if (!strcmp(name, "DirectDrawCreateEx")) return direct_draw_create_ex;
+ if (!strcmp(name, "DirectDrawEnumerateExA")) return enumerate_adapters;
+ return NULL;
}
diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c
@@ -4,12 +4,25 @@
#include <string.h>
#ifdef _WIN32
#include <windows.h>
-static uint32_t scratch[16], create_address;
+#include <ddraw.h>
+#include <stddef.h>
+_Static_assert(sizeof(DDDEVICEIDENTIFIER2) == 1072, "guest device identifier size");
+_Static_assert(offsetof(DDDEVICEIDENTIFIER2, guidDeviceIdentifier) == 1048, "guest identifier offset");
+_Static_assert(offsetof(DDDEVICEIDENTIFIER2, dwWHQLLevel) == 1064, "guest certification offset");
+static uint32_t scratch[512], create_address, create_ex_address, enumerate_address;
#else
#include "linux_graphics.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);
+recomp_func_t recomp_lookup(uint32_t address)
+{
+ assert(address == 0x11110000u);
+ return enumeration_callback;
+}
+recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
#endif
static uint32_t workspace(void)
@@ -36,15 +49,20 @@ static uint32_t invoke(uint32_t address, unsigned count, uint32_t a, uint32_t b,
if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(a);
return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(a, b, c);
#else
- g_esp = 0x5000ff00u;
+ uint32_t stack = g_esp, caller = g_cur_func;
+ g_esp -= (count + 1) * 4;
MEM32(g_esp) = RECOMP_RETADDR;
- MEM32(g_esp + 4) = a; MEM32(g_esp + 8) = b; MEM32(g_esp + 12) = c;
+ MEM32(g_esp + 4) = a;
+ if (count == 3) { MEM32(g_esp + 8) = b; MEM32(g_esp + 12) = c; }
g_ebx = 0x12345678u; g_esi = 0x23456789u;
g_edi = 0x3456789au; g_ebp = 0x456789abu;
- recomp_func_t function = address ? linux_graphics_virtual(address) : linux_graphics_lookup("DirectDrawCreate");
+ recomp_func_t function;
+ if (!address) function = linux_graphics_lookup("DirectDrawCreate");
+ else if (address == 1) function = linux_graphics_lookup("DirectDrawEnumerateExA");
+ else function = linux_graphics_virtual(address);
assert(function);
function();
- assert(g_esp == 0x5000ff00u + 4 + count * 4);
+ assert(g_esp == stack && g_cur_func == caller);
assert(g_ebx == 0x12345678u && g_esi == 0x23456789u);
assert(g_edi == 0x3456789au && g_ebp == 0x456789abu);
return g_eax;
@@ -73,6 +91,138 @@ static uint32_t ref(uint32_t object, unsigned slot)
return invoke(memory(*memory(object))[slot], 1, object, 0, 0);
}
+static uint32_t create_ex(uint32_t iid, uint32_t output, uint32_t outer)
+{
+#ifdef _WIN32
+ return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)create_ex_address)(0, output, iid, outer);
+#else
+ uint32_t stack = g_esp, caller = g_cur_func;
+ PUSH32(g_esp, outer);
+ PUSH32(g_esp, iid);
+ PUSH32(g_esp, output);
+ PUSH32(g_esp, 0);
+ PUSH32(g_esp, RECOMP_RETADDR);
+ linux_graphics_lookup("DirectDrawCreateEx")();
+ assert(g_esp == stack && g_cur_func == caller);
+ return g_eax;
+#endif
+}
+
+static void direct_draw_seven_creation_and_windowed_mode(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ const uint32_t draw1[] = {0x6c14db80, 0x11cea733, 0x200021a5, 0x60e50baf};
+ memcpy(memory(workspace() + 4), draw7, 16);
+
+ assert(create_ex(workspace() + 4, workspace(), 0) == 0);
+ uint32_t object = *memory(workspace());
+ uint32_t result = invoke(memory(*memory(object))[20], 3, object, 0, 8);
+
+ assert(result == 0);
+#ifndef _WIN32
+ assert(!(SDL_GetWindowFlags(SDL_GL_GetCurrentWindow()) & SDL_WINDOW_FULLSCREEN));
+#endif
+ assert(query(object, draw7) == 0 && *memory(workspace()) == object);
+ assert(ref(object, 2) == 1);
+ assert(query(object, draw1) == 0 && *memory(workspace()) != object);
+ assert(ref(*memory(workspace()), 2) == 0);
+ assert(ref(object, 2) == 0);
+ memcpy(memory(workspace() + 4), draw7, 16);
+ assert(create_ex(workspace() + 4, 0, 0) == 0x80070057u);
+ result = create_ex(workspace() + 4, workspace(), 1);
+ assert(result == 0x80040110u);
+ memcpy(memory(workspace() + 4), draw1, 16);
+ assert(create_ex(workspace() + 4, workspace(), 0) == 0x80070057u);
+}
+
+static void device_information_has_stable_identity_and_preserves_padding(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ memcpy(memory(workspace() + 4), draw7, 16);
+ assert(create_ex(workspace() + 4, workspace(), 0) == 0);
+ uint32_t object = *memory(workspace()), output = workspace() + 64;
+ uint32_t method = memory(*memory(object))[27];
+ memset(memory(output), 0xcc, 1072);
+
+ uint32_t result = invoke(method, 3, object, output, 0);
+
+ assert(result == 0 && *memory(output + 1068) == 0xccccccccu);
+ assert(*(char *)memory(output) && memchr(memory(output), 0, 512));
+ assert(*(char *)memory(output + 512) && memchr(memory(output + 512), 0, 512));
+ assert(*memory(output + 1048) || *memory(output + 1052) || *memory(output + 1056) || *memory(output + 1060));
+#ifndef _WIN32
+ assert(!strcmp((char *)memory(output), "ao-sdl-opengl"));
+ assert(!strcmp((char *)memory(output + 512), (const char *)glGetString(GL_RENDERER)));
+ for (unsigned offset = 1024; offset < 1048; offset += 4) assert(*memory(output + offset) == 0);
+ assert(*memory(output + 1064) == 0); /* No Windows driver certification. */
+#endif
+ unsigned char first[1072];
+ memcpy(first, memory(output), sizeof(first));
+ assert(invoke(method, 3, object, output, 1) == 0);
+ assert(!memcmp(first, memory(output), sizeof(first)));
+ assert(ref(object, 2) == 0);
+ assert(create_ex(workspace() + 4, workspace(), 0) == 0);
+ object = *memory(workspace());
+ method = memory(*memory(object))[27];
+ assert(invoke(method, 3, object, output, 0) == 0);
+ assert(!memcmp(first, memory(output), sizeof(first)));
+ assert(invoke(method, 3, object, output, 2) == 0x80070057u);
+ for (unsigned offset = 0; offset < 1064; offset += 4) assert(*memory(output + offset) == 0);
+ assert(*memory(output + 1068) == 0xccccccccu);
+ assert(invoke(method, 3, object, 0, 0) == 0x80070057u);
+ assert(ref(object, 2) == 0);
+}
+
+static unsigned callback_count, continue_enumeration;
+static int inspect_adapter(uint32_t guid, uint32_t description, uint32_t name, uint32_t data, uint32_t monitor)
+{
+ assert(!guid && !monitor && data == 0x12345678u);
+ assert(*(const char *)memory(description) && *(const char *)memory(name));
+#ifndef _WIN32
+ assert(!strcmp((const char *)memory(description), (const char *)glGetString(GL_RENDERER)));
+ assert(!strcmp((const char *)memory(name), SDL_GetDisplayName(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()))));
+#endif
+ ++callback_count;
+ uint32_t object = create();
+ assert(ref(object, 2) == 0); /* Reentrant calls must leave the callback stack intact. */
+ return continue_enumeration;
+}
+
+#ifdef _WIN32
+static BOOL WINAPI enumeration_callback(GUID *guid, char *description, char *name, void *data, HMONITOR monitor)
+{
+ return inspect_adapter((uintptr_t)guid, (uintptr_t)description, (uintptr_t)name, (uintptr_t)data, (uintptr_t)monitor);
+}
+#else
+static void enumeration_callback(void)
+{
+ g_cur_func = 0x11110000u;
+ g_eax = inspect_adapter(MEM32(g_esp + 4), MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16), MEM32(g_esp + 20));
+ g_esp += 24;
+}
+#endif
+
+static void adapter_enumeration_invokes_guest_callbacks(void)
+{
+#ifdef _WIN32
+ uint32_t callback = (uintptr_t)enumeration_callback;
+#else
+ uint32_t callback = 0x11110000u;
+#endif
+ callback_count = 0;
+ continue_enumeration = 1;
+
+ uint32_t result = invoke(enumerate_address, 3, callback, 0x12345678u, 0);
+
+ assert(result == 0 && callback_count == 1);
+ callback_count = 0;
+ continue_enumeration = 0;
+ assert(invoke(enumerate_address, 3, callback, 0x12345678u, 7) == 0 && callback_count == 1);
+ callback_count = 0;
+ assert(invoke(enumerate_address, 3, callback, 0x12345678u, 8) == 0x80070057u && callback_count == 0);
+ assert(invoke(enumerate_address, 3, 0, 0, 0) == 0x80070057u);
+}
+
static void independent_interface_counts_share_object_lifetime(void)
{
uint32_t original = create();
@@ -127,14 +277,20 @@ int main(void)
{
#ifdef _WIN32
create_address = (uintptr_t)GetProcAddress(LoadLibraryA("ddraw.dll"), "DirectDrawCreate");
- assert(create_address);
+ enumerate_address = (uintptr_t)GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawEnumerateExA");
+ create_ex_address = (uintptr_t)GetProcAddress(GetModuleHandleA("ddraw.dll"), "DirectDrawCreateEx");
+ assert(create_address && create_ex_address && enumerate_address);
#else
assert(guest_map("graphics test", 0x50000000u, 0x10000u));
+ g_esp = 0x5000ff00u;
#endif
+ adapter_enumeration_invokes_guest_callbacks();
+ direct_draw_seven_creation_and_windowed_mode();
+ device_information_has_stable_identity_and_preserves_padding();
independent_interface_counts_share_object_lifetime();
#ifndef _WIN32
native_context_renders_and_reads_pixels();
#endif
- puts("DirectDraw: distinct interfaces, independent counts, shared lifetime, identity, errors and ABI passed");
+ puts("DirectDraw: enumeration callbacks, reentrancy, DirectDrawCreateEx, windowed mode, device information, distinct interfaces, reference counts, lifetime and ABI passed");
return 0;
}