commit d0dc00f6948548ac83a407b47ccdac5d490c51e8
parent 5ffcc6519fec37a1a227633358bc3b79cab1ca3b
Author: anon <anon@example.com>
Date: Wed, 11 Nov 2026 00:36:07 +0000
Batch intermediate texture writes during startup
Diffstat:
4 files changed, 143 insertions(+), 6 deletions(-)
diff --git a/client/game/bbr.dll/lifted-0080.c b/client/game/bbr.dll/lifted-0080.c
@@ -1,5 +1,8 @@
#define RECOMP_GENERATED_CODE
#include "recomp_types.h"
+#ifdef RECOMP_PORTABLE
+#include "linux_graphics.h"
+#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -3480,7 +3483,7 @@ L_25138FBE:
RECOMP_REGS_OUT();
}
-void sub_25138FC5(void) {
+static void load_texture_memory(void) {
int _fpu_cmp = 0;
uint32_t _cf = 0;
int _df = 1;
@@ -3757,6 +3760,16 @@ L_2513914A:
RECOMP_REGS_OUT();
}
+void sub_25138FC5(void) {
+#ifdef RECOMP_PORTABLE
+ uint32_t texture = linux_graphics_begin_texture_update(MEM32(g_esp + 8));
+#endif
+ load_texture_memory();
+#ifdef RECOMP_PORTABLE
+ linux_graphics_end_texture_update(texture);
+#endif
+}
+
void sub_2513914A(void) {
int _fpu_cmp = 0;
uint32_t _cf = 0;
diff --git a/client/runtime/linux_graphics.c b/client/runtime/linux_graphics.c
@@ -75,7 +75,7 @@ static struct surface_object {
uint32_t width, height, caps, pixels, lock_flags, depth_bits, alpha, rgb_bits;
uint32_t caps2, description_flags, pitch, mip_count;
int32_t lock_rectangle[4];
- int locked;
+ int locked, texture_updates, pending_write;
struct draw_object *owner;
struct clipper_object *clipper;
struct gl_target *target;
@@ -1404,6 +1404,41 @@ static void transfer_surface_pixels(struct surface_object *surface, enum surface
}
}
+/* Keep intermediate mip pixels in guest format until a complete update finishes. */
+static void flush_surface_write(struct surface_object *surface)
+{
+ if (!surface->pending_write) return;
+ swap_surface_channels(surface);
+ transfer_surface_pixels(surface, SURFACE_WRITE);
+ if (surface->texture_updates || ((surface->caps & 0x800) && !(surface->caps2 & 0x10)))
+ swap_surface_channels(surface);
+ surface->pending_write = 0;
+}
+
+uint32_t linux_graphics_begin_texture_update(uint32_t address)
+{
+ struct surface_object *surface = find_surface(address);
+ if (!(surface->caps & 0x1000)) return 0;
+ for (struct surface_object *mip = surface; mip; mip = mip->attached)
+ if (mip->locked || mip->depth) return 0;
+ for (struct surface_object *mip = surface; mip; mip = mip->attached) ++mip->texture_updates;
+ return address;
+}
+
+void linux_graphics_end_texture_update(uint32_t address)
+{
+ if (!address) return;
+ for (struct surface_object *surface = find_surface(address); surface; surface = surface->attached) {
+ assert(surface->texture_updates && !surface->locked);
+ if (--surface->texture_updates) continue;
+ flush_surface_write(surface);
+ if (!(surface->caps & 0x800) || (surface->caps2 & 0x10)) {
+ guest_free(surface->pixels);
+ surface->pixels = 0;
+ }
+ }
+}
+
static void evict_managed_textures(void)
{
struct draw_object *owner = find_object(MEM32(g_esp + 4));
@@ -1414,6 +1449,7 @@ static void evict_managed_textures(void)
swap_surface_channels(surface);
transfer_surface_pixels(surface, SURFACE_EVICT);
swap_surface_channels(surface);
+ surface->pending_write = 0;
} else gl_target_evict(surface->target, NULL);
if (!surface->locked) {
guest_free(surface->pixels);
@@ -1442,7 +1478,7 @@ static void surface_lock(void)
if (rect) memcpy(rectangle, (void *)ADDR(rect), 16);
if (rectangle[0] < 0 || rectangle[1] < 0 || rectangle[2] < rectangle[0] || rectangle[3] < rectangle[1] ||
rectangle[2] > (int32_t)surface->width || rectangle[3] > (int32_t)surface->height) return;
- int readback = !surface->pixels || !(surface->caps & 0x800);
+ int readback = !surface->pixels || (!(surface->caps & 0x800) && !surface->texture_updates);
if (!surface->pixels) surface->pixels = guest_malloc(surface->pitch * surface->height);
if (!surface->pixels) { g_eax = 0x8007000eu; return; }
if (readback) {
@@ -1465,6 +1501,12 @@ static void surface_unlock(void)
if (!surface->locked || (rect && memcmp((void *)ADDR(rect), surface->lock_rectangle, 16))) {
g_eax = 0x88760248u; return;
}
+ if (surface->texture_updates) {
+ if (!(surface->lock_flags & 0x10)) surface->pending_write = 1;
+ surface->locked = 0;
+ g_eax = 0;
+ return;
+ }
int temporary = !(surface->caps & 0x800) || (surface->caps2 & 0x10);
if (!(surface->lock_flags & 0x10)) {
swap_surface_channels(surface);
@@ -1530,6 +1572,8 @@ static void surface_blit(void)
to[i] = clipped[i];
}
} else if (to[0] < 0 || to[1] < 0 || to[2] > (int32_t)target->width || to[3] > (int32_t)target->height) return;
+ flush_surface_write(source);
+ flush_surface_write(target);
gl_target_blit(source->target, target->target, from, to);
if (target->rgb_bits == 16) {
/* A blit into RGB565 also loses the low channel bits before rendering. */
@@ -1538,7 +1582,7 @@ static void surface_blit(void)
transfer_surface_pixels(target, SURFACE_READ);
transfer_surface_pixels(target, SURFACE_WRITE);
}
- if ((target->caps2 & 0x10) || (target->rgb_bits == 16 && !(target->caps & 0x800))) {
+ if ((target->caps2 & 0x10) || ((target->rgb_bits == 16 || target->texture_updates) && !(target->caps & 0x800))) {
guest_free(target->pixels);
target->pixels = 0;
} else if ((target->caps & 0x800) && target->rgb_bits != 16) {
@@ -2314,6 +2358,8 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin
struct texture_stage *stage = &device->stages[i];
int needs_texture = (stage->color_arg1 & 15) == 2 || (stage->color_op != 2 && (stage->color_arg2 & 15) == 2);
if (stage->color_op != 1 && (stage->texture || !needs_texture)) {
+ for (struct surface_object *mip = stage->texture; mip; mip = mip->attached)
+ flush_surface_write(mip);
struct gl_texture_state state = {stage->texture ? stage->texture->target : NULL,
stage->min_filter == 2, stage->mag_filter == 2,
stage->sampler_u == 3, stage->sampler_v == 3,
diff --git a/client/runtime/linux_graphics.h b/client/runtime/linux_graphics.h
@@ -13,4 +13,7 @@ int linux_graphics_display_mode(struct SDL_DisplayMode *mode);
int linux_graphics_resize(unsigned width, unsigned height, uint32_t drawing_surface);
int linux_graphics_project(const float position[3], float screen[3]);
+uint32_t linux_graphics_begin_texture_update(uint32_t address);
+void linux_graphics_end_texture_update(uint32_t address);
+
#endif
diff --git a/client/tests/surfaces.c b/client/tests/surfaces.c
@@ -200,6 +200,70 @@ static void surface_pixels_round_trip(uint32_t caps)
}
#ifndef _WIN32
+static void batched_texture_writes_preserve_reads_and_blits(uint32_t caps2)
+{
+ uint32_t draw = create_draw(), output = create_surface(draw, 0x2040);
+ uint32_t desc = workspace() + 128, rect = workspace() + 64;
+ const uint32_t format[] = {32, 0x40, 0, 32, 0xff0000, 0xff00, 0xff, 0};
+ memset(memory(desc), 0, 124);
+ *memory(desc) = 124; *memory(desc + 4) = 0x1007;
+ *memory(desc + 8) = 4; *memory(desc + 12) = 4;
+ memcpy(memory(desc + 72), format, sizeof(format));
+ *memory(desc + 104) = 0x1000; *memory(desc + 108) = caps2;
+ assert(!invoke(draw, 6, 4, desc, workspace(), 0u));
+ uint32_t texture = *memory(workspace());
+
+ assert(!linux_graphics_begin_texture_update(output));
+ linux_graphics_end_texture_update(0);
+ assert(linux_graphics_begin_texture_update(texture) == texture);
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x21u, 0u));
+ uint32_t pixels = *memory(desc + 36);
+ for (unsigned i = 0; i < 16; ++i) *memory(pixels + i * 4) = 0x00112233;
+ assert(!invoke(texture, 32, 2, 0u));
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
+ assert(*memory(desc + 36) == pixels && *memory(pixels) == 0x00112233);
+ assert(!invoke(texture, 32, 2, 0u));
+
+ // A nested partial update must retain the surrounding pixels and guest layout.
+ assert(linux_graphics_begin_texture_update(texture) == texture);
+ const int32_t middle[] = {1, 1, 3, 3};
+ memcpy(memory(rect), middle, sizeof(middle));
+ assert(!invoke(texture, 25, 5, rect, desc, 0x21u, 0u));
+ assert(*memory(desc + 36) == pixels + 20);
+ for (unsigned y = 1; y < 3; ++y) for (unsigned x = 1; x < 3; ++x)
+ *memory(pixels + y * 16 + x * 4) = 0x00445566;
+ assert(!invoke(texture, 32, 2, rect));
+ linux_graphics_end_texture_update(texture);
+
+ // A GPU consumer inside the batch sees every completed write.
+ const int32_t full[] = {0, 0, 4, 4};
+ memcpy(memory(rect), full, sizeof(full));
+ assert(!invoke(output, 5, 6, rect, texture, 0u, 0x1000000u, 0u));
+ assert(!invoke(output, 25, 5, rect, desc, 0x11u, 0u));
+ for (unsigned y = 0; y < 4; ++y) for (unsigned x = 0; x < 4; ++x)
+ assert((*memory(*memory(desc + 36) + y * *memory(desc + 16) + x * 4) & 0xffffff) ==
+ (x >= 1 && x < 3 && y >= 1 && y < 3 ? 0x00445566u : 0x00112233u));
+ assert(!invoke(output, 32, 2, rect));
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
+ assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00112233);
+ assert((*memory(*memory(desc + 36) + 20) & 0xffffff) == 0x00445566);
+ assert(!invoke(texture, 32, 2, 0u));
+
+ // A blit into the batch invalidates its retained CPU copy before the next lock.
+ assert(!invoke(output, 25, 5, rect, desc, 0x21u, 0u));
+ *memory(*memory(desc + 36)) = 0x00778899;
+ assert(!invoke(output, 32, 2, rect));
+ assert(!invoke(texture, 5, 6, 0u, output, rect, 0x1000000u, 0u));
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
+ assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00778899);
+ assert(!invoke(texture, 32, 2, 0u));
+ linux_graphics_end_texture_update(texture);
+ assert(!invoke(texture, 25, 5, 0u, desc, 0x11u, 0u));
+ assert((*memory(*memory(desc + 36)) & 0xffffff) == 0x00778899);
+ assert(!invoke(texture, 32, 2, 0u));
+ assert(!invoke(texture, 2, 1) && !invoke(output, 2, 1) && !invoke(draw, 2, 1));
+}
+
static void uploaded_surface_releases_temporary_pixels(uint32_t caps2)
{
uint32_t draw = create_draw();
@@ -713,7 +777,7 @@ static void absent_mip_attachment_returns_not_found(void)
assert(!invoke(surface, 2, 1) && !invoke(draw, 2, 1));
}
-static void mip_chain_levels_and_pixels(void)
+static void mip_chain_levels_and_pixels(int batch)
{
uint32_t draw = create_draw(), desc = workspace() + 128, caps = workspace() + 64;
const unsigned widths[] = {32, 16, 8, 4, 2, 1};
@@ -729,6 +793,9 @@ static void mip_chain_levels_and_pixels(void)
uint32_t levels[6] = {*memory(workspace())};
memset(memory(caps), 0, 16);
*memory(caps) = 0x401000;
+#ifndef _WIN32
+ if (batch) assert(linux_graphics_begin_texture_update(levels[0]) == levels[0]);
+#endif
for (unsigned i = 0; i < 6; ++i) {
*memory(desc) = 124;
@@ -744,6 +811,9 @@ static void mip_chain_levels_and_pixels(void)
}
}
+#ifndef _WIN32
+ if (batch) linux_graphics_end_texture_update(levels[0]);
+#endif
assert(invoke(draw, 1, 1) == 4 && invoke(draw, 2, 1) == 3);
for (unsigned i = 0; i < 6; ++i) {
assert(!invoke(levels[i], 25, 5, 0u, desc, 0x11u, 0u));
@@ -847,6 +917,8 @@ int main(void)
surface_pixels_round_trip(0x2040);
surface_pixels_round_trip(0x840);
#ifndef _WIN32
+ batched_texture_writes_preserve_reads_and_blits(0);
+ batched_texture_writes_preserve_reads_and_blits(0x10);
uploaded_surface_releases_temporary_pixels(0);
uploaded_surface_releases_temporary_pixels(0x10);
rgb565_textures_preserve_pixels_and_rectangle_locks(0);
@@ -862,7 +934,10 @@ int main(void)
surface_blits_copy_pixels();
palette_entries_and_lifetime();
absent_mip_attachment_returns_not_found();
- mip_chain_levels_and_pixels();
+ mip_chain_levels_and_pixels(0);
+#ifndef _WIN32
+ mip_chain_levels_and_pixels(1);
+#endif
partial_mip_chain_preserves_padded_rows();
surface_identity_collisions_and_alias_lifetimes();
puts("surfaces: descriptions, COM ownership, clipper regions, palettes, depth attachments, private data, rectangle locks, blits and pixel round trips passed");