commit 1a3967db65d12885ad9b4e05a0e62bab413e3f27
parent e6f66c2c1b1b5633e76e53cb0eca3c6f40d9ff63
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 01:33:28 +0000
Advance native graphics discovery through display modes
Diffstat:
2 files changed, 278 insertions(+), 13 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -7,10 +7,11 @@
static SDL_Window *window;
static SDL_GLContext context;
static struct draw_object {
- uint32_t interfaces[2], references[2];
+ uint32_t interfaces[3], references[2];
struct draw_object *next;
} *objects;
-static uint32_t vtables[2];
+static uint32_t vtables[3];
+static const unsigned interface_slots[] = {23, 30, 8};
static unsigned current_interface, current_slot;
static void close_graphics(void)
@@ -47,8 +48,8 @@ failed:
static struct draw_object *find_object(uint32_t address)
{
for (struct draw_object *object = objects; object; object = object->next)
- for (unsigned i = 0; i < 2; ++i)
- if (object->interfaces[i] == address && object->references[i]) return object;
+ for (unsigned i = 0; i < 3; ++i)
+ if (object->interfaces[i] == address && object->references[i ? 1 : 0]) return object;
fprintf(stderr, "Invalid guest DirectDraw interface %08x\n", address);
abort();
}
@@ -56,7 +57,7 @@ static struct draw_object *find_object(uint32_t address)
static uint32_t interface_pointer(struct draw_object *object, unsigned index)
{
if (!vtables[index]) {
- unsigned slots = index ? 30 : 23;
+ unsigned slots = interface_slots[index];
vtables[index] = guest_malloc(slots * 4);
if (!vtables[index]) abort();
for (unsigned slot = 0; slot < slots; ++slot)
@@ -67,7 +68,8 @@ static uint32_t interface_pointer(struct draw_object *object, unsigned index)
if (!object->interfaces[index]) abort();
MEM32(object->interfaces[index]) = vtables[index];
}
- ++object->references[index];
+ /* Direct3D7 and DirectDraw7 share a count; DirectDraw has its own. */
+ ++object->references[index ? 1 : 0];
return object->interfaces[index];
}
@@ -140,6 +142,84 @@ static void get_device_identifier(void)
g_esp += 16;
}
+static void get_caps(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t outputs[] = {MEM32(g_esp + 8), MEM32(g_esp + 12)};
+ g_eax = 0x80070057u;
+ if (!outputs[0] && !outputs[1]) { g_esp += 16; return; }
+ for (unsigned i = 0; i < 2; ++i) if (outputs[i]) {
+ uint32_t size = MEM32(outputs[i]);
+ if (size != 172 && size != 316 && size != 364 && size != 380) {
+ g_esp += 16;
+ return;
+ }
+ }
+ for (unsigned i = 0; i < 2; ++i) if (outputs[i]) {
+ 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 */
+ }
+ g_eax = 0;
+ g_esp += 16;
+}
+
+static void enumerate_display_modes(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t flags = MEM32(g_esp + 8), filter = MEM32(g_esp + 12);
+ uint32_t data = MEM32(g_esp + 16), callback = MEM32(g_esp + 20);
+ if (!callback || (flags & ~3u)) { g_eax = 0x80070057u; g_esp += 24; return; }
+ if (filter) { fprintf(stderr, "Filtered DirectDraw mode enumeration is not implemented\n"); exit(3); }
+ int display = SDL_GetWindowDisplayIndex(window), count = SDL_GetNumDisplayModes(display);
+ if (count < 0) { fprintf(stderr, "SDL mode query failed: %s\n", SDL_GetError()); abort(); }
+ uint32_t description = guest_malloc(124);
+ if (!description) abort();
+ recomp_func_t function = recomp_lookup(callback);
+ if (!function) function = recomp_lookup_import(callback);
+ uint32_t stack = g_esp, caller = g_cur_func;
+ SDL_DisplayMode previous = {0};
+ for (int i = 0; i < count; ++i) {
+ SDL_DisplayMode mode;
+ if (SDL_GetDisplayMode(display, i, &mode)) {
+ fprintf(stderr, "SDL mode query failed: %s\n", SDL_GetError()); abort();
+ }
+ /* SDL sorts refresh variants together. Without DDEDM_REFRESHRATES,
+ * report each resolution/format once with a zero refresh rate. */
+ if (!(flags & 1) && mode.w == previous.w && mode.h == previous.h && mode.format == previous.format) continue;
+ previous = mode;
+ int bits;
+ Uint32 red, green, blue, alpha;
+ if (!SDL_PixelFormatEnumToMasks(mode.format, &bits, &red, &green, &blue, &alpha) || !red || !green || !blue) continue;
+ memset((void *)ADDR(description), 0, 124);
+ MEM32(description) = 124;
+ MEM32(description + 4) = 0x4100eu; /* dimensions, pitch, pixel format, refresh */
+ MEM32(description + 8) = mode.h;
+ MEM32(description + 12) = mode.w;
+ MEM32(description + 16) = (mode.w * SDL_BYTESPERPIXEL(mode.format) + 3) & ~3u;
+ MEM32(description + 24) = (flags & 1) ? mode.refresh_rate : 0;
+ MEM32(description + 72) = 32; /* DDPIXELFORMAT */
+ MEM32(description + 76) = 0x40; /* DDPF_RGB; display alpha is unused */
+ MEM32(description + 84) = SDL_BYTESPERPIXEL(mode.format) * 8;
+ MEM32(description + 88) = red;
+ MEM32(description + 92) = green;
+ MEM32(description + 96) = blue;
+ PUSH32(g_esp, data);
+ PUSH32(g_esp, description);
+ PUSH32(g_esp, RECOMP_RETADDR);
+ function();
+ if (g_esp != stack) { fprintf(stderr, "DirectDraw mode callback unbalanced the guest stack\n"); abort(); }
+ g_cur_func = caller;
+ if (!g_eax) break;
+ }
+ guest_free(description);
+ g_eax = 0;
+ g_esp += 24;
+}
+
static void enumerate_adapters(void)
{
uint32_t callback = MEM32(g_esp + 4), data = MEM32(g_esp + 8), flags = MEM32(g_esp + 12);
@@ -183,6 +263,7 @@ static void query_interface(void)
static const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
static const uint32_t draw1[] = {0x6c14db80, 0x11cea733, 0x200021a5, 0x60e50baf};
static const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ static const uint32_t d3d7[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
if (!output) g_eax = 0x80070057u;
else if (!memcmp((const void *)ADDR(iid), unknown, 16) || !memcmp((const void *)ADDR(iid), draw1, 16)) {
MEM32(output) = interface_pointer(object, 0);
@@ -190,6 +271,9 @@ static void query_interface(void)
} else if (!memcmp((const void *)ADDR(iid), draw7, 16)) {
MEM32(output) = interface_pointer(object, 1);
g_eax = 0;
+ } else if (!memcmp((const void *)ADDR(iid), d3d7, 16)) {
+ MEM32(output) = interface_pointer(object, 2);
+ g_eax = 0;
} else { MEM32(output) = 0; g_eax = 0x80004002u; }
g_esp += 16;
}
@@ -209,8 +293,10 @@ static void release_reference(void)
unsigned index = address == object->interfaces[0] ? 0 : 1;
g_eax = --object->references[index];
if (!g_eax) {
- guest_free(object->interfaces[index]);
- object->interfaces[index] = 0;
+ for (unsigned i = 0; i < 3; ++i) if ((i ? 1 : 0) == index) {
+ guest_free(object->interfaces[i]);
+ object->interfaces[i] = 0;
+ }
}
if (!object->references[0] && !object->references[1]) {
struct draw_object **link = &objects;
@@ -223,20 +309,23 @@ static void release_reference(void)
static void unavailable_method(void)
{
- fprintf(stderr, "Unimplemented IDirectDraw%s method slot %u caller=%08x\n",
- current_interface ? "7" : "", current_slot, g_cur_func);
+ const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7"};
+ fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n",
+ names[current_interface], current_slot, g_cur_func);
exit(3);
}
recomp_func_t linux_graphics_virtual(uint32_t address)
{
- if (address < 0xf3000000u || address >= 0xf3000200u || (address & 3)) return NULL;
+ if (address < 0xf3000000u || address >= 0xf3000300u || (address & 3)) return NULL;
current_interface = (address - 0xf3000000u) / 0x100;
current_slot = (address & 0xff) / 4;
- if (current_slot >= (current_interface ? 30 : 23)) return NULL;
+ if (current_slot >= interface_slots[current_interface]) return NULL;
if (current_slot == 0) return query_interface;
if (current_slot == 1) return add_reference;
if (current_slot == 2) return release_reference;
+ 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;
if (current_interface == 1 && current_slot == 27) return get_device_identifier;
return unavailable_method;
diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c
@@ -9,6 +9,9 @@
_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_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 uint32_t scratch[512], create_address, create_ex_address, enumerate_address;
#else
#include "linux_graphics.h"
@@ -17,8 +20,10 @@ static uint32_t scratch[512], create_address, create_ex_address, enumerate_addre
#include <SDL2/SDL_opengl.h>
static uint32_t enumerate_address = 1;
static void enumeration_callback(void);
+static void mode_callback(void);
recomp_func_t recomp_lookup(uint32_t address)
{
+ if (address == 0x11110004u) return mode_callback;
assert(address == 0x11110000u);
return enumeration_callback;
}
@@ -173,6 +178,148 @@ static void device_information_has_stable_identity_and_preserves_padding(void)
assert(ref(object, 2) == 0);
}
+static void capability_queries_respect_structure_sizes(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ uint32_t original = create();
+ assert(query(original, draw7) == 0);
+ uint32_t object = *memory(workspace()), output = workspace() + 64;
+ uint32_t method = memory(*memory(object))[11];
+ const unsigned sizes[] = {172, 316, 364, 380};
+
+ for (unsigned i = 0; i < 4; ++i) {
+ memset(memory(output), 0xcc, 384);
+ *memory(output) = sizes[i];
+ uint32_t result = invoke(method, 3, object, output, 0);
+
+ assert(result == 0 && *memory(output) == sizes[i]);
+ assert(*memory(output + sizes[i]) == 0xccccccccu);
+#ifndef _WIN32
+ assert(*memory(output + 8) == 0x80000u);
+ for (unsigned offset = 4; offset < sizes[i]; offset += 4)
+ if (offset != 8) assert(*memory(output + offset) == 0);
+#endif
+ }
+ assert(invoke(method, 3, object, 0, output) == 0);
+#ifndef _WIN32
+ for (unsigned offset = 4; offset < 380; offset += 4) assert(*memory(output + offset) == 0);
+#endif
+ *memory(output) = 4;
+ *memory(output + 4) = 0xccccccccu;
+ assert(invoke(method, 3, object, output, 0) == 0x80070057u);
+ assert(*memory(output) == 4 && *memory(output + 4) == 0xccccccccu);
+ assert(invoke(method, 3, object, 0, 0) == 0x80070057u);
+ assert(ref(object, 2) == 0 && ref(original, 2) == 0);
+}
+
+static unsigned mode_count, mode_flags, cancel_modes;
+static uint32_t seen_modes[1024][4];
+static int inspect_mode(uint32_t description, uint32_t data)
+{
+ assert(data == 0x76543210u && *memory(description) == 124);
+ assert(*memory(description + 4) == 0x4100eu);
+ uint32_t width = *memory(description + 12), height = *memory(description + 8);
+ uint32_t bits = *memory(description + 84), refresh = *memory(description + 24);
+ assert(width && height && bits && *memory(description + 72) == 32);
+ assert(*memory(description + 76) & 0x40);
+ if (!(*memory(description + 76) & 0x20)) {
+ uint32_t red = *memory(description + 88), green = *memory(description + 92), blue = *memory(description + 96);
+ assert(red && green && blue && !(red & green) && !(green & blue) && !(red & blue));
+ }
+ assert(*memory(description + 16) >= width * bits / 8);
+ if (!(mode_flags & 1)) assert(refresh == 0);
+ for (unsigned i = 0; i < mode_count; ++i)
+ assert(seen_modes[i][0] != width || seen_modes[i][1] != height || seen_modes[i][2] != bits || seen_modes[i][3] != refresh);
+#ifndef _WIN32
+ int display = SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), found = 0;
+ for (int i = 0; i < SDL_GetNumDisplayModes(display); ++i) {
+ SDL_DisplayMode mode;
+ assert(SDL_GetDisplayMode(display, i, &mode) == 0);
+ if (mode.w == width && mode.h == height && SDL_BYTESPERPIXEL(mode.format) * 8 == bits &&
+ (!(mode_flags & 1) || mode.refresh_rate == refresh)) found = 1;
+ }
+ assert(found); /* Every reported mode must exist in the native display API. */
+#endif
+ assert(mode_count < 1024);
+ seen_modes[mode_count][0] = width; seen_modes[mode_count][1] = height;
+ seen_modes[mode_count][2] = bits; seen_modes[mode_count][3] = refresh;
+ ++mode_count;
+ uint32_t object = create();
+ assert(ref(object, 2) == 0);
+ return !cancel_modes;
+}
+
+#ifdef _WIN32
+static HRESULT WINAPI mode_callback(DDSURFACEDESC2 *description, void *data)
+{
+ return inspect_mode((uintptr_t)description, (uintptr_t)data);
+}
+#else
+static void mode_callback(void)
+{
+ g_cur_func = 0x11110004u;
+ g_eax = inspect_mode(MEM32(g_esp + 4), MEM32(g_esp + 8));
+ g_esp += 12;
+}
+#endif
+
+static uint32_t enumerate_modes(uint32_t object, uint32_t callback)
+{
+ uint32_t method = memory(*memory(object))[8];
+#ifdef _WIN32
+ return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t))(uintptr_t)method)
+ (object, mode_flags, 0, 0x76543210u, callback);
+#else
+ uint32_t stack = g_esp, caller = g_cur_func;
+ g_ebx = 0x12345678u; g_esi = 0x23456789u;
+ g_edi = 0x3456789au; g_ebp = 0x456789abu;
+ PUSH32(g_esp, callback);
+ PUSH32(g_esp, 0x76543210u);
+ PUSH32(g_esp, 0);
+ PUSH32(g_esp, mode_flags);
+ PUSH32(g_esp, object);
+ PUSH32(g_esp, RECOMP_RETADDR);
+ linux_graphics_virtual(method)();
+ 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;
+#endif
+}
+
+static void display_modes_describe_the_native_monitor(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());
+#ifdef _WIN32
+ uint32_t callback = (uintptr_t)mode_callback;
+#else
+ uint32_t callback = 0x11110004u;
+#endif
+ mode_flags = 0; mode_count = 0; cancel_modes = 0;
+
+ uint32_t result = enumerate_modes(object, callback);
+ unsigned resolutions = mode_count;
+ mode_flags = 1; mode_count = 0;
+ uint32_t refresh_result = enumerate_modes(object, callback);
+
+ assert(result == 0 && refresh_result == 0 && resolutions > 0 && mode_count >= resolutions);
+ fprintf(stderr, "Display modes: %u resolutions, %u refresh variants\n", resolutions, mode_count);
+#ifndef _WIN32
+ /* Follow the documented cancel contract. This Windows driver continues
+ * callbacks after DDENUMRET_CANCEL, so cancellation is tested natively. */
+ mode_count = 0; cancel_modes = 1;
+ assert(enumerate_modes(object, callback) == 0 && mode_count == 1);
+#endif
+ mode_flags = 4; mode_count = 0;
+ assert(enumerate_modes(object, callback) == 0x80070057u && mode_count == 0);
+ mode_flags = 0;
+ assert(enumerate_modes(object, 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)
{
@@ -256,6 +403,32 @@ static void independent_interface_counts_share_object_lifetime(void)
}
}
+static void direct3d_shares_the_directdraw_seven_reference_count(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ const uint32_t d3d7[] = {0xf5049e77, 0x11d24861, 0xa00007a4, 0xa82906c9};
+ const uint32_t unknown[] = {0, 0, 0xc0, 0x46000000};
+ 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());
+
+ assert(draw != d3d && *memory(draw) != *memory(d3d));
+ assert(ref(draw, 1) == 3 && ref(draw, 2) == 2);
+ assert(ref(d3d, 1) == 3 && ref(d3d, 2) == 2);
+ assert(query(d3d, d3d7) == 0 && *memory(workspace()) == d3d);
+ assert(ref(d3d, 2) == 2);
+ assert(query(d3d, unknown) == 0);
+ uint32_t identity = *memory(workspace());
+ assert(identity != d3d && identity != draw);
+ assert(ref(identity, 2) == 0);
+ assert(ref(draw, 2) == 1);
+ assert(query(d3d, draw7) == 0 && *memory(workspace()) == draw);
+ assert(ref(draw, 2) == 1 && ref(d3d, 2) == 0);
+}
+
#ifndef _WIN32
static void native_context_renders_and_reads_pixels(void)
{
@@ -287,10 +460,13 @@ int main(void)
adapter_enumeration_invokes_guest_callbacks();
direct_draw_seven_creation_and_windowed_mode();
device_information_has_stable_identity_and_preserves_padding();
+ capability_queries_respect_structure_sizes();
+ display_modes_describe_the_native_monitor();
independent_interface_counts_share_object_lifetime();
+ direct3d_shares_the_directdraw_seven_reference_count();
#ifndef _WIN32
native_context_renders_and_reads_pixels();
#endif
- puts("DirectDraw: enumeration callbacks, reentrancy, DirectDrawCreateEx, windowed mode, device information, distinct interfaces, reference counts, lifetime and ABI passed");
+ puts("DirectDraw: adapter/mode callbacks, reentrancy, capabilities, DirectDrawCreateEx, windowed mode, device information, DirectDraw/Direct3D interface lifetime and ABI passed");
return 0;
}