From cbf25510df832466f785cf3977b8dbc0078beede Mon Sep 17 00:00:00 2001 From: leroy ketelaars Date: Sat, 14 Nov 2026 21:35:28 +0000 Subject: [PATCH] Allocate XFBs for the quantized GX display-copy extent --- docs/framebuffer-copy-bounds.md | 86 ++++++++++ .../native-canary.json | 159 ++++++++++++++++++ include/dnd_display_gx.inc | 3 +- include/dnd_framebuffer_gx.inc | 13 ++ scripts/dolphin_framebuffer_canary.py | 76 +++++++++ source/graphics_gx.c | 3 +- tests/Makefile | 2 +- tests/display_mode_test.c | 65 ++++++- tests/framebuffer_canary_gamecube.c | 77 +++++++++ 9 files changed, 473 insertions(+), 11 deletions(-) create mode 100644 docs/framebuffer-copy-bounds.md create mode 100644 docs/framebuffer-copy-bounds/native-canary.json create mode 100644 include/dnd_framebuffer_gx.inc create mode 100644 scripts/dolphin_framebuffer_canary.py create mode 100644 tests/framebuffer_canary_gamecube.c diff --git a/docs/framebuffer-copy-bounds.md b/docs/framebuffer-copy-bounds.md new file mode 100644 index 000000000..08a3d10ee --- /dev/null +++ b/docs/framebuffer-copy-bounds.md @@ -0,0 +1,86 @@ +# Quantized GX display-copy bounds + +The standard 480-line AA presets use a 242-line EFB. In the pinned libogc2 +toolchain, `GX_GetYScaleFactor(242,480)` returns `1.98760330677` and +`GX_GetNumXfbLines` reports **483** copied rows. A normal +`SYS_AllocateFramebuffer` request for the VI mode reserves only 480 rows. +At a 640-pixel padded stride and two bytes per XFB pixel, that leaves a +**3,840-byte overrun** on each affected copy. + +The shared allocation helper queries the actual library's quantized copy +height, then allocates `max(VI rows, copied rows)`. Only the temporary storage +mode changes. The VI mode, EFB dimensions, stride, sample pattern and presented +480-line output remain intact. Both initialization and display transitions use +the helper. Transitions retain the existing allocate-both-before-publication +behavior and preserve the current buffers if either replacement allocation fails. + +## Verification + +The complete native host suite passes in its strict debug and shipped O3/LTO +configurations. The display fixture covers older libogc's 483-row result, a +479-row result from another quantizer, padded strides, ordinary 240/480/576 +modes, unchanged VI metadata and both transactional allocation failures. + +The optional PowerPC fixture in `tests/framebuffer_canary_gamecube.c` includes +the production helper. Its wrapped allocator reserves a separate 20 KiB red +zone, allowing an unsafe control copy to be observed without corrupting the +heap. The source EFB is cleared to black, so every copied YUYV byte differs +from the red-zone sentinel. + +Using the exact pinned toolchain image +`ghcr.io/extremscorner/libogc2@sha256:13bb658d3f18903816617223f5d7c6f776d3db3ea806aae67f8d35a7a69d4c85`, +the fixture ran in Dolphin 2503 / OpenGL with XFB-to-RAM copies, emulator AA +and postprocessing disabled. The measured legacy 480i-AA and 480p-AA controls +both wrote 618,240 bytes into a claimed 614,400-byte framebuffer, changing all +3,840 bytes of the adjacent red zone. The helper requested 618,240 bytes and +both copies left its red zone intact. + +| Native copy | Storage rows | Claimed bytes | Written bytes | Red-zone changes | +| --- | ---: | ---: | ---: | ---: | +| 480i AA, legacy | 480 | 614400 | 618240 | 3840 | +| 480p AA, legacy | 480 | 614400 | 618240 | 3840 | +| 480i/p AA, helper | 483 | 618240 | 618240 | 0 | +| Return from 576p to 480p AA | 483 | 618240 | 618240 | 0 | + +All twelve cases completed: 240p, non-AA 480i/p, unsafe and protected 480i/p AA, +576i/p, return to 480p AA, 240p AA and a safe 480i return. Every protected copy +stayed within its allocation. This establishes actual emulator GX-copy bounds; +it is not physical-console qualification or proof of visual AA quality. +The [complete result and provenance](framebuffer-copy-bounds/native-canary.json) +include all twelve cases and hashes of the fixture, production helper, binaries +and original row readback. + +## Reproduction + +Run the host regression suite, including debug and O3/LTO assertions: + +```sh +python3 scripts/ci_test_suite.py native +``` + +Use devkitPPC and libogc2 with the SDK's generated EXI overlay. The overlay is +needed for this pinned environment's pre-main RTC read; it does not replace +the GX math or copy implementation. Set `DND_EXI_OVERLAY` to the generated +`build/exi-overlay` directory, then build a matching DOL/ELF pair: + +```sh +powerpc-eabi-gcc -g -O2 -Wall -Wextra -Werror -DGEKKO -mogc -mcpu=750 \ + -meabi -mhard-float -Iinclude -I"$DEVKITPRO/libogc2/gamecube/include" \ + tests/framebuffer_canary_gamecube.c -L"$DND_EXI_OVERLAY" \ + -L"$DEVKITPRO/libogc2/gamecube/lib" -Wl,--wrap=SYS_AllocateFramebuffer \ + -logc -lm -o /tmp/framebuffer-canary.elf +elf2dol /tmp/framebuffer-canary.elf /tmp/framebuffer-canary.dol +``` + +With a working X11 display (or Xvfb), run: + +```sh +python3 scripts/dolphin_framebuffer_canary.py \ + --dol /tmp/framebuffer-canary.dol --elf /tmp/framebuffer-canary.elf \ + --logs /tmp/fresh-framebuffer-canary +``` + +The script records each mode's predicted and allocated rows, last written byte +and changed red-zone bytes in `result.json`, plus the original PowerPC readback +in `rows.bin`. It requires overwritten unsafe controls and intact protected +red zones before reporting success. No firmware or sample assets are required. diff --git a/docs/framebuffer-copy-bounds/native-canary.json b/docs/framebuffer-copy-bounds/native-canary.json new file mode 100644 index 000000000..71c38ac06 --- /dev/null +++ b/docs/framebuffer-copy-bounds/native-canary.json @@ -0,0 +1,159 @@ +{ + "success": true, + "scope": "Native PowerPC fixture; actual Dolphin OpenGL GX_CopyDisp writes, no emulator AA or postprocessing; not physical hardware", + "completion_pc": "0x800038e4", + "legacy_controls_overwrite_red_zone": true, + "production_helper_preserves_red_zone": true, + "rows": [ + { + "mode": "240p/helper", + "width": 320, + "efb_rows": 240, + "vi_rows": 240, + "predicted_rows": 240, + "storage_rows": 240, + "claimed_bytes": 153600, + "last_written_byte": 153600, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480i/helper", + "width": 640, + "efb_rows": 480, + "vi_rows": 480, + "predicted_rows": 480, + "storage_rows": 480, + "claimed_bytes": 614400, + "last_written_byte": 614400, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480p/helper", + "width": 640, + "efb_rows": 480, + "vi_rows": 480, + "predicted_rows": 480, + "storage_rows": 480, + "claimed_bytes": 614400, + "last_written_byte": 614400, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480i-AA/legacy", + "width": 640, + "efb_rows": 242, + "vi_rows": 480, + "predicted_rows": 483, + "storage_rows": 480, + "claimed_bytes": 614400, + "last_written_byte": 618240, + "red_zone_changed_bytes": 3840, + "legacy": 1 + }, + { + "mode": "480i-AA/helper", + "width": 640, + "efb_rows": 242, + "vi_rows": 480, + "predicted_rows": 483, + "storage_rows": 483, + "claimed_bytes": 618240, + "last_written_byte": 618240, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480p-AA/legacy", + "width": 640, + "efb_rows": 242, + "vi_rows": 480, + "predicted_rows": 483, + "storage_rows": 480, + "claimed_bytes": 614400, + "last_written_byte": 618240, + "red_zone_changed_bytes": 3840, + "legacy": 1 + }, + { + "mode": "480p-AA/helper", + "width": 640, + "efb_rows": 242, + "vi_rows": 480, + "predicted_rows": 483, + "storage_rows": 483, + "claimed_bytes": 618240, + "last_written_byte": 618240, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "576i/helper", + "width": 640, + "efb_rows": 480, + "vi_rows": 576, + "predicted_rows": 576, + "storage_rows": 576, + "claimed_bytes": 737280, + "last_written_byte": 737280, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "576p/helper", + "width": 640, + "efb_rows": 480, + "vi_rows": 576, + "predicted_rows": 576, + "storage_rows": 576, + "claimed_bytes": 737280, + "last_written_byte": 737280, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480p-AA/return", + "width": 640, + "efb_rows": 242, + "vi_rows": 480, + "predicted_rows": 483, + "storage_rows": 483, + "claimed_bytes": 618240, + "last_written_byte": 618240, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "240p-AA/helper", + "width": 320, + "efb_rows": 240, + "vi_rows": 240, + "predicted_rows": 240, + "storage_rows": 240, + "claimed_bytes": 153600, + "last_written_byte": 153600, + "red_zone_changed_bytes": 0, + "legacy": 0 + }, + { + "mode": "480i/safe-return", + "width": 640, + "efb_rows": 480, + "vi_rows": 480, + "predicted_rows": 480, + "storage_rows": 480, + "claimed_bytes": 614400, + "last_written_byte": 614400, + "red_zone_changed_bytes": 0, + "legacy": 0 + } + ], + "toolchain_image": "ghcr.io/extremscorner/libogc2@sha256:13bb658d3f18903816617223f5d7c6f776d3db3ea806aae67f8d35a7a69d4c85", + "fixture_sha256": "012ec4c1d9d7e4994a9b70f5c2abc79e0cc9c18229be19ffc105ce91eac7c6cf", + "production_helper_sha256": "55ef54883274b223869dab9eb1cc49d6385b46e35676eb3657f78fb8ee8e62fe", + "dol_sha256": "feb99d8670dd80e22b567e805ca28c63fcf34f557cd6dbcf1e8e10c2e47cd6f3", + "elf_sha256": "b2b9be70228896d4b88a6c2986df21d2c080faf559b08df4608843c9c8a5dc8b", + "raw_rows_sha256": "5b89743e4165d711a7e78b61c062264418853075c862b400471da553f47f0977" +} diff --git a/include/dnd_display_gx.inc b/include/dnd_display_gx.inc index 0aa581d15..7886db12d 100644 --- a/include/dnd_display_gx.inc +++ b/include/dnd_display_gx.inc @@ -1,5 +1,6 @@ /* Included by the GX backend and the isolated display lifecycle fixture. * mode/xfb/fb/first_frame are the backend's actual presentation state. */ +#include "dnd_framebuffer_gx.inc" static GXRModeObj configured_display_mode; static int select_display_mode(int resolution,int scan,GXRModeObj *selected) { if(resolution<0||resolution>3||scan<0||scan>2)return -1; @@ -55,7 +56,7 @@ static int configure_display_mode(int resolution,int scan,int aa) { if(!memcmp(mode,&next,sizeof next))return 0; /* Allocate both replacements first. An allocation failure cannot destroy * the current mode or scanout buffers. */ - void *a=SYS_AllocateFramebuffer(&next),*b=SYS_AllocateFramebuffer(&next); + void *a=allocate_display_framebuffer(&next),*b=allocate_display_framebuffer(&next); if(!a||!b){free(a);free(b);return -2;} GX_DrawDone(); VIDEO_SetBlack(TRUE);VIDEO_Flush();VIDEO_WaitVSync(); diff --git a/include/dnd_framebuffer_gx.inc b/include/dnd_framebuffer_gx.inc new file mode 100644 index 000000000..3fe188dfd --- /dev/null +++ b/include/dnd_framebuffer_gx.inc @@ -0,0 +1,13 @@ +#ifndef DND_FRAMEBUFFER_GX_INCLUDED +#define DND_FRAMEBUFFER_GX_INCLUDED +/* GX quantizes the vertical copy scale. Some libogc versions copy 483 + * rows for the 242-line EFB in a 480-line AA mode. Size storage for that + * extent without changing the VI mode, stride or allocation alignment. */ +static void *allocate_display_framebuffer(const GXRModeObj *display) { + GXRModeObj storage=*display; + int copied=GX_GetNumXfbLines(display->efbHeight, + GX_GetYScaleFactor(display->efbHeight,display->xfbHeight)); + if(copied>storage.xfbHeight)storage.xfbHeight=copied; + return SYS_AllocateFramebuffer(&storage); +} +#endif diff --git a/scripts/dolphin_framebuffer_canary.py b/scripts/dolphin_framebuffer_canary.py new file mode 100644 index 000000000..52424ae4e --- /dev/null +++ b/scripts/dolphin_framebuffer_canary.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""Qualify GX XFB copy bounds using the optional native red-zone fixture. + +Build tests/framebuffer_canary_gamecube.c with the SDK EXI overlay and +-Wl,--wrap=SYS_AllocateFramebuffer, preserving a matching DOL/ELF pair. +Run under the repository's Linux Xvfb/Dolphin setup; no original IPL is needed. +This is emulator GPU evidence, not physical-console qualification. +""" +import argparse +import json +from pathlib import Path +import socket +import struct +import subprocess +import time + +from dolphin_controller_test import request +from dolphin_gdb_probe import parse_stop_pc + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('--dol',type=Path,required=True) + parser.add_argument('--elf',type=Path,required=True) + parser.add_argument('--logs',type=Path,required=True) + parser.add_argument('--dolphin',default='/usr/games/dolphin-emu-nogui') + parser.add_argument('--port',type=int,default=7176) + args=parser.parse_args() + root=args.logs.resolve() + if root.exists() and any(root.iterdir()):parser.error('Use a fresh log directory') + root.mkdir(parents=True,exist_ok=True);config=root/'user/Config';config.mkdir(parents=True) + (config/'Dolphin.ini').write_text('[Core]\nGFXBackend = OGL\nCPUCore = 1\nCPUThread = False\n[General]\nGDBPort = '+str(args.port)+'\n[Interface]\nDebugModeEnabled = True\n[DSP]\nBackend = No Audio Output\n[Display]\nRenderWindowWidth = 640\nRenderWindowHeight = 480\n') + (config/'GFX.ini').write_text('[Settings]\nInternalResolution = 1\nMSAA = 1\nSSAA = False\n[Enhancements]\nPostProcessingShader = \n[Hacks]\nXFBToTextureEnable = False\nDeferEFBCopies = False\nForceProgressive = False\n') + wanted={'dnd_framebuffer_canary_done','dnd_framebuffer_canary_rows','dnd_framebuffer_canary_stage'};symbols={} + for line in subprocess.check_output(['nm','-a',str(args.elf.resolve())],text=True).splitlines(): + parts=line.split() + if len(parts)==3 and parts[2] in wanted:symbols[parts[2]]=int(parts[0],16) + if symbols.keys()!=wanted:raise ValueError('Missing native canary symbols') + log=(root/'emulator.log').open('w') + process=subprocess.Popen([args.dolphin,'--platform','x11','-u',str(root/'user'),'-C','SYSCONF.IPL.PGS=True','-e',str(args.dol.resolve())],stdout=log,stderr=subprocess.STDOUT) + connection=None + try: + deadline=time.monotonic()+30 + while time.monotonic()9I',raw,i*36)))) for i,name in enumerate(names)] + # The unsafe controls still have a separately allocated red zone. A + # changed control proves real DMA happened beyond the old claimed size. + controls=all(r['red_zone_changed_bytes']>0 and r['last_written_byte']>r['claimed_bytes'] for r in rows if r['legacy']) + protected=all(r['last_written_byte']>0 and r['last_written_byte']<=r['claimed_bytes'] and r['red_zone_changed_bytes']==0 for r in rows if not r['legacy']) + result={'success':controls and protected,'scope':'Native PowerPC fixture; actual Dolphin OpenGL GX_CopyDisp writes, no emulator AA or postprocessing; not physical hardware','completion_pc':hex(address),'legacy_controls_overwrite_red_zone':controls,'production_helper_preserves_red_zone':protected,'rows':rows} + (root/'result.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result,indent=2),flush=True) + if not result['success']:raise RuntimeError('Native framebuffer canary failed; inspect result.json') + finally: + if connection is not None:connection.close() + process.terminate() + try:process.wait(timeout=10) + except subprocess.TimeoutExpired:process.kill();process.wait() + log.close() + + +if __name__=='__main__':main() diff --git a/source/graphics_gx.c b/source/graphics_gx.c index 027b31198..0f84887bb 100644 --- a/source/graphics_gx.c +++ b/source/graphics_gx.c @@ -71,11 +71,12 @@ static uint32_t tint(uint32_t a,uint32_t b) { static uint32_t rgba(float r,float g,float b,float a) { return (uint32_t)(r*255)<<24|(uint32_t)(g*255)<<16|(uint32_t)(b*255)<<8|(uint32_t)(a*255); } +#include "dnd_framebuffer_gx.inc" void dnd_graphics_init(void) { if(mode)return; VIDEO_Init();mode=VIDEO_GetPreferredMode(NULL); if(!mode){dnd_exception_throw(DND_EXCEPTION_INVALID_OPERATION,"No video mode available.");return;} - void *first=SYS_AllocateFramebuffer(mode),*second=SYS_AllocateFramebuffer(mode); + void *first=allocate_display_framebuffer(mode),*second=allocate_display_framebuffer(mode); fifo=memalign(32,256*1024); /* A null allocation must be checked before the K0-to-K1 address alias. */ if(!first||!second||!fifo){free(first);free(second);free(fifo);fifo=NULL;mode=NULL;dnd_exception_throw(DND_EXCEPTION_OUT_OF_MEMORY,"Graphics initialization failed.");return;} diff --git a/tests/Makefile b/tests/Makefile index fc284fc04..f53ff9cee 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -244,7 +244,7 @@ graphics_phase_two_test: graphics_phase_two_test.c ../source/draw_resources.c graphics_resource_boundaries_test: graphics_resource_boundaries_test.c ../source/draw_resources.c $(CC) $(CFLAGS) -ffunction-sections -fdata-sections $^ $(GC_LDFLAGS) -lm -o $@ -display_mode_test: mesh_streams_test display_mode_test.c ../include/dnd_display_gx.inc +display_mode_test: mesh_streams_test display_mode_test.c ../include/dnd_display_gx.inc ../include/dnd_framebuffer_gx.inc $(CC) $(CFLAGS) display_mode_test.c -o $@ .PHONY: system_firmware_native_test diff --git a/tests/display_mode_test.c b/tests/display_mode_test.c index 20c4bb3bc..96db64c70 100644 --- a/tests/display_mode_test.c +++ b/tests/display_mode_test.c @@ -17,15 +17,36 @@ MODE(TVMpal480IntAa,8,640,242,480,480);MODE(TVMpal480ProgAa,10,640,242,480,480); MODE(TVEurgb60Hz480IntAa,20,640,242,480,480);MODE(TVEurgb60Hz480ProgAa,22,640,242,480,480); static GXRModeObj *mode=&TVNtsc480IntDf;static void *xfb[2];static unsigned fb;static bool first_frame; static int dtv,allocations,fail_at,live,waits,gpu_waits,configured,pending; -static void *allocate(GXRModeObj *m){assert(m);allocations++;if(allocations==fail_at)return NULL;live++;return malloc(16);} -static void release(void *p){if(p){assert(!pending);live--;free(p);}} +static int copy_rows_override; +typedef struct{void *data;size_t bytes;GXRModeObj storage;} Allocation; +static Allocation buffers[8]; +static Allocation *allocation(void *p){for(unsigned i=0;ifbWidth+15)&~15)*m->xfbHeight*2; + unsigned char *data=malloc(bytes+32);assert(data);memset(data,0,bytes);memset(data+bytes,0xa5,32); + buffers[i]=(Allocation){data,bytes,*m};live++;return data; + } + assert(!"Too many live framebuffers");return NULL; +} +static void guard(Allocation *a){for(int i=0;i<32;i++)assert(((unsigned char *)a->data)[a->bytes+i]==0xa5);} +static void release(void *p){if(p){assert(!pending);Allocation *a=allocation(p);guard(a);a->data=NULL;live--;free(p);}} +static void copy_rows(void *p,int rows){Allocation *a=allocation(p);size_t bytes=(size_t)((a->storage.fbWidth+15)&~15)*rows*2;assert(bytes<=a->bytes);memset(p,0x3c,bytes);guard(a);} static void dnd_graphics_init(void){}static bool dnd_exception_pending(void){return false;} static int VIDEO_HaveComponentCable(void){return dtv;} static void GX_DrawDone(void){gpu_waits++;}static void VIDEO_SetBlack(int b){assert(b==TRUE);} static void VIDEO_Flush(void){}static void VIDEO_WaitVSync(void){waits++;pending=0;} static void VIDEO_Configure(GXRModeObj *m){assert(m==mode);configured++;} static void VIDEO_SetNextFramebuffer(void *p){assert(p==xfb[0]);pending=1;} -static float GX_GetYScaleFactor(int a,int b){return (float)b/a;}static void GX_SetDispCopyYScale(float s){assert(s>0);} +static float GX_GetYScaleFactor(int a,int b){return (float)b/a;} +static int GX_GetNumXfbLines(int efbHeight,float scale){ + if(copy_rows_override)return copy_rows_override; + /* The pinned PPC libogc contract produces 483 rows from its 242-line AA + * preset. Keep this independent of the production storage calculation. */ + return efbHeight==242?483:(int)(efbHeight*scale+.5f); +} +static void GX_SetDispCopyYScale(float s){assert(s>0);} static void GX_SetDispCopySrc(int x,int y,int w,int h){assert(!x&&!y&&w==mode->fbWidth&&h==mode->efbHeight);} static void GX_SetDispCopyDst(int w,int h){assert(w==mode->fbWidth&&h==mode->xfbHeight);} static void GX_SetCopyFilter(int a,unsigned char p[12][2],int b,unsigned char f[7]){(void)a;(void)p;(void)f;assert(b==GX_TRUE);} @@ -41,6 +62,23 @@ static void dnd_draw_backend_invalidate_cache(void){}static void dnd_performance #undef free int main(void){ GXRModeObj next; + /* Both startup and mode switching use this helper. Cover older libogc's + * 483-line result and a newer reciprocal quantizer's 479-line result. */ + GXRModeObj request=TVNtsc480ProgAa;request.aa=1;request.sample_pattern[0][0]=7; + for(int rows=479;rows<=483;rows+=4){ + copy_rows_override=rows;void *p=allocate_display_framebuffer(&request);Allocation *a=allocation(p); + assert(request.xfbHeight==480&&request.efbHeight==242&&request.aa==1); + assert(a->storage.xfbHeight==(rows>480?rows:480)&&a->storage.efbHeight==242&&a->storage.aa==1&&a->storage.sample_pattern[0][0]==7); + assert(a->bytes==(size_t)640*(rows>480?rows:480)*2);copy_rows(p,rows);release(p); + } + request.fbWidth=641;copy_rows_override=511; + void *p=allocate_display_framebuffer(&request);assert(allocation(p)->bytes==(size_t)656*511*2&&request.xfbHeight==480);copy_rows(p,511);release(p); + copy_rows_override=0; + GXRModeObj *standard[]={&TVNtsc240Ds,&TVNtsc480IntDf,&TVNtsc480Prog,&TVPal576IntDfScale,&TVPal576ProgScale}; + for(unsigned i=0;istorage.xfbHeight==standard[i]->xfbHeight);copy_rows(p,standard[i]->xfbHeight);release(p); + } + assert(!live); for(int r=-1;r<=4;r++)for(int s=-1;s<=3;s++){ int result=select_display_mode(r,s,&next); bool invalid=r<0||r>3||s<0||s>2||(r==1&&s==1)||((r==0||r>=2)&&s==2); @@ -50,7 +88,7 @@ int main(void){ assert(select_display_mode(3,2,&next)==0&&next.xfbHeight==576&&next.efbHeight==480); mode=&TVPal576IntDfScale;assert(select_display_mode(2,1,&next)==0&&next.viTVMode==20); assert(select_display_mode(1,2,&next)==0&&next.fbWidth==320&&next.xfbHeight==240&&next.viTVMode==21); - mode=&TVNtsc480IntDf;xfb[0]=allocate(mode);xfb[1]=allocate(mode);assert(live==2); + mode=&TVNtsc480IntDf;xfb[0]=allocate_display_framebuffer(mode);xfb[1]=allocate_display_framebuffer(mode);assert(live==2); allocations=0;assert(dnd_graphics_configure_display(0,0)==0&&allocations==0&&waits==0&&gpu_waits==0); for(int failure=1;failure<=2;failure++){ allocations=0;fail_at=failure;void *a=xfb[0],*b=xfb[1];GXRModeObj *old=mode; @@ -63,14 +101,25 @@ int main(void){ assert(dnd_graphics_configure_display(1,1)<0&&allocations==before); assert(dnd_graphics_configure_display(1,2)==0&&mode->fbWidth==320&&mode->xfbHeight==240); TVNtsc480ProgAa.aa=TVNtsc480IntAa.aa=TVEurgb60Hz480ProgAa.aa=TVEurgb60Hz480IntAa.aa=1;TVEurgb60Hz480ProgAa.sample_pattern[0][0]=3; - allocations=0;fail_at=2;GXRModeObj *before_aa=mode; - assert(dnd_graphics_configure_quality(2,2,1)==-2&&mode==before_aa&&!mode->aa&&live==2);fail_at=0; + for(int failure=1;failure<=2;failure++){ + allocations=0;fail_at=failure;GXRModeObj *before_aa=mode;void *a=xfb[0],*b=xfb[1];int old_waits=waits,old_gpu_waits=gpu_waits,old_configured=configured; + assert(dnd_graphics_configure_quality(2,2,1)==-2&&mode==before_aa&&!mode->aa&&live==2&&xfb[0]==a&&xfb[1]==b); + assert(waits==old_waits&&gpu_waits==old_gpu_waits&&configured==old_configured); + } + fail_at=0; assert(dnd_graphics_configure_quality(2,2,1)==0&&mode->aa&&mode->efbHeight==242&&mode->sample_pattern[0][0]==3); - assert(dnd_graphics_display_stat(8)==1&&dnd_graphics_display_stat(5)==242); + assert(dnd_graphics_display_stat(8)==1&&dnd_graphics_display_stat(5)==242&&dnd_graphics_display_stat(3)==480); + for(int i=0;i<2;i++){assert(allocation(xfb[i])->storage.xfbHeight==483&&allocation(xfb[i])->bytes==(size_t)640*483*2);copy_rows(xfb[i],483);} + /* Do not shrink buffers below the VI extent on libogc versions whose + * quantized copy returns fewer rows than requested. */ + assert(dnd_graphics_configure_quality(0,0,0)==0);copy_rows_override=479; + assert(dnd_graphics_configure_quality(2,2,1)==0&&mode->xfbHeight==480&&mode->efbHeight==242); + for(int i=0;i<2;i++){assert(allocation(xfb[i])->storage.xfbHeight==480);copy_rows(xfb[i],479);} + copy_rows_override=0; assert(dnd_graphics_configure_quality(3,2,1)<0&&mode->aa); assert(dnd_graphics_configure_quality(0,0,0)==0&&!mode->aa&&mode->efbHeight==480); assert(dnd_graphics_configure_quality(1,2,1)==0&&mode->aa&&mode->fbWidth==320&&mode->efbHeight==240); assert(dnd_graphics_configure_quality(0,0,0)==0&&!mode->aa&&mode->fbWidth==320); release(xfb[0]);release(xfb[1]);assert(live==0); - puts("Display mode combinations, DTV restrictions, standard selection, transactional allocation, scanout lifetime and widescreen-only no-op passed."); + puts("Display copy bounds (483/479 rows), VI dimensions, padded stride, mode combinations, transactional allocation, scanout lifetime and widescreen-only no-op passed."); } diff --git a/tests/framebuffer_canary_gamecube.c b/tests/framebuffer_canary_gamecube.c new file mode 100644 index 000000000..2b811724f --- /dev/null +++ b/tests/framebuffer_canary_gamecube.c @@ -0,0 +1,77 @@ +/* Optional native GX-copy qualification. Run with Dolphin XFB-to-RAM copies; + * the red zone safely exposes a too-small legacy allocation without corrupting + * the heap. The production helper itself is included without modification. */ +#include +#include +#include +#include +#include +#include "dnd_framebuffer_gx.inc" + +#define CASES 12 +#define GUARD_BYTES (16 * 640 * 2) +#define SENTINEL 0xcd + +typedef struct { + u32 width,efb_rows,vi_rows,predicted_rows,storage_rows; + u32 claimed_bytes,last_written_byte,red_zone_changed,legacy; +} CanaryRow; +volatile CanaryRow dnd_framebuffer_canary_rows[CASES]; +volatile u32 dnd_framebuffer_canary_stage; +volatile u32 dnd_framebuffer_canary_complete; +static u32 allocation_bytes,allocation_rows; + +/* --wrap records the helper's requested size and adds a separate red zone. + * Width padding and row sizes remain the real pinned libogc calculation. */ +void *__wrap_SYS_AllocateFramebuffer(const GXRModeObj *mode) { + allocation_bytes=VIDEO_GetFrameBufferSize(mode); + allocation_rows=mode->xfbHeight; + u8 *memory=memalign(32,allocation_bytes+GUARD_BYTES); + if(memory){memset(memory,SENTINEL,allocation_bytes+GUARD_BYTES);DCFlushRange(memory,allocation_bytes+GUARD_BYTES);} + return memory; +} + +__attribute__((noinline)) void dnd_framebuffer_canary_done(void) { + dnd_framebuffer_canary_complete=0x41414346; + for(;;)__asm__ volatile("nop"); +} + +static void measure(u32 index,GXRModeObj mode,int legacy,void *warmup) { + u32 predicted=GX_GetNumXfbLines(mode.efbHeight,GX_GetYScaleFactor(mode.efbHeight,mode.xfbHeight)); + void *target=legacy?__wrap_SYS_AllocateFramebuffer(&mode):allocate_display_framebuffer(&mode); + u32 claimed=allocation_bytes,storage=allocation_rows; + if(!target){dnd_framebuffer_canary_stage=0xe0000000|index;dnd_framebuffer_canary_done();} + GX_SetPixelFmt(mode.aa?GX_PF_RGB565_Z16:GX_PF_RGB8_Z24,GX_ZC_LINEAR); + GX_SetCopyFilter(mode.aa,mode.sample_pattern,GX_TRUE,mode.vfilter); + GX_SetDispCopySrc(0,0,mode.fbWidth,mode.efbHeight); + GX_SetDispCopyYScale(GX_GetYScaleFactor(mode.efbHeight,mode.xfbHeight)); + GX_SetDispCopyDst(mode.fbWidth,mode.xfbHeight); + GX_SetFieldMode(mode.field_rendering,mode.viHeight==2*mode.xfbHeight?GX_ENABLE:GX_DISABLE); + GX_SetCopyClear((GXColor){0,0,0,255},0x00ffffff); + GX_SetZMode(GX_TRUE,GX_LEQUAL,GX_TRUE);GX_SetColorUpdate(GX_TRUE);GX_SetAlphaUpdate(GX_TRUE); + /* Clear the EFB before measuring, making every written black YUYV byte + * different from SENTINEL. Both buffers have space for the full write. */ + GX_CopyDisp(MEM_K0_TO_K1(warmup),GX_TRUE);GX_DrawDone(); + GX_CopyDisp(MEM_K0_TO_K1(target),GX_TRUE);GX_DrawDone(); + volatile u8 *read=(volatile u8*)MEM_K0_TO_K1(target); + u32 last=0,changed=0; + for(u32 at=0;at=claimed)changed++;} + dnd_framebuffer_canary_rows[index]=(CanaryRow){mode.fbWidth,mode.efbHeight,mode.xfbHeight,predicted,storage,claimed,last,changed,legacy}; + free(target); +} + +int main(void) { + VIDEO_Init();void *fifo=memalign(32,256*1024);memset(fifo,0,256*1024);GX_Init(fifo,256*1024); + GXRModeObj *modes[]={&TVNtsc240Ds,&TVNtsc480IntDf,&TVNtsc480Prog,&TVNtsc480IntAa,&TVNtsc480IntAa,&TVNtsc480ProgAa,&TVNtsc480ProgAa,&TVPal576IntDfScale,&TVPal576ProgScale,&TVNtsc480ProgAa,&TVNtsc240Ds,&TVNtsc480IntDf}; + for(u32 i=0;i