commit 2ec3e42b17fb15881a3508befa8ed52f602eee99
parent 1128c5346906824c7c3fd950e5cf620652c678b0
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 03:33:26 +0000
Advance native graphics initialization through managed textures
Diffstat:
4 files changed, 170 insertions(+), 14 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -20,6 +20,7 @@ static struct clipper_object {
static struct surface_object {
uint32_t interfaces[2], references[2];
uint32_t width, height, caps, pixels, lock_flags, depth_bits, alpha;
+ uint32_t caps2, description_flags;
int locked;
struct draw_object *owner;
struct clipper_object *clipper;
@@ -210,6 +211,34 @@ static void get_caps(void)
g_esp += 16;
}
+static void get_display_mode(void)
+{
+ find_object(MEM32(g_esp + 4));
+ uint32_t output = MEM32(g_esp + 8);
+ g_esp += 12;
+ g_eax = 0x80070057u;
+ if (!output || (MEM32(output) != 108 && MEM32(output) != 124)) return;
+ SDL_DisplayMode mode;
+ if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode)) abort();
+ int bits;
+ Uint32 red, green, blue, alpha;
+ if (!SDL_PixelFormatEnumToMasks(mode.format, &bits, &red, &green, &blue, &alpha)) abort();
+ memset((void *)ADDR(output), 0, MEM32(output));
+ MEM32(output) = 124;
+ MEM32(output + 4) = 0x4100e; /* Dimensions, pitch, pixel format and refresh. */
+ MEM32(output + 8) = mode.h;
+ MEM32(output + 12) = mode.w;
+ MEM32(output + 16) = (mode.w * SDL_BYTESPERPIXEL(mode.format) + 3) & ~3u;
+ MEM32(output + 24) = mode.refresh_rate;
+ MEM32(output + 72) = 32;
+ MEM32(output + 76) = 0x40;
+ MEM32(output + 84) = SDL_BYTESPERPIXEL(mode.format) * 8;
+ MEM32(output + 88) = red;
+ MEM32(output + 92) = green;
+ MEM32(output + 96) = blue;
+ g_eax = 0;
+}
+
static void enumerate_display_modes(void)
{
find_object(MEM32(g_esp + 4));
@@ -567,7 +596,7 @@ static void create_surface(void)
uint32_t flags = MEM32(desc + 4), caps = MEM32(desc + 104);
uint32_t width = MEM32(desc + 12), height = MEM32(desc + 8);
int depth = flags == 0x1007 && caps == 0x24000;
- int texture = flags == 0x1007 && (caps == 0x1000 || caps == 0x5000);
+ int texture = (flags == 0x1007 || flags == 0x101007) && (caps == 0x1000 || caps == 0x5000);
if (outer || !((flags == 1 && caps == 0x200) || depth || texture ||
(flags == 7 && (caps == 0x40 || caps == 0x2040 || caps == 0x6040)))) {
fprintf(stderr, "Unsupported DirectDraw surface flags=%08x caps=%08x outer=%08x\n", flags, caps, outer);
@@ -577,7 +606,9 @@ static void create_surface(void)
const uint32_t rgb[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0};
const uint32_t argb[] = {32, 0x41, 0, 32, 0xff0000, 0xff00, 0xff, 0xff000000};
if ((memcmp((void *)ADDR(desc + 72), rgb, 32) && memcmp((void *)ADDR(desc + 72), argb, 32)) ||
- MEM32(desc + 108) || MEM32(desc + 112) || MEM32(desc + 116)) {
+ (MEM32(desc + 108) != 0 && MEM32(desc + 108) != 0x10) ||
+ MEM32(desc + 112) || MEM32(desc + 116) || ((flags & 0x100000) && MEM32(desc + 120)) ||
+ (MEM32(desc + 108) && caps != 0x1000)) {
fprintf(stderr, "Unsupported texture format or extended caps\n"); exit(3);
}
}
@@ -615,11 +646,22 @@ static void create_surface(void)
if (!surface->target && !surface->depth) { free(surface); g_eax = 0x8007000eu; return; }
if (texture) {
surface->alpha = MEM32(desc + 76) & 1;
+ surface->caps2 = MEM32(desc + 108);
gl_target_set_depth(surface->target, NULL);
+ if (surface->caps2 == 0x10) {
+ /* Managed textures retain CPU storage and upload writes on unlock. */
+ surface->pixels = guest_malloc(width * height * 4);
+ if (!surface->pixels) {
+ gl_target_destroy(surface->target); free(surface); g_eax = 0x8007000eu; return;
+ }
+ memset((void *)ADDR(surface->pixels), 0, width * height * 4);
+ gl_target_write(surface->target, (void *)ADDR(surface->pixels));
+ }
}
surface->width = width;
surface->height = height;
- surface->caps = caps | 0x10004000u; /* Local video memory. */
+ surface->caps = caps | (surface->caps2 ? 0x800u : 0x10004000u); /* Managed system or local video memory. */
+ surface->description_flags = 0x100f | (flags & 0x100000);
surface->owner = owner;
++owner->references[1];
surface->interfaces[0] = create_interface(3);
@@ -700,12 +742,13 @@ static void describe_surface(struct surface_object *surface, uint32_t output)
{
memset((void *)ADDR(output), 0, 124);
MEM32(output) = 124;
- MEM32(output + 4) = 0x100f;
+ MEM32(output + 4) = surface->description_flags;
MEM32(output + 8) = surface->height;
MEM32(output + 12) = surface->width;
MEM32(output + 16) = surface->width * 4;
surface_pixel_format(surface, output + 72);
MEM32(output + 104) = surface->caps;
+ MEM32(output + 108) = surface->caps2;
}
static void surface_description(void)
@@ -848,8 +891,10 @@ static void surface_lock(void)
if (surface->locked) { g_eax = 0x887601aeu; return; }
if (!surface->pixels) surface->pixels = guest_malloc(surface->width * surface->height * 4);
if (!surface->pixels) { g_eax = 0x8007000eu; return; }
- gl_target_read(surface->target, (void *)ADDR(surface->pixels));
- swap_surface_channels(surface);
+ if (!surface->caps2) {
+ gl_target_read(surface->target, (void *)ADDR(surface->pixels));
+ swap_surface_channels(surface);
+ }
surface->locked = 1;
surface->lock_flags = flags;
describe_surface(surface, output);
@@ -1049,6 +1094,22 @@ static void device_clear(void)
static uint32_t device_state(struct device_object *device, uint32_t state)
{
switch (state) {
+ /* Inactive legacy states retain the values returned by Direct3D7. */
+ case 2: case 40: return UINT32_MAX; /* Retired antialias state sentinels. */
+ case 4: case 16: return 1; /* Perspective textures and last line pixel. */
+ case 10: case 15: case 24: case 29: case 30: case 33: return 0;
+ case 34: case 35: case 36: case 47: case 48: return 0; /* Fog/depth bias. */
+ case 37: case 38: return 0x3f800000; /* Fog end and density: 1.0. */
+ case 25: case 56: return 8; /* ALWAYS alpha/stencil comparison. */
+ case 41: case 52: case 57: return 0; /* Color key and stencil disabled. */
+ case 53: case 54: case 55: return 1; /* KEEP stencil operations. */
+ case 58: case 59: case 60: return UINT32_MAX; /* Stencil masks/texture factor. */
+ case 128: case 129: case 130: case 131:
+ case 132: case 133: case 134: case 135: return 0; /* Coordinate wrapping. */
+ case 136: case 141: case 142: case 145: return 1;
+ case 138: case 139: case 140: case 143: case 144:
+ case 147: case 148: case 151: case 152: return 0;
+ case 146: return 2; /* Specular material uses vertex color 2. */
case 7: case 14: case 137: return 1; /* Z enable/write and lighting. */
case 8: return 3; /* Solid fill. */
case 9: return 2; /* Gouraud shading. */
@@ -1174,7 +1235,8 @@ static uint32_t texture_stage_state(struct device_object *device, uint32_t state
case 3: return 1; /* Current/diffuse argument. */
case 6: return device->alpha_arg2;
case 4: return device->alpha_op;
- case 11: case 15: case 19: case 20: case 24: return 0;
+ case 7: case 8: case 9: case 10: /* Inactive bump matrix. */
+ case 11: case 15: case 19: case 20: case 22: case 23: case 24: return 0;
case 12: return 1; /* Legacy ADDRESS is separate from ADDRESSU/V. */
case 13: return device->address_u;
case 14: return device->address_v;
@@ -1190,9 +1252,14 @@ static void device_get_texture_state(void)
{
struct device_object *device = find_device(MEM32(g_esp + 4));
uint32_t stage = MEM32(g_esp + 8), state = MEM32(g_esp + 12), output = MEM32(g_esp + 16);
- if (stage) { fprintf(stderr, "Unsupported texture stage %u\n", stage); exit(3); }
g_eax = 0x80070057u;
- if (output) { MEM32(output) = texture_stage_state(device, state); g_eax = 0; }
+ if (output && stage < 8 && state >= 1 && state <= 24) {
+ /* Disabled stages still have queryable defaults for game state caches. */
+ const uint32_t inactive[] = {1, 2, 1, 1, 2, 1, 0, 0, 0, 0, 0, 1,
+ 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0};
+ MEM32(output) = stage ? inactive[state - 1] : texture_stage_state(device, state);
+ g_eax = 0;
+ }
g_esp += 20;
}
@@ -1317,6 +1384,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address)
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;
+ if (current_interface == 1 && current_slot == 12) return get_display_mode;
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;
diff --git a/decompiled/tests/devices.c b/decompiled/tests/devices.c
@@ -126,10 +126,12 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
0xff000000, 0x80777777, 0x40bbbbbb, 0x00336699
};
- for (unsigned alpha = 0; alpha < 2; ++alpha) {
+ for (unsigned variant = 0; variant < 4; ++variant) {
+ unsigned alpha = variant & 1, managed = variant >> 1;
memset(memory(desc), 0, 124);
- *memory(desc) = 124; *memory(desc + 4) = 0x1007;
+ *memory(desc) = 124; *memory(desc + 4) = managed ? 0x101007 : 0x1007;
*memory(desc + 8) = 4; *memory(desc + 12) = 4; *memory(desc + 104) = 0x1000;
+ *memory(desc + 108) = managed ? 0x10 : 0;
const uint32_t format[] = {32, 0x40 | alpha, 0, 32, 0xff0000, 0xff00, 0xff, alpha ? 0xff000000 : 0};
memcpy(memory(desc + 72), format, 32);
assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
@@ -137,8 +139,15 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16);
assert(!memcmp(memory(desc + 72), format, 32));
+ assert(*memory(desc + 4) == (managed ? 0x10100f : 0x100f));
+ assert(*memory(desc + 104) == (managed ? 0x1800 : 0x10005000));
+ assert(*memory(desc + 108) == (managed ? 0x10 : 0));
for (unsigned y = 0; y < 4; ++y) memcpy(memory(pixels + y * pitch), texels + y * 4, 16);
assert(!invoke(texture, 32, 2, 0u));
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
+ pixels = *memory(desc + 36); pitch = *memory(desc + 16);
+ for (unsigned y = 0; y < 4; ++y) assert(!memcmp(memory(pixels + y * pitch), texels + y * 4, 16));
+ assert(!invoke(texture, 32, 2, 0u));
assert(!invoke(device, 35, 3, 0u, texture));
assert(!invoke(device, 35, 3, 0u, texture));
assert(!invoke(device, 34, 3, 0u, workspace()) && *memory(workspace()) == texture);
@@ -195,6 +204,39 @@ static void textured_triangles(uint32_t draw, uint32_t device, uint32_t target,
}
}
+static void initial_device_states(uint32_t device)
+{
+ const uint32_t states[][2] = {
+ {2, 0xffffffff}, {4, 1}, {7, 1}, {8, 3}, {9, 2}, {10, 0},
+ {14, 1}, {15, 0}, {16, 1}, {19, 2}, {20, 1}, {22, 3}, {23, 4},
+ {24, 0}, {25, 8}, {26, 0}, {27, 0}, {28, 0}, {29, 0}, {30, 0},
+ {33, 0}, {34, 0}, {35, 0}, {36, 0}, {37, 0x3f800000}, {38, 0x3f800000},
+ {40, 0xffffffff}, {41, 0}, {47, 0}, {48, 0}, {52, 0}, {53, 1},
+ {54, 1}, {55, 1}, {56, 8}, {57, 0}, {58, 0xffffffff},
+ {59, 0xffffffff}, {60, 0xffffffff}, {128, 0}, {129, 0}, {130, 0},
+ {131, 0}, {132, 0}, {133, 0}, {134, 0}, {135, 0}, {136, 1},
+ {137, 1}, {138, 0}, {139, 0}, {140, 0}, {141, 1}, {142, 1},
+ {143, 0}, {144, 0}, {145, 1}, {146, 2}, {147, 0}, {148, 0}, {151, 0}, {152, 0}
+ };
+ const uint32_t texture_defaults[] = {4, 2, 1, 2, 2, 1, 0, 0, 0, 0, 0, 1,
+ 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0};
+
+ for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) {
+ assert(!invoke(device, 21, 3, states[i][0], workspace() + 4 * i));
+ }
+ for (unsigned stage = 0; stage < 8; ++stage) for (unsigned state = 1; state <= 24; ++state) {
+ assert(!invoke(device, 36, 4, stage, state, workspace() + 256 + (stage * 24 + state - 1) * 4));
+ }
+
+ for (unsigned i = 0; i < sizeof(states) / sizeof(states[0]); ++i) {
+ assert(*memory(workspace() + 4 * i) == states[i][1]);
+ }
+ for (unsigned stage = 0; stage < 8; ++stage) for (unsigned state = 1; state <= 24; ++state) {
+ uint32_t expected = stage && (state == 1 || state == 4) ? 1 : texture_defaults[state - 1];
+ assert(*memory(workspace() + 256 + (stage * 24 + state - 1) * 4) == expected);
+ }
+}
+
static void device_lifetime_and_rendering(const char *pixels_path)
{
const uint32_t draw_iid[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
@@ -230,6 +272,7 @@ static void device_lifetime_and_rendering(const char *pixels_path)
memcpy(memory(workspace() + 16), hal_iid, 16);
assert(!invoke(d3d, 4, 4, workspace() + 16, color, workspace()));
uint32_t device = *memory(workspace());
+ initial_device_states(device);
uint32_t identity = query(device, unknown);
assert(!invoke(device, 9, 2, workspace()));
diff --git a/decompiled/tests/graphics.c b/decompiled/tests/graphics.c
@@ -59,13 +59,15 @@ static uint32_t invoke(uint32_t address, unsigned count, uint32_t a, uint32_t b,
{
#ifdef _WIN32
if (count == 1) return ((uint32_t (WINAPI *)(uint32_t))(uintptr_t)address)(a);
+ if (count == 2) return ((uint32_t (WINAPI *)(uint32_t, uint32_t))(uintptr_t)address)(a, b);
return ((uint32_t (WINAPI *)(uint32_t, uint32_t, uint32_t))(uintptr_t)address)(a, b, c);
#else
uint32_t stack = g_esp, caller = g_cur_func;
g_esp -= (count + 1) * 4;
MEM32(g_esp) = RECOMP_RETADDR;
MEM32(g_esp + 4) = a;
- if (count == 3) { MEM32(g_esp + 8) = b; MEM32(g_esp + 12) = c; }
+ if (count >= 2) MEM32(g_esp + 8) = b;
+ if (count == 3) MEM32(g_esp + 12) = c;
g_ebx = 0x12345678u; g_esi = 0x23456789u;
g_edi = 0x3456789au; g_ebp = 0x456789abu;
recomp_func_t function;
@@ -339,6 +341,48 @@ static void display_modes_describe_the_native_monitor(void)
assert(ref(object, 2) == 0);
}
+static void current_display_mode_preserves_buffer_bounds(void)
+{
+ const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
+ memcpy(memory(workspace() + 4), draw7, 16);
+ assert(!create_ex(workspace() + 4, workspace(), 0));
+ uint32_t object = *memory(workspace()), output = workspace() + 128;
+ uint32_t method = memory(*memory(object))[12];
+#ifdef _WIN32
+ DEVMODEA mode = {0};
+ mode.dmSize = sizeof(mode);
+ assert(EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &mode));
+ uint32_t width = mode.dmPelsWidth, height = mode.dmPelsHeight, refresh = mode.dmDisplayFrequency;
+ assert(mode.dmBitsPerPel == 32);
+#else
+ SDL_DisplayMode mode;
+ assert(!SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(SDL_GL_GetCurrentWindow()), &mode));
+ uint32_t width = mode.w, height = mode.h, refresh = mode.refresh_rate;
+ assert(SDL_BYTESPERPIXEL(mode.format) == 4);
+#endif
+ const uint32_t sizes[] = {0, 4, 108, 124, 128};
+ for (unsigned i = 0; i < 5; ++i) {
+ memset(memory(output), 0xa5, 132);
+ *memory(output) = sizes[i];
+ uint32_t expected[33];
+ memcpy(expected, memory(output), sizeof(expected));
+ if (sizes[i] == 108 || sizes[i] == 124) {
+ memset(expected, 0, sizes[i]);
+ expected[0] = 124; expected[1] = 0x4100e;
+ expected[2] = height; expected[3] = width; expected[4] = width * 4;
+ expected[6] = refresh; expected[18] = 32; expected[19] = 0x40;
+ expected[21] = 32; expected[22] = 0xff0000; expected[23] = 0xff00; expected[24] = 0xff;
+ }
+
+ uint32_t result = invoke(method, 2, object, output, 0);
+
+ assert(result == ((sizes[i] == 108 || sizes[i] == 124) ? 0 : 0x80070057u));
+ assert(!memcmp(memory(output), expected, sizeof(expected)));
+ }
+ assert(invoke(method, 2, object, 0, 0) == 0x80070057u);
+ assert(!ref(object, 2));
+}
+
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)
{
@@ -668,6 +712,7 @@ int main(void)
device_information_has_stable_identity_and_preserves_padding();
capability_queries_respect_structure_sizes();
display_modes_describe_the_native_monitor();
+ current_display_mode_preserves_buffer_bounds();
independent_interface_counts_share_object_lifetime();
direct3d_shares_the_directdraw_seven_reference_count();
direct3d_enumerates_the_native_renderer();
diff --git a/tools/reconstruction/test_portable_devices.py b/tools/reconstruction/test_portable_devices.py
@@ -25,8 +25,8 @@ actual = subprocess.check_output(wsl + ["env", "GALLIUM_DRIVER=d3d12", linux_wor
assert actual == expected, (expected, actual)
windows_pixels = (work / "reference.rgb").read_bytes()
linux_pixels = (work / "native.rgb").read_bytes()
-assert len(windows_pixels) == len(linux_pixels) == 251904
+assert len(windows_pixels) == len(linux_pixels) == 497664
error = max(abs(a - b) for a, b in zip(windows_pixels, linux_pixels))
assert error <= 1, error
print("Windows == Linux: " + actual.strip())
-print("All 83968 pixels agree within one 8-bit channel step; maximum error:", error)
+print("All 165888 pixels agree within one 8-bit channel step; maximum error:", error)