commit 80db1369f46a98e1d961bb741a7ae4240b04794b
parent 244c008181729edd076f4f258b0dca4299c82b88
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:23:27 +0000
Implement native DirectDraw palette objects
Diffstat:
2 files changed, 185 insertions(+), 6 deletions(-)
diff --git a/decompiled/runtime/linux_graphics.c b/decompiled/runtime/linux_graphics.c
@@ -19,6 +19,12 @@ static struct clipper_object {
struct draw_object *owner;
struct clipper_object *next;
} *clippers;
+static struct palette_object {
+ uint32_t interface, references, caps, count, entry_size;
+ unsigned char entries[1024];
+ struct draw_object *owner;
+ struct palette_object *next;
+} *palettes;
struct surface_data {
uint32_t guid[4], size;
struct surface_data *next;
@@ -56,8 +62,8 @@ static struct vertex_buffer {
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 uint32_t vtables[9];
+static const unsigned interface_slots[] = {23, 30, 8, 49, 36, 9, 49, 9, 7};
static unsigned current_interface, current_slot;
static void close_graphics(void)
@@ -500,6 +506,124 @@ static struct clipper_object *find_clipper(uint32_t address)
abort();
}
+static struct palette_object *find_palette(uint32_t address)
+{
+ for (struct palette_object *palette = palettes; palette; palette = palette->next)
+ if (palette->interface == address) return palette;
+ fprintf(stderr, "Invalid guest palette interface %08x\n", address);
+ abort();
+}
+
+static void palette_fixed_colors(struct palette_object *palette)
+{
+ /* Without ALLOW256, the 8-bit palette's endpoints remain black and white. */
+ if (palette->count == 256 && !(palette->caps & 0x40)) {
+ memset(palette->entries, 0, 3);
+ memset(palette->entries + 255 * 4, 255, 3);
+ }
+}
+
+static void create_palette(void)
+{
+ struct draw_object *owner = find_object(MEM32(g_esp + 4));
+ uint32_t caps = MEM32(g_esp + 8), entries = MEM32(g_esp + 12);
+ uint32_t output = MEM32(g_esp + 16), outer = MEM32(g_esp + 20);
+ g_esp += 24;
+ g_eax = 0x80070057u;
+ if (!output) return;
+ MEM32(output) = 0;
+ uint32_t size = caps & 0x305;
+ uint32_t count = size == 0x100 ? 2 : size == 0x200 ? 4 : size == 1 ? 16 : size == 4 ? 256 : 0;
+ if (!entries || outer || !count || (caps & ~0x74fu) ||
+ ((caps & 0x40) && count != 256) || ((caps & 2) && count == 256)) return;
+ struct palette_object *palette = calloc(1, sizeof(*palette));
+ if (!palette) abort();
+ palette->interface = create_interface(8);
+ palette->references = 1;
+ palette->caps = caps & ~(0x400u | 8u);
+ palette->count = count;
+ palette->entry_size = caps & 2 ? 1 : 4;
+ memcpy(palette->entries, (void *)ADDR(entries), count * palette->entry_size);
+ palette_fixed_colors(palette);
+ palette->owner = owner;
+ ++owner->references[1];
+ palette->next = palettes;
+ palettes = palette;
+ MEM32(output) = palette->interface;
+ g_eax = 0;
+}
+
+static void palette_query_interface(void)
+{
+ struct palette_object *palette = find_palette(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 palette_iid[] = {0x6c14db84, 0x11cea733, 0x200021a5, 0x60e50baf};
+ 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), palette_iid, 16)) return;
+ MEM32(output) = palette->interface;
+ ++palette->references;
+ g_eax = 0;
+}
+
+static void palette_add_reference(void)
+{
+ g_eax = ++find_palette(MEM32(g_esp + 4))->references;
+ g_esp += 8;
+}
+
+static void palette_release_reference(void)
+{
+ struct palette_object *palette = find_palette(MEM32(g_esp + 4));
+ g_eax = --palette->references;
+ if (!g_eax) {
+ struct palette_object **link = &palettes;
+ while (*link != palette) link = &(*link)->next;
+ *link = palette->next;
+ release_draw(palette->owner, 1);
+ guest_free(palette->interface);
+ free(palette);
+ }
+ g_esp += 8;
+}
+
+static void palette_caps(void)
+{
+ struct palette_object *palette = find_palette(MEM32(g_esp + 4));
+ uint32_t output = MEM32(g_esp + 8);
+ g_eax = 0x80070057u;
+ if (output) { MEM32(output) = palette->caps; g_eax = 0; }
+ g_esp += 12;
+}
+
+static void palette_entries(void)
+{
+ struct palette_object *palette = find_palette(MEM32(g_esp + 4));
+ uint32_t flags = MEM32(g_esp + 8), start = MEM32(g_esp + 12);
+ uint32_t count = MEM32(g_esp + 16), entries = MEM32(g_esp + 20);
+ g_esp += 24;
+ g_eax = 0x80070057u;
+ if (flags || !entries || !count || start >= palette->count || count > palette->count - start) return;
+ unsigned char *data = palette->entries + start * palette->entry_size;
+ if (current_slot == 4) memcpy((void *)ADDR(entries), data, count * palette->entry_size);
+ else {
+ memcpy(data, (void *)ADDR(entries), count * palette->entry_size);
+ palette_fixed_colors(palette);
+ }
+ g_eax = 0;
+}
+
+static void palette_initialize(void)
+{
+ find_palette(MEM32(g_esp + 4));
+ g_eax = 0x88760005u; /* DDERR_ALREADYINITIALIZED */
+ g_esp += 20;
+}
+
static uint32_t release_clipper(struct clipper_object *clipper)
{
uint32_t remaining = --clipper->references;
@@ -1717,7 +1841,7 @@ static void vertex_buffer_description(void)
static void unavailable_method(void)
{
- const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7", "IDirect3DVertexBuffer7"};
+ const char *names[] = {"IDirectDraw", "IDirectDraw7", "IDirect3D7", "IDirectDrawSurface7", "IDirectDrawSurface", "IDirectDrawClipper", "IDirect3DDevice7", "IDirect3DVertexBuffer7", "IDirectDrawPalette"};
fprintf(stderr, "Unimplemented %s method slot %u caller=%08x\n",
names[current_interface], current_slot, g_cur_func);
exit(3);
@@ -1725,10 +1849,18 @@ static void unavailable_method(void)
recomp_func_t linux_graphics_virtual(uint32_t address)
{
- if (address < 0xf3000000u || address >= 0xf3000800u || (address & 3)) return NULL;
+ if (address < 0xf3000000u || address >= 0xf3000900u || (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 == 8) {
+ if (current_slot == 0) return palette_query_interface;
+ if (current_slot == 1) return palette_add_reference;
+ if (current_slot == 2) return palette_release_reference;
+ if (current_slot == 3) return palette_caps;
+ if (current_slot == 4 || current_slot == 6) return palette_entries;
+ if (current_slot == 5) return palette_initialize;
+ }
if (current_interface == 7) {
if (current_slot == 0) return vertex_buffer_query_interface;
if (current_slot == 1) return vertex_buffer_add_reference;
@@ -1802,6 +1934,7 @@ recomp_func_t linux_graphics_virtual(uint32_t address)
if (current_interface == 2 && current_slot == 5) return create_vertex_buffer;
if (current_interface == 2 && current_slot == 7) return evict_managed_textures;
if (current_interface == 1 && current_slot == 4) return create_clipper;
+ if (current_interface == 1 && current_slot == 5) return create_palette;
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;
diff --git a/decompiled/tests/surfaces.c b/decompiled/tests/surfaces.c
@@ -9,7 +9,7 @@
#include <stddef.h>
_Static_assert(sizeof(DDSURFACEDESC2) == 124, "guest surface descriptor size");
_Static_assert(offsetof(DDSURFACEDESC2, lpSurface) == 36, "guest pixels offset");
-static uint32_t scratch[256];
+static uint32_t scratch[1024];
#else
#include "linux_graphics.h"
#include "linux_window.h"
@@ -543,6 +543,51 @@ static void private_data_is_copied_and_owned_by_each_surface(void)
assert(!invoke(first, 2, 1) && !invoke(second, 2, 1) && !invoke(draw, 2, 1));
}
+static void palette_entries_and_lifetime(void)
+{
+ const uint32_t caps[] = {0x100, 0x200, 1, 4, 0x44, 0x404, 0x108, 0x102};
+ const unsigned counts[] = {2, 4, 16, 256, 256, 256, 2, 2};
+ uint32_t draw = create_draw(), input = workspace() + 1024, output = workspace() + 2048;
+ uint32_t iid = workspace() + 16;
+ const uint32_t palette_iid[] = {0x6c14db84, 0x11cea733, 0x200021a5, 0x60e50baf};
+ memcpy(memory(iid), palette_iid, 16);
+
+ for (unsigned c = 0; c < sizeof(caps) / sizeof(caps[0]); ++c) {
+ for (unsigned i = 0; i < 256; ++i) memory(input)[i] = 0xa5030201u + i;
+ assert(!invoke(draw, 5, 5, caps[c], input, workspace(), 0u));
+ uint32_t palette = *memory(workspace());
+ assert(!invoke(palette, 3, 2, workspace()));
+ assert(*memory(workspace()) == (caps[c] & ~(0x400u | 8u)));
+ assert(!invoke(palette, 0, 3, iid, workspace()) && *memory(workspace()) == palette);
+ assert(invoke(palette, 2, 1) == 1);
+ assert(invoke(draw, 1, 1) == 3 && invoke(draw, 2, 1) == 2);
+ memset(memory(output), 0xcc, 1028);
+ assert(!invoke(palette, 4, 5, 0u, 0u, counts[c], output));
+ unsigned bytes = counts[c] * (caps[c] & 2 ? 1 : 4);
+ if (counts[c] == 256 && !(caps[c] & 0x40)) {
+ memory(input)[0] &= 0xff000000;
+ memory(input)[255] |= 0xffffff;
+ }
+
+ assert(!memcmp(memory(output), memory(input), bytes));
+ assert(*((unsigned char *)memory(output) + bytes) == 0xcc);
+ *memory(input) = 0xa5191817;
+ assert(!invoke(palette, 6, 5, 0u, 1u, 1u, input));
+ assert(!invoke(palette, 4, 5, 0u, 1u, 1u, output));
+ assert(!memcmp(memory(output), memory(input), caps[c] & 2 ? 1 : 4));
+ assert(invoke(palette, 4, 5, 0u, 0u, 0u, output) == 0x80070057u);
+ assert(invoke(palette, 6, 5, 0u, counts[c], 1u, input) == 0x80070057u);
+ assert(invoke(palette, 4, 5, 0u, 0u, 0xffffffffu, output) == 0x80070057u);
+ assert(invoke(palette, 4, 5, 1u, 0u, 1u, output) == 0x80070057u);
+ assert(invoke(palette, 5, 4, draw, caps[c], input) == 0x88760005u);
+ assert(!invoke(palette, 2, 1));
+ }
+ *memory(workspace()) = 0xa5a5a5a5;
+ assert(invoke(draw, 5, 5, 0x104u, input, workspace(), 0u) == 0x80070057u);
+ assert(!*memory(workspace()));
+ assert(!invoke(draw, 2, 1));
+}
+
int main(void)
{
#ifndef _WIN32
@@ -560,6 +605,7 @@ int main(void)
window_clipper_regions();
private_data_is_copied_and_owned_by_each_surface();
surface_blits_copy_pixels();
- puts("surfaces: descriptions, COM ownership, clipper regions, depth attachments, private data, rectangle locks, blits and pixel round trips passed");
+ palette_entries_and_lifetime();
+ puts("surfaces: descriptions, COM ownership, clipper regions, palettes, depth attachments, private data, rectangle locks, blits and pixel round trips passed");
return 0;
}