Triforce v0.2.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ruckus7382sfgsgdf
2026-11-10 19:32:48 -06:00
commit 34b3683441
1730 changed files with 554853 additions and 0 deletions

View File

@@ -0,0 +1,214 @@
/* Synthetic device calls around the real nn_audio.c output section. No device or game is opened. */
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void *HWAVEOUT;
typedef char *LPSTR;
typedef unsigned MMRESULT;
typedef struct {
unsigned wFormatTag, nChannels, nSamplesPerSec, wBitsPerSample, nBlockAlign, nAvgBytesPerSec;
} WAVEFORMATEX;
typedef struct { LPSTR lpData; unsigned dwBufferLength, dwFlags; } WAVEHDR;
#define MMSYSERR_NOERROR 0
#define WHDR_DONE 1
#define WHDR_PREPARED 2
#define WHDR_INQUEUE 16
#define WAVE_MAPPER ((unsigned)-1)
#define WAVE_FORMAT_PCM 1
#define CALLBACK_NULL 0
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL %s:%d: %s\n", scenario, __LINE__, #x); exit(1); } } while (0)
static const char *scenario;
static int prepare_calls, write_calls, accepted, unprepare_calls, open_calls, close_calls, reset_calls, logs;
static unsigned last_device, last_channels;
static uint64_t now_ms;
static int fail_prepare, fail_write, fail_unprepare, fail_open, fail_open6;
static int queued[10];
static WAVEHDR *known[10];
static char last_log[512];
static const char *test_getenv(const char *name) { (void)name; return NULL; }
static void hos_log(const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(last_log, sizeof last_log, format, args);
va_end(args);
logs++;
}
static int slot(WAVEHDR *h) {
for (int i = 0; i < 10; i++) if (known[i] == h) return i;
for (int i = 0; i < 10; i++) if (!known[i]) { known[i] = h; return i; }
CHECK(0);
return -1;
}
static MMRESULT test_open(HWAVEOUT *out, unsigned device, const WAVEFORMATEX *f,
uintptr_t callback, uintptr_t instance, unsigned flags) {
(void)callback; (void)instance; (void)flags;
last_device = device;
CHECK((f->nChannels == 2 || f->nChannels == 6) && f->nSamplesPerSec == 48000 && f->wBitsPerSample == 16);
CHECK(f->nBlockAlign == 2 * f->nChannels && f->nAvgBytesPerSec == 48000 * f->nBlockAlign);
last_channels = f->nChannels;
open_calls++;
if (fail_open || (fail_open6 && f->nChannels == 6)) return 7; /* (MMSYSERR_NOMEM; a device without 5.1: 32) */
*out = (void *)(uintptr_t)1;
return 0;
}
static MMRESULT test_prepare(HWAVEOUT out, WAVEHDR *h, unsigned size) {
(void)out; (void)size;
CHECK(!queued[slot(h)]);
prepare_calls++;
if (fail_prepare && (fail_prepare < 0 || prepare_calls == 1)) return 7;
h->dwFlags |= WHDR_PREPARED;
return 0;
}
static MMRESULT test_write(HWAVEOUT out, WAVEHDR *h, unsigned size) {
(void)out; (void)size;
const int i = slot(h);
CHECK(!queued[i]);
write_calls++;
if (!(h->dwFlags & WHDR_PREPARED)) return 34; /* WAVERR_UNPREPARED: no completion follows. */
if (fail_write && write_calls == 1) return 7; /* MMSYSERR_NOMEM: no buffer was queued. */
queued[i] = 1;
h->dwFlags = (h->dwFlags & ~WHDR_DONE) | WHDR_INQUEUE;
accepted++;
return 0;
}
static MMRESULT test_unprepare(HWAVEOUT out, WAVEHDR *h, unsigned size) {
(void)out; (void)size;
CHECK(!queued[slot(h)]);
unprepare_calls++;
if (fail_unprepare && unprepare_calls == 1) return 33; /* WAVERR_STILLPLAYING: do not reuse. */
h->dwFlags &= ~WHDR_PREPARED;
return 0;
}
static MMRESULT test_reset(HWAVEOUT out) {
(void)out;
reset_calls++;
for (int i = 0; i < 10; i++) if (queued[i]) { /* (waveOutReset: every queued block marked done) */
queued[i] = 0;
known[i]->dwFlags = (known[i]->dwFlags & ~WHDR_INQUEUE) | WHDR_DONE;
}
return 0;
}
static MMRESULT test_close(HWAVEOUT out) { (void)out; close_calls++; return 0; }
/* (the platform helpers nn_audio.c defines before its output section) */
static unsigned au_device_count(void) { return 2; }
static void au_device_name(unsigned device, char *out, size_t size) { snprintf(out, size, "test device %u", device); }
static uint64_t au_ms(void) { return now_ms; }
static void au_opened(HWAVEOUT w) { (void)w; }
static int speakers = 2; /* (the game's speakers: 6 in the "surround" and "fallback" cases) */
static int au_speakers(void) { return speakers; }
static const WAVEFORMATEX *au_format(const WAVEFORMATEX *f) { return f; }
#define getenv test_getenv
#define waveOutOpen test_open
#define waveOutPrepareHeader test_prepare
#define waveOutWrite test_write
#define waveOutUnprepareHeader test_unprepare
#define waveOutReset test_reset
#define waveOutClose test_close
#include "audio_output.inc"
static void complete_all(void) {
for (int i = 0; i < 10; i++) if (queued[i]) {
queued[i] = 0;
known[i]->dwFlags = (known[i]->dwFlags & ~WHDR_INQUEUE) | WHDR_DONE;
}
}
static void frames(int count, int complete) {
int16_t pcm[FRAME * 6];
for (int j = 0; j < count; j++) {
for (int i = 0; i < FRAME * out_channels; i++) pcm[i] = (int16_t)(j * 31 + i);
output_frame(pcm);
if (complete) complete_all();
}
}
int main(int argc, char **argv) {
CHECK(argc == 2);
scenario = argv[1];
fail_prepare = !strcmp(scenario, "prepare");
fail_write = !strcmp(scenario, "write");
fail_unprepare = !strcmp(scenario, "unprepare");
fail_open = !strcmp(scenario, "open");
if (!strcmp(scenario, "bounded")) fail_prepare = -1;
if (!strcmp(scenario, "surround") || !strcmp(scenario, "fallback")) speakers = 6;
fail_open6 = !strcmp(scenario, "fallback");
output_open();
if (fail_open6) {
CHECK(open_calls == 4 && last_channels == 2 && last_device == WAVE_MAPPER); /* (5.1 on all three, then stereo) */
CHECK(out_channels == 2 && hdr[0].dwBufferLength == FRAME * 2 * 2 * 2);
CHECK(strstr(last_log, "default device") && strstr(last_log, "stereo"));
logs = 0;
frames(40, 1);
CHECK(accepted == 20 && !logs);
} else if (fail_open) {
CHECK(open_calls == 3); /* (the default, then both devices; HOS_AUDIO_DEVICE unset) */
CHECK(!wout);
frames(40, 1);
CHECK(!prepare_calls && !write_calls && logs == 3);
CHECK(strstr(last_log, "waveOutOpen") && strstr(last_log, "7"));
} else if (open_calls != 1 || last_device != WAVE_MAPPER || logs != 1 || !strstr(last_log, "default device")) {
CHECK(0); /* (a working default is opened first, and named in the log) */
} else if ((logs = 0), !strcmp(scenario, "surround")) {
CHECK(last_channels == 6 && out_channels == 6 && strstr(last_log, "5.1"));
CHECK(hdr[0].dwBufferLength == FRAME * 2 * 6 * 2); /* (two frames of six 16-bit channels) */
frames(40, 1);
CHECK(accepted == 20 && prepare_calls == 20 && write_calls == 20 && !logs);
CHECK(blocks[9][0] == 38 * 31 && blocks[9][FRAME * 6] == 39 * 31);
CHECK(blocks[9][FRAME * 6 - 1] == (int16_t)(38 * 31 + FRAME * 6 - 1));
} else if (!strcmp(scenario, "stall")) {
frames(20, 0); /* every block queued, none ever completes: the device takes nothing */
now_ms += 1000;
frames(1, 0); /* the stall noticed */
CHECK(open_calls == 1);
now_ms += 1600;
frames(1, 0); /* 1.6 s later: closed, the next candidate (device 0) opened */
CHECK(reset_calls == 1 && close_calls == 1 && open_calls == 2 && last_device == 0);
CHECK(strstr(last_log, "test device 0"));
const int before = accepted;
frames(4, 1);
CHECK(accepted == before + 2); /* the new device takes blocks */
} else if (fail_prepare > 0 || fail_write) {
frames(40, 1);
fprintf(stderr, "ring %s: accepted %d, prepare %d, write %d\n", scenario, accepted, prepare_calls, write_calls);
CHECK(accepted == 19); /* One failed block, then the same slot works on the next ring. */
CHECK(fill_block == 0 && fill_frames == 0);
CHECK(logs == 1);
if (fail_prepare) CHECK(write_calls == 19); /* Never write an unprepared header. */
} else if (fail_unprepare) {
frames(20, 1);
int16_t before[FRAME * 4];
memcpy(before, blocks[0], sizeof before);
frames(1, 0);
CHECK(fill_frames == 0 && fill_block == 0);
CHECK(!memcmp(before, blocks[0], sizeof before));
CHECK(hdr[0].dwFlags == (WHDR_DONE | WHDR_PREPARED));
frames(2, 1);
CHECK(accepted == 11 && unprepare_calls == 2 && logs == 1);
} else if (!strcmp(scenario, "busy")) {
frames(20, 0);
int16_t before[FRAME * 4];
memcpy(before, blocks[0], sizeof before);
frames(5, 0);
CHECK(accepted == 10 && fill_frames == 0 && fill_block == 0);
CHECK(!memcmp(before, blocks[0], sizeof before));
CHECK(!unprepare_calls);
complete_all();
frames(2, 1);
CHECK(accepted == 11);
} else if (fail_prepare < 0) {
frames(80, 1);
CHECK(prepare_calls == 40 && write_calls == 0 && accepted == 0);
CHECK(logs > 0 && logs <= 8);
} else {
CHECK(!strcmp(scenario, "normal"));
frames(40, 1);
CHECK(accepted == 20 && prepare_calls == 20 && write_calls == 20 && unprepare_calls == 10);
CHECK(fill_block == 0 && fill_frames == 0 && !logs);
CHECK(blocks[9][0] == 38 * 31 && blocks[9][FRAME * 2] == 39 * 31);
}
printf("PASS %s: actual output ring\n", scenario);
return 0;
}

View File

@@ -0,0 +1,182 @@
/* Authored packets and samples around actual source bodies. No audio device or game is opened. */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { uint64_t x[32]; } Ctx;
static _Alignas(8) uint8_t guest[4096];
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
static void *guest_ptr(uint64_t address) { CHECK(address < sizeof guest); return guest + address; }
#define GP(a) guest_ptr(a)
#define G64(a) (*(uint64_t *)GP(a))
#define ARG(n) (c->x[n])
#define HLE(sym) void imp_##sym(Ctx *c)
#define A64_WEAK
#define AUT(...) ((void)0)
/* Single-threaded test boundaries. Scheduling, allocation failure and output devices are outside this fixture. */
static int lock_depth;
static void au_lock(void) { CHECK(lock_depth++ == 0); }
static void au_unlock(void) { CHECK(--lock_depth == 0); }
static const char *fixture_getenv(const char *name) { (void)name; return NULL; }
#define getenv fixture_getenv
/* The extracted production types precede these globals and the actual queue functions. */
#define FLOAT_SETTER imp__ZN2nn5audio29SetVoiceBiquadFilterParameterEPNS0_9VoiceTypeEiRKNS0_22TBiquadFilterParameterIfEE
#define LEGACY_SETTER imp__ZN2nn5audio29SetVoiceBiquadFilterParameterEPNS0_9VoiceTypeEiRKNS0_21BiquadFilterParameterE
/* The generated section keeps real Voice/Mix/Splitter types ahead of its fixture globals and production functions. */
#include "actual_audio_biquad.inc"
enum { OBJECT0 = 0x40, OBJECT1 = 0x50, PACKET = 0x200 };
static void close_to(float actual, float expected) { CHECK(fabsf(actual - expected) < 0.000001f); }
static void clear_samples(float out[MAX_CH][FRAME]) { memset(out, 0, sizeof(float) * MAX_CH * FRAME); }
static void reset(void) {
memset(guest, 0, sizeof guest);
memset(voices, 0, sizeof voices);
ncmds = 0;
for (int i = 0; i < 2; i++) voices[i].used = 1, voices[i].channels = 2;
put_handle(OBJECT0, K_VOICE, 0);
put_handle(OBJECT1, K_VOICE, 1);
}
static void float_packet(int enabled, float b0, float b1, float b2, float a1, float a2) {
/* Authored 24-byte packet: bool, three padding bytes, three f32 numerators, two additive f32 feedback values. */
uint8_t packet[24];
memset(packet, 0xa5, sizeof packet);
packet[0] = (uint8_t)enabled;
const float values[] = {b0, b1, b2, a1, a2};
memcpy(packet + 4, values, sizeof values);
memcpy(GP(PACKET), packet, sizeof packet);
}
static void set_float(uint64_t object, int filter, uint64_t packet) {
Ctx c = {0};
c.x[0] = object; c.x[1] = (uint64_t)(int64_t)filter; c.x[2] = packet;
FLOAT_SETTER(&c);
CHECK(lock_depth == 0);
}
static void configure(float b0, float b1, float b2, float a1, float a2) {
float_packet(1, b0, b1, b2, a1, a2);
set_float(OBJECT0, 0, PACKET);
CHECK(ncmds == 1); /* The old import fallback must fail here, after compiling and returning normally. */
commit();
}
static void identity_and_packet_lifetime(void) {
reset();
float_packet(1, 1, 0, 0, 0, 0);
set_float(OBJECT0, 0, PACKET);
CHECK(ncmds == 1);
CHECK(!voices[0].bq_on[0]);
memset(GP(PACKET), 0xa5, 24); /* The caller may immediately reuse its packet before RequestUpdate commits. */
commit();
CHECK(ncmds == 0 && voices[0].bq_on[0]);
close_to(voices[0].bq_b[0][0], 1);
float out[MAX_CH][FRAME] = {{0}};
for (int i = 0; i < FRAME; i++) out[0][i] = (float)(i % 5) * 0.125f, out[1][i] = -out[0][i];
filter_samples(&voices[0], out);
for (int i = 0; i < FRAME; i++) {
close_to(out[0][i], (float)(i % 5) * 0.125f);
close_to(out[1][i], -(float)(i % 5) * 0.125f);
}
}
static void attenuation_and_additive_feedback(void) {
reset();
configure(0.25f, 0, 0, 0, 0);
float out[MAX_CH][FRAME] = {{0}};
out[0][0] = 1;
filter_samples(&voices[0], out);
close_to(out[0][0], 0.25f);
close_to(out[0][1], 0);
reset();
configure(0.75f, 0.25f, 0.125f, 0.5f, -0.25f);
clear_samples(out);
out[0][0] = 1;
filter_samples(&voices[0], out);
/* Fixed analytic impulse values, not another implementation of the production DSP loop. */
const float expected[] = {0.75f, 0.625f, 0.25f, -0.03125f, -0.078125f};
for (unsigned i = 0; i < sizeof expected / sizeof *expected; i++) close_to(out[0][i], expected[i]);
}
static void state_and_reenable(void) {
reset();
configure(1, 0, 0, 0.5f, 0);
float out[MAX_CH][FRAME] = {{0}};
out[0][FRAME - 1] = 1;
filter_samples(&voices[0], out);
close_to(voices[0].bq_z[0][0][0], 0.5f);
close_to(voices[0].bq_z[0][1][0], 0);
clear_samples(out);
filter_samples(&voices[0], out);
close_to(out[0][0], 0.5f);
close_to(out[0][1], 0.25f);
close_to(out[1][0], 0);
clear_samples(out);
out[0][FRAME - 1] = 1; out[1][FRAME - 1] = 2;
filter_samples(&voices[0], out);
close_to(voices[0].bq_z[0][0][0], 0.5f);
close_to(voices[0].bq_z[0][1][0], 1);
float_packet(0, 1, 0, 0, 0.5f, 0);
set_float(OBJECT0, 0, PACKET); commit();
out[0][0] = 0.75f;
filter_samples(&voices[0], out);
close_to(out[0][0], 0.75f);
float_packet(1, 1, 0, 0, 0.5f, 0);
set_float(OBJECT0, 0, PACKET); commit();
CHECK(voices[0].bq_on[0]);
for (int ch = 0; ch < MAX_CH; ch++)
CHECK(voices[0].bq_z[0][ch][0] == 0 && voices[0].bq_z[0][ch][1] == 0);
}
static void invalid_inputs(void) {
reset();
float_packet(1, 1, 0, 0, 0, 0);
set_float(OBJECT0, -1, PACKET); set_float(OBJECT0, 2, PACKET);
set_float(0, 0, PACKET); set_float(OBJECT0, 0, 0);
voices[0].released = 1; set_float(OBJECT0, 0, PACKET); voices[0].released = 0;
voices[0].used = 0; set_float(OBJECT0, 0, PACKET); voices[0].used = 1;
put_handle(OBJECT0, K_MIX, 0); set_float(OBJECT0, 0, PACKET);
CHECK(ncmds == 0 && !voices[0].bq_on[0] && !voices[0].bq_on[1]);
}
static void voice_filter_channel_isolation(void) {
reset();
float_packet(1, 0.5f, 0, 0, 0, 0); set_float(OBJECT0, 0, PACKET);
float_packet(1, 0.25f, 0, 0, 0, 0); set_float(OBJECT1, 1, PACKET);
CHECK(ncmds == 2); commit();
CHECK(voices[0].bq_on[0] && !voices[0].bq_on[1]);
CHECK(!voices[1].bq_on[0] && voices[1].bq_on[1]);
float out[MAX_CH][FRAME] = {{0}};
out[0][0] = 1; out[1][0] = 2;
filter_samples(&voices[0], out);
close_to(out[0][0], 0.5f); close_to(out[1][0], 1);
clear_samples(out); out[0][0] = 1; out[1][0] = 3;
filter_samples(&voices[1], out);
close_to(out[0][0], 0.25f); close_to(out[1][0], 0.75f);
float_packet(1, 0.25f, 0, 0, 0, 0); set_float(OBJECT0, 1, PACKET); commit();
clear_samples(out); out[0][0] = 1;
filter_samples(&voices[0], out); close_to(out[0][0], 0.125f);
}
static void legacy_q14(void) {
reset();
uint8_t packet[12];
memset(packet, 0xa5, sizeof packet); packet[0] = 1;
const int16_t coefs[] = {8192, 0, 0, 8192, 0};
memcpy(packet + 2, coefs, sizeof coefs); memcpy(GP(PACKET), packet, sizeof packet);
Ctx c = {0}; c.x[0] = OBJECT0; c.x[1] = 0; c.x[2] = PACKET;
LEGACY_SETTER(&c);
CHECK(ncmds == 1 && !voices[0].bq_on[0]);
memset(GP(PACKET), 0xa5, sizeof packet); commit();
close_to(voices[0].bq_b[0][0], 0.5f); close_to(voices[0].bq_a[0][0], 0.5f);
float out[MAX_CH][FRAME] = {{0}}; out[0][0] = 1;
filter_samples(&voices[0], out);
close_to(out[0][0], 0.5f); close_to(out[0][1], 0.25f); close_to(out[0][2], 0.125f);
}
int main(void) {
identity_and_packet_lifetime();
attenuation_and_additive_feedback();
state_and_reenable();
invalid_inputs();
voice_filter_channel_isolation();
legacy_q14();
free(cmds);
puts("PASS: actual float/Q14 voice setters, queued commit, biquad response/history/reset and isolation");
return 0;
}

View File

@@ -0,0 +1,113 @@
/* Authored wave buffers and passes around actual source bodies. No audio device or game is opened. */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { uint64_t x[32]; } Ctx;
static _Alignas(8) uint8_t guest[4096];
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
static void *guest_ptr(uint64_t address) { CHECK(address < sizeof guest); return guest + address; }
#define GP(a) guest_ptr(a)
#define G64(a) (*(uint64_t *)GP(a))
#define G32(a) (*(uint32_t *)GP(a))
#define ARG(n) (c->x[n])
#define RET(v) (c->x[0] = (uint64_t)(v))
#define HLE(sym) void imp_##sym(Ctx *c)
#define AUT(...) ((void)0)
static void hos_log(const char *format, ...) { (void)format; }
/* Single-threaded test boundaries: the mixer's frames run between the game's calls, as its thread can. */
static int lock_depth;
static void au_lock(void) { CHECK(lock_depth++ == 0); }
static void au_unlock(void) { CHECK(--lock_depth == 0); }
static const char *fixture_getenv(const char *name) { (void)name; return NULL; }
#define getenv fixture_getenv
#include "actual_audio_wave_buffers.inc"
#define APPEND imp__ZN2nn5audio16AppendWaveBufferEPNS0_9VoiceTypeEPKNS0_10WaveBufferE
#define RELEASED imp__ZN2nn5audio21GetReleasedWaveBufferEPNS0_9VoiceTypeE
#define COUNT imp__ZN2nn5audio18GetWaveBufferCountEPKNS0_9VoiceTypeE
#define STATE imp__ZN2nn5audio17SetVoicePlayStateEPNS0_9VoiceTypeENS1_9PlayStateE
enum { VOICES = 6, OBJECT = 0x40, BUFFER = 0x200, SAMPLES = 0x400, LENGTH = 64 };
static uint64_t object(int i) { return OBJECT + (uint64_t)i * 16; }
static uint64_t call(void (*hle)(Ctx *), uint64_t a0, uint64_t a1) {
Ctx c = {0};
c.x[0] = a0; c.x[1] = a1;
hle(&c);
CHECK(lock_depth == 0);
return c.x[0];
}
static int count(int i) { return (int)call(COUNT, object(i), 0); }
static uint64_t released(int i) { return call(RELEASED, object(i), 0); }
static void reset(void) {
memset(guest, 0, sizeof guest);
memset(voices, 0, sizeof voices);
ncmds = 0;
/* One authored WaveBuffer struct for every append, as the game does: PCM16, LENGTH samples. */
G64(BUFFER) = SAMPLES; G64(BUFFER + 8) = LENGTH * 2;
G32(BUFFER + 0x10) = 0; G32(BUFFER + 0x14) = LENGTH;
for (int i = 0; i < VOICES; i++) {
voices[i].used = 1, voices[i].channels = 1, voices[i].format = FMT_PCM16, voices[i].rate = RATE;
put_handle(object(i), K_VOICE, i);
}
}
/* the mixer plays voice i through its first queued buffer and into the next: that one is finished */
static void finish_one(int i) {
for (int s = 0; s <= LENGTH; s++) CHECK(next_source_frame(&voices[i]));
}
static void finished_between_updates(void) {
reset();
for (int n = 0; n < 4; n++)
for (int i = 0; i < VOICES; i++) CHECK(call(APPEND, object(i), BUFFER) == 1);
for (int i = 0; i < VOICES; i++) CHECK(count(i) == 4); /* (appended counts at once, before the update) */
commit();
for (int i = 0; i < VOICES; i++) CHECK(count(i) == 4 && !released(i));
/* The game's pass reads voices 0 and 1; the mixer finishes a buffer of voice 2; the pass goes on. */
CHECK(count(0) == 4 && count(1) == 4);
finish_one(2);
CHECK(count(2) == 4); /* the old way: 3, the game appended voice 2 an extra buffer and drifted from there */
CHECK(!released(2));
for (int i = 3; i < VOICES; i++) CHECK(count(i) == 4);
/* The update: the finished buffer is the game's now, the count one less. */
commit();
CHECK(count(2) == 3);
CHECK(released(2) == BUFFER);
CHECK(!released(2) && count(2) == 3);
for (int i = 0; i < VOICES; i++) if (i != 2) CHECK(count(i) == 4 && !released(i));
/* Every voice finishing one in the same frame: all reach the game together, at the next update. */
for (int i = 0; i < VOICES; i++) if (i != 2) finish_one(i);
for (int i = 0; i < VOICES; i++) if (i != 2) CHECK(count(i) == 4 && !released(i));
commit();
for (int i = 0; i < VOICES; i++) {
if (i != 2) CHECK(released(i) == BUFFER);
CHECK(count(i) == 3 && !released(i));
}
}
static void stopped_releases_at_the_update(void) {
reset();
for (int n = 0; n < 3; n++) CHECK(call(APPEND, object(0), BUFFER) == 1);
commit();
call(STATE, object(0), 1); /* Stopped: every queued buffer released, when the update applies it */
CHECK(count(0) == 3 && !released(0));
commit();
CHECK(count(0) == 0);
for (int n = 0; n < 3; n++) CHECK(released(0) == BUFFER);
CHECK(!released(0));
}
int main(void) {
finished_between_updates();
stopped_releases_at_the_update();
free(cmds);
puts("PASS: actual wave-buffer calls: buffers the mixer finishes reach the game at the update, never mid-pass");
return 0;
}

View File

@@ -0,0 +1,87 @@
using HookshotBuilder;
sealed class Tools;
static class Storage { public static string NgxSdk = ""; }
sealed class Step { public void Done() { } public void Small(string value) { } }
// Only the UI and the build launcher are spies: UpdateBuild, settings and shader arguments are production code.
sealed class Window
{
object? download = new();
readonly Dictionary<string, Step> rows = new() { ["download"] = new Step() };
public static string SettingsPath = "";
public bool prepareNow = true;
public bool? Dlss;
public List<string>? Args;
void Run(Tools tools, List<string> args, bool keepRows, bool dlss)
{
if (!keepRows || download != null) throw new Exception("Update setup changed.");
Dlss = dlss;
Args = args;
}
/* ACTUAL_UPDATE */
}
static class Program
{
static int checks;
static void Check(bool condition, string message)
{
if (!condition) throw new Exception(message);
checks++;
}
static void Update(string game, bool expected, bool shaders = true)
{
var w = new Window { prepareNow = shaders };
w.UpdateBuild(new Tools(), game);
Check(w.Dlss == expected, "Wrong DLSS decision for the saved choice and existing installation.");
var args = new List<string> { "--update", game };
if (!shaders) args.Add("--no-prepare-shaders");
Check(w.Args!.SequenceEqual(args), "The DLSS choice changed the update/shader arguments.");
}
static void Main()
{
// Authored placeholders only: no NVIDIA SDK, runtime or game data is used.
var root = Path.Combine(Path.GetTempPath(), "hookshot-dlss-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(root);
try
{
Window.SettingsPath = Path.Combine(root, "builder-settings.json");
Storage.NgxSdk = Path.Combine(root, "sdk without files");
var game = Path.Combine(root, "game with spaces é");
Directory.CreateDirectory(game);
// Simulates the saved, license-accepted checkbox before a self-update creates a new window.
File.WriteAllText(Window.SettingsPath, "{\"Dlss\":true}");
Update(game, true);
Update(game, true, shaders: false);
Check(!Directory.Exists(Storage.NgxSdk), "The decision unexpectedly provisioned an SDK.");
Check(!Directory.Exists(Path.Combine(game, "ngx")), "The decision unexpectedly modified the game.");
File.WriteAllText(Window.SettingsPath, "{\"Dlss\":false}");
Update(game, false);
File.WriteAllText(Window.SettingsPath, "{}");
Update(game, false);
File.WriteAllText(Window.SettingsPath, "not json");
Update(game, false);
File.Delete(Window.SettingsPath);
Update(game, false);
Directory.CreateDirectory(Storage.NgxSdk);
File.WriteAllText(Path.Combine(Storage.NgxSdk, "LICENSE.txt"), "authored license placeholder");
Update(game, false); // A license alone did not build this game's DLSS.
Directory.CreateDirectory(Path.Combine(game, "ngx"));
var runtime = Path.Combine(game, "ngx", "nvngx_dlss.dll");
File.WriteAllText(runtime, "authored runtime placeholder");
Update(game, true); // Preserve the existing update behavior for an opted-in installation.
File.WriteAllText(Window.SettingsPath, "{\"Dlss\":false}");
Update(game, true);
File.Delete(Path.Combine(Storage.NgxSdk, "LICENSE.txt"));
Update(game, false); // A leftover DLL alone does not accept NVIDIA's license.
File.WriteAllText(Window.SettingsPath, "{\"Dlss\":true}");
Update(game, true); // The accepted preference also repairs a partial SDK.
Check(File.ReadAllText(runtime) == "authored runtime placeholder", "Update decision modified the existing runtime.");
Console.WriteLine($"Builder DLSS update checks passed ({checks} assertions).");
}
finally { Directory.Delete(root, recursive: true); }
}
}

2
tests/builder_workers/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
bin/
obj/

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Compile Include="../../builder/HookshotBuilder/BuildWorkers.cs" Link="BuildWorkers.cs"/>
<Compile Include="../../builder/HookshotBuilder/Runner.cs" Link="Runner.cs"/>
<Compile Include="../../builder/HookshotBuilder/BuilderSettings.cs" Link="BuilderSettings.cs"/>
<Compile Include="../../builder/HookshotBuilder/ShaderStep.cs" Link="ShaderStep.cs"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration><packageSources><clear/></packageSources></configuration>

View File

@@ -0,0 +1,186 @@
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using HookshotBuilder;
static class Program
{
const string Key = "HOOKSHOT_JOBS";
static readonly MethodInfo InfoMethod = typeof(Runner).GetMethod("Info", BindingFlags.Static | BindingFlags.NonPublic)!;
static int checks;
static void Require(bool ok, string message)
{
if (!ok) throw new Exception(message);
checks++;
}
static ProcessStartInfo Info(Tools tools, string[] args, int? workers = null) =>
(ProcessStartInfo)InfoMethod.Invoke(null, new object?[] { tools, args, workers, null })!; // (ngxSdk: none)
static async Task<JsonElement> Delivery(ProcessStartInfo info)
{
using var child = Process.Start(info)!;
var output = await child.StandardOutput.ReadToEndAsync();
var error = await child.StandardError.ReadToEndAsync();
await child.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(15));
Require(child.ExitCode == 0 && error.Length == 0, "The authored argument/environment child failed.");
using var json = JsonDocument.Parse(output.Trim());
return json.RootElement.Clone();
}
static void CheckInfo(ProcessStartInfo info, Tools tools, string[] args, string? expected)
{
Require(info.ArgumentList.SequenceEqual(new[] { "-I", Path.Combine(tools.Root, "nx2", "make_game.py") }.Concat(args)),
"The worker choice changed the original source/update arguments.");
Require(info.WorkingDirectory == tools.Root && !info.UseShellExecute && info.CreateNoWindow
&& info.RedirectStandardOutput && info.RedirectStandardError, "Runner process options changed.");
info.Environment.TryGetValue(Key, out var actual);
Require(actual == expected, "Unexpected child worker override.");
}
static async Task EmptyParent(string python, Tools tools)
{
// Environment.SetEnvironmentVariable(Key, "") may remove the variable on Windows instead of keeping it empty.
// Start a real parent with an explicit empty environment entry, then exercise the actual Runner.Info inheritance.
Require(Environment.GetEnvironmentVariable(Key) == "", "The empty-parent fixture did not inherit an empty entry.");
var info = Info(tools, new[] { "--out", "authored output" });
CheckInfo(info, tools, new[] { "--out", "authored output" }, "");
var value = await Delivery(info);
Require(value.GetProperty("jobs").GetString() == "", "Automatic changed an inherited empty entry.");
}
/// The shaders' step: the update's argument from the saved choice (the launcher's Update and the --update start read it, not
/// the tick), and what the row says when make_game left the shaders to the first start
static void ShaderStepChecks(string root)
{
var settings = Path.Combine(root, "builder-settings.json");
Require(BuilderSettings.PrepareNow(settings), "No saved settings must prepare the shaders, as the tick's default.");
File.WriteAllText(settings, "not json");
Require(BuilderSettings.PrepareNow(settings), "Unreadable settings must prepare the shaders.");
File.WriteAllText(settings, """{"Out":"authored output","PrepareLater":true}""");
Require(!BuilderSettings.PrepareNow(settings), "A saved unticked choice must leave the shaders to the first start.");
Require(ShaderStep.Args(new List<string> { "--update", "authored game" }, BuilderSettings.PrepareNow(settings))
.SequenceEqual(new[] { "--update", "authored game", "--no-prepare-shaders" }), "The update ignored the saved choice.");
File.WriteAllText(settings, """{"Out":"authored output","PrepareLater":false}""");
Require(ShaderStep.Args(new List<string> { "--update", "authored game" }, BuilderSettings.PrepareNow(settings))
.SequenceEqual(new[] { "--update", "authored game" }), "A saved ticked choice must prepare the shaders.");
Require(ShaderStep.Reason("the shaders were not prepared now (no usable Vulkan here): the game prepares them at its first start")
== "no usable Vulkan here", "make_game's not-prepared line was not read.");
Require(ShaderStep.Reason("the shaders were not prepared now (\"Prepare shaders now\" is unticked (here)): the game prepares them at its first start")
== "\"Prepare shaders now\" is unticked (here)", "A reason with brackets was cut.");
Require(ShaderStep.Reason("@@stage shaders Preparing shaders") is null && ShaderStep.Reason("done shaders") is null,
"Other lines are not a skipped step.");
Require(ShaderStep.Skipped(began: true, reason: null) is null, "Prepared shaders must get their check mark.");
Require(ShaderStep.Skipped(began: false, reason: null) is { } none && none.Contains("first start"),
"No @@stage shaders (an older make_game, or none ran) must not be a check mark.");
Require(ShaderStep.Skipped(began: true, reason: "the game printed nothing for 15 minutes and was stopped") is { } stopped
&& stopped.Contains("15 minutes"), "A stopped preparation must say why under its row.");
}
static async Task Main(string[] args)
{
if (args.Length is < 1 or > 2) throw new Exception("Pass the Python executable for the authored child.");
var emptyParent = args.Length == 2 && args[0] == "--empty-parent";
var python = Path.GetFullPath(args[^1]);
var original = Environment.GetEnvironmentVariable(Key);
var root = Path.Combine(Path.GetTempPath(), "hookshot-workers-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(root, "nx2"));
try
{
// This is authored test content, not the real pipeline, game files or a compiler invocation.
File.WriteAllText(Path.Combine(root, "nx2", "make_game.py"), """
import json, os, sys
text = json.dumps({'argv': sys.argv[1:], 'jobs': os.environ.get('HOOKSHOT_JOBS')})
print(('@@check ok ' if '--check' in sys.argv else '') + text, flush=True)
""");
var tools = new Tools(root, python);
if (emptyParent)
{
await EmptyParent(python, tools);
Console.WriteLine("Empty inherited worker entry passed.");
return;
}
ShaderStepChecks(root);
Require(BuildWorkers.Choices(4).SequenceEqual(new object[] { BuildWorkers.Automatic, 1, 2, 3, 4 }),
"The menu must offer Automatic first and one count per logical processor.");
Require(BuildWorkers.Choices(0).SequenceEqual(new object[] { BuildWorkers.Automatic, 1 }), "CPU fallback must still offer one worker.");
Require(BuildWorkers.Selected(BuildWorkers.Automatic) is null && BuildWorkers.Selected(3) == 3, "Worker selection failed.");
foreach (var invalid in new object?[] { null, "4", "automatic", 0, -1, 2L, new object() })
{
try { BuildWorkers.Selected(invalid); throw new Exception("Invalid selection accepted."); }
catch (ArgumentException e) { Require(e.Message.Contains("Automatic"), "Invalid choice needs an actionable message."); }
}
foreach (var invalid in new[] { 0, -1 })
{
try { new Runner(tools, new List<string>(), invalid); throw new Exception("Invalid worker count accepted."); }
catch (ArgumentOutOfRangeException) { checks++; }
}
var sourceArguments = new[]
{
new[] { "--nup", "authored file with spaces.nup", "--out", "authored output", "--language", "en-US" },
new[] { "--romfs", "authored romfs", "--exefs", "authored exefs", "--out", "authored output", "--language", "fr" },
new[] { "--update", "authored existing game" },
};
foreach (var inherited in new string?[] { null, "4", "0" })
{
Environment.SetEnvironmentVariable(Key, inherited);
foreach (var command in sourceArguments)
{
var info = Info(tools, command);
CheckInfo(info, tools, command, inherited);
Require(Environment.GetEnvironmentVariable(Key) == inherited, "Automatic changed its parent environment.");
}
}
foreach (var inherited in new[] { "2", "not a number" })
{
Environment.SetEnvironmentVariable(Key, inherited);
var command = sourceArguments[0];
var info = Info(tools, command, 4);
CheckInfo(info, tools, command, "4");
var value = await Delivery(info);
Require(value.GetProperty("jobs").GetString() == "4", "The selected worker count did not reach the child.");
Require(value.GetProperty("argv").EnumerateArray().Select(e => e.GetString()).SequenceEqual(command), "Authored child arguments changed.");
Require(Environment.GetEnvironmentVariable(Key) == inherited, "Manual selection changed the parent environment.");
}
Environment.SetEnvironmentVariable(Key, "6");
var check = await Runner.Check(tools, new[] { "--check", "--nup", "authored check" });
Require(check.Ok, "Runner.Check no longer recognizes its real protocol.");
using (var value = JsonDocument.Parse(check.Text))
Require(value.RootElement.GetProperty("jobs").GetString() == "6", "Check must retain the inherited override.");
Require(Environment.GetEnvironmentVariable(Key) == "6", "Check changed its parent environment.");
var completed = new TaskCompletionSource<string?>(TaskCreationOptions.RunContinuationsAsynchronously);
string? delivered = null;
var runner = new Runner(tools, sourceArguments[2].ToList(), 3);
runner.Line += line => delivered = line;
runner.Finished += failure => completed.TrySetResult(failure);
runner.Start();
Require(await completed.Task.WaitAsync(TimeSpan.FromSeconds(15)) is null, "Actual Runner.Start did not finish its authored child.");
using (var value = JsonDocument.Parse(delivered!))
Require(value.RootElement.GetProperty("jobs").GetString() == "3", "Runner.Start dropped the selected count.");
Require(Environment.GetEnvironmentVariable(Key) == "6", "Runner.Start changed its parent environment.");
var self = new ProcessStartInfo(Environment.ProcessPath!) { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true };
if (string.Equals(Path.GetFileNameWithoutExtension(Environment.ProcessPath), "dotnet", StringComparison.OrdinalIgnoreCase))
self.ArgumentList.Add(Assembly.GetExecutingAssembly().Location);
self.ArgumentList.Add("--empty-parent"); self.ArgumentList.Add(python);
self.Environment[Key] = "";
using var empty = Process.Start(self)!;
var emptyOutput = await empty.StandardOutput.ReadToEndAsync();
var emptyError = await empty.StandardError.ReadToEndAsync();
await empty.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(15));
Require(empty.ExitCode == 0 && emptyError.Length == 0 && emptyOutput.Contains("Empty inherited worker entry passed."), "Empty inheritance check failed: " + emptyError);
Console.WriteLine($"Windows Builder worker checks passed ({checks} assertions). Only authored argv/HOOKSHOT_JOBS were inspected.");
}
finally
{
Environment.SetEnvironmentVariable(Key, original);
Directory.Delete(root, recursive: true);
}
}
}

View File

@@ -0,0 +1,70 @@
#define _CRT_SECURE_NO_WARNINGS
#include <vulkan/vulkan.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <cerrno>
#include <exception>
#include <string>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using VmaAllocation = void*;
struct { VkExtent2D swap_extent{1720,720}; VkPhysicalDeviceProperties props{}; VkDevice device{}; bool offscreen=false; } dev;
static unsigned calls=0, capture_entries=0, presented=0, errors=0;
static uintptr_t serial=0;
static uint64_t expected_bytes=0;
void logf(const char*, ...) { errors++; }
void require(bool ok,const char* why) { if(!ok) { std::fprintf(stderr,"FAIL: %s\n",why); std::exit(1); } }
bool create_image(const VkImageCreateInfo& ci,VkImage& image,VmaAllocation& allocation) {
calls++;
if(!ci.extent.width || !ci.extent.height || ci.extent.width>dev.props.limits.maxImageDimension2D ||
ci.extent.height>dev.props.limits.maxImageDimension2D) return false;
image=reinterpret_cast<VkImage>(++serial); allocation=reinterpret_cast<void*>(serial); return true;
}
bool create_buffer(uint64_t size,VkBufferUsageFlags,bool,VkBuffer& buffer,VmaAllocation& allocation,void** map) {
calls++; require(size!=0 && size==expected_bytes,"readback byte count must match the validated request");
buffer=reinterpret_cast<VkBuffer>(++serial); allocation=reinterpret_cast<void*>(serial); *map=allocation; return true;
}
VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(VkDevice,const VkImageViewCreateInfo* ci,const VkAllocationCallbacks*,VkImageView* view) {
calls++; require(ci->image!=VK_NULL_HANDLE,"view without image"); *view=reinterpret_cast<VkImageView>(++serial); return VK_SUCCESS;
}
void destroy_image(VkImage,VmaAllocation) {}
void destroy_buffer(VkBuffer,VmaAllocation) {}
VKAPI_ATTR void VKAPI_CALL vkDestroyImageView(VkDevice,VkImageView,const VkAllocationCallbacks*) {}
/* SETUP */
void present_image(VkImage,uint32_t,uint32_t,const int*,VkImageView) { presented++; }
void tick() {
VkImage src=reinterpret_cast<VkImage>(uintptr_t(0xff)); const char* shots="test";
uint32_t sw=1720,sh=720; const int* crop=nullptr; VkImageView direct_view{}; bool interpolated=false;
/* GATE */ { capture_entries++; }
/* PRESENT_TAIL */
}
/* PNG */
int main(int argc,char**argv) {
require(argc>=3,"fixture arguments");
if(std::strcmp(argv[1],"png")==0) {
std::vector<uint8_t> rgba(257*251*4,19);
for(size_t i=0;i<257*251;i++) { rgba[i*4]=17; rgba[i*4+1]=73; rgba[i*4+2]=211; }
write_png(argv[2],rgba.data(),257,251); return 0;
}
dev.props.limits.maxImageDimension2D=(uint32_t)std::strtoull(argv[1],nullptr,10);
dev.swap_extent.width=std::min(1720u,dev.props.limits.maxImageDimension2D);
dev.swap_extent.height=std::min(720u,dev.props.limits.maxImageDimension2D);
const bool valid=std::atoi(argv[2])!=0; dev.offscreen=std::atoi(argv[3])!=0;
if(valid) expected_bytes=(uint64_t)kShotW*kShotH*4;
tick(); tick();
require(capture_entries==(valid?2u:0u),"invalid dimensions reached the capture producer");
require(valid?calls==5:calls==0,"dimension rejection must precede every Vulkan allocation");
require(errors==(valid?0u:1u),"invalid capture must be reported exactly once");
require(presented==(dev.offscreen?0u:2u),"capture rejection must preserve window/headless presentation policy");
}

View File

@@ -0,0 +1,146 @@
#define _CRT_SECURE_NO_WARNINGS
#include <vulkan/vulkan.h>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <vector>
constexpr uint32_t kShotW = 344, kShotH = 144, kLogW = 320, kLogH = 180;
struct Allocation { std::vector<uint8_t> cpu, gpu; bool copied = false, host = false; } normal, made, binary;
using VmaAllocation = Allocation*;
void* shot_maps[2]{};
VmaAllocation shot_allocs[2]{};
void* made_map{};
VmaAllocation made_buf_alloc{};
struct LogSlot { VmaAllocation alloc{}; void* map{}; bool used{}; int64_t at{}; uint32_t seq{}, kind{}; VkBuffer buf{}; };
LogSlot log_slots[3];
std::vector<uint8_t> captured;
bool gpu_done = false;
bool coherent = false;
bool implicit_host = false;
int waits = 0, invalidations = 0, pngs = 0;
[[noreturn]] void device_lost() { std::abort(); }
void require(bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "%s\n", why); std::exit(1); }
}
void complete() {
gpu_done = true;
if (coherent)
for (auto a : {&normal, &made, &binary})
if (a->copied && a->host) std::memcpy(a->cpu.data(), a->gpu.data(), a->gpu.size());
}
void wait_value(uint64_t value) { require(value == 17, "wrong GPU value waited"); complete(); ++waits; }
void invalidate_mapped(VmaAllocation allocation, uint64_t size) {
require(gpu_done, "cache invalidated before GPU completion");
require(allocation && size == allocation->gpu.size(), "wrong allocation/range invalidated");
if (!coherent && allocation->host) std::memcpy(allocation->cpu.data(), allocation->gpu.data(), size);
++invalidations;
}
VKAPI_ATTR VkResult VKAPI_CALL vkWaitForFences(VkDevice, uint32_t n, const VkFence*, VkBool32, uint64_t) {
require(n == 1, "wrong fence count"); complete(); ++waits; return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL vkCmdCopyImageToBuffer(VkCommandBuffer, VkImage, VkImageLayout, VkBuffer buffer,
uint32_t count, const VkBufferImageCopy* copy) {
auto a = reinterpret_cast<Allocation*>(buffer);
require(count == 1 && copy->bufferOffset == 0 && copy->bufferRowLength == 0 &&
copy->bufferImageHeight == 0 && (uint64_t)copy->imageExtent.width * copy->imageExtent.height * 4 == a->gpu.size(),
"wrong capture copy range");
a->copied = true; a->host = implicit_host;
}
VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier2(VkCommandBuffer, const VkDependencyInfo* info) {
for (uint32_t i = 0; i < info->bufferMemoryBarrierCount; ++i) {
const auto& b = info->pBufferMemoryBarriers[i];
auto a = reinterpret_cast<Allocation*>(b.buffer);
require(a && a->copied && b.offset == 0 && b.size == a->gpu.size() &&
b.srcQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED && b.dstQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED,
"wrong host barrier copy/range/order");
const auto stages = VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
if ((b.srcStageMask & stages) && (b.srcAccessMask & (VK_ACCESS_2_TRANSFER_WRITE_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT)) &&
(b.dstStageMask & VK_PIPELINE_STAGE_2_HOST_BIT) && (b.dstAccessMask & VK_ACCESS_2_HOST_READ_BIT))
a->host = true;
}
}
/* HOST_BARRIER */
void record_captures() {
struct { VkCommandBuffer cmd{}; } r, f;
VkImage shot_image{}, made_image{}, log_image{};
VkBuffer shot_bufs[2] = {VK_NULL_HANDLE, reinterpret_cast<VkBuffer>(&normal)};
VkBuffer made_buf = reinterpret_cast<VkBuffer>(&made);
int64_t presents = 1;
// Each block is the actual production copy and the dependency following it.
{ /* NORMAL_COPY */ }
{ /* MADE_COPY */ }
LogSlot& l = log_slots[1];
l.buf = reinterpret_cast<VkBuffer>(&binary);
{ /* LOG_COPY */ }
}
void write_png(const char*, const uint8_t* bytes, uint32_t w, uint32_t h) {
require(w == kShotW && h == kShotH, "PNG dimensions changed");
require(std::equal(made.gpu.begin(), made.gpu.end(), bytes), "generated screenshot read stale CPU cache");
++pngs;
}
/* COLLECT */
void read_log_after_fence() {
struct { VkDevice device{}; } dev;
struct { VkFence done{}; } f;
const int log_slot = 1;
/* FENCE */
}
/* SHOT_PIXELS */
void read_shot(bool generated, bool owned = false) {
struct { bool on=false; int64_t presents=0; std::shared_ptr<std::vector<uint8_t>> pixels; } pending;
if (owned) {
pending.on = true; pending.presents = 1; pending.pixels = shot_pixels(17, 1);
std::fill(normal.cpu.begin(), normal.cpu.end(), 0);
}
/* TAKE */
if (!generated) {
auto px = take(17, 1);
require(*px == normal.gpu, "normal screenshot read stale CPU cache");
return;
}
bool shot = true;
VkImage made_src = reinterpret_cast<VkImage>(uintptr_t(123));
uint64_t value = 17;
int64_t presents = 1;
const char* shots = "private-test";
/* MADE */
}
void seed(Allocation& allocation, size_t bytes, uint8_t value) {
allocation.cpu.assign(bytes, 0);
allocation.gpu.assign(bytes, value);
}
int main(int argc, char** argv) {
require(argc == 4, "expected phase, cache and host visibility mode");
coherent = !std::strcmp(argv[2], "coherent");
implicit_host = !std::strcmp(argv[3], "implicit");
seed(normal, size_t(kShotW) * kShotH * 4, 91);
seed(made, size_t(kShotW) * kShotH * 4, 173);
seed(binary, size_t(kLogW) * kLogH * 4, 27);
shot_maps[1] = normal.cpu.data(); shot_allocs[1] = &normal;
made_map = made.cpu.data(); made_buf_alloc = &made;
record_captures();
if (!std::strcmp(argv[1], "normal") || !std::strcmp(argv[1], "owned")) {
read_shot(false, !std::strcmp(argv[1], "owned"));
require(waits == 1 && invalidations == 1, "normal completion/visibility contract");
} else if (!std::strcmp(argv[1], "made")) {
read_shot(true);
require(waits == 1 && invalidations == 2 && pngs == 1, "generated completion/visibility contract");
} else {
log_slots[1] = {&binary, binary.cpu.data(), true, 123456789, 42, 1};
read_log_after_fence();
require(captured.size() == 16 + binary.gpu.size(), "binary record size changed");
require(std::equal(binary.gpu.begin(), binary.gpu.end(), captured.begin() + 16), "binary log read stale CPU cache");
require(!log_slots[1].used && waits == 1 && invalidations == 1, "binary completion/visibility contract");
int64_t timestamp{}; uint32_t seq{}, kind{};
std::memcpy(&timestamp, captured.data(), 8); std::memcpy(&seq, captured.data() + 8, 4);
std::memcpy(&kind, captured.data() + 12, 4);
require(timestamp == 123456789 && seq == 42 && kind == 1, "binary metadata changed");
const int before = invalidations;
framelog_collect(1); require(invalidations == before, "unused slot invalidated");
}
std::printf("%s %s %s production copy/read observes GPU bytes after host dependency/completion/invalidation\n", argv[1], argv[2], argv[3]);
}

View File

@@ -0,0 +1,154 @@
#include <vulkan/vulkan.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
using VmaAllocation = void*;
struct { VkExtent2D swap_extent{1720, 720}; VkDevice device{}; VkPhysicalDeviceProperties props{}; } dev;
VkImage off_image{}, shot_image{};
VkImageView shot_view{};
VkImage made_image{};
VkBuffer made_buf{};
VmaAllocation made_image_alloc{}, made_buf_alloc{};
void* made_map{};
VmaAllocation off_alloc{}, shot_image_alloc{}, shot_allocs[2]{};
VkBuffer shot_bufs[2]{};
void* shot_maps[2]{};
const uint32_t kShotW = (uint32_t)std::atoi("3440"), kShotH = (uint32_t)std::atoi("1440");
int calls = 0, fail_at = 0;
uintptr_t serial = 0;
std::set<uintptr_t> live;
std::map<VkImageView, VkImage> views;
void require(bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "%s\n", why); std::exit(1); }
}
template<class T> bool make(T& handle, VmaAllocation& allocation) {
if (++calls == fail_at) return false;
live.insert(++serial);
handle = reinterpret_cast<T>(serial);
allocation = reinterpret_cast<void*>(serial);
return true;
}
void logf(const char*, ...) { require(false, "valid capture sizing rejected"); }
bool create_image(const VkImageCreateInfo& ci, VkImage& image, VmaAllocation& allocation) {
require(ci.extent.width && ci.extent.height, "empty image");
return make(image, allocation);
}
bool create_buffer(uint64_t size, VkBufferUsageFlags, bool, VkBuffer& buffer,
VmaAllocation& allocation, void** map) {
require(size == uint64_t(kShotW) * kShotH * 4, "readback byte size");
if (!make(buffer, allocation)) return false;
*map = allocation;
return true;
}
void destroy_image(VkImage image, VmaAllocation allocation) {
for (const auto& view : views) require(view.second != image, "image destroyed before its view");
require(reinterpret_cast<uintptr_t>(image) == reinterpret_cast<uintptr_t>(allocation), "image ownership");
require(live.erase(reinterpret_cast<uintptr_t>(image)) == 1, "image double free");
}
void destroy_buffer(VkBuffer buffer, VmaAllocation allocation) {
require(reinterpret_cast<uintptr_t>(buffer) == reinterpret_cast<uintptr_t>(allocation), "buffer ownership");
require(live.erase(reinterpret_cast<uintptr_t>(buffer)) == 1, "buffer double free");
}
VKAPI_ATTR VkResult VKAPI_CALL vkCreateImageView(VkDevice, const VkImageViewCreateInfo* ci,
const VkAllocationCallbacks*, VkImageView* view) {
require(ci->image && live.count(reinterpret_cast<uintptr_t>(ci->image)), "view of an unmade image");
VmaAllocation unused{};
if (!make(*view, unused)) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
views[*view] = ci->image;
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL vkDestroyImageView(VkDevice, VkImageView view, const VkAllocationCallbacks*) {
require(views.erase(view) == 1 && live.erase(reinterpret_cast<uintptr_t>(view)) == 1, "view double free");
}
/* ACTUAL_FUNCTION */
int commands = 0;
VkImage made_src{};
void image_barrier(VkCommandBuffer, VkImage image, VkImageLayout, VkImageLayout) {
require(image && live.count(reinterpret_cast<uintptr_t>(image)), "barrier on unmade image");
++commands;
}
void blit_crop(VkCommandBuffer, VkImage src, uint32_t, uint32_t, const int*, VkImage dst,
VkImageLayout, uint32_t, uint32_t) {
require(src && dst && live.count(reinterpret_cast<uintptr_t>(dst)), "blit on unmade image");
++commands;
}
VKAPI_ATTR void VKAPI_CALL vkCmdCopyImageToBuffer(VkCommandBuffer, VkImage image, VkImageLayout,
VkBuffer buffer, uint32_t n, const VkBufferImageCopy*) {
require(image && buffer && live.count(reinterpret_cast<uintptr_t>(buffer)) && n == 1,
"copy on unmade readback");
++commands;
}
void record_made() {
struct { VkCommandBuffer cmd{}; } r;
uint32_t mw = 320, mh = 180;
/* ACTUAL_RECORDING */
}
VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier2(VkCommandBuffer, const VkDependencyInfo* info) {
require(info->bufferMemoryBarrierCount == 1, "capture host dependency missing");
const auto& b = info->pBufferMemoryBarriers[0];
require(b.buffer && live.count(reinterpret_cast<uintptr_t>(b.buffer)) && b.offset == 0 &&
b.size == uint64_t(kShotW) * kShotH * 4 && b.dstStageMask == VK_PIPELINE_STAGE_2_HOST_BIT &&
b.dstAccessMask == VK_ACCESS_2_HOST_READ_BIT, "host dependency on unmade or incorrect readback");
++commands;
}
void cleanup() {
if (shot_view) vkDestroyImageView(dev.device, shot_view, nullptr);
shot_view = {};
if (off_image) destroy_image(off_image, off_alloc);
if (shot_image) destroy_image(shot_image, shot_image_alloc);
for (int i = 0; i < 2; ++i) {
if (shot_bufs[i]) destroy_buffer(shot_bufs[i], shot_allocs[i]);
shot_bufs[i] = {}; shot_allocs[i] = {}; shot_maps[i] = {};
}
off_image = {}; shot_image = {}; off_alloc = {}; shot_image_alloc = {};
}
int main() {
dev.props.limits.maxImageDimension2D = 16384;
for (int failure = 1; failure <= 5; ++failure) {
calls = 0; fail_at = failure;
require(!offscreen_init(), "failed setup accepted");
require(!off_image && !shot_image && !shot_view && !shot_bufs[0] && !shot_bufs[1], "partial capture handles published");
require(!off_alloc && !shot_image_alloc && !shot_allocs[0] && !shot_allocs[1]
&& !shot_maps[0] && !shot_maps[1], "partial capture allocation/map published");
require(live.empty(), "partial capture resources leaked");
calls = 0; fail_at = 0;
require(offscreen_init(), "retry did not recover");
require(off_image && shot_image && shot_view && shot_bufs[0] && shot_bufs[1]
&& shot_maps[0] && shot_maps[1], "ready capture incomplete");
const int before = calls;
require(offscreen_init() && calls == before, "ready setup allocated again");
cleanup(); require(live.empty(), "cleanup leaked");
}
std::puts("capture allocation failures, retry and ready reuse passed");
for (int failure = 1; failure <= 2; ++failure) {
calls = 0; fail_at = failure;
require(!made_shot_init(), "failed made capture accepted");
require(!made_image && !made_buf && !made_image_alloc && !made_buf_alloc && !made_map,
"partial made capture published");
require(live.empty(), "made capture allocation leaked");
calls = 0; fail_at = 0;
require(made_shot_init() && made_image && made_buf && made_map, "made capture retry failed");
const int before = calls;
require(made_shot_init() && calls == before, "ready made capture allocated again");
destroy_image(made_image, made_image_alloc); destroy_buffer(made_buf, made_buf_alloc);
made_image = {}; made_buf = {}; made_image_alloc = {}; made_buf_alloc = {}; made_map = {};
require(live.empty(), "made capture cleanup leaked");
}
std::puts("made-frame capture failures, retry and ready reuse passed");
for (int failure = 1; failure <= 2; ++failure) {
calls = 0; fail_at = failure; commands = 0;
made_src = reinterpret_cast<VkImage>(uintptr_t(0xffff));
record_made();
require(!made_src && !commands && live.empty(), "failed made shot recorded or scheduled a read");
calls = 0; fail_at = 0;
made_src = reinterpret_cast<VkImage>(uintptr_t(0xffff));
record_made();
require(made_src && made_map && commands == 5, "recovered made shot copy/host dependency was not recorded");
destroy_image(made_image, made_image_alloc); destroy_buffer(made_buf, made_buf_alloc);
made_image = {}; made_buf = {}; made_image_alloc = {}; made_buf_alloc = {}; made_map = {};
}
std::puts("production made-shot recorder suppresses failed captures and recovers");
}

View File

@@ -0,0 +1,80 @@
// The production statements are inserted by test_capture_slot_ownership.py.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <utility>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line%d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
constexpr uint32_t kShotW=2,kShotH=1;
enum { VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL=0 };
struct { int cmd=0; } r;
int shot_image=0,c=0;
uint8_t pixels[2][8]{};
void* shot_maps[2]={pixels[0],pixels[1]};
int shot_bufs[2]={0,1},shot_allocs[2]={0,1};
struct Copy { int slot; uint64_t frame; bool done=false; };
std::vector<Copy> queue;
Copy recorded{};
uint64_t finished=1;
bool submitted=false,fast=true;
int waits=0,waits_before_submit=0;
int64_t presents=0;
void complete_through(uint64_t value) {
for (auto& copy:queue) if (!copy.done && copy.frame<=value) {
std::memset(pixels[copy.slot],int(copy.frame & 255),sizeof pixels[copy.slot]);
copy.done=true; finished=std::max(finished,copy.frame);
}
}
void wait_value(uint64_t value) {
++waits; if (!submitted) ++waits_before_submit;
complete_through(value); CHECK(finished>=value);
}
uint64_t completed() { return finished; }
void invalidate_mapped(int, uint64_t bytes) { CHECK(bytes==sizeof pixels[0]); }
void vkCmdCopyImageToBuffer(int,int,int,int slot,int,const int*) { recorded={slot,uint64_t(presents),false}; }
void submit_recording(decltype(r)&) {
queue.push_back(recorded); submitted=true;
complete_through(fast ? uint64_t(presents) : 2);
}
/* STATE */
/* READ_HELPER */
void run(int64_t current,bool fast_new,bool pending_old) {
queue.clear(); std::memset(pixels,0,sizeof pixels);finished=1;submitted=false;fast=fast_new;
waits=waits_before_submit=0;
PendingShot pending;
if (pending_old) { pending.on=true;pending.value=2;pending.presents=2;queue.push_back({0,2,false}); }
std::vector<std::pair<int64_t,std::vector<uint8_t>>> captures;
/* TAKE */
captures.emplace_back(at_present,*px);
};
bool shot=false;
// This is the actual consume condition on the intervening non-shot frame.
/* CONSUME */
CHECK(captures.empty() && waits==0 && pending.on==pending_old);
presents=current;uint64_t value=uint64_t(current);shot=true;
/* PRODUCER */
/* CONSUME */
if (pending_old) {
CHECK(captures.size()==1 && captures[0].first==2);
CHECK(std::all_of(captures[0].second.begin(),captures[0].second.end(),[](uint8_t b){return b==2;}));
CHECK(waits==1);
CHECK(waits_before_submit==int((current & 1)==0));
} else { CHECK(captures.empty() && waits==0); }
// Re-arming must discard a saved snapshot, not attach it to the new frame.
/* REARM */
CHECK(pending.on && pending.value==value && pending.presents==current);
take(value,current);
CHECK(captures.back().first==current);
CHECK(std::all_of(captures.back().second.begin(),captures.back().second.end(),[current](uint8_t b){return b==(current & 255);}));
}
int main() {
for (int64_t current=3;current<100;current++) {
run(current,true,true);
run(current,false,true);
run(current,true,false);
}
std::puts("PASS: pending-shot pixels and metadata survive slot reuse; different slots stay asynchronous, no-pending path does not wait, rearm uses new pixels");
}

View File

@@ -0,0 +1,80 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Real Eden IR/pass/emitter regression; no game or GPU.
#include <filesystem>
#include <fstream>
#include <string>
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/object_pool.h"
#include "shader_recompiler/environment.h"
#include "shader_recompiler/host_translate_info.h"
#include "shader_recompiler/ir_opt/passes.h"
using namespace Shader;
struct TestEnv:Environment {
bool integer; TextureType type; unsigned queries=0;
TestEnv(bool i, TextureType t):integer(i),type(t){stage=Stage::Compute;}
u64 ReadInstruction(u32)override{return 0;}
u32 ReadCbufValue(u32,u32)override{return 1;}
TextureType ReadTextureType(u32)override{return type;}
TexturePixelFormat ReadTexturePixelFormat(u32)override{return {};}
bool IsTexturePixelFormatInteger(u32)override{queries++;return integer;}
u32 ReadViewportTransformState()override{return 1;}
u32 TextureBoundBuffer()const override{return 0;}
u32 LocalMemorySize()const override{return 0;}
u32 SharedMemorySize()const override{return 0;}
std::array<u32,3> WorkgroupSize()const override{return {1,1,1};}
bool HasHLEMacroState()const override{return false;}
std::optional<ReplaceConstant> GetReplaceConstBuffer(u32,u32)override{return {};}
void Dump(u64,u64)override{}
};
int main(int argc, char** argv) {
try {
if (argc != 2) return 2;
std::filesystem::create_directories(argv[1]);
for (bool array : {false, true}) for (bool integer : {false, true}) {
ObjectPool<IR::Inst> pool;
IR::Block block{pool};
IR::Program program;
program.stage = Stage::Compute;
program.workgroup_size = {1, 1, 1};
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode body{};
body.type = IR::AbstractSyntaxNode::Type::Block;
body.data.block = &block;
program.syntax_list.push_back(body);
IR::AbstractSyntaxNode ret{};
ret.type = IR::AbstractSyntaxNode::Type::Return;
program.syntax_list.push_back(ret);
IR::IREmitter ir{block};
const auto type = array ? TextureType::ColorArray2D : TextureType::Color2D;
IR::TextureInstInfo flags{};
flags.type.Assign(type);
flags.is_depth.Assign(1);
const auto coords = array
? ir.CompositeConstruct(ir.Imm32(.5f), ir.Imm32(.5f), ir.Imm32(0.f))
: ir.CompositeConstruct(ir.Imm32(.5f), ir.Imm32(.5f));
(void)ir.ImageSampleDrefExplicitLod(ir.Imm32(0u), coords, ir.Imm32(.5f),
ir.Imm32(0.f), IR::Value{}, flags);
ir.Epilogue();
TestEnv env(integer, type);
Optimization::TexturePass(env, program, HostTranslateInfo{});
Optimization::CollectShaderInfoPass(env, program);
Profile profile{};
profile.supported_spirv = 0x00010600;
profile.support_int64 = true;
auto words = Backend::SPIRV::EmitSPIRV(profile, program);
const auto name = std::string(array ? "array" : "2d") + (integer ? "-integer" : "-float");
std::ofstream file(std::filesystem::path(argv[1]) / (name + ".spv"), std::ios::binary);
file.write(reinterpret_cast<const char*>(words.data()), words.size() * sizeof(u32));
if (!file) return 3;
std::printf("%s integer-queries=%u\n", name.c_str(), env.queries);
}
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}

View File

@@ -0,0 +1,165 @@
// Authored SDL and thread stubs. Production startup code is extracted by the
// driver; no SDL library, OS window, renderer, game, or real thread is started.
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>
#define HOS_APP_ID "authored.app"
#define HOS_GAME_TITLE "authored window"
using HANDLE = void*;
constexpr int TRUE = 1, FALSE = 0;
constexpr unsigned INFINITE = ~0u;
constexpr unsigned HOS_SDL_VIDEO = 1, HOS_SDL_EVENTS = 2;
struct HosSdlEvent {};
struct ProcessExit { int code; };
static int sdl_ready, start_asked, visible_window_started;
static int game_argc, game_token, event_token;
static char** game_argv;
static int hos_posix_window[4];
[[maybe_unused]] static HANDLE renderer_up;
static void* window;
float hos_aspect_ratio = 16.0f / 9;
static std::string mode;
static unsigned loads, inits, windows, threads, offscreen, signals, sleeps, flushes, waited, prepares;
static std::vector<std::string> logs;
void hos_discord_start() {} // Inert optional IPC startup; the fixture exercises display startup only.
static void require(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
static void hos_log(const char* format, ...) {
char text[512];
va_list args;
va_start(args, format);
vsnprintf(text, sizeof text, format, args);
va_end(args);
logs.emplace_back(text);
}
[[maybe_unused]] static void hos_log_flush() { ++flushes; }
static const char* hos_sdl_error() { return "authored missing Vulkan loader"; }
static int hos_sdl_load() { ++loads; return mode == "load_failure" || mode.starts_with("prepare_") ? 0 : 3; }
static void hos_sdl_identity(const char*, const char*) {}
static void hos_sdl_hint(const char*, const char*) {}
static int hos_sdl_init(unsigned flags) {
require(flags == (HOS_SDL_VIDEO | HOS_SDL_EVENTS), "changed SDL startup flags");
++inits;
return mode != "init_failure";
}
static int hos_sdl_desktop_size(int* w, int* h) { *w = 2560; *h = 1440; return 1; }
static void* hos_sdl_window(const char*, int w, int h) {
require(w == 1280 && h == 720, "changed default window size");
++windows;
return mode == "window_failure" ? nullptr : &event_token;
}
static void hos_sdl_wait_event(HosSdlEvent*, int) {
throw std::runtime_error("unexpected startup event wait");
}
static HANDLE CreateEventA(void*, int, int, void*) { return &event_token; }
[[maybe_unused]] static void SetEvent(HANDLE h) {
require(h == &event_token, "wrong readiness event");
++signals;
}
[[noreturn]] static void Sleep(unsigned) {
++sleeps;
throw std::runtime_error("startup entered the hidden-window sleep loop");
}
[[maybe_unused, noreturn]] static void ExitProcess(unsigned code) { throw ProcessExit{static_cast<int>(code)}; }
static int rx_start(void* hwnd) {
require(!hwnd, "unexpected renderer start in authored fixture");
++offscreen;
return 1;
}
void hos_display_start(void);
static unsigned game_thread(void*) {
if (mode.starts_with("prepare_")) {
// Authored builder worker: verify actual POSIX main forwards the flag,
// and let its ordinary completion or no-Vulkan exit reach the caller.
require(game_argc == 3 && !std::strcmp(game_argv[2], "--prepare-shaders"), "lost preparation flag");
++prepares;
if (mode == "prepare_no_vulkan") throw ProcessExit{3};
return 0;
}
hos_display_start();
return 0;
}
static HANDLE CreateThread(void*, unsigned, unsigned (*entry)(void*), void* arg, unsigned, void*) {
if (mode == "thread_failure") return nullptr;
++threads;
entry(arg); // Deterministic startup handshake only; not a concurrency test.
return &game_token;
}
static void SetThreadDescription(HANDLE h, const wchar_t*) { require(h == &game_token, "wrong game thread"); }
static void WaitForSingleObject(HANDLE h, unsigned timeout) {
require(h == &game_token && timeout == INFINITE, "changed game-thread wait");
++waited;
}
static void load_hookshot_env() {}
#include "display_startup_actual.inc"
static void headless_setting(const char* value) {
#ifdef _WIN32
require(_putenv_s("HOS_HEADLESS", value ? value : "") == 0, "authored child environment failed");
_putenv_s("HOS_WINDOW_SIZE", "");
#else
require((value ? setenv("HOS_HEADLESS", value, 1) : unsetenv("HOS_HEADLESS")) == 0,
"authored child environment failed");
unsetenv("HOS_WINDOW_SIZE");
#endif
}
int main(int argc, char** argv) {
mode = argc == 2 ? argv[1] : "";
try {
headless_setting(mode == "headless" ? "1" : mode == "headless_zero" ? "0" : nullptr);
char prepare_flag[] = "--prepare-shaders";
char* prepare_argv[] = {argv[0], argv[1], prepare_flag, nullptr};
char romfs_flag[] = "--romfs", user_flag[] = "--user";
char* path_argv[] = {argv[0], mode == "flag_as_romfs" ? romfs_flag : user_flag, prepare_flag, nullptr};
const bool preparing = mode.starts_with("prepare_");
const bool path_value = mode == "flag_as_romfs" || mode == "flag_as_user";
const int app_argc = preparing || path_value ? 3 : argc;
char** app_argv = preparing ? prepare_argv : path_value ? path_argv : argv;
int status;
try { status = app_main(app_argc, app_argv); }
catch (const ProcessExit& exit) { status = exit.code; }
require(game_argc == app_argc && game_argv == app_argv, "changed argument handoff");
if (mode == "load_failure" || mode == "init_failure" || mode == "window_failure") {
require(status == 1, "unexpected display failure did not exit nonzero");
require(flushes == 1 && logs.size() == 1, "startup error not logged and flushed once");
require(logs[0].find(mode == "load_failure" ? "no SDL" : hos_sdl_error()) != std::string::npos,
"missing actual startup error detail");
require(!offscreen && !signals && !sleeps && !visible_window_started,
"unexpected startup failure continued offscreen or signaled success");
require(threads == (mode == "window_failure" ? 1u : 0u),
"SDL initialization failure started game work");
} else if (mode == "headless" || mode == "headless_zero") {
require(status == 0 && threads == 1 && offscreen == 1 && waited == 1,
"changed explicit headless/offscreen behavior");
require(!loads && !inits && !windows && !visible_window_started && !flushes,
"explicit headless used the display");
} else if (mode == "visible_success" || path_value) {
require(status == 0 && threads == 1 && waited == 1 && loads && inits == 1 && windows == 1 &&
visible_window_started == 1 && !offscreen && !flushes && !sleeps,
"changed successful visible startup handshake");
} else if (mode == "thread_failure") {
require(status == 1 && !threads && !windows && !offscreen && !waited,
"changed game-thread creation failure behavior");
} else if (preparing) {
require(status == (mode == "prepare_no_vulkan" ? 3 : 0) && threads == 1 && prepares == 1,
"display startup intercepted the preparation worker's exit");
require(!loads && !inits && !windows && !visible_window_started && !offscreen && !flushes,
"shader preparation tried to use the display");
} else throw std::runtime_error("unknown fixture case");
std::printf("PASS %s\n", mode.c_str());
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "FAIL %s: %s\n", mode.c_str(), e.what());
return 1;
}
}

View File

@@ -0,0 +1,152 @@
#define _CRT_SECURE_NO_WARNINGS
#include <vulkan/vulkan.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>
static void require(bool ok,const char* why) { if(!ok) { std::fprintf(stderr,"FAIL: %s\n",why); std::exit(1); } }
struct Allocation { std::vector<uint8_t> cpu,visible; };
using VmaAllocation=Allocation*;
struct { VkDevice device{}; VkQueue queue{}; VkPhysicalDeviceProperties props{}; std::mutex present_mu; } dev;
std::map<VkBuffer,Allocation> buffers;
uintptr_t serial=100;
template<class T> T handle() { return reinterpret_cast<T>(++serial); }
bool coherent=false,fail_buffer=false;
unsigned flushed=0,queued=0;
std::mutex par_mutex;
struct ParLock { std::lock_guard<std::mutex> lock{par_mutex}; };
void logf(const char*,...) {}
bool create_buffer(uint64_t size,VkBufferUsageFlags,bool,VkBuffer& buffer,VmaAllocation& allocation,void** mapped,bool=false) {
if(fail_buffer) return false;
buffer=handle<VkBuffer>(); auto& a=buffers[buffer]; a.cpu.assign((size_t)size,0x9a); a.visible.assign((size_t)size,0xc3);
allocation=&a; *mapped=a.cpu.data(); return true;
}
void destroy_buffer(VkBuffer buffer,VmaAllocation allocation) { require(&buffers.at(buffer)==allocation,"allocation owner"); buffers.erase(buffer); }
void flush_mapped(VmaAllocation allocation,uint64_t size) {
if(!size)return;
require(allocation && size<=allocation->cpu.size(),"valid stream flush range");
std::copy_n(allocation->cpu.begin(),(size_t)size,allocation->visible.begin()); flushed++;
}
/* RES */
std::deque<RecordingRes> res;
thread_local RecordingRes* cur=nullptr;
const uint64_t kStream=1024;
constexpr VkBufferUsageFlags kStreamUse=VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT|VK_BUFFER_USAGE_STORAGE_BUFFER_BIT|VK_BUFFER_USAGE_TRANSFER_SRC_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT|VK_BUFFER_USAGE_INDEX_BUFFER_BIT|VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
std::atomic<uint64_t> stream_extra{0},stream_grown{0},stream_peak{0};
bool stream_grow_off() { return false; }
struct BufferRef { VkBuffer buffer{}; uint64_t offset=0,size=0,address=0; };
void draw_flush_recording(int slot);
/* STREAM */
VkCommandBuffer rec_cmd{};
uint64_t rec_value=0,targets_serial=0;
VkBuffer zero_buffer{}; VmaAllocation zero_alloc{};
VkDescriptorPool make_pool() { return handle<VkDescriptorPool>(); }
VKAPI_ATTR VkResult VKAPI_CALL vkResetDescriptorPool(VkDevice,VkDescriptorPool,VkDescriptorPoolResetFlags) { return VK_SUCCESS; }
VKAPI_ATTR void VKAPI_CALL vkDestroyDescriptorPool(VkDevice,VkDescriptorPool,const VkAllocationCallbacks*) {}
struct Pass {}; Pass& pass() { static Pass p;return p; }
bool work_since_barrier=false,last_dyn_valid=false,last_desc_valid=false,last_vb_valid=false;
VkPipeline last_pipeline{};
struct { bool on=false; } vertex_set,last_ib;
void descriptors_disturbed() {}
std::unordered_map<int,int>& handle_hits() { static std::unordered_map<int,int> h;return h; }
struct BankCache { uint64_t at=0; };
std::array<std::array<BankCache,2>,2>& bank_cache() { static std::array<std::array<BankCache,2>,2> b;return b; }
void make_dummies() {}
/* FLUSH */
/* BEGIN */
struct Recording { int slot;VkCommandBuffer cmd,upload,init; VkFence fence;uint64_t value=1,batch=0;bool submitted=false; };
struct Expected { BufferRef ref;std::vector<uint8_t> bytes; };
std::map<VkCommandBuffer,std::vector<Expected>> expected;
std::vector<int> submitted_slots;
std::map<VkCommandBuffer,int> slots;
bool texture_uploads_recorded() { return true; }
void gprof_submitted(int,bool) {}
void gpu_time_end(Recording&) {}
VKAPI_ATTR VkResult VKAPI_CALL vkEndCommandBuffer(VkCommandBuffer) { return VK_SUCCESS; }
VkSemaphore timeline{},rec_timeline{};
uint64_t rec_seq=0;
std::mutex rec_map_mu;
std::multimap<uint64_t,uint64_t> value_rec;
std::set<uint64_t> rec_open_values;
struct { std::vector<VkCommandBufferSubmitInfo> cbs;uint64_t signal=0,rec=0;bool any_signal=false; } pend;
uint64_t batch_next=0;std::chrono::steady_clock::time_point pend_since;
bool batch=false;bool batch_on() { return batch; }
int flush_reason=0;constexpr int FR_CAP=1;
void device_lost() { require(false,"unexpected device loss"); }
#define PROF(x) ((void)0)
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit2(VkQueue,uint32_t count,const VkSubmitInfo2* submits,VkFence) {
for(unsigned i=0;i<count;i++) for(unsigned j=0;j<submits[i].commandBufferInfoCount;j++) {
auto cmd=submits[i].pCommandBufferInfos[j].commandBuffer;
if(slots.count(cmd))submitted_slots.push_back(slots.at(cmd));
for(auto& e:expected[cmd]) {
auto& a=buffers.at(e.ref.buffer);auto& seen=coherent?a.cpu:a.visible;
require(e.ref.offset+e.bytes.size()<=seen.size(),"consumer bounds");
require(std::equal(e.bytes.begin(),e.bytes.end(),seen.begin()+e.ref.offset),"GPU draw input must see the CPU-authored bytes");
}
}
queued++;return VK_SUCCESS;
}
void queue_flush_locked() {
if(pend.cbs.empty())return;
VkSubmitInfo2 si{VK_STRUCTURE_TYPE_SUBMIT_INFO_2};si.commandBufferInfoCount=(uint32_t)pend.cbs.size();si.pCommandBufferInfos=pend.cbs.data();
vkQueueSubmit2(dev.queue,1,&si,VK_NULL_HANDLE);pend.cbs.clear();
}
/* SUBMIT */
struct Job { Recording* r;int inits=0;bool uploads=true; };
void submit_segments(std::vector<Job>& jobs) {
bool init_lost=false;
for(auto& job:jobs) { Job* j=&job;
/* SEGMENT */
}
}
static Recording recording(int slot) {
Recording r{slot,handle<VkCommandBuffer>(),handle<VkCommandBuffer>(),handle<VkCommandBuffer>(),handle<VkFence>()};
slots[r.cmd]=slot; return r;
}
static void write(Recording& r,size_t bytes,unsigned seed) {
std::vector<uint8_t> data(bytes);for(size_t i=0;i<bytes;i++)data[i]=uint8_t(i*37+seed*61);
BufferRef ref=stream_copy(data.data(),data.size());require(ref.buffer!=VK_NULL_HANDLE && ref.size==bytes,"stream copy accepted");
expected[r.cmd].push_back({ref,std::move(data)});
}
int main(int argc,char**argv) {
require(argc==3,"fixture arguments");std::string mode=argv[1];coherent=std::string(argv[2])=="coherent";
dev.props.limits.minUniformBufferOffsetAlignment=256;dev.props.limits.minStorageBufferOffsetAlignment=64;
Recording a=recording(0);draw_begin_recording(0,a.cmd,1);
if(mode=="empty") { submit_recording(a);require(!flushed,"empty recording must not flush old bytes");return 0; }
write(a,701,1);
if(mode=="grow" || mode=="reuse") {
write(a,3107,2);write(a,1100011,3);require(res[0].si==2,"exercise multiple grown streams");
}
if(mode=="allocation-failure") {
fail_buffer=true;std::vector<uint8_t> large(3000,9);auto missing=stream_copy(large.data(),large.size());fail_buffer=false;
require(!missing.buffer && res[0].si==0,"failed growth must leave the current stream usable");
}
if(mode=="segments" || mode=="failed-segment") {
Recording b=recording(1),c=recording(2);
std::thread worker([&]{draw_begin_recording(1,b.cmd,1);write(b,997,4);write(b,3009,5);});worker.join();
draw_begin_recording(2,c.cmd,1);write(c,487,6);require(cur==&res[2],"main thread current slot differs from submitted worker slots");
std::vector<Job> jobs{{&a,1,true},{&b,mode=="failed-segment"?-1:0,true},{&c,0,false}};submit_segments(jobs);
require(submitted_slots==(mode=="failed-segment"?std::vector<int>{0}:std::vector<int>{0,1,2}),"segment failure/ordering contract");return 0;
}
batch=mode=="batch";submit_recording(a);
if(batch) {require(queued==0,"batch retained before queue use");queue_flush_locked();}
if(mode=="reuse") {
expected[a.cmd].clear();draw_begin_recording(0,a.cmd,2);require(res[0].si==0 && res[0].used==0 && res[0].streams.size()==3,"real begin resets used range and retains grown streams");
write(a,69,7);submit_recording(a);
}
require(a.submitted,"healthy recording submitted");std::puts("PASS: production draw stream input visibility and submission paths");
}

View File

@@ -0,0 +1,193 @@
#define VK_NO_PROTOTYPES
#define _CRT_SECURE_NO_WARNINGS
#include <vulkan/vulkan.h>
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <limits>
#include <memory>
#include <string>
#include <vector>
namespace mkwvk {
struct Allocation {
std::vector<uint8_t> gpu, cpu;
bool copied=false, host=false, completed=false, destroyed=false;
};
using VmaAllocation=Allocation*;
struct Texture {
VkImage image{}; VkSampleCountFlagBits samples=VK_SAMPLE_COUNT_1_BIT;
VkImageAspectFlags aspect=VK_IMAGE_ASPECT_COLOR_BIT;
VkFormat format=VK_FORMAT_R32G32B32A32_SFLOAT;
uint64_t gpu=0; int id=1;
uint32_t levels=1,width=2,height=1,layers=1,depth=1;
VkImageType type=VK_IMAGE_TYPE_2D;
};
struct Device {VkPhysicalDevice physical{};} dev;
std::vector<std::unique_ptr<Allocation>> allocations;
std::vector<uint8_t> payload, png;
std::vector<std::string> lines;
bool coherent=false, implicit_host=false;
int invalidations=0;
int waits=0;
std::atomic<bool> par_active{false};
std::atomic<uint64_t> work_epoch{0};
VkCommandBuffer last_barrier_cmd{};
uint64_t last_barrier_epoch=UINT64_MAX;
std::atomic<int64_t> st_barriers_left_out{0}, st_barriers_made{0};
struct GprofSpan { GprofSpan(VkCommandBuffer,const char*){} };
void logf(const char* format,...) {
char b[2048];va_list a;va_start(a,format);vsnprintf(b,sizeof b,format,a);va_end(a);lines.emplace_back(b);
}
bool create_buffer(uint64_t n,VkBufferUsageFlags,bool,VkBuffer& b,VmaAllocation& a,void** map) {
auto x=std::make_unique<Allocation>();x->gpu.resize(n);x->cpu.resize(n);
a=x.get();b=reinterpret_cast<VkBuffer>(a);*map=x->cpu.data();allocations.push_back(std::move(x));return true;
}
void destroy_buffer(VkBuffer,VmaAllocation a){a->destroyed=true;}
bool create_image(const VkImageCreateInfo&,VkImage& i,VmaAllocation& a){i=reinterpret_cast<VkImage>(2);a=nullptr;return true;}
void destroy_image(VkImage,VmaAllocation){}
void invalidate_mapped(VmaAllocation a,uint64_t n) {
if(!a->completed || n!=a->gpu.size()) {std::fprintf(stderr,"bad invalidate order/range\n");std::exit(9);}
invalidations++;
if(a->host) a->cpu=a->gpu;
}
void vkGetPhysicalDeviceFormatProperties(VkPhysicalDevice,VkFormat,VkFormatProperties* p){p->optimalTilingFeatures=VK_FORMAT_FEATURE_BLIT_SRC_BIT;}
void vkCmdPipelineBarrier2(VkCommandBuffer,const VkDependencyInfo* d) {
for(uint32_t i=0;i<d->memoryBarrierCount;i++) {
const auto& b=d->pMemoryBarriers[i];
bool transfer=(b.srcStageMask&(VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT|VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT|VK_PIPELINE_STAGE_2_COPY_BIT))!=0;
bool writes=(b.srcAccessMask&(VK_ACCESS_2_MEMORY_WRITE_BIT|VK_ACCESS_2_TRANSFER_WRITE_BIT))!=0;
if(transfer && writes && (b.dstStageMask&VK_PIPELINE_STAGE_2_HOST_BIT) && (b.dstAccessMask&(VK_ACCESS_2_HOST_READ_BIT|VK_ACCESS_2_MEMORY_READ_BIT)))
for(auto& a:allocations) if(a->copied&&!a->destroyed)a->host=true;
}
work_epoch++;
}
void copy_to(VkBuffer b,size_t n) {
auto a=reinterpret_cast<Allocation*>(b);
if(n!=a->gpu.size()||payload.size()!=n){std::fprintf(stderr,"bad copy range\n");std::exit(8);}
a->gpu=payload;a->copied=true;a->host=implicit_host;work_epoch++;
}
void vkCmdCopyImageToBuffer(VkCommandBuffer,VkImage,VkImageLayout,VkBuffer b,uint32_t,const VkBufferImageCopy*){copy_to(b,payload.size());}
void vkCmdCopyBuffer(VkCommandBuffer,VkBuffer,VkBuffer b,uint32_t,const VkBufferCopy* c){copy_to(b,c->size);}
void vkCmdBlitImage(VkCommandBuffer,VkImage,VkImageLayout,VkImage,VkImageLayout,uint32_t,const VkImageBlit*,VkFilter){work_epoch++;}
void wait_value(uint64_t) {
waits++;
for(auto& a:allocations) if(a->copied&&!a->destroyed){a->completed=true;if(coherent&&a->host)a->cpu=a->gpu;}
}
void write_png(const char*,const uint8_t* p,uint32_t w,uint32_t h){png.assign(p,p+(size_t)w*h*4);}
int _mkdir(const char*){return 0;}
/* FULL_BARRIER */
} // namespace mkwvk
/* SOURCE */
namespace mkwvk {
void consume_dump(bool burst_open) {
const uint64_t value=1;
/* CALLER */
}
}
void require(bool ok,const char* what) {
if(!ok){std::fprintf(stderr,"FAIL %s\n",what);std::exit(1);}
}
template<class T> void upload(const std::vector<T>& v) {
mkwvk::payload.resize(v.size()*sizeof(T));
std::memcpy(mkwvk::payload.data(),v.data(),mkwvk::payload.size());
}
int main(int argc,char** argv) {
if(argc!=4)return 2;
using namespace mkwvk;
const std::string which=argv[1];
dir=".";
coherent=atoi(argv[2])!=0;implicit_host=atoi(argv[3])!=0;
Texture t;t.image=reinterpret_cast<VkImage>(1);
const auto cmd=reinterpret_cast<VkCommandBuffer>(1);
std::vector<uint8_t> expected{64,128,191,255,128,64,255,255};
std::vector<float> floats{.25f,.5f,.75f,1.f,.5f,.25f,1.f,1.f};
if(which=="empty") {
consume_dump(false);require(waits==0 && invalidations==0 && png.empty(),"empty queue reads/waits");return 0;
}
armed=true;
if(which=="ssbo" || which=="ssbo-tail") {
std::vector<uint32_t> words(which=="ssbo"?8:16384);
words[1]=1;words[3]=2;words.back()=3;upload(words);
dump_buffer(cmd,reinterpret_cast<VkBuffer>(5),12,which=="ssbo"?35:65539,"ssbo");
expected.clear();
} else {
if(which=="uint") {
t.format=VK_FORMAT_R32G32B32A32_UINT;
upload(std::vector<uint32_t>{0,5,4000000000u,1,100,10,UINT32_MAX,1});
expected={0,0,0,255,255,255,255,255};
} else if(which=="sint") {
t.format=VK_FORMAT_R32G32B32A32_SINT;
upload(std::vector<int32_t>{INT32_MIN,-5,-2,1,INT32_MAX,5,2,1});
expected={0,0,0,255,255,255,255,255};
} else if(which=="nan-inf") {
t.width=3;
floats={.25f,.5f,.75f,1.f,std::numeric_limits<float>::quiet_NaN(),0,0,1,
std::numeric_limits<float>::infinity(),0,0,1};
upload(floats);expected={64,128,191,255,255,0,255,255,0,255,0,255};
} else if(which=="d16" || which=="d24" || which=="d32" || which=="depth-scaled") {
t.aspect=VK_IMAGE_ASPECT_DEPTH_BIT;
t.width=which=="depth-scaled"?64:4;
t.format=which=="d16"?VK_FORMAT_D16_UNORM:which=="d24"?VK_FORMAT_D24_UNORM_S8_UINT:VK_FORMAT_D32_SFLOAT;
if(which=="d16")upload(std::vector<uint16_t>{0,16384,32768,65535});
else if(which=="d24")upload(std::vector<uint32_t>{0xaa000000,0xbb400000,0xcc800000,0xddffffff});
else {
floats.resize(t.width);
for(uint32_t i=0;i<t.width;i++)floats[i]=(float)i/(t.width-1);
upload(floats);
}
expected.clear();
const uint32_t w=std::min(16u,t.width);
for(uint32_t i=0;i<w;i++) {
// Golden nearest sample positions, independent of the source expression.
const float v=which=="depth-scaled"?(float)(i*4)/63.f:
(which=="d32"?(float)i/3.f:(i==0?0.f:i==1?.25f:i==2?.5f:1.f));
const uint8_t x=(uint8_t)std::lround(v*255.f);
expected.insert(expected.end(),{x,x,x,255});
}
} else if(which=="rgb9e5") {
t.format=VK_FORMAT_B10G11R11_UFLOAT_PACK32;
upload(std::vector<uint32_t>{(16u<<27)|(256u<<18)|(128u<<9)|64u,
(16u<<27)|(64u<<18)|(256u<<9)|128u});
expected={64,128,255,255,128,255,64,255};
} else upload(floats);
dump_image(cmd,&t,0,0,which.c_str());
}
require(dump_pending() && allocations.size()==1,"producer queues copy");
consume_dump(true);
require(waits==0 && invalidations==0 && png.empty() && !allocations[0]->destroyed,"open burst deferred");
consume_dump(false);
require(waits==1,"completed-submission wait");
if(which=="ssbo" || which=="ssbo-tail") {
const auto wanted=which=="ssbo"?"3 of 8 words not zero:":"3 of 16384 words not zero:";
require(lines.size()==1 && lines[0].find(wanted)!=std::string::npos,"actual buffer nonzero statistics");
require(lines[0].find(which=="ssbo"?"[7]=0x3":"[16383]=0x3")!=std::string::npos,"buffer tail visible");
} else if(which=="stats") {
require(png.empty() && lines.size()==1 && lines[0].find("mean 0.375 0.375 0.875 1")!=std::string::npos,"statistics-only values");
} else {
require(png==expected,"actual typed dump output");
if(which=="nan-inf")require(lines[0].find("NaN 1, Inf 1 of 3")!=std::string::npos,"NaN/Inf statistics");
if(which=="raw") {
std::ifstream f;
for(const auto& entry:std::filesystem::directory_iterator("."))
if(entry.path().extension()==".f32")f=std::ifstream(entry.path(),std::ios::binary);
std::vector<char> bytes((std::istreambuf_iterator<char>(f)),{});
require(bytes.size()==8+floats.size()*sizeof(float),"raw size");
uint32_t wh[2]{};memcpy(wh,bytes.data(),8);
require(wh[0]==2 && wh[1]==1 && !memcmp(bytes.data()+8,floats.data(),floats.size()*4),"raw dimensions/pixels");
}
}
require(!dump_pending() && allocations[0]->destroyed,"resource cleanup");
consume_dump(false);require(waits==1,"consumed queue does not wait again");
std::puts("PASS actual dump readback output, burst deferral, wait and cleanup");
}

View File

@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.20)
project(f2i_immediate LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT DEFINED REPO_ROOT OR NOT DEFINED SPIRV_HEADERS_ROOT)
message(FATAL_ERROR "REPO_ROOT and SPIRV_HEADERS_ROOT are required")
endif()
file(TO_CMAKE_PATH "${REPO_ROOT}" REPO_ROOT)
file(TO_CMAKE_PATH "${SPIRV_HEADERS_ROOT}" SPIRV_HEADERS_ROOT)
set(SHADER "${REPO_ROOT}/gpu/vk/eden/shader_recompiler")
file(GLOB_RECURSE BACKEND_SOURCES CONFIGURE_DEPENDS "${SHADER}/backend/spirv/*.cpp")
file(GLOB_RECURSE IR_SOURCES CONFIGURE_DEPENDS "${SHADER}/frontend/ir/*.cpp")
file(GLOB_RECURSE OPTIMIZATION_SOURCES CONFIGURE_DEPENDS "${SHADER}/ir_opt/*.cpp")
file(GLOB_RECURSE SIRIT_SOURCES CONFIGURE_DEPENDS "${REPO_ROOT}/gpu/vk/sirit/src/*.cpp")
set(IMPL "${SHADER}/frontend/maxwell/translate/impl")
add_executable(f2i_immediate
"${REPO_ROOT}/tests/f2i_immediate/fixture.cpp"
"${SHADER}/frontend/maxwell/decode.cpp"
"${SHADER}/frontend/maxwell/opcodes.cpp" "${IMPL}/common_funcs.cpp"
"${IMPL}/impl.cpp" "${IMPL}/register_map_nx2.cpp" "${IMPL}/move_register.cpp"
"${IMPL}/floating_point_conversion_integer.cpp"
"${IMPL}/floating_point_compare_and_set.cpp" "${IMPL}/floating_point_min_max.cpp"
"${IMPL}/logic_operation.cpp"
${BACKEND_SOURCES} ${IR_SOURCES} ${OPTIMIZATION_SOURCES} ${SIRIT_SOURCES})
target_include_directories(f2i_immediate PRIVATE
"${REPO_ROOT}/gpu/vk/compat" "${REPO_ROOT}/gpu/compat"
"${REPO_ROOT}/gpu/vk/eden" "${REPO_ROOT}/gpu/third_party/suyu"
"${REPO_ROOT}/gpu/vk/sirit/include" "${REPO_ROOT}/gpu/vk/sirit/src"
"${SPIRV_HEADERS_ROOT}/include")
target_compile_definitions(f2i_immediate PRIVATE NDEBUG MKWVK_INCLUDES=2 VK_NO_PROTOTYPES)
if(MSVC)
target_compile_options(f2i_immediate PRIVATE /O1 /EHsc)
else()
target_compile_options(f2i_immediate PRIVATE -O1)
endif()

View File

@@ -0,0 +1,37 @@
# Native F2I immediate regression
This game-free fixture constructs its instructions from independently chosen
registers, float values and bit patterns. It calls the production Ampere lowerer,
Maxwell decoder and conversion implementations, then emits real IR and SPIR-V.
No game, dump, cache, driver or GPU is used.
The immediate contains all 32 source bits. Bits 62 and 63 of the native
instruction belong to that literal; they are not the register source's abs/neg
modifiers. The test checks literal preservation, signedness, destination width,
four rounding modes, FTZ, ordinary/inverted predicates and `@!PT` skipping. It
also checks existing register/cbuf/uniform source forms and explicitly rejects
immediate F16, F64, 64-bit output, non-NTZ and unknown modes. The accepted U16
form remains FTZ/nearest only.
```sh
CC=clang CXX=clang++ cmake -S tests/f2i_immediate -B build-f2i \
-DREPO_ROOT="$PWD" -DSPIRV_HEADERS_ROOT=/path/to/SPIRV-Headers
cmake --build build-f2i --parallel 2
build-f2i/f2i_immediate build-f2i/modules
for module in build-f2i/modules/*.spv; do
spirv-val --target-env vulkan1.3 --uniform-buffer-standard-layout "$module"
done
```
The unmodified `232eecb` lowerer fails the first authored immediate case with
`unsupported native opcode`. The fixture tests the conversion path and its IR
controls; SPIR-V validation does not establish numerical results on a GPU or a
fix for any particular visible game symptom. In particular, it does not change
or independently certify existing register-path saturation at numeric limits.
Encoding reference: [Mesa NAK, pinned sm70_encode.rs](https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/b1fe47e94409d81924c8663f58fac97e66ab4527/src/nouveau/compiler/nak/sm70_encode.rs#2134)
(`OpF2I`, `encode_alu_base` and `encode_alu_imm`). The `0x105` base plus immediate
form 4 in bits 9–11 gives `0x905`. Inputs or outputs wider than 32 bits use a
different base, `0x111`. [NVIDIA PTX conversion documentation](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#data-movement-and-conversion-instructions-cvt)
describes rounding, FTZ, saturation and NaN results; it is not a SASS encoding
specification.

View File

@@ -0,0 +1,227 @@
// Authored instructions only. No dump, native program, GPU or game is read.
#include <array>
#include <bit>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <optional>
#include <string>
#include <vector>
#include <spirv/unified1/spirv.hpp>
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
#include "shader_recompiler/frontend/maxwell/decode.h"
#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
#include "shader_recompiler/ir_opt/passes.h"
#include "shader_recompiler/object_pool.h"
using namespace Shader;
using Native = Ampere::Instruction;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
// Mesa NAK sm70_encode.rs: OpF2I base 0x105, immediate form 4,
// source bits 32..63; sign 72, destination width 75..76, NTZ 77,
// rounding 78..79, FTZ 80, source width 84..85.
static u32 mode(bool sign, u32 rounding, bool ftz, u32 dst = 2, u32 src = 2) {
return (u32{sign} << 8) | (dst << 11) | (1U << 13) |
(rounding << 14) | (u32{ftz} << 16) | (src << 20);
}
static Native instruction(u32 op, u32 dest, u32 source, u32 flags, u32 pred = 7) {
return {{{op | (pred << 12) | (dest << 16), source, flags, 0}}};
}
static Ampere::LoweredProgram lower(Native i) {
const std::array code{i, instruction(0x94d, 0, 0, 0x03800000)};
return Ampere::LowerToMaxwell(code);
}
struct FixtureEnvironment final : Shader::Environment {
FixtureEnvironment() { stage = Stage::Compute; }
u64 ReadInstruction(u32) override { std::abort(); }
u32 ReadCbufValue(u32, u32) override { return 0; }
TextureType ReadTextureType(u32) override { return TextureType::Color2D; }
TexturePixelFormat ReadTexturePixelFormat(u32) override { return TexturePixelFormat::R8_UNORM; }
bool IsTexturePixelFormatInteger(u32) override { return false; }
u32 ReadViewportTransformState() override { return 0; }
u32 TextureBoundBuffer() const override { return 0; }
u32 LocalMemorySize() const override { return 0; }
u32 SharedMemorySize() const override { return 0; }
std::array<u32, 3> WorkgroupSize() const override { return {1, 1, 1}; }
bool HasHLEMacroState() const override { return false; }
std::optional<ReplaceConstant> GetReplaceConstBuffer(u32, u32) override { return std::nullopt; }
void Dump(u64, u64) override { std::abort(); }
};
// Dispatch the real decoder's results to the actual implementations used by
// this lowering. This fixture does not reimplement conversion arithmetic.
static void translate(Maxwell::TranslatorVisitor& visitor, u64 word) {
switch (Maxwell::Decode(word)) {
case Maxwell::Opcode::MOV32I: visitor.MOV32I(word); break;
case Maxwell::Opcode::F2I_reg: visitor.F2I_reg(word); break;
case Maxwell::Opcode::FSET_reg: visitor.FSET_reg(word); break;
case Maxwell::Opcode::FMNMX_reg: visitor.FMNMX_reg(word); break;
case Maxwell::Opcode::LOP_reg: visitor.LOP_reg(word); break;
case Maxwell::Opcode::EXIT: break;
default: CHECK(false);
}
}
static unsigned modules = 0;
static void check_pipeline(const std::filesystem::path& output, u32 literal, bool sign,
u32 rounding, bool ftz, u32 dst_width = 2) {
constexpr u32 dest = 13;
const auto lowered = lower(instruction(0x905, dest, literal, mode(sign, rounding, ftz, dst_width)));
if (!lowered) std::fprintf(stderr, "native F2I immediate: %.*s at +%u\n",
int(lowered.error.size()), lowered.error.data(), lowered.error_offset);
CHECK(lowered);
CHECK(lowered.instructions.size() > 2);
const u64 first = lowered.instructions.front();
CHECK(Maxwell::Decode(first) == Maxwell::Opcode::MOV32I);
CHECK(u32(first >> 20) == literal); // all 32 bits, including former modifier positions
const u32 scratch = first & 255;
CHECK(scratch >= 246 && scratch < 255);
bool converted = false;
for (u64 word : lowered.instructions) {
CHECK(((word >> 16) & 15) == 7);
CHECK(Maxwell::Decode(word) != Maxwell::Opcode::F2I_imm);
if (Maxwell::Decode(word) == Maxwell::Opcode::F2I_reg) {
CHECK((word & 255) == dest);
CHECK(((word >> 8) & 3) == dst_width);
CHECK(((word >> 10) & 3) == 2);
CHECK(((word >> 12) & 1) == u32(sign));
CHECK(((word >> 39) & 3) == rounding);
CHECK(((word >> 44) & 1) == u32(ftz));
CHECK(((word >> 45) & 1) == 0 && ((word >> 49) & 1) == 0);
if (sign) CHECK(((word >> 20) & 255) == scratch);
converted = true;
}
}
CHECK(converted);
ObjectPool<IR::Inst> pool;
IR::Block block{pool};
IR::Program program;
program.stage = Stage::Compute;
program.workgroup_size = {1, 1, 1};
program.shared_memory_size = 4;
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode node{};
node.type = IR::AbstractSyntaxNode::Type::Block;
node.data.block = &block;
program.syntax_list.push_back(node);
IR::AbstractSyntaxNode ret{};
ret.type = IR::AbstractSyntaxNode::Type::Return;
program.syntax_list.push_back(ret);
FixtureEnvironment env;
Maxwell::TranslatorVisitor visitor{env, block};
for (u64 word : lowered.instructions) translate(visitor, word);
visitor.ir.WriteShared(32, visitor.ir.Imm32(0), visitor.X(static_cast<IR::Reg>(dest)));
visitor.ir.Epilogue();
const IR::Opcode expected_round[] = {IR::Opcode::FPRoundEven32, IR::Opcode::FPFloor32,
IR::Opcode::FPCeil32, IR::Opcode::FPTrunc32};
bool saw_round = false, saw_convert = false, saw_nan_zero = false;
for (const IR::Inst& inst : block.Instructions()) {
if (inst.GetOpcode() == expected_round[rounding]) {
CHECK(inst.Flags<IR::FpControl>().fmz_mode == (ftz ? IR::FmzMode::FTZ : IR::FmzMode::None));
saw_round = true;
}
saw_convert |= inst.GetOpcode() == (sign ? IR::Opcode::ConvertS32F32 : IR::Opcode::ConvertU32F32);
if (inst.GetOpcode() == IR::Opcode::SelectU32 && !inst.Arg(0).IsImmediate() &&
inst.Arg(0).InstRecursive()->GetOpcode() == IR::Opcode::FPIsNan32 &&
inst.Arg(1).IsImmediate() && inst.Arg(1).U32() == 0)
saw_nan_zero = true;
}
CHECK(saw_round && saw_convert);
if (sign) CHECK(saw_nan_zero); // existing NTZ conversion explicitly selects zero for NaN
Optimization::SsaRewritePass(program);
Optimization::ConstantPropagationPass(env, program);
Optimization::DeadCodeEliminationPass(program);
Optimization::IdentityRemovalPass(program);
Optimization::CollectShaderInfoPass(env, program);
Profile profile{};
profile.supported_spirv = 0x00010600;
const auto spv = Backend::SPIRV::EmitSPIRV(profile, program);
CHECK(spv.size() > 5);
bool spirv_conversion = false, spirv_store = false;
for (std::size_t at = 5; at < spv.size();) {
const u32 count = spv[at] >> 16;
CHECK(count && at + count <= spv.size());
const auto op = static_cast<spv::Op>(spv[at] & 0xffff);
spirv_conversion |= op == (sign ? spv::Op::OpConvertFToS : spv::Op::OpConvertFToU);
spirv_store |= op == spv::Op::OpStore;
at += count;
}
CHECK(spirv_conversion && spirv_store); // result survives the production cleanup passes
const auto path = output / ("case-" + std::to_string(modules++) + ".spv");
std::ofstream stream(path, std::ios::binary);
stream.write(reinterpret_cast<const char*>(spv.data()), spv.size() * sizeof(u32));
CHECK(stream.good());
}
static void check_source_controls() {
// Existing source forms remain accepted with their original source handling.
for (u32 op : {0x305U, 0xb05U, 0xd05U}) {
const u32 source = op == 0xb05 ? 0x100U << 6 : 19U;
const u32 flags = mode(true, 3, false) | (op == 0xd05 ? 1U << 27 : 0);
const auto result = lower(instruction(op, 13, source, flags));
CHECK(result);
bool converted = false;
for (u64 word : result.instructions) converted |= Maxwell::Decode(word) == Maxwell::Opcode::F2I_reg;
CHECK(converted);
u64 hash = 14695981039346656037ULL;
for (u64 word : result.instructions) hash = (hash ^ word) * 1099511628211ULL;
std::printf("control %03x: %016llx\n", op, static_cast<unsigned long long>(hash));
}
// Existing register F16 support is preserved; adding an immediate does not
// silently add half-precision, double-precision, 64-bit output or non-NTZ modes.
CHECK(lower(instruction(0x305, 13, 19, mode(true, 3, false, 2, 1))));
}
static void check_controls() {
check_source_controls();
CHECK(!lower(instruction(0x905, 13, 0x3f800000, mode(true, 3, false, 2, 1))));
CHECK(!lower(instruction(0x905, 13, 0x3f800000, mode(true, 3, false, 2, 3))));
CHECK(!lower(instruction(0x905, 12, 0x3f800000, mode(false, 3, true, 3))));
CHECK(!lower(instruction(0x911, 12, 0x3f800000, mode(false, 3, true, 3))));
CHECK(!lower(instruction(0x905, 13, 0x3f800000, mode(true, 3, false) & ~(1U << 13))));
CHECK(!lower(instruction(0x905, 13, 0x3f800000, mode(true, 3, false) | 1U)));
auto unused_source = instruction(0x905, 13, 0x3f800000, mode(true, 3, false));
unused_source.words[0] |= 1U << 24;
CHECK(!lower(unused_source));
// The materialization and conversion keep ordinary and inverted guards.
for (bool sign : {false, true})
for (u32 pred : {0U, 10U, 7U}) {
const auto result = lower(instruction(0x905, 13, 0xc1540001, mode(sign, 3, false), pred));
CHECK(result);
for (std::size_t n = 0; n + 1 < result.instructions.size(); ++n)
CHECK(((result.instructions[n] >> 16) & 15) == pred);
}
const auto skipped = lower(instruction(0x905, 13, 0xffffffff, 0xffffffff, 15));
CHECK(skipped && skipped.instructions.size() == 1);
CHECK(Maxwell::Decode(skipped.instructions.front()) == Maxwell::Opcode::EXIT);
}
int main(int argc, char** argv) {
CHECK(argc == 2);
if (std::string(argv[1]) == "--existing-controls") {
check_source_controls();
return 0;
}
const std::filesystem::path output{argv[1]};
std::filesystem::create_directories(output);
// Bit-casts from authored finite values plus chosen zeros, subnormals,
// infinities and NaN payloads; none originate in a program dump.
const std::array literals{std::bit_cast<u32>(13.75f), std::bit_cast<u32>(-9.25f),
0U, 0x80000000U, 1U, 0x80000001U, 0x7f800000U, 0xff800000U, 0x7fc12345U, 0xffa54321U};
for (bool sign : {false, true})
for (u32 rounding = 0; rounding < 4; ++rounding)
for (bool ftz : {false, true})
check_pipeline(output, literals[(u32{sign} * 8 + rounding * 2 + u32{ftz}) % literals.size()],
sign, rounding, ftz);
for (u32 literal : literals) check_pipeline(output, literal, true, 3, true);
check_pipeline(output, std::bit_cast<u32>(32768.5f), false, 0, true, 1);
check_controls();
std::printf("PASS: %u authored native F2I immediate -> Maxwell -> IR -> SPIR-V cases; source/mode/predicate controls pass\n", modules);
}

View File

@@ -0,0 +1,170 @@
// Authored allocation/address failures, no Vulkan device or driver. The struct and
// initialization/BDA functions are included verbatim from current vk_draw.cpp.
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
using VkBuffer = uint64_t;
using VkBufferUsageFlags = uint32_t;
struct Allocation { unsigned id = 0; };
using VmaAllocation = Allocation*;
constexpr VkBuffer VK_NULL_HANDLE = 0;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_STORAGE_BUFFER_BIT = 1;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_TRANSFER_DST_BIT = 2;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT = 4;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT = 8;
constexpr int VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO = 1;
struct VkBufferDeviceAddressInfo { int sType; VkBuffer buffer = 0; };
struct { int device = 1; } dev;
namespace Shader::Backend::SPIRV {
constexpr uint64_t MESH_FALLBACK_CAPACITY = 5, MESH_FALLBACK_HEADER_WORDS = 8, MESH_FALLBACK_RECORD_WORDS = 16;
}
struct Program {
uint32_t fallback_vertices_per_workgroup = 3;
uint32_t fallback_record_words = 4;
uint32_t fallback_payload_words = 2;
uint32_t fallback_task_threads = 2;
bool fallback_task = true;
};
static std::array<Allocation, 16> allocations;
static Allocation poison;
static std::unordered_map<VkBuffer, VmaAllocation> live;
static unsigned create_calls, destroy_calls, address_calls;
static unsigned fail_create, zero_address;
static void require(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
static bool create_buffer(uint64_t size, VkBufferUsageFlags, bool,
VkBuffer& buffer, VmaAllocation& allocation, void**) {
require(size != 0, "fixture received a zero allocation size");
++create_calls;
if (create_calls == fail_create) {
// An output written on failure must never be treated as a confirmed allocation.
buffer = 0xffff;
allocation = &poison;
return false;
}
require(create_calls < allocations.size(), "fixture allocation limit");
buffer = create_calls;
allocation = &allocations[create_calls];
allocation->id = create_calls;
require(live.emplace(buffer, allocation).second, "duplicate mock handle");
return true;
}
[[maybe_unused]] static void destroy_buffer(VkBuffer buffer, VmaAllocation allocation) {
const auto it = live.find(buffer);
require(it != live.end() && it->second == allocation,
"destroyed an unconfirmed or already-destroyed allocation");
++destroy_calls;
live.erase(it);
}
static uint64_t vkGetBufferDeviceAddress(int device, const VkBufferDeviceAddressInfo* info) {
require(device == 1 && info->sType == VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
"invalid authored BDA request");
require(live.contains(info->buffer), "queried an unconfirmed allocation");
return ++address_calls == zero_address ? 0 : info->buffer * 4096;
}
#include "fallback_actual.inc"
// (the buffers are a recording thread's, not a program's (#1383): a failure is not remembered, the next draw tries again, and
// with the injected failure spent that try succeeds; an invalid program still allocates nothing)
static void no_retry(const Program& p, FallbackBuffers*& out) {
const auto calls = create_calls;
const bool ok = fallback_buffers_of(p, out);
if (!p.fallback_record_words || !p.fallback_vertices_per_workgroup)
require(!ok && create_calls == calls, "an invalid program allocated on a retry");
else
require(ok && out && out->records_address, "a retry after a failure did not recover");
}
static void cleaned(const Program& p, unsigned expected_destroyed) {
require(live.empty(), "failed initialization retained a successfully created buffer");
require(destroy_calls == expected_destroyed, "wrong confirmed-allocation cleanup count");
(void)p;
require(fallback_buffers().empty(), "a failed pair was kept for later draws");
}
static void success_reuse(bool task) {
Program p;
p.fallback_task = task;
FallbackBuffers* out = nullptr;
require(fallback_buffers_of(p, out), "valid initialization failed");
require(out && out->records_address, "successful result incomplete");
require(out->records_bytes == 96, "expected a fixed record arena with an indirect command header");
require(live.size() == (task ? 2u : 1u) && destroy_calls == 0,
"successful allocations were destroyed");
if (task) {
require(out->payload_address != 0, "successful payload address missing");
require(out->payload_bytes == 44, "expected a slot map and a payload for each task group, including a partial last group");
}
const auto calls = create_calls;
auto* same = out;
require(fallback_buffers_of(p, out) && out == same && create_calls == calls,
"successful reuse changed");
}
static void run(const std::string& name) {
Program p;
FallbackBuffers* out = nullptr;
if (name == "initial_allocation_failure") {
fail_create = 1;
require(!fallback_buffers_of(p, out), "first allocation failure was accepted");
require(!out && live.empty() && destroy_calls == 0, "unconfirmed first output was destroyed");
no_retry(p, out);
} else if (name == "payload_allocation_failure") {
fail_create = 2;
require(!fallback_buffers_of(p, out), "payload allocation failure was accepted");
require(!out, "failed result was published");
cleaned(p, 1);
no_retry(p, out);
} else if (name == "records_address_zero") {
zero_address = 1;
require(!fallback_buffers_of(p, out), "zero records address was accepted");
require(!out, "zero-address result was published");
cleaned(p, 1); // (the payload is not made once the records have no address)
no_retry(p, out);
} else if (name == "records_only_address_zero") {
p.fallback_task = false;
zero_address = 1;
require(!fallback_buffers_of(p, out), "zero records-only address was accepted");
cleaned(p, 1);
no_retry(p, out);
} else if (name == "payload_address_zero") {
zero_address = 2;
require(!fallback_buffers_of(p, out), "zero required payload address was accepted");
require(!out, "zero-payload result was published");
cleaned(p, 2);
no_retry(p, out);
} else if (name == "records_only_success") {
success_reuse(false);
} else if (name == "task_success") {
success_reuse(true);
} else if (name == "invalid_program") {
p.fallback_record_words = 0;
require(!fallback_buffers_of(p, out), "invalid program was accepted");
require(!out && create_calls == 0 && destroy_calls == 0, "invalid program allocated resources");
no_retry(p, out);
} else if (name == "program_isolation") {
fail_create = 2;
require(!fallback_buffers_of(p, out), "first program payload failure was accepted");
cleaned(p, 1);
Program next;
require(fallback_buffers_of(next, out) && out && out->records_address, "another program inherited failure");
require(live.size() == 2 && destroy_calls == 1, "another program's buffers were freed or leaked");
} else throw std::runtime_error("unknown authored case");
}
int main(int argc, char** argv) {
if (argc != 2) return 2;
try {
run(argv[1]);
std::printf("PASS %s\n", argv[1]);
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "FAIL %s: %s\n", argv[1], e.what());
return 1;
}
}

View File

@@ -0,0 +1,98 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
using Pixels = std::shared_ptr<std::vector<uint8_t>>;
static void require(bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", why); std::exit(1); }
}
static Pixels sample(Pixels px, uint32_t kShotW, uint32_t kShotH) {
/* SAMPLING */
return small;
}
struct Detection { bool event; int hits, up, peak, region_hits, region_peak; };
static Detection detect(Pixels sa, Pixels sb, Pixels small) {
constexpr uint32_t W = 64, H = 36;
int flashes_left = 1;
/* DETECTOR */
return {/* DECISION */, hits, up, peak, region_hits, region_peak};
}
static Pixels solid(uint32_t w, uint32_t h, std::array<uint8_t, 3> rgb) {
auto p = std::make_shared<std::vector<uint8_t>>((size_t)w * h * 4, 19);
for (size_t i = 0; i < (size_t)w * h; i++)
for (int ch = 0; ch < 3; ch++) (*p)[i * 4 + ch] = rgb[ch];
return p;
}
static void constants() {
const uint32_t sizes[][2] = {{1280,720},{1920,1080},{3440,1440},{5120,1440},{3840,2160},
{1439,701},{65,37},{64,36},{63,35},{1,1},{17,1440},{3440,9}};
const std::array<uint8_t, 3> colors[] = {{0,0,0},{64,64,64},{128,128,128},{200,200,200},
{254,254,254},{255,255,255},{17,73,211}};
for (auto& size : sizes) for (auto rgb : colors) {
auto result = sample(solid(size[0], size[1], rgb), size[0], size[1]);
require(result->size() == 64 * 36 * 3, "the fixed detector grid size");
for (size_t i = 0; i < result->size(); i++)
require((*result)[i] == rgb[i % 3], "a constant channel must keep its brightness at every capture size");
}
}
static void partitions() {
const uint32_t sizes[][2] = {{3440,1440},{1439,701},{65,37},{5120,1440}};
for (auto& size : sizes) {
uint32_t w = size[0], h = size[1];
auto p = solid(w, h, {0,0,0});
// Label each source pixel by the normalized cell owning it. Every sample
// inside that cell must return its label, including remainder rows/columns.
for (uint32_t y = 0; y < h; y++) for (uint32_t x = 0; x < w; x++) {
const unsigned cx = ((uint64_t)(x + 1) * 64 + w - 1) / w - 1;
const unsigned cy = ((uint64_t)(y + 1) * 36 + h - 1) / h - 1;
(*p)[((size_t)y*w+x)*4] = (uint8_t)(cx*3);
(*p)[((size_t)y*w+x)*4+1] = (uint8_t)(cy*7);
(*p)[((size_t)y*w+x)*4+2] = (uint8_t)((cx+cy)*2);
}
auto result = sample(p,w,h);
for (unsigned y=0;y<36;y++) for(unsigned x=0;x<64;x++) {
const size_t at = (y*64+x)*3;
require((*result)[at] == x*3 && (*result)[at+1] == y*7 && (*result)[at+2] == (x+y)*2,
"normalized cells must retain their own source region through the image edges");
}
}
}
static void downstream() {
for (const auto size : {std::array<uint32_t,2>{1280,720},{1920,1080},{3440,1440},{5120,1440},{65,37}}) {
const auto w=size[0],h=size[1];
auto a=sample(solid(w,h,{200,200,200}),w,h);
auto b=sample(solid(w,h,{255,255,255}),w,h);
auto pulse=detect(a,b,a);
require(pulse.event && pulse.hits==64*36 && pulse.up==64*36 && pulse.peak==55 && pulse.region_peak==55,
"the production detector must preserve the polarity and magnitude of a bright middle-frame pulse");
require(!detect(a,a,a).event,"a stable picture is not a flash");
auto low=sample(solid(w,h,{210,210,210}),w,h), high=sample(solid(w,h,{250,250,250}),w,h);
require(!detect(low,high,low).event,"a below-threshold pulse must not become a flash through byte wrapping");
Pixels edges[3];
for(unsigned frame=0;frame<3;frame++) {
auto p=solid(w,h,{0,0,0});
for(uint32_t y=0;y<h;y++) for(uint32_t x=0;x<w*(frame+1)/4;x++)
for(unsigned ch=0;ch<3;ch++) (*p)[((size_t)y*w+x)*4+ch]=255;
edges[frame]=sample(p,w,h);
}
require(!detect(edges[0],edges[1],edges[2]).event,"an edge moving in one direction is not a returning one-frame flash");
}
}
int main(int argc,char**argv) {
#ifdef _WIN32
_putenv_s("RX_VK_FLASH_REGION","48");
#else
setenv("RX_VK_FLASH_REGION","48",1);
#endif
std::string which=argc>1?argv[1]:"all";
if(which=="all"||which=="constant") constants();
if(which=="all"||which=="partition") partitions();
if(which=="all"||which=="detector") downstream();
std::puts("PASS: production flash sampling and cell/region detection preserve brightness, partitions and controls");
}

262
tests/fpx_sticky_test.c Normal file
View File

@@ -0,0 +1,262 @@
/* tests/test_fpx_sticky.py's driver: the generated FPX1 wrappers a64_fpx_mul32_fma32 (FMUL/FNMUL S) and
a64_fpx_fma32_fma32 (FMLA family S) against the exact body they fall back to (recomp_fpe_fma32), value and FPSR.
Built against the real a64.h with the a64_suyu.h / a64_suyu.c / a64_fpx.h the generators write, at -march=x86-64-v3
(-DEXPECT_FMA=1: a hardware FMA, both ops rescued) or -march=x86-64-v2 (-DEXPECT_FMA=0: only FMUL is; FMLA's rescue
must decline every case). The rescues are counted inside the generated wrappers (A64_FPX_STICKY_PROBE, empty in the
real build), and one under a closed gate (an FPCR control bit but FZ, or IXC clear) counts as a mismatch: the exact
body models none of FIZ/AH/NEP, so only the gate keeps the rescue away from them.
fpx_sticky_test lifted guest FMUL/FNMUL/FMADD/FMLA S words as the lifter emits them (fpx_lifted.h: the
outlined and the hot-function form) against the same words with FPX off, on
inputs that must be rescued and inputs that must fall back: whole Ctx and the
rescue count
fpx_sticky_test random <n> <seed> n random operand triples (both ops each) over random FPCR/FPSR states
fpx_sticky_test sweep <stride> every a with biased exponent <= 4 or >= 250 whose fraction is below 2^12, above
2^23 - 2^12 or a multiple of stride, x a fixed set of b and addends, x FZ on/off,
x flags all set / IXC only, and one more state per a (partial flags, IXC
clear, FIZ/AH/NEP)
Exit 0: no mismatch, the wrappers rescued FMUL cases, and FMLA ones exactly when EXPECT_FMA. 1: a mismatch.
3: the CPU lacks this build's -march (x86-64-v3: FMA/AVX2; x86-64-v2: SSSE3/SSE4.2/POPCNT). 4: rescue counts wrong. */
static unsigned long long g_sticky; /* the generated wrappers' rescues */
#define A64_FPX_STICKY_PROBE() ((void)g_sticky++)
#include "a64.h"
#include <stdio.h>
#include <stdlib.h>
#include <cpuid.h>
#if !defined(EXPECT_FMA) || RECOMP_FPX_HOST != 2 || RECOMP_FPX_FMA64 != EXPECT_FMA
#error "build with -DEXPECT_FMA=1 at x86-64-v3 or -DEXPECT_FMA=0 at x86-64-v2 (RECOMP_FPX_FMA64 must agree)"
#endif
/* The FPCR bits the rescue's soundness needs clear (the test's own copy of RECOMP_FPX_FPCR_MASK): every control bit
but FZ (FIZ AH NEP, trap enables, FZ16, RMode, DN, AHP) and the host's kill switch. */
#define GATE_FPCR (UINT64_C(0x06C8FF07) | (UINT64_C(1) << 32))
#include "fpx_lifted.h"
static uint64_t rs;
static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; }
static uint32_t r32(void) { return (uint32_t)(rnd() >> 16); }
static uint32_t rbelow(uint32_t n) { return (uint32_t)(((rnd() >> 32) * n) >> 32); }
static uint32_t mkf(uint32_t sign, int e, uint32_t frac) { return (sign << 31) | ((uint32_t)e << 23) | (frac & 0x7fffff); }
static float F(uint32_t x) { float f; memcpy(&f, &x, 4); return f; }
static uint32_t B(float f) { uint32_t x; memcpy(&x, &f, 4); return x; }
/* ---- operand classes and states (fpstub's harness) ---- */
static uint32_t sparse_frac(void) { /* fractions with few bits: exact products, ties, cancellations */
switch (rbelow(4)) {
case 0: return 0;
case 1: return (uint32_t)1 << rbelow(23);
case 2: return r32() & 0x7ff000;
default: return r32() & 0x7fffff;
}
}
static uint32_t any_operand(void) {
const uint32_t s = r32() & 1;
switch (rbelow(16)) {
case 0: return s << 31; /* zero */
case 1: return mkf(s, 0, r32() | 1); /* subnormal */
case 2: return mkf(s, 0, rbelow(8) + 1); /* tiny subnormal */
case 3: return mkf(s, 0, 0x7fffff - rbelow(8)); /* large subnormal */
case 4: return mkf(s, 1 + rbelow(3), sparse_frac()); /* smallest normals */
case 5: return mkf(s, 254 - rbelow(3), sparse_frac()); /* largest normals */
case 6: return mkf(s, 255, 0); /* inf */
case 7: return mkf(s, 255, (r32() | 1) & 0x7fffff); /* NaN, quiet or signalling */
case 8: return mkf(s, 255, 0x400000 | (r32() & 0x3fffff)); /* quiet NaN */
case 9: case 10: return mkf(s, 120 + rbelow(15), sparse_frac()); /* near 1 */
default: return mkf(s, 1 + rbelow(254), r32()); /* any normal */
}
}
static void tiny_pair(uint32_t *a, uint32_t *b) { /* a product near the bottom of the normal range, or the overflow edge */
const int target = (rbelow(8) == 0) ? 255 - (int)rbelow(3) : (int)rbelow(60) - 30; /* biased product exponent */
int ea = 1 + (int)rbelow(253);
int eb = target - ea + 127;
if (eb < 1 || eb > 254) { eb = 1 + (int)rbelow(254); ea = target - eb + 127; }
if (ea < 1) ea = rbelow(4) ? 1 : 0;
if (ea > 254) ea = 254;
*a = mkf(r32() & 1, ea, rbelow(2) ? sparse_frac() : r32());
*b = mkf(r32() & 1, eb, rbelow(2) ? sparse_frac() : r32());
}
static uint32_t addend_for(uint32_t a, uint32_t b) {
const double p = (double)F(a) * (double)F(b);
switch (rbelow(10)) {
case 0: return any_operand();
case 1: return B((float)-p); /* near (often exact) cancellation */
case 2: return B((float)-p) + (uint32_t)((int)rbelow(9) - 4); /* a few ulps off */
case 3: return r32() & 0x80000000u; /* a zero */
case 4: return B((float)p) ^ (rbelow(2) ? 0x80000000u : 0);
case 5: return mkf(r32() & 1, 0, r32()); /* subnormal */
case 6: return mkf(r32() & 1, 1 + rbelow(4), r32()); /* near the smallest normal */
default: return mkf(r32() & 1, 1 + rbelow(254), r32());
}
}
static void gen_state(uint64_t *fpcr, uint64_t *fpsr) {
uint64_t cr = 0, sr = 0;
if (rbelow(2)) cr |= 1u << 24; /* FZ */
if (rbelow(10) == 0) cr |= 1u << 25; /* DN (gate closed) */
if (rbelow(10) == 0) cr |= (uint64_t)rbelow(4) << 22; /* RMode */
if (rbelow(40) == 0) cr |= r32() & 0x07ff9f07u; /* anything else (FIZ, AH, NEP included) */
if (rbelow(16) == 0) cr |= 1u << rbelow(3); /* FIZ, AH or NEP alone (the gate must stay closed) */
if (rbelow(200) == 0) cr |= RECOMP_FPX_INHIBIT;
static const uint32_t bits[6] = {1, 2, 4, 8, 16, 128}; /* IOC DZC OFC UFC IXC IDC */
for (int k = 0; k < 6; k++)
if (k == 4 ? rbelow(10) != 0 : rbelow(3) != 0) sr |= bits[k]; /* IXC 90%, the others 67% */
if (rbelow(8) == 0) sr |= 1u << 27; /* QC */
*fpcr = cr;
*fpsr = sr;
}
/* ---- one check: both wrappers against the exact body ---- */
static uint64_t checks, mismatches, rescued[2], closed_checks;
static int report_left = 20;
static void check(uint64_t cr, uint64_t sr, uint32_t a, uint32_t b, uint32_t z, int mul) {
Ctx ref, got;
memset(&ref, 0, sizeof ref);
ref.fpcr = cr; ref.fpsr = sr;
got = ref;
const uint64_t r = recomp_fpe_fma32(&ref, a, b, z);
const unsigned long long before = g_sticky;
const uint64_t v = mul ? a64_fpx_mul32_fma32(&got, a, b, z) : a64_fpx_fma32_fma32(&got, a, b, z);
const int kept = g_sticky != before, closed = (cr & GATE_FPCR) || !(sr & 16u);
checks++;
rescued[mul] += kept;
closed_checks += closed;
if (kept && closed) {
mismatches++;
if (report_left-- > 0)
printf("GATE %s a=%08x b=%08x z=%08x fpcr=%llx fpsr=%llx: rescued under a closed gate\n", mul ? "FMUL" : "FMLA",
a, b, z, (unsigned long long)cr, (unsigned long long)sr);
} else if (v != r || got.fpsr != ref.fpsr) {
mismatches++;
if (report_left-- > 0)
printf("MISMATCH %s a=%08x b=%08x z=%08x fpcr=%llx fpsr=%llx: exact %08llx/%llx wrapper %08llx/%llx\n",
mul ? "FMUL" : "FMLA", a, b, z, (unsigned long long)cr, (unsigned long long)sr, (unsigned long long)r,
(unsigned long long)ref.fpsr, (unsigned long long)v, (unsigned long long)got.fpsr);
}
}
static void both(uint64_t cr, uint64_t sr, uint32_t a, uint32_t b, uint32_t z) {
check(cr, sr, a, b, (a ^ b) & 0x80000000u, 1); /* FMUL: the lifter passes a zero of the product's sign */
check(cr, sr, a, b, z, 0);
}
static void random_cases(uint64_t n, uint64_t seed) {
rs = seed * 0x9E3779B97F4A7C15ULL + 1;
for (uint64_t i = 0; i < n; i++) {
uint32_t a, b; uint64_t cr, sr;
if (rbelow(2)) tiny_pair(&a, &b);
else { a = any_operand(); b = any_operand(); }
const uint32_t z = addend_for(a, b);
gen_state(&cr, &sr);
both(cr, sr, a, b, z);
}
}
static const uint32_t B_SET[] = {
0x3f800000, /* 1 */ 0x3f000000, /* 0.5 */ 0x3fc00001, /* 1.5+ulp */ 0x00800000, /* min normal */ 0x7f7fffff, /* max */
0x80000001, /* -min subnormal */ 0x007fffff, /* max subnormal */ 0x7f800000, /* inf */ 0x00000000, /* +0 */
0x5f000000, /* 2^63 */ 0x3fffffff, /* ~2 */ 0x1f800000 /* 2^-64 */};
/* one more state per a: partial flags (each needed bit clear in turn), IXC clear, and FIZ / AH / NEP (gate closed) */
static const uint32_t MORE[16][2] = {
{0x01000000u, 0x18u}, {0, 0x18u}, {0x01000000u, 0x90u}, {0x01000000u, 0x94u}, {0, 0x14u}, {0x01000000u, 0x98u},
{0x01000000u, 0x1cu}, {0, 0x1cu}, {0x01000000u, 0x9bu}, {0x01000000u, 0x97u}, {0x01000000u, 0x1fu},
{0x01000000u, 0x8fu}, {0x01000002u, 0x9fu}, {0x01000001u, 0x9fu}, {0x01000004u, 0x9fu}, {0x00000002u, 0x9fu}};
static void sweep(uint32_t stride) {
for (unsigned bi = 0; bi < sizeof B_SET / sizeof B_SET[0]; bi++) {
const uint32_t b = B_SET[bi];
for (uint32_t hi = 0; hi < 2 * 256; hi++) { /* sign and biased exponent of a */
const uint32_t e = hi & 255;
if (!(e <= 4 || e >= 250)) continue;
for (uint32_t f = 0; f < (1u << 23); f++) {
if (f >= 4096 && f < (1u << 23) - 4096 && f % stride) continue;
const uint32_t a = (hi >> 8) << 31 | e << 23 | f;
const uint64_t cr = (f & 1) ? 0x01000000u : 0; /* FZ */
const uint64_t sr = (f & 2) ? 0x9fu : 0x10u; /* every sticky bit set, or IXC only */
uint32_t z;
switch ((f >> 2) & 3) { /* addend: cancellation, near the smallest normal, subnormal, zero */
case 0: z = B(-(float)((double)F(a) * (double)F(b))); break;
case 1: z = 0x00800000u ^ (a & 0x80000000u); break;
case 2: z = 0x00000003u ^ (f << 31); break;
default: z = 0x80000000u; break;
}
both(cr, sr, a, b, z);
both(cr ^ 0x01000000u, sr ^ 0x8fu, a, b, z); /* the other FZ and flag state, same operands */
const uint32_t *m = MORE[((f >> 4) + hi + bi) & 15];
both(m[0], m[1], a, b, z);
}
}
}
}
/* ---- the lifted route: guest words through a64emit and a64_lift.op_text ---- */
#define FZ 0x01000000u
static const struct { uint32_t a, b, z; uint32_t fpcr, fpsr; int rescue; const char *what; } LIFTED[] = {
{0x1c800000u, 0x1c800000u, 0, FZ, 0x9fu, 1, "tiny product, FZ, flags set: a zero"},
{0x1c800000u, 0x9c800000u, 0x80000000u, FZ, 0x9fu, 1, "tiny negative product, FZ, flags set: -0"},
{0x1c800000u, 0x1e000000u, 0, 0, 0x9fu, 1, "tiny product, no FZ, UFC set: the subnormal"},
{0x71800000u, 0x71800000u, 0x3f800000u, FZ, 0x9fu, 1, "overflow, OFC set: inf"},
{0x00000005u, 0x3fc00000u, 0x3f800000u, FZ, 0x9fu, 1, "subnormal operand, FZ, IDC set: flushed"},
{0x1c800000u, 0x1c800000u, 0, FZ, 0x10u, 0, "tiny product, FZ, IXC only: falls back"},
{0x1c800000u, 0x1e000000u, 0, 0, 0x97u, 0, "tiny product, no FZ, UFC clear: falls back"},
{0x71800000u, 0x71800000u, 0x3f800000u, FZ, 0x9bu, 0, "overflow, OFC clear: falls back"},
{0x00000005u, 0x3fc00000u, 0x3f800000u, FZ, 0x1fu, 0, "subnormal operand, FZ, IDC clear: falls back"},
{0x7fc00001u, 0x3f800000u, 0x3f800000u, FZ, 0x9fu, 0, "NaN operand: falls back"},
{0x1c800000u, 0x1c800000u, 0, FZ | 2u, 0x9fu, 0, "tiny product, FZ|AH (gate closed): falls back"},
{0x3fc00000u, 0x40000000u, 0x3e800000u, FZ, 0x9fu, 0, "normal (the fast path keeps it)"},
};
static int lifted_cases(void) {
unsigned bad = 0, n = 0, kept = 0;
for (unsigned g = 0; g < sizeof GUEST / sizeof GUEST[0]; g++)
for (unsigned k = 0; k < sizeof LIFTED / sizeof LIFTED[0]; k++)
for (int hot = 0; hot < 2; hot++) {
Ctx ref, got;
memset(&ref, 0, sizeof ref);
for (int r = 0; r < 32; r++) { ref.vreg[r][0] = 0x0123456789abcdefULL * (r + 1); ref.vreg[r][1] = ~ref.vreg[r][0]; }
ref.vreg[1][0] = (ref.vreg[1][0] & ~0xffffffffULL) | LIFTED[k].a;
ref.vreg[2][0] = (ref.vreg[2][0] & ~0xffffffffULL) | LIFTED[k].b;
ref.vreg[3][0] = (ref.vreg[3][0] & ~0xffffffffULL) | LIFTED[k].z; /* FMADD's addend */
ref.vreg[0][0] = (ref.vreg[0][0] & ~0xffffffffULL) | LIFTED[k].z; /* FMLA's accumulator */
ref.fpcr = LIFTED[k].fpcr;
ref.fpsr = LIFTED[k].fpsr;
got = ref;
GUEST[g].exact(&ref);
const unsigned long long before = g_sticky;
(hot ? GUEST[g].hot : GUEST[g].lifted)(&got);
const unsigned long long rescued = g_sticky - before;
const unsigned want = LIFTED[k].rescue && (GUEST[g].mul || EXPECT_FMA);
n++;
kept += (unsigned)rescued;
if (rescued != want || memcmp(&ref, &got, sizeof ref)) {
bad++;
printf("LIFTED %s (%s), %s: rescued %llu, want %u; s0 exact %08x/fpsr %llx, lifted %08x/fpsr %llx%s\n",
GUEST[g].name, hot ? "hot" : "outlined", LIFTED[k].what, rescued, want, (uint32_t)ref.vreg[0][0],
(unsigned long long)ref.fpsr, (uint32_t)got.vreg[0][0], (unsigned long long)got.fpsr,
memcmp(&ref, &got, sizeof ref) ? " (Ctx differs)" : "");
}
}
printf("lifted (%s): %u runs of %u guest words, %u mismatches, %u rescued in the wrappers\n",
EXPECT_FMA ? "x86-64-v3" : "x86-64-v2", n, (unsigned)(sizeof GUEST / sizeof GUEST[0]), bad, kept);
return bad ? 1 : 0;
}
int main(int argc, char **argv) {
unsigned eax, ebx, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx) || !(ecx >> 9 & 1) || !(ecx >> 20 & 1) || !(ecx >> 23 & 1)) {
printf("this CPU has no SSSE3/SSE4.2/POPCNT (x86-64-v2)\n");
return 3;
}
if (EXPECT_FMA && (!(ecx >> 12 & 1) || !__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx) || !(ebx >> 5 & 1))) {
printf("this CPU has no FMA/AVX2 (x86-64-v3)\n");
return 3;
}
if ((_mm_getcsr() & 0xffc0) != 0x1f80) { printf("MXCSR %x: not the FPX1 host contract\n", _mm_getcsr()); return 2; }
if (argc == 2 && !strcmp(argv[1], "lifted")) return lifted_cases();
if (argc == 4 && !strcmp(argv[1], "random")) random_cases(strtoull(argv[2], 0, 0), strtoull(argv[3], 0, 0));
else if (argc == 3 && !strcmp(argv[1], "sweep")) sweep((uint32_t)strtoul(argv[2], 0, 0));
else { fprintf(stderr, "usage: lifted | random <n> <seed> | sweep <stride>\n"); return 2; }
printf("%s (%s): %llu checks (%llu with the gate closed), %llu mismatches, the wrappers rescued %llu FMUL / %llu FMLA\n",
argv[1], EXPECT_FMA ? "x86-64-v3" : "x86-64-v2", (unsigned long long)checks, (unsigned long long)closed_checks,
(unsigned long long)mismatches, (unsigned long long)rescued[1], (unsigned long long)rescued[0]);
if (mismatches) return 1;
if (!rescued[1] || (EXPECT_FMA ? !rescued[0] : rescued[0] != 0)) {
printf("rescue counts wrong: want FMUL > 0 and FMLA %s\n", EXPECT_FMA ? "> 0" : "== 0 (no hardware FMA)");
return 4;
}
return 0;
}

View File

@@ -0,0 +1,92 @@
// Actual replay parsing and ring block, with only a pre-index range/setting observer
// inserted by the Python driver. No Vulkan, GPU, timing waits or game data.
#include <algorithm>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <vector>
static int expected_ahead;
static unsigned observations;
static std::vector<uint64_t> waited;
static std::vector<std::string> logs;
[[maybe_unused]] static void logf(const char* format, ...) {
char text[256];
va_list args;
va_start(args, format);
vsnprintf(text, sizeof text, format, args);
va_end(args);
logs.emplace_back(text);
}
static void require(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
static void check_ahead(int ahead, std::size_t capacity) {
++observations;
require(ahead <= 0 || static_cast<std::size_t>(ahead) <= capacity,
"positive frame wait exceeds ring capacity before indexing");
require((ahead > 0 ? ahead : 0) == expected_ahead, "unexpected effective parsed setting");
}
static void wait_value(uint64_t value) { waited.push_back(value); }
static void replay_frame(uint64_t presents, uint64_t value) {
#include "frame_wait_actual.inc"
}
static void setting(const char* value) {
#ifdef _WIN32
require(_putenv_s("RX_VK_WAIT_GPU", value ? value : "") == 0, "authored child environment failed");
#else
require((value ? setenv("RX_VK_WAIT_GPU", value, 1) : unsetenv("RX_VK_WAIT_GPU")) == 0,
"authored child environment failed");
#endif
}
int main(int argc, char** argv) {
const std::string mode = argc == 2 ? argv[1] : "";
try {
const char* input = nullptr;
if (mode == "unset") expected_ahead = 0;
else if (mode == "empty") input = "", expected_ahead = 0;
else if (mode == "zero") input = "0", expected_ahead = 0;
else if (mode == "negative") input = "-7", expected_ahead = 0;
else if (mode == "one") input = "1", expected_ahead = 1;
else if (mode == "two") input = "2", expected_ahead = 2;
else if (mode == "three") input = "3", expected_ahead = 3;
else if (mode == "four") input = "4", expected_ahead = 4;
else if (mode == "five") input = "5", expected_ahead = 5;
else if (mode == "six") input = "6", expected_ahead = 6;
else if (mode == "seven") input = "7", expected_ahead = 7;
else if (mode == "eight") input = "8", expected_ahead = 8;
else if (mode == "nine") input = "9", expected_ahead = 8;
else if (mode == "prefix") input = " \t+3frames", expected_ahead = 3;
else if (mode == "malformed") input = "frames", expected_ahead = 0;
else if (mode == "int_max") input = "2147483647", expected_ahead = 8;
else if (mode == "positive_overflow") input = "999999999999999999999999999999999999999999999999999999999999", expected_ahead = 8;
else if (mode == "negative_overflow") input = "-999999999999999999999999999999999999999999999999999999999999", expected_ahead = 0;
else throw std::runtime_error("unknown fixture case");
setting(input);
for (uint64_t frame = 0; frame < 40; ++frame) {
// A later environment change must not reinitialize the production ring policy.
if (frame == 5) setting(expected_ahead ? "0" : "8");
const auto before = waited.size();
replay_frame(frame, 101 + frame);
const bool should_wait = expected_ahead > 0 && frame >= static_cast<unsigned>(expected_ahead);
require(waited.size() == before + (should_wait ? 1 : 0), "changed wait count");
if (should_wait)
require(waited.back() == 101 + frame - expected_ahead, "changed ring wait ordering");
}
require(observations == 40, "pre-index observer did not see every frame");
const bool clamped = mode == "nine" || mode == "int_max" || mode == "positive_overflow";
require(logs.size() == (clamped ? 1u : 0u), "clamped setting diagnostic must appear once, valid settings stay quiet");
if (clamped)
require(logs[0].find("RX_VK_WAIT_GPU=") != std::string::npos && logs[0].find("using 8") != std::string::npos,
"clamped setting diagnostic omits the option or effective ring capacity");
std::printf("PASS %s\n", mode.c_str());
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "FAIL %s: %s\n", mode.c_str(), e.what());
return 1;
}
}

View File

@@ -0,0 +1,130 @@
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#define VK_NO_PROTOTYPES
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <initializer_list>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#define CHECK(c) do { if (!(c)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); std::exit(1); } } while (0)
namespace fixture {
using VmaAllocation = void*;
constexpr int kMaxMult = 8;
struct Device { VkDevice device{}; bool push_descriptor = true, offscreen = false; std::mutex present_mu; } dev;
struct Options { int framegen = 2; } opts;
auto vkCmdPushDescriptorSetKHR = reinterpret_cast<PFN_vkCmdPushDescriptorSetKHR>(uintptr_t(1));
constexpr uint32_t FG_COPY_COMP_SPV[] = {0}, FG_LUMA_COMP_SPV[] = {0}, FG_FLOW_COMP_SPV[] = {0},
FG_MEDIAN_COMP_SPV[] = {0}, FG_SYNTH_COMP_SPV[] = {0}, FG_SCALE_COMP_SPV[] = {0};
/* ACTUAL_STRUCTS */
int multiplier() { return opts.framegen; }
int display_refresh_mhz() { return 60000; }
void logf(const char*, ...) {}
struct Object { char kind; uint64_t parent; };
std::map<uint64_t, Object> live;
uint64_t serial = 0;
int attempts = 0, fail_at = 0, barriers = 0, waits = 0;
bool idle = true, submitted = false;
bool fails() { return ++attempts == fail_at; }
uint64_t allocate(char kind, uint64_t parent = 0) { live[++serial] = {kind, parent}; return serial; }
void release(uint64_t handle, char kind) {
if (!handle) return;
CHECK(live.count(handle) && live.at(handle).kind == kind);
CHECK(!submitted || idle);
for (const auto& [id, obj] : live) { (void)id; CHECK(obj.parent != handle); }
live.erase(handle);
}
VkResult vkCreateDescriptorSetLayout(VkDevice, const VkDescriptorSetLayoutCreateInfo*, const VkAllocationCallbacks*, VkDescriptorSetLayout* p) {
*p = 0; if (fails()) return VK_ERROR_OUT_OF_DEVICE_MEMORY; *p = allocate('D'); return VK_SUCCESS;
}
VkResult vkCreatePipelineLayout(VkDevice, const VkPipelineLayoutCreateInfo* info, const VkAllocationCallbacks*, VkPipelineLayout* p) {
*p = 0; if (fails()) return VK_ERROR_OUT_OF_DEVICE_MEMORY; *p = allocate('L', *info->pSetLayouts); return VK_SUCCESS;
}
VkResult vkCreateShaderModule(VkDevice, const VkShaderModuleCreateInfo*, const VkAllocationCallbacks*, VkShaderModule* p) {
*p = 0; if (fails()) return VK_ERROR_OUT_OF_DEVICE_MEMORY; *p = allocate('M'); return VK_SUCCESS;
}
VkResult vkCreateComputePipelines(VkDevice, VkPipelineCache, uint32_t count, const VkComputePipelineCreateInfo* info,
const VkAllocationCallbacks*, VkPipeline* p) {
CHECK(count == 1); const bool fail = fails();
// Vulkan may return owned pipeline handles together with an error: cleanup must inspect the output.
*p = allocate('P', info->layout); return fail ? VK_ERROR_OUT_OF_DEVICE_MEMORY : VK_SUCCESS;
}
void vkDestroyPipeline(VkDevice, VkPipeline h, const VkAllocationCallbacks*) { release(h, 'P'); }
void vkDestroyPipelineLayout(VkDevice, VkPipelineLayout h, const VkAllocationCallbacks*) { release(h, 'L'); }
void vkDestroyDescriptorSetLayout(VkDevice, VkDescriptorSetLayout h, const VkAllocationCallbacks*) { release(h, 'D'); }
void vkDestroyShaderModule(VkDevice, VkShaderModule h, const VkAllocationCallbacks*) { release(h, 'M'); }
VkResult vkCreateSampler(VkDevice, const VkSamplerCreateInfo*, const VkAllocationCallbacks*, VkSampler* p) {
*p = 0; if (fails()) return VK_ERROR_OUT_OF_DEVICE_MEMORY; *p = allocate('S'); return VK_SUCCESS;
}
void vkDestroySampler(VkDevice, VkSampler h, const VkAllocationCallbacks*) { release(h, 'S'); }
bool create_image(const VkImageCreateInfo&, VkImage& image, VmaAllocation& memory) {
if (fails()) return false;
image = allocate('I'); memory = reinterpret_cast<void*>(uintptr_t(allocate('A'))); return true;
}
void destroy_image(VkImage image, VmaAllocation memory) { release(image, 'I'); release(uintptr_t(memory), 'A'); }
VkResult vkCreateImageView(VkDevice, const VkImageViewCreateInfo* info, const VkAllocationCallbacks*, VkImageView* p) {
*p = 0; if (fails()) return VK_ERROR_OUT_OF_DEVICE_MEMORY; *p = allocate('V', info->image); return VK_SUCCESS;
}
void vkDestroyImageView(VkDevice, VkImageView h, const VkAllocationCallbacks*) { release(h, 'V'); }
void device_wait_idle_locked() { idle = true; ++waits; }
void to_general(VkCommandBuffer, VkImage image) { CHECK(image && live.count(image)); ++barriers; }
/* ACTUAL_FUNCTIONS */
void clear_model() {
CHECK(live.empty()); fg.init = fg.failed = false; fg.w = fg.h = 0; fg.mult = 0;
fg.have_prev = false; fg.recorded = nullptr; fg.nearest = fg.linear = 0;
fg.copy = fg.luma = fg.flow = fg.median = fg.synth = {};
fg.cur = fg.next_set = 0; fg.jobs.clear();
for (int i = 0; i < 2; ++i) fg.frames[i] = fg.lumas[i] = fg.flows[i] = {};
for (auto& set : fg.sets) { set.busy = false; for (auto& image : set.frame) image = {}; }
scale.init = scale.failed = false; scale.pipe = {}; scale.linear = 0;
attempts = fail_at = barriers = waits = 0; submitted = false; idle = true;
}
void inspect_disabled() {
CHECK(fg.failed && !framegen_on()); CHECK(live.empty());
const int before = attempts; CHECK(!init() && attempts == before);
}
int run(const std::string& phase) {
clear_model();
if (phase == "samplers") {
fail_at = 21; CHECK(!init()); inspect_disabled(); return 0;
}
if (phase == "all") {
for (int failure = 1; failure <= 22; ++failure) {
clear_model(); fail_at = failure; CHECK(!init()); inspect_disabled();
}
clear_model(); dev.push_descriptor = false; CHECK(!init() && live.empty()); dev.push_descriptor = true;
}
// Every image or view failure at fixed2x and max8x, after successful pipeline setup.
for (int mult : {2, 8}) for (int failed = 1; failed <= 2 * (6 + 3 * mult); ++failed) {
clear_model(); opts.framegen = mult; CHECK(init() && live.size() == 17);
fail_at = attempts + failed; CHECK(!ensure_size({}, 3440, 1440));
CHECK(barriers == 0 && !fg.w && !fg.h && !fg.mult && !fg.have_prev && !fg.recorded);
inspect_disabled();
}
// Resize must drain old work before releasing it, then roll back a partially allocated replacement.
clear_model(); opts.framegen = 2; CHECK(init()); CHECK(ensure_size({}, 1720, 720));
CHECK(live.size() == 53 && barriers == 12 && framegen_on());
const int count = attempts; CHECK(ensure_size({}, 1720, 720) && attempts == count);
submitted = true; idle = false; fail_at = attempts + 8;
CHECK(!ensure_size({}, 3440, 1440)); CHECK(waits == 2 && idle);
CHECK(!fg.w && !fg.h && !fg.mult && !fg.have_prev && !fg.recorded); inspect_disabled();
// The independent scale setup must also release each partial pipeline/sampler on failure.
for (int failure = 1; failure <= 5; ++failure) {
clear_model(); fail_at = failure; CHECK(!init_scale() && scale.failed && live.empty());
}
std::puts("PASS: framegen setup/image rollback, checked samplers, no stale size, queue drain and direct fallback gate");
return 0;
}
}
int main(int argc, char** argv) { return fixture::run(argc > 1 ? argv[1] : "all"); }

View File

@@ -0,0 +1,128 @@
#define VK_NO_PROTOTYPES
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
#include <atomic>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <mutex>
#include <set>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr,"FAIL line%d: %s\n",__LINE__,#x); std::exit(1); } } while (0)
constexpr uint32_t kLogW=320,kLogH=180;
struct Allocation { std::vector<uint8_t> bytes; };
using VmaAllocation=Allocation*;
struct LogSlot { VkBuffer buf{}; VmaAllocation alloc{}; void* map{}; bool used{}; int64_t at{}; uint32_t seq{},kind{}; };
LogSlot log_slots[3];
VkImage log_image{};
VmaAllocation log_image_alloc{};
std::mutex log_mu;
std::condition_variable log_cv;
std::deque<std::vector<uint8_t>> log_queue;
std::atomic<int> log_state{0};
std::set<uint64_t> live;
int allocations=0,fail_allocation=0,missing_map=0;
int opens=0,closes=0,starts=0,detaches=0,ready_messages=0,errors=0;
bool fail_file=false,fail_thread=false,file_live=false;
int writes=0,flushes=0,fail_at=1;
bool fail_flush=false;
std::vector<std::vector<uint8_t>> written;
const char* framelog_path() { return "synthetic-frame-log"; }
void logf(const char* format,...) {
if(std::strstr(format,"writes every frame")) ++ready_messages;
else ++errors;
}
int GetCurrentThread() { return 0; }
void SetThreadDescription(int,const wchar_t*) {}
bool create_image(const VkImageCreateInfo&,VkImage& image,VmaAllocation& allocation) {
if(++allocations==fail_allocation)return false;
image=uint64_t(allocations);allocation=new Allocation;live.insert(image);return true;
}
bool create_buffer_cached(uint64_t bytes,VkBufferUsageFlags,VkBuffer& buffer,VmaAllocation& allocation,void** mapped) {
if(++allocations==fail_allocation)return false;
buffer=uint64_t(allocations);allocation=new Allocation;allocation->bytes.resize(size_t(bytes));
*mapped=allocations==missing_map?nullptr:allocation->bytes.data();live.insert(buffer);return true;
}
void destroy_buffer(VkBuffer buffer,VmaAllocation allocation) { CHECK(live.erase(buffer)==1);delete allocation; }
void destroy_image(VkImage image,VmaAllocation allocation) { CHECK(live.erase(image)==1);delete allocation; }
FILE* token() { return reinterpret_cast<FILE*>(uintptr_t(1234)); }
FILE* fake_open(const char*,const char*) { ++opens;if(fail_file)return nullptr;file_live=true;return token(); }
struct UnexpectedWrite {};
size_t fake_write(const void* bytes,size_t size,size_t count,FILE* file) {
CHECK(file==token() && file_live && size==1);++writes;
if(writes>fail_at)throw UnexpectedWrite{}; // Bounds an old worker that ignores the first failure.
written.emplace_back(static_cast<const uint8_t*>(bytes),static_cast<const uint8_t*>(bytes)+count);
return !fail_flush && writes==fail_at?count-1:count;
}
int fake_flush(FILE* file) { CHECK(file==token() && file_live);++flushes;return fail_flush && writes==fail_at?EOF:0; }
int fake_close(FILE* file) { CHECK(file==token() && file_live);file_live=false;++closes;return 0; }
struct FakeThread {
template<class F> explicit FakeThread(F&&) { if(fail_thread)throw std::runtime_error("injected creation failure");++starts; }
void detach() { ++detaches; }
};
#define fopen fake_open
#define fwrite fake_write
#define fflush fake_flush
#define fclose fake_close
/* DROP */
/* WRITER */
/* ENQUEUE */
/* READY */
#undef fopen
#undef fwrite
#undef fflush
#undef fclose
void startup(const std::string& name) {
if(name=="image")fail_allocation=1;
if(name.rfind("buffer",0)==0)fail_allocation=2+name.back()-'0';
if(name.rfind("map",0)==0)missing_map=2+name.back()-'0';
fail_file=name=="file";fail_thread=name=="thread";
bool ok=false;
try { ok=framelog_ready(); } catch(const std::exception&) { CHECK(false && "startup escaped instead of disabling the sink"); }
if(name=="healthy") {
CHECK(ok && live.size()==4 && opens==1 && file_live && starts==1 && detaches==1 && ready_messages==1);
CHECK(framelog_ready() && allocations==4 && opens==1 && starts==1);
fake_close(token());framelog_resources_drop();CHECK(live.empty());
} else {
CHECK(!ok && live.empty() && !file_live && ready_messages==0 && errors==1);
const int attempts=allocations;CHECK(!framelog_ready() && allocations==attempts);
for(const auto& slot:log_slots)CHECK(!slot.buf && !slot.alloc && !slot.map);
CHECK(!log_image && !log_image_alloc && starts==0);
CHECK(closes==int(fail_thread));
}
}
void worker(const std::string& name) {
fail_flush=name.rfind("flush",0)==0;fail_at=name.back()-'0';log_state=1;
FILE* f=fake_open("synthetic","wb");
for(int i=1;i<=6;i++)framelog_enqueue(std::vector<uint8_t>(8,uint8_t(i)));
bool unexpected=false;
std::atomic<bool> done{false};
std::thread thread([&] { try { framelog_write(f); } catch(const UnexpectedWrite&) { unexpected=true; } done=true; });
std::thread producer([&] { while(log_state.load()>0 && !done.load())std::this_thread::yield();framelog_enqueue(std::vector<uint8_t>(8,99)); });
thread.join();producer.join();
CHECK(!unexpected && log_state.load()<0 && closes==1 && !file_live && errors==1);
CHECK(writes==fail_at && flushes==fail_at-int(!fail_flush));
CHECK(log_queue.empty() && written.size()==size_t(fail_at));
for(size_t i=0;i<written.size();i++)CHECK(written[i]==std::vector<uint8_t>(8,uint8_t(i+1)));
framelog_enqueue(std::vector<uint8_t>(8,100));CHECK(log_queue.empty());
}
void queue_bounds() {
framelog_enqueue({1});CHECK(log_queue.empty());
log_state=1;
for(int i=0;i<513;i++)framelog_enqueue({uint8_t(i)});
CHECK(log_queue.size()==512 && log_queue.front()[0]==0 && log_queue.back()[0]==255);
log_queue.clear();log_state=-1;framelog_enqueue({1});CHECK(log_queue.empty());
}
int main(int argc,char** argv) {
CHECK(argc==2);const std::string name=argv[1];
if(name=="queue")queue_bounds();
else if(name.rfind("write",0)==0 || name.rfind("flush",0)==0)worker(name);
else startup(name);
std::printf("PASS %s\n",name.c_str());
}

View File

@@ -0,0 +1,43 @@
cmake_minimum_required(VERSION 3.20)
project(global_load_16_ir LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT DEFINED REPO_ROOT OR NOT DEFINED SPIRV_HEADERS_ROOT)
message(FATAL_ERROR "REPO_ROOT and SPIRV_HEADERS_ROOT are required")
endif()
file(GLOB_RECURSE BACKEND_SOURCES CONFIGURE_DEPENDS
"${REPO_ROOT}/gpu/vk/eden/shader_recompiler/backend/spirv/*.cpp")
file(GLOB_RECURSE IR_SOURCES CONFIGURE_DEPENDS
"${REPO_ROOT}/gpu/vk/eden/shader_recompiler/frontend/ir/*.cpp")
file(GLOB_RECURSE OPTIMIZATION_SOURCES CONFIGURE_DEPENDS
"${REPO_ROOT}/gpu/vk/eden/shader_recompiler/ir_opt/*.cpp")
file(GLOB_RECURSE SIRIT_SOURCES CONFIGURE_DEPENDS
"${REPO_ROOT}/gpu/vk/sirit/src/*.cpp")
add_executable(global_load_16_ir
"${REPO_ROOT}/tests/global_load_16_ir.cpp"
${BACKEND_SOURCES}
${IR_SOURCES}
${OPTIMIZATION_SOURCES}
${SIRIT_SOURCES})
target_include_directories(global_load_16_ir PRIVATE
"${REPO_ROOT}/gpu/vk/compat"
"${REPO_ROOT}/gpu/compat"
"${REPO_ROOT}/gpu/vk/eden"
"${REPO_ROOT}/gpu/third_party/suyu"
"${REPO_ROOT}/gpu/vk/sirit/include"
"${REPO_ROOT}/gpu/vk/sirit/src"
"${SPIRV_HEADERS_ROOT}/include")
target_compile_definitions(global_load_16_ir PRIVATE
NDEBUG MKWVK_INCLUDES=2 VK_NO_PROTOTYPES)
if(MSVC)
target_compile_options(global_load_16_ir PRIVATE /W0 /O1 /EHsc)
else()
target_compile_options(global_load_16_ir PRIVATE -w -O1)
endif()

View File

@@ -0,0 +1,46 @@
# SPIR-V global narrow load/store regression
This fixture builds a small `IR::Program`, runs the real shader-info pass and
`EmitSPIRV`, then checks both the emitted instructions and the generated module
with `spirv-val`. It covers signed and unsigned halfword loads at naturally
aligned offsets, preservation of high guest-address bits, and a page-end access
at byte offset `0xffe` whose helper word base is `0xffc`. Narrow 8- and 16-bit
stores use an aligned-word compare-exchange loop and bit-field insert. A
mesh-stage load and store preserve the upstream regression cases. The existing
zero-result behavior without 64-bit integer support is checked too.
Eight single-storage-buffer scenarios trace the actual called U32 helper's
range branch. Its logical end must use the original descriptor address plus
size, then be capped by `OpArrayLength`. Concrete cbuf/descriptor inputs cover
a hit at `0x1004` with size 2 and alignment 16, a miss at `0x1008`, an aligned-base
control, short and zero-length descriptor caps, signed/unsigned loads and a
high 64-bit base. These eight scenarios and the eleven upstream cases emit
nineteen modules. Two more vertex fixtures check that the 32-bit and
128-bit load helpers default to `DontInline` and that the runtime workaround
switches only the 128-bit helper to `Inline`.
The additional `--cas-loops OUTPUT_DIRECTORY` case emits one real compute IR
program with shared/storage byte writes, a storage/global float atomic add and
a global compare-and-swap. With `RX_VK_FOLLOW_STORES=1`, it checks seven emitted
retry loops across the SSBO and page-table routes. Each counter must be a leading
entry-block variable, reset before its loop, incremented once per failed or
successful exchange, and tested against 1024 alongside the actual exchange
success. The failed-attempt path must feed back the returned word and retry.
The two alternative single-exchange global CAS routes must return directly,
without being counted as retry loops. CI validates all twenty-two modules.
The fixture checks the emitted SPIR-V dependency chain; it does not execute a
shader or driver, and does not validate odd guest addresses or AMD hardware. It
tests the SPIR-V backend's lowering contract only.
The workflow checks out the pinned SPIR-V-Headers revision and uses the system
SPIR-V Tools validator with `--uniform-buffer-standard-layout`: the single-SSBO
fixture profiles use four-byte array strides in a uniform block. This validation
option describes those fixture profiles; it does not test device feature enablement.
Reproduce it with Clang in C++20 mode, as in the Linux workflow. The focused
target includes project compatibility headers with a function-local static
inside a `constexpr` assertion macro. GCC rejects that construct in C++20,
while the project's production Linux compiler is Clang in C++20 mode. The
fixture does not establish an unmodified MSVC build.
To include the CAS case when reproducing manually, run the built fixture again
with `RX_VK_FOLLOW_STORES=1` and `--cas-loops` before validating the output modules.

844
tests/global_load_16_ir.cpp Normal file
View File

@@ -0,0 +1,844 @@
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <spirv/unified1/spirv.hpp>
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/exception.h"
#include "shader_recompiler/environment.h"
#include "shader_recompiler/frontend/ir/ir_emitter.h"
#include "shader_recompiler/ir_opt/passes.h"
#include "shader_recompiler/object_pool.h"
using namespace Shader;
using Shader::Backend::SPIRV::EmitSPIRV;
static std::filesystem::path output_dir{"work/global16-modules"};
struct TestEnvironment final : Environment {
explicit TestEnvironment(Stage s = Stage::Compute) { stage = s; }
u64 ReadInstruction(u32) override { return 0; }
u32 ReadCbufValue(u32, u32) override { return 0; }
TextureType ReadTextureType(u32) override { return TextureType::Color2D; }
TexturePixelFormat ReadTexturePixelFormat(u32) override { return TexturePixelFormat::R8_UNORM; }
bool IsTexturePixelFormatInteger(u32) override { return false; }
u32 ReadViewportTransformState() override { return 0; }
u32 TextureBoundBuffer() const override { return 0; }
u32 LocalMemorySize() const override { return 0; }
u32 SharedMemorySize() const override { return 0; }
std::array<u32, 3> WorkgroupSize() const override { return {1, 1, 1}; }
bool HasHLEMacroState() const override { return false; }
std::optional<ReplaceConstant> GetReplaceConstBuffer(u32, u32) override {
return std::nullopt;
}
void Dump(u64, u64) override {}
};
[[noreturn]] static void fail(const char* what) {
std::fprintf(stderr, "FAIL: %s\n", what);
std::exit(1);
}
static std::vector<u32> emit_program(bool is_signed, u64 address, bool support_int64,
bool lower_address, Stage stage = Stage::Compute, bool with_single_ssbo = false) {
ObjectPool<IR::Inst> inst_pool;
IR::Block block{inst_pool};
IR::Program program;
program.stage = stage;
program.output_vertices = stage == Stage::Mesh ? 1 : 0;
program.mesh.max_vertices = stage == Stage::Mesh ? 1 : 0;
program.mesh.max_primitives = stage == Stage::Mesh ? 1 : 0;
program.mesh.payload_words = stage == Stage::Mesh ? 1 : 0;
program.workgroup_size = {1, 1, 1};
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode node{};
node.type = IR::AbstractSyntaxNode::Type::Block;
node.data.block = &block;
program.syntax_list.push_back(node);
IR::AbstractSyntaxNode ret{};
ret.type = IR::AbstractSyntaxNode::Type::Return;
program.syntax_list.push_back(ret);
IR::IREmitter ir{block};
IR::U64 guest_address;
if (lower_address) {
guest_address = ir.UConvert(64, ir.Imm32(static_cast<u32>(address)));
} else {
guest_address = ir.Imm64(address);
}
if (is_signed) {
const IR::U32 value{ir.LoadGlobalS16(guest_address)};
(void)ir.IAdd(value, ir.Imm32(1));
} else {
const IR::U32 value{ir.LoadGlobalU16(guest_address)};
(void)ir.IAdd(value, ir.Imm32(1));
}
ir.Epilogue();
TestEnvironment env{stage};
if (lower_address) Optimization::LowerInt64ToInt32(program);
Optimization::CollectShaderInfoPass(env, program);
if (!program.info.uses_global_memory) fail("CollectShaderInfoPass did not see the actual global load");
if (with_single_ssbo) {
program.info.constant_buffer_descriptors.push_back({0, 1});
program.info.storage_buffers_descriptors.push_back({0, 0, 1, false});
program.info.used_constant_buffer_types |= IR::Type::U32 | IR::Type::U32x2;
}
Profile profile{};
profile.supported_spirv = 0x00010600;
profile.support_int64 = support_int64;
if (with_single_ssbo) {
profile.support_descriptor_aliasing = true;
profile.min_ssbo_alignment = 16;
}
return EmitSPIRV(profile, program);
}
struct Instruction {
spv::Op op;
std::vector<u32> words;
};
struct ModuleInfo {
std::vector<Instruction> instructions;
std::map<u32, u32> integer_widths;
std::map<u32, bool> signed_integer_types;
std::map<u32, u64> constants;
};
static ModuleInfo inspect(const std::vector<u32>& code) {
if (code.size() < 5 || code[0] != spv::MagicNumber) fail("bad SPIR-V header");
ModuleInfo result;
for (size_t at = 5; at < code.size();) {
const u32 count = code[at] >> 16;
if (count == 0 || at + count > code.size()) fail("bad SPIR-V instruction length");
const auto op = static_cast<spv::Op>(code[at] & 0xffffu);
std::vector<u32> words(code.begin() + at + 1, code.begin() + at + count);
if (op == spv::Op::OpTypeInt && words.size() >= 3) {
result.integer_widths[words[0]] = words[1];
result.signed_integer_types[words[0]] = words[2] != 0;
} else if (op == spv::Op::OpConstant && words.size() >= 3) {
const u32 type = words[0], id = words[1];
const auto width_it = result.integer_widths.find(type);
const u32 width = width_it == result.integer_widths.end() ? 0 : width_it->second;
u64 value = words[2];
if (width == 64 && words.size() >= 4) value |= static_cast<u64>(words[3]) << 32;
result.constants[id] = value;
} else if (op == spv::Op::OpConstantNull && words.size() >= 2) {
result.constants[words[1]] = 0;
}
result.instructions.push_back({op, std::move(words)});
at += count;
}
return result;
}
static u64 constant(const ModuleInfo& module, u32 id, const char* name) {
const auto it = module.constants.find(id);
if (it == module.constants.end()) fail(name);
return it->second;
}
static std::vector<u32> save_module(const std::string& name, std::vector<u32> code) {
std::filesystem::create_directories(output_dir);
std::ofstream file(output_dir / (name + ".spv"), std::ios::binary);
if (!file) fail("could not create emitted SPIR-V module");
file.write(reinterpret_cast<const char*>(code.data()),
static_cast<std::streamsize>(code.size() * sizeof(u32)));
if (!file) fail("could not write emitted SPIR-V module");
return code;
}
struct SingleSsboScenario {
u64 descriptor_address;
u32 descriptor_size;
u32 descriptor_words;
bool expected_in_range;
};
static void check_single_ssbo_range(const ModuleInfo& module, u64 address,
const SingleSsboScenario& scenario, const char* name) {
u32 u64_type = 0;
u32 u32x2_type = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpTypeInt && w.size() >= 3 && w[1] == 64 && w[2] == 0)
u64_type = w[0];
if (inst.op == spv::Op::OpTypeVector && w.size() >= 3 && w[2] == 2) {
const auto component = module.integer_widths.find(w[1]);
if (component != module.integer_widths.end() && component->second == 32) {
const auto signedness = module.signed_integer_types.find(w[1]);
if (signedness != module.signed_integer_types.end() && !signedness->second)
u32x2_type = w[0];
}
}
}
if (!u64_type || !u32x2_type) fail("single-SSBO fixture lacks U64 or U32x2 types");
u32 input_address = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
const auto width = w.empty() ? module.integer_widths.end() : module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpConstant && w.size() >= 3 && width != module.integer_widths.end() &&
width->second == 64 && constant(module, w[1], "missing IR address constant") == address)
input_address = w[1];
}
if (!input_address) fail("single-SSBO fixture lost its actual IR address constant");
u32 aligned_call_address = 0;
u64 helper_argument = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpBitwiseAnd && w.size() >= 4 && w[2] == input_address &&
constant(module, w[3], "missing 4-byte alignment mask") == ~u64{3}) {
aligned_call_address = w[1];
helper_argument = constant(module, w[2], "missing global address") &
constant(module, w[3], "missing global alignment mask");
}
}
if (!aligned_call_address) fail("single-SSBO fixture did not align its real U16/S16 address");
u32 helper_function = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
const auto width = w.empty() ? module.integer_widths.end() : module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpFunctionCall && w.size() >= 4 && w[3] == aligned_call_address &&
width != module.integer_widths.end() && width->second == 32)
helper_function = w[2];
}
if (!helper_function) fail("single-SSBO fixture did not call the global U32 helper");
std::vector<const Instruction*> body;
bool in_helper = false;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpFunction && w.size() >= 2 && w[1] == helper_function) {
in_helper = true;
continue;
}
if (in_helper && inst.op == spv::Op::OpFunctionEnd) break;
if (in_helper) body.push_back(&inst);
}
if (body.empty()) fail("could not inspect the called global U32 helper body");
std::map<u32, const Instruction*> loads;
std::map<u32, const Instruction*> converts;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpLoad && w.size() >= 3) loads[w[1]] = inst;
if (inst->op == spv::Op::OpUConvert && w.size() >= 3) converts[w[1]] = inst;
}
u32 unaligned_address = 0;
u32 address_load = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op != spv::Op::OpBitcast || w.size() < 3 || w[0] != u64_type) continue;
const auto source = loads.find(w[2]);
if (source != loads.end() && source->second->words[0] == u32x2_type) {
unaligned_address = w[1];
address_load = w[2];
}
}
if (!unaligned_address) fail("could not find the loaded, unaligned SSBO guest address");
u32 aligned_ssbo_base = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpBitwiseAnd && w.size() >= 4 && w[0] == u64_type &&
w[2] == unaligned_address && constant(module, w[3], "missing SSBO alignment mask") == ~u64{15})
aligned_ssbo_base = w[1];
}
if (!aligned_ssbo_base) fail("single-SSBO helper did not align the descriptor base to 16 bytes");
u32 raw_end = 0;
u32 size_load = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op != spv::Op::OpIAdd || w.size() < 4 || w[0] != u64_type) continue;
const auto size = converts.find(w[3]);
if (w[2] == unaligned_address && size != converts.end() && size->second->words[0] == u64_type) {
raw_end = w[1];
size_load = size->second->words[2];
}
}
if (!raw_end || !size_load) fail("single-SSBO logical end is not computed from original address plus size");
u32 array_length = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpArrayLength && w.size() >= 4) array_length = w[1];
}
if (!array_length) fail("single-SSBO range no longer caps itself to OpArrayLength");
u32 byte_length = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpShiftLeftLogical && w.size() >= 4 && w[2] == array_length &&
constant(module, w[3], "missing descriptor-word shift") == 2)
byte_length = w[1];
}
u32 descriptor_bytes = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpUConvert && w.size() >= 3 && w[2] == byte_length && w[0] == u64_type)
descriptor_bytes = w[1];
}
if (!byte_length || !descriptor_bytes) fail("OpArrayLength is not converted to the descriptor byte length");
u32 bound_end = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpIAdd && w.size() >= 4 && w[0] == u64_type &&
w[2] == aligned_ssbo_base && w[3] == descriptor_bytes)
bound_end = w[1];
}
if (!bound_end) fail("descriptor byte length is not added to the aligned SSBO base");
u32 bound_is_shorter = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpULessThan && w.size() >= 4 &&
w[2] == bound_end && w[3] == raw_end)
bound_is_shorter = w[1];
}
if (!bound_is_shorter) fail("descriptor cap does not compare its end against the logical end");
u32 capped_end = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpSelect && w.size() >= 5 && w[0] == u64_type &&
w[2] == bound_is_shorter && w[3] == bound_end && w[4] == raw_end)
capped_end = w[1];
}
if (!capped_end) fail("single-SSBO helper does not select the lesser of logical and bound ends");
u32 helper_parameter = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpFunctionParameter && w.size() >= 2 && w[0] == u64_type)
helper_parameter = w[1];
}
u32 lower_check = 0;
u32 upper_check = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpUGreaterThanEqual && w.size() >= 4 &&
w[2] == helper_parameter && w[3] == aligned_ssbo_base)
lower_check = w[1];
if (inst->op == spv::Op::OpULessThan && w.size() >= 4 &&
w[2] == helper_parameter && w[3] == capped_end)
upper_check = w[1];
}
u32 membership = 0;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpLogicalAnd && w.size() >= 4 &&
w[2] == lower_check && w[3] == upper_check)
membership = w[1];
}
bool is_actual_branch = false;
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpBranchConditional && w.size() >= 3 && w[0] == membership)
is_actual_branch = true;
}
if (!helper_parameter || !lower_check || !upper_check || !membership || !is_actual_branch)
fail("capped end is not used by the called helper's range branch");
std::map<u32, u64> values;
const auto get_value = [&](u32 id) -> std::optional<u64> {
const auto value = values.find(id);
if (value != values.end()) return value->second;
const auto literal = module.constants.find(id);
if (literal != module.constants.end()) return literal->second;
return std::nullopt;
};
for (const Instruction* inst : body) {
const auto& w = inst->words;
if (inst->op == spv::Op::OpFunctionParameter && w.size() >= 2 && w[1] == helper_parameter) {
values[w[1]] = helper_argument;
} else if (inst->op == spv::Op::OpLoad && w.size() >= 3 && w[1] == address_load) {
values[w[1]] = scenario.descriptor_address;
} else if (inst->op == spv::Op::OpLoad && w.size() >= 3 && w[1] == size_load) {
values[w[1]] = scenario.descriptor_size;
} else if (inst->op == spv::Op::OpArrayLength && w.size() >= 4 && w[1] == array_length) {
values[w[1]] = scenario.descriptor_words;
} else if (inst->op == spv::Op::OpBitcast && w.size() >= 3) {
if (const auto value = get_value(w[2])) values[w[1]] = *value;
} else if (inst->op == spv::Op::OpBitwiseAnd && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs) values[w[1]] = *lhs & *rhs;
} else if (inst->op == spv::Op::OpUConvert && w.size() >= 3) {
if (const auto value = get_value(w[2])) values[w[1]] = *value;
} else if (inst->op == spv::Op::OpIAdd && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs) values[w[1]] = *lhs + *rhs;
} else if (inst->op == spv::Op::OpShiftLeftLogical && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs && *rhs < 64) values[w[1]] = *lhs << *rhs;
} else if (inst->op == spv::Op::OpULessThan && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs) values[w[1]] = *lhs < *rhs;
} else if (inst->op == spv::Op::OpUGreaterThanEqual && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs) values[w[1]] = *lhs >= *rhs;
} else if (inst->op == spv::Op::OpLogicalAnd && w.size() >= 4) {
const auto lhs = get_value(w[2]);
const auto rhs = get_value(w[3]);
if (lhs && rhs) values[w[1]] = *lhs && *rhs;
} else if (inst->op == spv::Op::OpSelect && w.size() >= 5) {
const auto condition = get_value(w[2]);
const auto yes = get_value(w[3]);
const auto no = get_value(w[4]);
if (condition && yes && no) values[w[1]] = *condition ? *yes : *no;
}
}
const auto in_range = get_value(membership);
if (!in_range) fail("could not evaluate the called helper's emitted range branch");
const bool actual_in_range = *in_range != 0;
if (actual_in_range != scenario.expected_in_range) {
fail("single-SSBO helper range branch disagrees with the concrete descriptor scenario");
}
std::printf("PASS: %s helper branch is %s with descriptor %016llx+%x and %u words\n",
name, actual_in_range ? "in range" : "out of range",
static_cast<unsigned long long>(scenario.descriptor_address),
scenario.descriptor_size, scenario.descriptor_words);
}
static void check_case(bool is_signed, u64 address, const char* name, Stage stage = Stage::Compute,
std::optional<SingleSsboScenario> ssbo_scenario = std::nullopt) {
std::string filename;
for (const char* c = name; *c; ++c) filename += (*c == ' ' ? '-' : *c);
const auto module = inspect(save_module(
filename, emit_program(is_signed, address, true, false, stage, ssbo_scenario.has_value())));
const u64 aligned = address & ~u64{3};
const u32 expected_bit_offset = static_cast<u32>((address & 3u) << 3);
u32 address_id = 0, aligned_id = 0, low_lane_id = 0, low_id = 0, bit_offset_id = 0;
bool saw_s_extract = false, saw_u_extract = false;
u32 global_word_id = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
const auto width_it = w.empty() ? module.integer_widths.end() : module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpConstant && w.size() >= 3 && width_it != module.integer_widths.end() && width_it->second == 64 &&
constant(module, w[1], "missing 64-bit guest address constant") == address) {
address_id = w[1];
}
}
if (!address_id) fail("actual IR address was not emitted as a 64-bit constant");
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
const auto width_it = w.empty() ? module.integer_widths.end() : module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpBitwiseAnd && w.size() >= 4 && width_it != module.integer_widths.end() && width_it->second == 64 &&
w[2] == address_id) {
const u64 mask = constant(module, w[3], "missing address mask");
if (mask == ~u64{3}) {
aligned_id = w[1];
if ((constant(module, address_id, "missing source address") & ~u64{3}) != aligned)
fail("aligned helper base changed address high bits");
} else if (mask == 3) {
low_lane_id = w[1];
}
}
const auto width32_it = w.empty() ? module.integer_widths.end() : module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpUConvert && w.size() >= 3 && w[2] == low_lane_id &&
width32_it != module.integer_widths.end() && width32_it->second == 32) low_id = w[1];
}
if (!aligned_id || !low_lane_id || !low_id)
fail("missing full-U64 alignment or original-address lane extraction");
u32 shifted_id = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpShiftLeftLogical && w.size() >= 4 && w[2] == low_id &&
constant(module, w[3], "missing byte-to-bit shift") == 3) shifted_id = w[1];
const auto result_width = w.empty() ? module.integer_widths.end()
: module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpFunctionCall && w.size() >= 4 && w[3] == aligned_id &&
result_width != module.integer_widths.end() && result_width->second == 32)
global_word_id = w[1];
}
if (!shifted_id || !global_word_id)
fail("global32 helper is not called with the aligned original U64 address");
bit_offset_id = shifted_id;
if (!bit_offset_id) fail("extract offset is not derived from the original address low bits");
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if ((inst.op == spv::Op::OpBitFieldSExtract || inst.op == spv::Op::OpBitFieldUExtract) &&
w.size() >= 5 && w[2] == global_word_id && w[3] == bit_offset_id &&
constant(module, w[4], "missing 16-bit extract width") == 16) {
if (inst.op == spv::Op::OpBitFieldSExtract) saw_s_extract = true;
if (inst.op == spv::Op::OpBitFieldUExtract) saw_u_extract = true;
if (expected_bit_offset != 0 && expected_bit_offset != 16) fail("test used non-halfword-aligned address");
}
}
if (is_signed ? (!saw_s_extract || saw_u_extract) : (!saw_u_extract || saw_s_extract))
fail("wrong signed/unsigned extraction operation for the requested IR opcode");
if (ssbo_scenario) check_single_ssbo_range(module, address, *ssbo_scenario, name);
std::printf("PASS: %s emitted aligned global32 helper + %s16 extraction (base=%016llx offset=%u)\n",
name, is_signed ? "signed " : "unsigned ", static_cast<unsigned long long>(aligned),
expected_bit_offset);
}
static std::vector<u32> emit_write_program(bool is_signed, u64 address, bool support_int64, u32 bits,
Stage stage = Stage::Compute) {
ObjectPool<IR::Inst> inst_pool;
IR::Block block{inst_pool};
IR::Program program;
program.stage = stage;
program.output_vertices = stage == Stage::Mesh ? 1 : 0;
program.mesh.max_vertices = stage == Stage::Mesh ? 1 : 0;
program.mesh.max_primitives = stage == Stage::Mesh ? 1 : 0;
program.mesh.payload_words = stage == Stage::Mesh ? 1 : 0;
program.workgroup_size = {1, 1, 1};
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode node{};
node.type = IR::AbstractSyntaxNode::Type::Block;
node.data.block = &block;
program.syntax_list.push_back(node);
IR::AbstractSyntaxNode ret{};
ret.type = IR::AbstractSyntaxNode::Type::Return;
program.syntax_list.push_back(ret);
IR::IREmitter ir{block};
const IR::U64 guest_address{ir.Imm64(address)};
const IR::U32 value{ir.Imm32(bits == 8 ? 0xA5u : 0xA5A5u)};
if (is_signed) {
if (bits == 8) ir.WriteGlobalS8(guest_address, value);
else ir.WriteGlobalS16(guest_address, value);
} else {
if (bits == 8) ir.WriteGlobalU8(guest_address, value);
else ir.WriteGlobalU16(guest_address, value);
}
ir.Epilogue();
TestEnvironment env{stage};
Optimization::CollectShaderInfoPass(env, program);
if (!program.info.stores_global_memory) fail("CollectShaderInfoPass did not see the actual global store");
if (!program.info.uses_global_memory) fail("CollectShaderInfoPass did not see the global store's memory");
Profile profile{};
profile.supported_spirv = 0x00010600;
profile.support_int64 = support_int64;
return EmitSPIRV(profile, program);
}
static void check_write_case(bool is_signed, u64 address, u32 bits, const char* name,
Stage stage = Stage::Compute) {
std::string filename;
for (const char* c = name; *c; ++c) filename += (*c == ' ' ? '-' : *c);
const auto module = inspect(save_module(filename, emit_write_program(is_signed, address, true, bits, stage)));
const u32 expected_bit_offset = static_cast<u32>((address & 3u) << 3);
bool saw_insert = false, saw_cas = false, saw_call = false, saw_shift = false;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpBitFieldInsert && w.size() >= 6) saw_insert = true;
if (inst.op == spv::Op::OpAtomicCompareExchange) saw_cas = true;
// (the compare-exchange helper's call: address, value, bit offset, width)
if (inst.op == spv::Op::OpFunctionCall && w.size() >= 7 &&
constant(module, w[6], "missing narrow-store width") == bits)
saw_call = true;
if (inst.op == spv::Op::OpShiftLeftLogical && w.size() >= 4 &&
constant(module, w[3], "missing byte-to-bit shift") == 3)
saw_shift = true;
}
if (!saw_insert) fail("narrow global store did not insert into its aligned word");
if (!saw_cas) fail("narrow global store did not use a compare-exchange loop");
if (!saw_call) fail("narrow global store did not call the compare-exchange helper with its width");
if (!saw_shift) fail("narrow global store did not derive its bit offset from the address low bits");
std::printf("PASS: %s emitted aligned-word compare-exchange + %u-bit insert (offset=%u)\n", name, bits,
expected_bit_offset);
}
static void check_unsupported_profile() {
const auto module = inspect(save_module(
"unsupported-int64", emit_program(true, 0x0000000100000002ull, false, true)));
u32 zero_id = 0;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpBitFieldSExtract || inst.op == spv::Op::OpBitFieldUExtract)
fail("unsupported-int64 profile emitted a halfword extract instead of zero fallback");
const auto type_width = w.empty() ? module.integer_widths.end()
: module.integer_widths.find(w[0]);
if (inst.op == spv::Op::OpConstant && w.size() >= 3 && w[2] == 0 &&
type_width != module.integer_widths.end() && type_width->second == 32)
zero_id = w[1];
}
bool zero_fallback_used = false;
for (const auto& inst : module.instructions) {
const auto& w = inst.words;
if (inst.op == spv::Op::OpIAdd && w.size() >= 4 && w[2] == zero_id &&
constant(module, w[3], "missing fallback's observable addend") == 1)
zero_fallback_used = true;
}
if (!zero_id || !zero_fallback_used)
fail("unsupported-int64 result was not the zero constant consumed by the fixture");
std::puts("PASS: unsupported-int64 result is zero and emits no halfword extraction");
}
static void check_original_stub_failure() {
try {
(void)emit_program(true, 0x0000000100000000ull, true, false);
} catch (const NotImplementedException& error) {
if (std::string(error.what()).find("LoadGlobalS16") == std::string::npos)
fail("original-stub negative control failed for the wrong IR opcode");
std::puts("PASS: original S16 stub fails specifically at LoadGlobalS16");
return;
}
fail("original-stub negative control unexpectedly emitted a module");
}
static void check_cas_functions(const ModuleInfo& module) {
u32 bounded = 0, single = 0;
for (size_t first = 0; first < module.instructions.size(); ++first) {
if (module.instructions[first].op != spv::Op::OpFunction) continue;
size_t last = first + 1;
while (last < module.instructions.size() && module.instructions[last].op != spv::Op::OpFunctionEnd) ++last;
std::map<u32, const Instruction*> defs;
std::map<u32, size_t> labels;
bool entry = true, variables = true;
for (size_t i = first + 1; i < last; ++i) {
const auto& inst = module.instructions[i];
const auto& w = inst.words;
if (inst.op == spv::Op::OpLabel) {
labels[w[0]] = i;
if (!entry) variables = false;
entry = false;
} else if (inst.op == spv::Op::OpVariable) {
if (!variables) fail("CAS helper declares a Function variable after an instruction or entry block");
defs[w[1]] = &inst;
} else if (inst.op != spv::Op::OpFunctionParameter) {
variables = false;
if (inst.op == spv::Op::OpLoad || inst.op == spv::Op::OpIAdd ||
inst.op == spv::Op::OpUGreaterThanEqual || inst.op == spv::Op::OpLogicalOr ||
inst.op == spv::Op::OpIEqual) defs[w[1]] = &inst;
}
}
const auto def = [&](u32 id, spv::Op op) -> const std::vector<u32>& {
const auto it = defs.find(id);
if (it == defs.end() || it->second->op != op) fail("CAS exit lost its success-or-incremented-ceiling dependency");
return it->second->words;
};
std::map<size_t, bool> covered;
bool has_loop = false;
for (size_t i = first + 1; i < last; ++i) {
const auto& loop = module.instructions[i];
if (loop.op != spv::Op::OpLoopMerge) continue;
has_loop = true;
const u32 merge = loop.words[0];
const size_t end = labels.at(merge);
u32 header = 0;
for (const auto& [id, at] : labels) if (at < i && (!header || at > labels.at(header))) header = id;
size_t preheader = first;
for (const auto& [id, at] : labels) if (at < labels.at(header) && at > preheader) preheader = at;
for (size_t a = i + 1; a < end; ++a) {
const auto& atomic = module.instructions[a];
if (atomic.op != spv::Op::OpAtomicCompareExchange) continue;
bool exits = false;
for (size_t b = a + 1; b < end; ++b) {
const auto& branch = module.instructions[b];
if (branch.op != spv::Op::OpBranchConditional || branch.words[1] != merge) continue;
const auto& either = def(branch.words[0], spv::Op::OpLogicalOr);
const auto& success = def(either[2], spv::Op::OpIEqual);
const auto& cap = def(either[3], spv::Op::OpUGreaterThanEqual);
const auto& add = def(cap[2], spv::Op::OpIAdd);
const auto& load = def(add[2], spv::Op::OpLoad);
const auto& variable = def(load[2], spv::Op::OpVariable);
if (variable[2] != static_cast<u32>(spv::StorageClassFunction) ||
constant(module, add[3], "missing attempt increment") != 1 ||
constant(module, cap[3], "missing attempt ceiling") != 1024 ||
!((success[2] == atomic.words[1] && success[3] == atomic.words[7]) ||
(success[3] == atomic.words[1] && success[2] == atomic.words[7])))
fail("CAS exit does not compare its exchange result or count exactly one attempt");
bool reset = false, incremented = false, feedback = false;
const auto& expected = def(atomic.words[7], spv::Op::OpLoad);
for (size_t s = first + 1; s < b; ++s) {
const auto& store = module.instructions[s];
if (store.op != spv::Op::OpStore) continue;
if (s > preheader && s < labels.at(header) && store.words[0] == load[2]) {
const auto value = module.constants.find(store.words[1]);
if (value != module.constants.end() && value->second == 0) reset = true;
}
if (s > a && store.words[0] == load[2] && store.words[1] == add[1]) incremented = true;
if (s > a && store.words[0] == expected[2] && store.words[1] == atomic.words[1]) feedback = true;
}
const u32 retry = branch.words[2];
if (retry != header) {
const auto& back = module.instructions[labels.at(retry) + 1];
if (back.op != spv::Op::OpBranch || back.words[0] != header)
fail("CAS failed-attempt path no longer returns to its loop header");
}
if (!reset || !incremented || !feedback) fail("CAS loop lost reset, increment or compare-exchange feedback");
exits = true;
}
if (!exits) fail("CAS retry loop has no success-or-ceiling exit");
covered[a] = true;
++bounded;
}
}
for (size_t i = first + 1; i < last; ++i) {
const auto& inst = module.instructions[i];
if (inst.op != spv::Op::OpAtomicCompareExchange || covered.contains(i)) continue;
bool returned = false;
for (size_t r = i + 1; r < last; ++r)
if (module.instructions[r].op == spv::Op::OpReturnValue &&
module.instructions[r].words[0] == inst.words[1]) returned = true;
if (has_loop || !returned) fail("unclassified compare-exchange bypasses the retry-loop checks");
++single;
}
first = last;
}
if (bounded != 7 || single != 2) fail("fixture did not emit all shared/storage/global loops and both one-shot CAS routes");
std::printf("PASS: %u retry loops reset/count/exit on success or 1024; %u one-shot exchanges return directly\n", bounded, single);
}
static void check_cas_program() {
if (!std::getenv("RX_VK_FOLLOW_STORES")) fail("CAS fixture needs RX_VK_FOLLOW_STORES=1 to exercise the page-table route");
ObjectPool<IR::Inst> inst_pool;
IR::Block block{inst_pool};
IR::Program program;
program.stage = Stage::Compute;
program.workgroup_size = {1, 1, 1};
program.shared_memory_size = 16;
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode node{};
node.type = IR::AbstractSyntaxNode::Type::Block;
node.data.block = &block;
program.syntax_list.push_back(node);
IR::AbstractSyntaxNode ret{};
ret.type = IR::AbstractSyntaxNode::Type::Return;
program.syntax_list.push_back(ret);
IR::IREmitter ir{block};
ir.WriteShared(8, ir.Imm32(0u), ir.Imm32(0xa5u));
// Storage IR is the actual lowered form; its descriptor is authored fixture metadata.
block.AppendNewInst(IR::Opcode::WriteStorageU8, {ir.Imm32(0u), ir.Imm32(0u), ir.Imm32(0xa5u)});
block.AppendNewInst(IR::Opcode::StorageAtomicAddF32, {ir.Imm32(0u), ir.Imm32(0u), ir.Imm32(1.0f)});
// Recreate the emitter after appending the lowered instructions, preserving their order.
IR::IREmitter tail{block};
(void)tail.GlobalAtomicF32Add(tail.Imm64(u64{0x1000}), tail.Imm32(1.0f));
(void)tail.GlobalAtomicCompareExchange(tail.Imm64(u64{0x1000}), tail.Imm32(1u), tail.Imm32(2u));
tail.Epilogue();
TestEnvironment env;
Optimization::CollectShaderInfoPass(env, program);
program.info.constant_buffer_descriptors.push_back({0, 1});
program.info.storage_buffers_descriptors.push_back({0, 0, 1, true});
program.info.used_constant_buffer_types |= IR::Type::U32 | IR::Type::U32x2;
Profile profile{};
profile.supported_spirv = 0x00010600;
profile.support_int64 = true;
profile.support_descriptor_aliasing = true;
profile.min_ssbo_alignment = 16;
const auto module = inspect(save_module("cas-helper-contracts", EmitSPIRV(profile, program)));
check_cas_functions(module);
}
static void check_vertex_load_controls(bool inline_128, const char* name) {
ObjectPool<IR::Inst> inst_pool;
IR::Block block{inst_pool};
IR::Program program;
program.stage = Stage::VertexB;
program.blocks.push_back(&block);
program.post_order_blocks.push_back(&block);
IR::AbstractSyntaxNode node{};
node.type = IR::AbstractSyntaxNode::Type::Block;
node.data.block = &block;
program.syntax_list.push_back(node);
program.syntax_list.push_back({.type = IR::AbstractSyntaxNode::Type::Return});
IR::IREmitter ir{block};
(void)ir.LoadGlobal32(ir.Imm64(u64{0x1000}));
(void)ir.LoadGlobal128(ir.Imm64(u64{0x1010}));
ir.Epilogue();
TestEnvironment env{program.stage};
Optimization::CollectShaderInfoPass(env, program);
Profile profile{};
profile.supported_spirv = 0x00010600;
profile.support_int64 = true;
profile.unified_descriptor_binding = true;
RuntimeInfo runtime_info{};
runtime_info.inline_global_128_loads = inline_128;
Backend::Bindings bindings{};
const auto module = inspect(save_module(name, EmitSPIRV(profile, runtime_info, program, bindings)));
using Control = spv::FunctionControlMask;
std::map<u32, Control> function_controls;
std::vector<Control> load_controls;
for (const auto& inst : module.instructions) {
if (inst.op == spv::Op::OpFunction)
function_controls.emplace(inst.words[1], static_cast<Control>(inst.words[2]));
if (inst.op == spv::Op::OpFunctionCall)
load_controls.push_back(function_controls.at(inst.words[2]));
}
const Control expected_128 = inline_128 ? spv::FunctionControlInlineMask : spv::FunctionControlDontInlineMask;
if (load_controls.size() != 2 || load_controls[0] != spv::FunctionControlDontInlineMask ||
load_controls[1] != expected_128)
fail("vertex global-load inlining must affect only the 128-bit helper");
std::printf("PASS: %s\n", name);
}
int main(int argc, char** argv) {
if (argc == 3 && std::string(argv[1]) == "--cas-loops") {
output_dir = argv[2];
check_cas_program();
return 0;
}
if (argc == 2 && std::string(argv[1]) == "--expect-original-stub") {
check_original_stub_failure();
return 0;
}
if (argc == 2) output_dir = argv[1];
check_case(true, 0x0000000100000000ull, "signed offset 0 high address");
check_case(false, 0x0000000100000002ull, "unsigned offset 2 high address");
check_case(true, 0x0000007f00000ffeull, "signed guest-page end 0xffe");
check_case(false, 0x0000007f00000ffeull, "unsigned guest-page end 0xffe");
// These supply concrete cbuf/descriptor inputs to the evaluator for the
// actual emitted helper branch, including the descriptor-length cap.
const SingleSsboScenario offset_four{0x1004, 2, 4, true};
check_case(true, 0x0000000000001004ull, "single SSBO signed halfword at offset four", Stage::Compute, offset_four);
check_case(false, 0x0000000000001004ull, "single SSBO unsigned halfword at offset four", Stage::Compute, offset_four);
check_case(false, 0x0000000000001008ull, "single SSBO following word misses logical range", Stage::Compute,
SingleSsboScenario{0x1004, 2, 4, false});
check_case(true, 0x0000000000001000ull, "single SSBO aligned-base control", Stage::Compute,
SingleSsboScenario{0x1000, 2, 4, true});
check_case(false, 0x0000000000001004ull, "single SSBO descriptor cap admits first word", Stage::Compute,
SingleSsboScenario{0x1004, 0x40, 2, true});
check_case(false, 0x0000000000001008ull, "single SSBO descriptor cap excludes end", Stage::Compute,
SingleSsboScenario{0x1004, 0x40, 2, false});
check_case(false, 0x0000000000001004ull, "single SSBO zero-length descriptor", Stage::Compute,
SingleSsboScenario{0x1004, 2, 0, false});
check_case(true, 0x0000000100001004ull, "single SSBO high-64-bit base", Stage::Compute,
SingleSsboScenario{0x0000000100001004ull, 2, 4, true});
check_write_case(true, 0x0000000100000000ull, 16, "signed 16-bit store high address");
check_write_case(false, 0x0000000100000002ull, 16, "unsigned 16-bit store offset 2");
check_write_case(true, 0x0000000100000003ull, 8, "signed 8-bit store offset 3");
check_write_case(false, 0x0000007f00000fffull, 8, "unsigned 8-bit store page end");
check_case(true, 0x0000000100000000ull, "mesh signed offset 0 high address", Stage::Mesh);
check_write_case(true, 0x0000000100000000ull, 16, "mesh signed 16-bit store high address", Stage::Mesh);
check_unsupported_profile();
check_vertex_load_controls(false, "vertex-global-load-default");
check_vertex_load_controls(true, "vertex-global-load-inline128");
}

View File

@@ -0,0 +1,26 @@
#include "import_hold.h"
#include <cstdio>
static int fail(int n) {
std::fprintf(stderr, "import hold check %d failed\n", n);
return n;
}
int main() {
mkwvk::ImportHolds h;
if (!h.allow(1, 2, 0)) return fail(1);
h.deny_for(1, 2, 0);
if (h.allow(1, 2, mkwvk::ImportHolds::kGapMs - 1)) return fail(2);
if (!h.allow(1, 2, mkwvk::ImportHolds::kGapMs)) return fail(3);
if (!h.allow(3, 4, 100)) return fail(4);
h.deny_for(1, 2, 5000);
if (h.allow(1, 2, 5000 + mkwvk::ImportHolds::kGapMs - 1)) return fail(5);
mkwvk::ImportHolds capped;
for (uint64_t i = 0; i < mkwvk::ImportHolds::kMax + 6; i++) capped.deny_for(i, i + 1, 0);
if (capped.rows.size() != mkwvk::ImportHolds::kMax) return fail(6);
if (!capped.allow(0, 1, 0)) return fail(7);
const uint64_t newest = mkwvk::ImportHolds::kMax + 5;
if (capped.allow(newest, newest + 1, 0)) return fail(8);
return 0;
}

View File

@@ -0,0 +1,215 @@
/* Authored memory, clock and API spies; input_actual.inc contains the unmodified production path. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
#include "input_constants.inc"
/* API spies must not collide with Windows SDK/import-library names. */
#define GetCurrentProcessId fixture_GetCurrentProcessId
#define InterlockedCompareExchange64 fixture_InterlockedCompareExchange64
typedef void *HWND;
typedef uint32_t DWORD;
typedef intptr_t LONG_PTR;
typedef struct { uint64_t x[32]; } Ctx;
enum { GWL_EXSTYLE = -20, WS_EX_TOOLWINDOW = 0x80, WS_EX_NOACTIVATE = 0x8000000,
VK_MENU = 0x12, VK_RETURN = 0x0d, VK_LEFT = 0x25, VK_UP, VK_RIGHT, VK_DOWN,
VK_LBUTTON = 1, VK_RBUTTON = 2, VK_MBUTTON = 4, VK_XBUTTON1 = 5, VK_XBUTTON2 = 6 };
static int front, menu_background, bootstrap_polls, pad_merges, key_queries, mouse_queries, focus_queries, commands;
typedef int64_t LONG64;
volatile LONG64 hos_present_count = 1000;
static int frame_reads;
LONG64 InterlockedCompareExchange64(volatile LONG64 *p, LONG64 value, LONG64 expected) {
const LONG64 old = *p;
frame_reads++;
if (old == expected) *p = value;
return old;
}
static int64_t now_ns = 10000000000LL, sampling;
static int key_bind[HOS_KEY_N][2], pad_raw_now; /* (the keys-only controls after the Switch's buttons) */
static unsigned char *memory;
static char last_log[256];
volatile int64_t hos_hid_last_read;
volatile int hos_nvn_sync_log_on;
static void *at(uint64_t address) { CHECK(address <= 512 - 8); return memory + address; }
#define GP(a) at(a)
#define G32(a) (*(uint32_t *)at(a))
#define G64(a) (*(uint64_t *)at(a))
#define ARG(n) (c->x[n])
#define RET(value) (c->x[0] = (uint64_t)(value))
int rx_option(int id, int otherwise) { return id == 4 ? menu_background : id == 1 ? 0 : otherwise; }
int64_t hos_now_ns(void) { return now_ns; }
void hos_log(const char *format, ...) {
va_list args; va_start(args, format); vsnprintf(last_log, sizeof last_log, format, args); va_end(args);
}
HWND GetForegroundWindow(void) { focus_queries++; return (HWND)(uintptr_t)(front ? 1 : 2); }
DWORD GetWindowThreadProcessId(HWND window, DWORD *pid) { *pid = window == (HWND)(uintptr_t)1 ? 10 : 20; return 1; }
DWORD GetCurrentProcessId(void) { return 10; }
LONG_PTR GetWindowLongPtrA(HWND window, int index) { (void)window; (void)index; return 0; }
int GetClassNameA(HWND window, char *text, int size) { (void)window; return snprintf(text, (size_t)size, "fixture"); }
short GetAsyncKeyState(int key) { key_queries++; return key == 'P' || key == 'D' ? (short)-32768 : 0; }
int hos_sdl_pads(uint64_t *buttons, int32_t stick[4]) {
bootstrap_polls++; *buttons = 1ULL << B_Y; stick[0] = -31000; return 1;
}
void pads_read(uint64_t *buttons, int32_t stick[4]) {
/* The physical backend boundary is a spy; actual routing merges its result with the script below. */
pad_merges++; *buttons |= 1ULL << B_B; stick[0] = -12000; stick[2] = 8000;
}
void binds_load(void) {
key_bind[B_X][0] = 'P';
/* (the left stick's and the camera's keys are bindings too: the defaults, WASD and the arrows) */
key_bind[HOS_KEY_MOVE_UP][0] = 'W', key_bind[HOS_KEY_MOVE_DOWN][0] = 'S', key_bind[HOS_KEY_MOVE_LEFT][0] = 'A';
key_bind[HOS_KEY_MOVE_RIGHT][0] = 'D', key_bind[HOS_KEY_CAM_UP][0] = VK_UP, key_bind[HOS_KEY_CAM_DOWN][0] = VK_DOWN;
key_bind[HOS_KEY_CAM_LEFT][0] = VK_LEFT, key_bind[HOS_KEY_CAM_RIGHT][0] = VK_RIGHT;
}
/* the native mouse look (no camera found: the motion goes to mouse_stick) and the mouse's sensitivity, which
read_input calls */
int hos_mouse_native(void) { return -1; }
static void mouse_setup(void) {}
static void mouse_sens_keys(void) {}
int mouse_stick(int32_t *x, int32_t *y, int held) { (void)x; (void)y; (void)held; mouse_queries++; return 0; }
void rx_request_frame_dump(void) { commands++; }
void rx_set_screenshot_ms(int64_t ms) { (void)ms; commands++; }
void rx_request_rdoc_capture(int frames) { (void)frames; commands++; }
void rx_flash_dark_arm(void) { commands++; }
#include "input_actual.inc"
static void packet(uint64_t buttons, int32_t lx, int32_t ly, int32_t rx) {
static int64_t expected_sampling;
Ctx context = {{0}};
context.x[0] = 64; context.x[1] = 2; context.x[2] = 16; context.x[30] = 0x1234;
G32(16) = NPAD_NO1;
memset(memory + 64, 0xa5, 2 * STATE_SIZE + 8);
npad_states(&context);
CHECK(context.x[0] == 2);
expected_sampling++;
for (int i = 0; i < 2; i++) {
const uint64_t address = 64 + (uint64_t)i * STATE_SIZE;
CHECK(G64(address) == (uint64_t)(expected_sampling - i));
CHECK(G64(address + 8) == buttons);
CHECK((int32_t)G32(address + 16) == lx && (int32_t)G32(address + 20) == ly);
CHECK((int32_t)G32(address + 24) == rx && G32(address + 28) == 0);
CHECK(G32(address + 32) == ATTR_CONNECTED && G32(address + 36) == 0);
}
for (int i = 0; i < 8; i++) CHECK(memory[64 + 2 * STATE_SIZE + i] == 0xa5);
CHECK(commands == 0);
}
static void append_live(const char *text) {
const char *path = getenv("HOS_INPUT_LIVE"); CHECK(path);
FILE *file = fopen(path, "ab"); CHECK(file);
CHECK(fputs(text, file) >= 0 && fclose(file) == 0);
}
static void frame_clock_case(const char *name) {
const int hold = !strcmp(name, "frame-hold");
const int live = !strcmp(name, "live-frame");
const int reuse = !strcmp(name, "reuse-frame");
const int wall = !strcmp(name, "wall") || !strcmp(name, "zero") || !strcmp(name, "empty");
CHECK(hold || live || reuse || wall);
front = hold || wall;
packet(hold || wall ? 1ULL << B_A : 0, 0, hold || wall ? 32767 : 0, 0);
if (hold) {
now_ns += 7000000000LL; /* no native present returns during a compile */
packet(1ULL << B_A, 0, 32767, 0);
CHECK(hos_hid_last_read == now_ns); /* HID diagnostics stay wall-clock */
hos_present_count = 1011;
packet(1ULL << B_A, 0, 32767, 0);
hos_present_count = 1012; /* exactly 200 virtual ms, relative to initial frame 1000 */
packet(0, 0, 0, 0);
CHECK(pad_merges == 0 && key_queries == 0 && mouse_queries == 0);
hos_present_count = 1311;
packet(0, 0, 0, 0); /* exclusive margin still held at 5183 ms */
hos_present_count = 1312;
packet((1ULL << B_B) | (1ULL << B_X), 32767, 0, 8000);
CHECK(pad_merges > 0 && frame_reads > 0);
} else if (wall) {
now_ns += 7000000000LL;
packet((1ULL << B_B) | (1ULL << B_X), 32767, 0, 8000);
CHECK(pad_merges > 0 && frame_reads == 0);
} else if (live) {
append_live("0:A+LUP/200"); now_ns += 30000000;
packet(0, 0, 0, 0); /* incomplete live line remains unconsumed */
hos_present_count = 1060;
append_live("\n"); now_ns += 30000000;
packet(1ULL << B_A, 0, 32767, 0);
CHECK(presses[0].start_ms == 1000 && presses[0].end_ms == 1200);
append_live("0:B/200\n"); now_ns += 5000000;
packet(1ULL << B_A, 0, 32767, 0); /* file polling stays on the 30 ms wall clock */
now_ns += 30000000;
packet((1ULL << B_A) | (1ULL << B_B), 0, 32767, 0);
CHECK(presses[1].start_ms == 1000);
now_ns += 7000000000LL;
packet((1ULL << B_A) | (1ULL << B_B), 0, 32767, 0);
hos_present_count = 1072;
packet(0, 0, 0, 0);
} else {
for (int i = 0; i < MAX_PRESSES; i++) CHECK(parse_presses("0:A/200", 0, 1) == 1);
now_ns += 7000000000LL;
CHECK(parse_presses("0:B/200", 0, 1) == 0);
hos_present_count = 1012;
CHECK(parse_presses("0:B/200", 200, 1) == 0); /* original strict reuse boundary */
hos_present_count = 1013;
CHECK(parse_presses("0:B/200", 216, 1) == 1);
packet(1ULL << B_B, 0, 0, 0);
CHECK(npresses == MAX_PRESSES);
}
CHECK(bootstrap_polls == 1);
}
int main(int argc, char **argv) {
CHECK(argc == 2);
CHECK(!pads_in_background(NULL, 0)); /* out of the box: the window in front only */
CHECK(pads_in_background(NULL, 1)); /* the menu's row */
CHECK(pads_in_background("1", 0)); /* HOS_INPUT_BACKGROUND=1 without the menu */
CHECK(!pads_in_background("0", 1)); /* HOS_INPUT_BACKGROUND=0 wins over the menu */
memory = calloc(512, 1); CHECK(memory);
const char *name = argv[1];
if (!strcmp(name, "frame-hold") || !strcmp(name, "live-frame") || !strcmp(name, "reuse-frame") ||
!strcmp(name, "wall") || !strcmp(name, "zero") || !strcmp(name, "empty")) {
frame_clock_case(name);
printf("PASS: actual input frame clock %s\n", name);
free(memory);
return 0;
}
front = !strcmp(name, "foreground") || !strcmp(name, "exclusive-foreground");
menu_background = !strcmp(name, "pads-menu") || !strcmp(name, "pads-off");
const int exclusive = !strncmp(name, "exclusive-", 10);
const int pads = !exclusive && (front || !strcmp(name, "pads-env") || !strcmp(name, "pads-menu"));
const uint64_t physical = pads ? (1ULL << B_B) | (front ? 1ULL << B_X : 0) : 0;
if (!strcmp(name, "live-background")) {
packet(0, 0, 0, 0);
append_live("0:A+LUP/200"); now_ns += 30000000;
packet(0, 0, 0, 0); /* incomplete authored line is not consumed */
append_live("\n"); now_ns += 30000000;
packet(1ULL << B_A, 0, 32767, 0);
now_ns += 200000000;
packet(0, 0, 0, 0); /* consumed once, then released at the actual hold boundary */
} else if (!strcmp(name, "no-script-background")) {
packet(0, 0, 0, 0);
} else {
packet((1ULL << B_A) | physical, pads ? (front ? 32767 : -12000) : 0, 32767, pads ? 8000 : 0);
now_ns += 201000000;
packet(physical, pads ? (front ? 32767 : -12000) : 0, 0, pads ? 8000 : 0);
if (exclusive) {
CHECK(pad_merges == 0 && key_queries == 0 && mouse_queries == 0);
now_ns += 5100000000LL;
packet(front ? (1ULL << B_B) | (1ULL << B_X) : 0, front ? 32767 : 0, 0, front ? 8000 : 0);
}
}
CHECK(bootstrap_polls == 1); /* production startup poll is allowed, but its Y/stick result is discarded */
if (!front) {
CHECK(key_queries == 0 && mouse_queries == 0);
if (!pads) CHECK(pad_merges == 0);
}
if (!strcmp(name, "script-background")) CHECK(strstr(last_log, "game window not in front"));
if (front) CHECK(pad_merges > 0 && key_queries > 0 && mouse_queries > 0);
CHECK(focus_queries > 0);
printf("PASS: actual scripted parse/merge/Npad route %s\n", name);
free(memory);
return 0;
}

View File

@@ -0,0 +1,123 @@
// The production dispatch and away/back functions are included unchanged. Only the core grant and renderer are synthetic.
#define _Static_assert static_assert
#include "gpu/vk/vk_api.h"
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <thread>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
using namespace std::chrono_literals;
struct HosThread { int vc_held = 0, releases = 0, takes = 0; bool returning_caller = false; };
static thread_local HosThread* cur;
static thread_local bool callback_active;
static std::mutex lease_mu, renderer_mu;
static std::condition_variable lease_cv, progress_cv;
static HosThread* owner;
static bool enabled = true, producer_queued, producer_done;
static bool require_no_lease = true, need_producer;
static int vulkan_calls, fallback_calls;
static uint64_t want_gpu = 0x123456789abULL, want_size = 0x100002345ULL;
static const MkwVkRenderer* vk;
static int hos_vcores_on() { return enabled; }
static void vc_release(HosThread* t) {
std::lock_guard<std::mutex> lock(lease_mu);
CHECK(owner == t && t->vc_held == 1);
owner = nullptr;
t->vc_held = 0;
t->releases++;
lease_cv.notify_all();
}
static void vc_acquire(HosThread* t) {
// Returning to guest execution must happen after the renderer's lock has unwound.
if (t->returning_caller) {
CHECK(!callback_active);
CHECK(renderer_mu.try_lock());
renderer_mu.unlock();
}
std::unique_lock<std::mutex> lock(lease_mu);
CHECK(lease_cv.wait_for(lock, 5s, [] { return owner == nullptr; }));
owner = t;
t->vc_held = 1;
t->takes++;
}
static void renderer(uint64_t gpu, uint64_t size) {
std::lock_guard<std::mutex> render_lock(renderer_mu);
callback_active = true;
CHECK(gpu == want_gpu && size == want_size);
if (require_no_lease) CHECK(!cur || !cur->vc_held);
if (need_producer) {
std::unique_lock<std::mutex> lock(lease_mu);
// This is a liveness bound, not a performance assertion. The producer needs the caller's one allowed core.
CHECK(progress_cv.wait_for(lock, 2s, [] { return producer_done; }));
}
callback_active = false;
}
static void vulkan_invalidate(uint64_t gpu, uint64_t size) { vulkan_calls++; renderer(gpu, size); }
static void wgr_nvn_invalidate(uint64_t gpu, uint64_t size) { fallback_calls++; renderer(gpu, size); }
#include "actual_invalidate.inc"
int main(int argc, char** argv) {
CHECK(argc == 2);
const char* name = argv[1];
const bool fallback = !std::strcmp(name, "fallback") || !std::strcmp(name, "fallback-immediate");
const bool already_away = !std::strcmp(name, "already-away");
const bool non_guest = !std::strcmp(name, "non-guest");
enabled = std::strcmp(name, "disabled") != 0;
need_producer = !std::strcmp(name, "vulkan") || !std::strcmp(name, "fallback") || !std::strcmp(name, "producer");
// A separate case demonstrates producer starvation on the original wrapper, beyond the entry invariant.
require_no_lease = std::strcmp(name, "producer") != 0;
if (!std::strcmp(name, "zero")) want_gpu = want_size = 0;
MkwVkRenderer backend{};
backend.nvn_invalidate = vulkan_invalidate;
vk = fallback ? nullptr : &backend;
HosThread caller, producer;
caller.returning_caller = true;
cur = non_guest ? nullptr : &caller;
if (cur && enabled) owner = &caller, caller.vc_held = 1;
int outer_away = 0;
if (already_away) outer_away = hos_vcore_away();
const int before_release = caller.releases, before_take = caller.takes;
std::thread worker;
if (need_producer) {
worker = std::thread([&] {
cur = &producer;
{
std::lock_guard<std::mutex> lock(lease_mu);
producer_queued = true;
progress_cv.notify_all();
}
vc_acquire(&producer);
{
std::lock_guard<std::mutex> lock(lease_mu);
producer_done = true;
progress_cv.notify_all();
}
vc_release(&producer);
});
std::unique_lock<std::mutex> lock(lease_mu);
CHECK(progress_cv.wait_for(lock, 2s, [] { return producer_queued; }));
}
rx_nvn_invalidate(want_gpu, want_size);
if (worker.joinable()) worker.join();
CHECK(vulkan_calls == (fallback ? 0 : 1));
CHECK(fallback_calls == (fallback ? 1 : 0));
const bool owned_on_entry = cur && enabled && !already_away;
CHECK(caller.releases - before_release == (owned_on_entry ? 1 : 0));
CHECK(caller.takes - before_take == (owned_on_entry ? 1 : 0));
CHECK(caller.vc_held == (owned_on_entry ? 1 : 0));
CHECK(owner == (owned_on_entry ? &caller : nullptr));
if (need_producer) CHECK(producer_done && producer.takes == 1 && producer.releases == 1 && !producer.vc_held);
if (outer_away) {
hos_vcore_back();
CHECK(caller.vc_held == 1 && caller.releases == 1 && caller.takes == 1);
}
std::printf("PASS: %s actual invalidate wrapper\n", name);
}

122
tests/log_batch_test.c Normal file
View File

@@ -0,0 +1,122 @@
/* The host's log writer (host/hos_thread.c, the Windows queue, log_main and log_flush) extracted unmodified, with the CRT
stream calls as spies: what goes out, in how many writes, and when. Real threads, SRW locks and condition variables. */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static SRWLOCK spy_lock = SRWLOCK_INIT;
static char out[1 << 20];
static size_t out_len;
static volatile LONG writes;
static void spy_append(const void *p, size_t n) {
AcquireSRWLockExclusive(&spy_lock);
if (out_len + n <= sizeof out) memcpy(out + out_len, p, n), out_len += n;
ReleaseSRWLockExclusive(&spy_lock);
InterlockedIncrement(&writes);
}
__attribute__((unused)) static int spy_fputs(const char *s, FILE *f) {
(void)f;
spy_append(s, strlen(s));
return 0;
}
__attribute__((unused)) static size_t spy_fwrite(const void *p, size_t size, size_t n, FILE *f) {
(void)f;
spy_append(p, size * n);
return n;
}
__attribute__((unused)) static int spy_fflush(FILE *f) {
(void)f;
return 0;
}
static size_t out_size(void) {
AcquireSRWLockExclusive(&spy_lock);
const size_t n = out_len;
ReleaseSRWLockExclusive(&spy_lock);
return n;
}
#define fputs spy_fputs
#define fwrite spy_fwrite
#define fflush spy_fflush
/* (log_main is a thread that never returns: its for (;;) has no return after it. GCC's C front end warns for a static function
with no return statement at all, whatever its loop; clang, which builds the game, does not) */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
/* ACTUAL_LOG */
#pragma GCC diagnostic pop
#undef fflush
#undef fwrite
#undef fputs
#define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); return 1; } } while (0)
static char expect[1 << 20];
static size_t expect_len;
static int queue(int i) {
char line[64];
const int n = snprintf(line, sizeof line, "[hos test] line %d\n", i);
memcpy(expect + expect_len, line, (size_t)n);
expect_len += (size_t)n;
return log_queue(line);
}
static int wait_for(size_t n, DWORD ms) {
const ULONGLONG end = GetTickCount64() + ms;
while (out_size() < n && GetTickCount64() < end) Sleep(1);
return out_size() >= n;
}
static int same(void) {
return out_size() == expect_len && memcmp(out, expect, expect_len) == 0;
}
static int run(const char *name) {
if (!strcmp(name, "batch")) { /* (HOS_LOG_BATCH_MS=1000: the lines of that time in one write) */
CHECK(queue(0));
Sleep(20);
for (int i = 1; i < 50; i++) CHECK(queue(i));
Sleep(100);
CHECK(out_size() == 0); /* (still waiting for more) */
CHECK(wait_for(expect_len, 4000) && same());
CHECK(writes == 1);
Sleep(400);
CHECK(queue(50)); /* (the next batch: the writer back to waiting for a line) */
CHECK(wait_for(expect_len, 4000) && same() && writes == 2);
} else if (!strcmp(name, "default")) { /* (no setting: a line waits its 250 ms, then goes out) */
const ULONGLONG t0 = GetTickCount64();
CHECK(queue(0));
CHECK(wait_for(expect_len, 3000) && same());
CHECK(GetTickCount64() - t0 >= 200); /* (GetTickCount64 steps in 10-16 ms) */
} else if (!strcmp(name, "off")) { /* (HOS_LOG_BATCH_MS=0: each wake's lines at once, as before) */
const ULONGLONG t0 = GetTickCount64();
CHECK(queue(0));
CHECK(wait_for(expect_len, 3000) && same());
CHECK(GetTickCount64() - t0 < 200);
} else if (!strcmp(name, "burst")) { /* (HOS_LOG_BATCH_MS=2000: a queue half full goes out without the wait) */
size_t half = 0;
for (int i = 0; i < 1500; i++) {
CHECK(queue(i)); /* (every line queued: none written by this thread) */
if (i == 1023) half = expect_len;
}
CHECK(wait_for(half, 1000));
CHECK(wait_for(expect_len, 4000) && same());
} else if (!strcmp(name, "flush")) { /* (HOS_LOG_BATCH_MS=2000: a way out writes what waits, once) */
for (int i = 0; i < 10; i++) CHECK(queue(i));
Sleep(50);
CHECK(out_size() == 0);
log_flush(); /* (as atexit, hos_fatal and the window's close do) */
CHECK(same());
Sleep(2500); /* (the writer's wait ends with nothing left to write) */
CHECK(same());
} else {
CHECK(0);
}
printf("PASS: %s\n", name);
return 0;
}
int main(int argc, char **argv) {
if (argc != 2) return 2;
return run(argv[1]);
}

View File

@@ -0,0 +1,72 @@
// (hookshot) Every DEPBAR form lowers, to nothing. Until the AMD fixes the Ampere -> Maxwell lowering accepted
// DEPBAR.LE SB5, n alone and rejected the waits OoT's programs use, DEPBAR.LE SB0, 0x0, {1,2,3,4} ("native dependency
// barrier mode"), so three compute programs and a vertex program never ran. The encodings here are built from the
// fields (.LE bit 47, the scoreboard 44..46, the count 38..43, the set of scoreboards 32..37), not taken from a program.
// lower_depbar_test (built and run by tests/test_lower_depbar.py)
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
using Shader::Ampere::Instruction;
namespace {
// @PT EXIT, unconditional: the end every captured program needs. The control word (bits 96..127) is not read.
constexpr Instruction kExit{{0x0000794d, 0x00000000, 0x03800000, 0x000fea00}};
// @PT DEPBAR with the given fields
Instruction depbar(bool le, u32 scoreboard, u32 count, u32 set) {
return Instruction{{0x791a, (u32{le} << 15) | (scoreboard << 12) | (count << 6) | set, 0, 0x000fc600}};
}
int failed = 0;
void check(bool ok, const char* what) {
if (ok) return;
std::fprintf(stderr, "lower_depbar: %s\n", what);
failed++;
}
// The barrier then an EXIT lower, and the barrier adds no instruction.
void lowers(Instruction barrier, const char* what) {
const std::vector<Instruction> code{barrier, kExit};
const auto r = Shader::Ampere::LowerToMaxwell(code);
char text[200];
std::snprintf(text, sizeof text, "%s: %s", what, r ? "lowered to more than the EXIT" : std::string(r.error).c_str());
check(r && r.instructions.size() == 1, text);
}
// A word with unused bits set is still a rejected mode, not a silent pass.
void rejected(Instruction barrier, const char* what) {
const std::vector<Instruction> code{barrier, kExit};
const auto r = Shader::Ampere::LowerToMaxwell(code);
check(!r && r.error == std::string_view{"native dependency barrier mode"}, what);
}
} // namespace
int main() {
lowers(depbar(true, 5, 1, 0), "DEPBAR.LE SB5, 0x1 (the one form accepted at first)");
lowers(depbar(true, 0, 0, 0x1e), "DEPBAR.LE SB0, 0x0, {1,2,3,4} (OoT's compute programs)");
lowers(depbar(true, 0, 0, 0x0e), "DEPBAR.LE SB0, 0x0, {1,2,3} (OoT's vertex program)");
lowers(depbar(true, 0, 0, 0), "DEPBAR.LE SB0, 0x0");
lowers(depbar(true, 2, 63, 0), "DEPBAR.LE SB2, 0x3f");
lowers(depbar(false, 0, 0, 0x21), "DEPBAR {0,5} (no .LE: the set alone)");
lowers(depbar(true, 0, 0, 0x3f), "DEPBAR.LE, every scoreboard");
Instruction bad = depbar(true, 0, 0, 0);
bad.words[0] |= 0x00010000;
rejected(bad, "bits 16..31 set: not rejected");
bad = depbar(true, 0, 0, 0);
bad.words[1] |= 0x00010000;
rejected(bad, "bits 48..63 set: not rejected");
bad = depbar(true, 0, 0, 0);
bad.words[2] = 1;
rejected(bad, "bits 64..95 set: not rejected");
if (failed) return 1;
std::printf("lower_depbar: every dependency barrier form lowers to nothing\n");
return 0;
}

View File

@@ -0,0 +1,85 @@
// Synthetic native instructions built from encoding fields, not copied from a game program.
// Check the effective FFMA operands after interpreting the lowering's REGMAP prefixes.
// python -I -B tests/test_lower_register_markers.py
#include <array>
#include <cstdio>
#include <vector>
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
using Shader::Ampere::Instruction;
namespace {
constexpr Instruction kExit{{0x794d, 0, 0x03800000, 0}};
Instruction fma(u32 dst, u32 a, u32 b, u32 c) {
// @PT FFMA.FTZ Rd, Ra, Rb, Rc; register source form, round-to-nearest.
return Instruction{{0x7223U | (dst << 16) | (a << 24), b, c | 0x10000U, 0}};
}
bool check(u32 dst, u32 a, u32 b, u32 c, bool uniform) {
std::vector<Instruction> code;
// Referencing UR5 reserves the emulated uniform bank, so native GPRs >=216 are extended.
// Without a uniform reference, GPRs 216..245 are ordinary GPRs and still cannot be markers.
if (uniform) code.push_back(Instruction{{0x7c02U, 5, 0x08000f00U, 0}}); // MOV R0, UR5
code.push_back(fma(dst, a, b, c));
code.push_back(kExit);
const auto lowered = Shader::Ampere::LowerToMaxwell(code);
if (!lowered) {
std::fprintf(stderr, "register markers: lowering failed: %.*s\n",
int(lowered.error.size()), lowered.error.data());
return false;
}
std::array<u32, 256> mapping{};
for (u64 word : lowered.instructions) {
if ((word >> 51) == (0xefe0ULL >> 3)) {
const u32 runs = (word >> 40) & 3;
for (u32 i = 0; i < runs && i < 2; ++i) {
const u32 number = (word >> (i ? 20 : 0)) & 0xff;
const u32 target = (word >> (i ? 28 : 8)) & 0xff;
const u32 length = ((word >> (36 + 2 * i)) & 3) + 1;
for (u32 k = 0; k < length; ++k) mapping[number + k] = 256 + target + k;
}
continue;
}
if ((word & 0xff80000000000000ULL) == 0x5980000000000000ULL) {
const auto reg = [&](u32 shift) {
const u32 number = (word >> shift) & 0xff;
return mapping[number] ? mapping[number] : number;
};
const auto expected = [&](u32 native) {
return native >= (uniform ? 216U : 246U) ? native + 256 : native;
};
const std::array<u32, 4> got{reg(0), reg(8), reg(20), reg(39)};
const std::array<u32, 4> want{expected(dst), expected(a), expected(b), expected(c)};
if (got != want) {
std::fprintf(stderr,
"register markers: FFMA %u,%u,%u,%u (uniform=%d) became %u,%u,%u,%u; expected %u,%u,%u,%u\n",
dst, a, b, c, int(uniform), got[0], got[1], got[2], got[3], want[0], want[1], want[2], want[3]);
return false;
}
return true;
}
mapping.fill(0); // Prefixes apply to the next non-prefix instruction only.
}
std::fprintf(stderr, "register markers: missing FFMA\n");
return false;
}
} // namespace
int main() {
u32 cases = 0, failures = 0;
for (u32 source = 216; source < 238; ++source) {
for (bool uniform : {false, true}) {
const u32 dst = uniform ? 238 : 248;
failures += !check(dst, 17, source, dst, uniform);
++cases;
failures += !check(dst, source, 17, dst, uniform);
++cases;
}
}
// Two occupied marker-bank blocks, with a repeated destination/addend that must remain shared.
failures += !check(248, 220, 216, 248, false);
++cases;
std::printf("register markers: %u cases, %u failures\n", cases, failures);
return failures ? 1 : 0;
}

View File

@@ -0,0 +1,75 @@
// Production Bitmap/Upload/copy_over are inserted by the Python runner; CPU-only spies.
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <mutex>
#include <vector>
#define VK_NO_PROTOTYPES
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
using VmaAllocation = void*;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
/* ACTUAL_STRUCTS */
std::mutex bmp_mu;
std::map<VkBuffer, std::vector<uint8_t>> live;
uint64_t serial = 0;
int allocations = 0, destructions = 0, copies = 0;
bool fail = false;
VkBuffer copied_buffer = 0;
uint32_t copied_count = 0;
VkBufferImageCopy copied_region{};
bool create_buffer(uint64_t size, VkBufferUsageFlags usage, bool host_visible, VkBuffer& b, VmaAllocation& a, void** map) {
++allocations;
CHECK(usage == VK_BUFFER_USAGE_TRANSFER_SRC_BIT && host_visible);
if (fail) { b = 0; a = nullptr; return false; }
b = ++serial; a = reinterpret_cast<void*>(uintptr_t(b));
live[b].resize(size); *map = live.at(b).data(); return true;
}
void destroy_buffer(VkBuffer b, VmaAllocation a) {
CHECK(live.contains(b) && a == reinterpret_cast<void*>(uintptr_t(b)));
live.erase(b); ++destructions;
}
void vkCmdCopyBufferToImage(VkCommandBuffer, VkBuffer b, VkImage, VkImageLayout layout,
uint32_t count, const VkBufferImageCopy* region) {
CHECK(live.contains(b) && layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && count);
++copies; copied_buffer = b; copied_count = count; copied_region = *region;
}
/* ACTUAL_FUNCTION */
void bitmap(Bitmap& b, uint32_t side, uint64_t version) {
b.w = b.h = side; b.version = version; b.px.assign(size_t(side) * side * 4, uint8_t(version));
}
void upload(Bitmap& b, Upload& u) { copy_over(nullptr, 1, b, u, 10, 20, 4096, 4096); }
void check_bytes(const Bitmap& b, const Upload& u) {
CHECK(u.map == live.at(u.buf).data() && u.size >= b.px.size() && u.version == b.version);
CHECK(std::memcmp(u.map, b.px.data(), b.px.size()) == 0);
}
int main(int argc, char** argv) {
CHECK(argc == 2);
const bool failure = std::strcmp(argv[1], "failure") == 0;
Bitmap b; Upload u;
bitmap(b, 16, 1); upload(b, u); CHECK(live.size() == 1 && allocations == 1 && copies == 1); check_bytes(b, u);
const Upload before = u;
bitmap(b, 32, 2);
if (failure) {
fail = true; upload(b, u);
CHECK(u.buf == before.buf && u.alloc == before.alloc && u.map == before.map && u.size == before.size && u.version == before.version);
CHECK(live.size() == 1 && destructions == 0 && copies == 1);
fail = false;
}
upload(b, u); CHECK(live.size() == 1 && destructions == 1 && u.buf != before.buf); check_bytes(b, u);
CHECK(copied_region.imageOffset.x == 10 && copied_region.imageOffset.y == 20 && copied_region.imageExtent.width == 32);
const int a = allocations;
upload(b, u); CHECK(allocations == a); // cached capacity/version
bitmap(b, 8, 3); upload(b, u); CHECK(allocations == a && live.size() == 1); check_bytes(b, u);
b.keyed = true;
VkBufferImageCopy row{}; row.bufferOffset = 4; row.imageOffset = {2, 3, 0}; row.imageExtent = {4, 1, 1};
b.runs.push_back(row); upload(b, u);
CHECK(copied_buffer == u.buf && copied_count == 1 && copied_region.imageOffset.x == 12 && copied_region.imageOffset.y == 23);
CHECK(b.runs[0].imageOffset.x == 2 && b.runs[0].imageOffset.y == 3);
b.keyed = false;
for (uint32_t side : {64, 128, 256}) { bitmap(b, side, side); upload(b, u); CHECK(live.size() == 1); check_bytes(b, u); }
destroy_buffer(u.buf, u.alloc); CHECK(live.empty());
std::puts("PASS: menu uploads release replaced buffers, preserve old ownership on allocation failure, cache capacity and copy current pixels");
}

View File

@@ -0,0 +1,130 @@
// Authored allocator/address failures only; no Vulkan device, driver or game data.
// Null mapping and zero BDA are defensive injections, not conforming-driver outcomes.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <vector>
using VkBuffer = uint64_t;
using VkBufferUsageFlags = uint32_t;
struct Allocation {};
using VmaAllocation = Allocation*;
constexpr VkBuffer VK_NULL_HANDLE = 0;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT = 1;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_TRANSFER_DST_BIT = 2;
constexpr VkBufferUsageFlags VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT = 4;
constexpr int VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO = 1;
struct VkBufferDeviceAddressInfo { int sType; VkBuffer buffer = 0; };
struct { int device = 1; } dev;
static Allocation confirmed, poison;
static std::vector<uint32_t> storage;
static bool live;
static unsigned creates, destroys, addresses;
static std::string mode;
static constexpr uint32_t sentinel = 0xcccccccc;
static void require(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
static bool create_buffer(uint64_t size, VkBufferUsageFlags usage, bool host_visible,
VkBuffer& buffer, VmaAllocation& allocation, void** mapped) {
++creates;
require(creates == 1, "initialization allocated more than once");
require(size != 0 && size % 16 == 0 && size <= 4 * 1024 * 1024,
"unexpected allocation size");
require(usage == (VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) && host_visible && mapped,
"changed production allocation flags");
if (mode == "allocation_failure" || mode == "allocation_failure_outputs") {
if (mode == "allocation_failure_outputs") {
buffer = 0xffff;
allocation = &poison; // Failure outputs are not a confirmed allocation.
}
return false;
}
buffer = 7;
allocation = &confirmed;
live = true;
storage.assign(size / sizeof(uint32_t) + 2, sentinel);
*mapped = mode == "null_mapping" ? nullptr : storage.data() + 1;
return true;
}
[[maybe_unused]] static void destroy_buffer(VkBuffer buffer, VmaAllocation allocation) {
require(live && buffer == 7 && allocation == &confirmed,
"destroyed an unconfirmed or already-destroyed allocation");
live = false;
++destroys;
}
static uint64_t vkGetBufferDeviceAddress(int device, const VkBufferDeviceAddressInfo* info) {
require(live && device == 1 && info->sType == VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO &&
info->buffer == 7, "queried an unconfirmed allocation");
++addresses;
return mode == "zero_address" ? 0 : 0x7000;
}
#include "mesh_args_actual.inc"
static void initialized_ring() {
require(storage.size() == static_cast<std::size_t>(kMeshArgs) * 4 + 2,
"allocation does not hold the actual ring");
require(storage.front() == sentinel && storage.back() == sentinel,
"initialization wrote beyond the ring");
for (uint32_t i = 0; i < kMeshArgs; ++i) {
const auto* w = storage.data() + 1 + i * 4;
require(w[0] == 0 && w[1] == 1 && w[2] == 1 && w[3] == 0,
"changed initial indirect argument contents");
}
}
static void failed_no_retry() {
const auto prior_addresses = addresses;
require(!mesh_args_ready(), "failed initialization became ready");
require(creates == 1 && addresses == prior_addresses, "failed initialization was retried");
}
int main(int argc, char** argv) {
mode = argc == 2 ? argv[1] : "";
try {
if (mode == "allocation_failure" || mode == "allocation_failure_outputs") {
require(!mesh_args_ready(), "failed allocation reported ready");
require(!live && destroys == 0 && addresses == 0,
"failed create destroyed or used unconfirmed output");
require(mesh_args_buffer == (mode == "allocation_failure_outputs" ? 0xffffu : 0u) &&
mesh_args_alloc == (mode == "allocation_failure_outputs" ? &poison : nullptr) &&
!mesh_args_words && !mesh_args_address,
"changed initial allocation-failure behavior");
failed_no_retry();
} else if (mode == "null_mapping" || mode == "zero_address") {
require(!mesh_args_ready(), "invalid initialization reported ready");
require(!live && destroys == 1, "failed initialization retained a confirmed buffer");
require(!mesh_args_buffer && !mesh_args_alloc && !mesh_args_words && !mesh_args_address,
"failed initialization retained stale handles, pointer or address");
require(addresses == (mode == "zero_address" ? 1u : 0u), "unexpected BDA query");
if (mode == "zero_address") initialized_ring();
else require(std::all_of(storage.begin(), storage.end(), [](auto w) { return w == sentinel; }),
"null mapping caused a write");
failed_no_retry();
require(destroys == 1, "permanent failure repeated cleanup");
} else if (mode == "success_reuse") {
require(mesh_args_ready(), "valid initialization failed");
require(live && destroys == 0 && creates == 1 && addresses == 1 && mesh_args_buffer == 7 &&
mesh_args_alloc == &confirmed && mesh_args_words == storage.data() + 1 &&
mesh_args_address == 0x7000, "successful result incomplete");
initialized_ring();
mesh_args_words[4] = 123;
mesh_args_words[7] = 456;
require(mesh_args_ready(), "successful initialization stopped being ready");
require(creates == 1 && destroys == 0 && addresses == 1 &&
mesh_args_words[4] == 123 && mesh_args_words[7] == 456,
"reuse recreated, destroyed or reset the ring");
} else {
throw std::runtime_error("unknown fixture case");
}
std::printf("PASS %s\n", mode.c_str());
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "FAIL %s: %s\n", mode.c_str(), e.what());
return 1;
}
}

251
tests/motion_test.c Normal file
View File

@@ -0,0 +1,251 @@
/* host/hos_motion.h with synthetic SDL readings: no SDL, controller or game. tests/test_motion.py builds and runs it.
A simulated pad: its true up vector (the world's up in its frame) turns with the true rotation, the accelerometer
reads it (times G), and the gyroscope reads the true rotation plus an offset (its bias) and noise (a tiny one, a
real sensor's, so no two readings repeat; or a hand's tremor). */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "hos_motion.h"
static int failures;
#define NEAR(a, b, tol) do { const double _a = (a), _b = (b); if (fabs(_a - _b) > (tol)) { \
fprintf(stderr, "FAIL %s:%d: %s = %.5f, want %.5f\n", __FILE__, __LINE__, #a, _a, _b); failures++; } } while (0)
static float su[3]; /* the true up, in the pad's frame */
static float offset[3]; /* the gyroscope's bias */
static float tremor; /* the noise's size, rad/s (each axis, uniform) */
static float anoise; /* the accelerometer's, G (each axis, uniform; 0: a real sensor's least) */
static unsigned rng = 1;
static float noise(void) {
rng = rng * 1664525u + 1013904223u;
return ((float)(rng >> 8) / 16777216.0f * 2 - 1);
}
static void pose(float x, float y, float z) {
const float n = sqrtf(x * x + y * y + z * z);
su[0] = x / n, su[1] = y / n, su[2] = z / n;
}
#define FLAT 0, 1, 0 /* face up on a table: gravity's reaction on SDL's +Y */
#define UPRIGHT 0, 0, -1 /* held upright, face to the player: the top (SDL's -Z) up */
/* the true rotation (gx, gy, gz) rad/s for seconds, read every dt */
static void run_dt(HosMotion *m, float gx, float gy, float gz, float seconds, float dt, float sens_h, float sens_v,
int inv_h, int inv_v) {
const int n = (int)lroundf(seconds / dt);
for (int i = 0; i < n; i++) {
const float w[3] = {gx, gy, gz};
const float an = (anoise > 0 ? anoise * HOS_MOTION_G : 1e-5f);
const float a[3] = {su[0] * HOS_MOTION_G + an * noise(), su[1] * HOS_MOTION_G + an * noise(),
su[2] * HOS_MOTION_G + an * noise()};
float g[3];
for (int k = 0; k < 3; k++) g[k] = w[k] + offset[k] + (tremor > 0 ? tremor : 1e-6f) * noise();
hos_motion_update(m, a, g, dt, sens_h, sens_v, inv_h, inv_v);
/* the truth turns: a world vector seen from a frame turning at w turns the other way */
for (int s = 0; s < 10; s++) {
const float h = dt / 10;
const float p[3] = {su[0] + (-w[1] * su[2] + w[2] * su[1]) * h, su[1] + (-w[2] * su[0] + w[0] * su[2]) * h,
su[2] + (-w[0] * su[1] + w[1] * su[0]) * h};
pose(p[0], p[1], p[2]);
}
}
}
static void run(HosMotion *m, float gx, float gy, float gz, float seconds, float sens_h, float sens_v, int inv_h,
int inv_v) {
run_dt(m, gx, gy, gz, seconds, 0.004f, sens_h, sens_v, inv_h, inv_v);
}
static void start(HosMotion *m, float x, float y, float z) { /* a fresh filter, the pad at rest, no bias or tremor */
hos_motion_reset(m);
pose(x, y, z);
offset[0] = offset[1] = offset[2] = 0;
tremor = 0, anoise = 0;
}
static float dot3(const float a[3], const float b[3]) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; }
static void same_direction(const HosMotion *a, const HosMotion *b, double tol) {
for (int i = 0; i < 9; i++) NEAR(a->out.direction[i], b->out.direction[i], tol);
}
int main(void) {
HosMotion m, n;
/* before any reading: a pad lying still, face up */
hos_motion_reset(&m);
NEAR(m.out.accel[2], -1, 1e-6);
NEAR(m.out.direction[0] + m.out.direction[4] + m.out.direction[8], 3, 1e-6);
/* lying face up: (0, 0, -1) G in the Switch's frame, no rate, the direction the identity */
start(&m, FLAT);
run(&m, 0, 0, 0, 1, 1, 1, 0, 0);
NEAR(m.out.accel[0], 0, 1e-3);
NEAR(m.out.accel[1], 0, 1e-3);
NEAR(m.out.accel[2], -1, 1e-3);
NEAR(m.out.gyro[0] + m.out.gyro[1] + m.out.gyro[2], 0, 1e-6);
NEAR(m.out.direction[0] + m.out.direction[4] + m.out.direction[8], 3, 0.01);
/* the Switch's frame and units (Eden's sdl_driver.cpp): a turn about SDL's +Y (the pad flat, turning left) at one
turn a second is +1 in z; about SDL's +X (the front up), +1 in x; held upright, SDL's -Z up, -Z is +y */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
run(&m, 0, HOS_MOTION_TAU, 0, 0.05f, 1, 1, 0, 0);
NEAR(m.out.gyro[2], 1, 1e-3);
NEAR(m.out.gyro[0], 0, 1e-3);
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
run(&m, HOS_MOTION_TAU, 0, 0, 0.02f, 1, 1, 0, 0);
NEAR(m.out.gyro[0], 1, 0.02);
start(&m, UPRIGHT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
NEAR(m.out.accel[1], -1, 1e-3);
run(&m, 0, 0, -HOS_MOTION_TAU, 0.05f, 1, 1, 0, 0);
NEAR(m.out.gyro[1], 1, 1e-3);
/* the rotation is the angular velocity summed: half a second at a quarter turn a second about +z is 0.125 */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
run(&m, 0, HOS_MOTION_TAU / 4, 0, 0.5f, 1, 1, 0, 0);
NEAR(m.out.rotation[2], 0.125, 3e-3);
/* how the pad is held when the filter starts does not matter: started flat and then tilted 0.7 rad nose up, or
started there, the direction is the same (Link-22's start-pose case: the console's orientation follows gravity) */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.5f, 1, 1, 0, 0);
run(&m, 1, 0, 0, 0.7f, 1, 1, 0, 0);
run(&m, 0, 0, 0, 2, 1, 1, 0, 0);
start(&n, su[0], su[1], su[2]);
run(&n, 0, 0, 0, 2.5f, 1, 1, 0, 0);
same_direction(&m, &n, 0.03);
/* ... rolled 40 degrees and pitched 0.6 at the start, then levelled: the same as a level start in that pose */
start(&m, 0, cosf(0.6f), -sinf(0.6f));
{
const float c = cosf(0.7f), sn = sinf(0.7f), x = su[0] * c - su[1] * sn, y = su[0] * sn + su[1] * c;
pose(x, y, su[2]);
}
run(&m, 0, 0, 0, 0.5f, 1, 1, 0, 0);
for (int i = 0; i < 80 && fabsf(su[0]) > 0.01f; i++) run(&m, 0, 0, su[0] > 0 ? -0.5236f : 0.5236f, 0.02f, 1, 1, 0, 0);
run(&m, 0, 0, 0, 2, 1, 1, 0, 0);
start(&n, su[0], su[1], su[2]);
run(&n, 0, 0, 0, 2.5f, 1, 1, 0, 0);
/* (the tilt agrees: the gravity each matrix implies; the heading about the vertical is the gyro's own, as on the
console, so compare the matrices' third columns, the world's vertical seen from the pad) */
{
const float a3[3] = {m.out.direction[2], m.out.direction[5], m.out.direction[8]};
const float b3[3] = {n.out.direction[2], n.out.direction[5], n.out.direction[8]};
NEAR(dot3(a3, b3), 1, 2e-3);
}
/* the gyroscope's resting rate is learned on a table and taken off */
start(&m, FLAT);
offset[0] = 0.012f, offset[1] = -0.008f, offset[2] = 0.010f;
run(&m, 0, 0, 0, 4, 1, 1, 0, 0);
NEAR(m.bias[0], 0.012 / HOS_MOTION_TAU, 2e-4);
NEAR(m.bias[2], -0.008 / HOS_MOTION_TAU, 2e-4); /* (SDL's y is the Switch's z) */
NEAR(m.out.gyro[0] + m.out.gyro[1] + m.out.gyro[2], 0, 1e-6);
/* ... a large one too, and a noisier sensor's (0.0067 rad/s and 5 mG on each axis) */
start(&m, FLAT);
offset[1] = 0.2f;
run(&m, 0, 0, 0, 4, 1, 1, 0, 0);
NEAR(m.bias[2], 0.2 / HOS_MOTION_TAU, 2e-4);
start(&m, FLAT);
offset[1] = 0.02f, tremor = 0.0116f, anoise = 0.0087f;
run(&m, 0, 0, 0, 6, 1, 1, 0, 0);
NEAR(m.bias[2], 0.02 / HOS_MOTION_TAU, 4e-4);
/* ... but a slow turn in the hand (tremor) is aim, and a steady machine pitch is not stillness: neither is learned */
start(&m, FLAT);
run(&m, 0, 0, 0, 1, 1, 1, 0, 0);
tremor = 0.03f;
run(&m, 0, 0.0524f, 0, 2, 1, 1, 0, 0);
NEAR(m.bias[2], 0, 2e-4);
start(&m, FLAT);
run(&m, 0.2f, 0, 0, 3, 1, 1, 0, 0);
NEAR(m.bias[0], 0, 2e-4);
/* under the console's zero-drift threshold (0.007 turns a second) a rate counts as none */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
run(&m, 0, 0.03f, 0, 0.1f, 1, 1, 0, 0);
NEAR(m.out.gyro[2], 0, 1e-6);
/* the Motion deadzone row: Low (the console's tight mode) lets a 0.005 turns/s turn through, Standard does not,
High (loose) also drops 0.008 */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
m.threshold = HOS_MOTION_THRESHOLD_TIGHT;
run(&m, 0, 0.03f, 0, 0.1f, 1, 1, 0, 0);
NEAR(m.out.gyro[2], 0.03 / HOS_MOTION_TAU, 1e-4);
m.threshold = HOS_MOTION_THRESHOLD_LOOSE;
run(&m, 0, 0.05f, 0, 0.1f, 1, 1, 0, 0);
NEAR(m.out.gyro[2], 0, 1e-6);
m.threshold = 0;
/* the sensitivities and the inverts: the turn (about the world's up) and the tilt (the rest) each scaled */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.3f, 1, 1, 0, 0);
run(&m, 0, HOS_MOTION_TAU, 0, 0.05f, 2, 0.5f, 0, 0);
NEAR(m.out.gyro[2], 2, 0.01);
run(&m, 0, HOS_MOTION_TAU, 0, 0.05f, 1, 1, 1, 0);
NEAR(m.out.gyro[2], -1, 0.01);
start(&m, FLAT);
run(&m, 0, 0, 0, 0.3f, 1, 1, 0, 0);
run(&m, HOS_MOTION_TAU, 0, 0, 0.01f, 3, 0.5f, 0, 0);
NEAR(m.out.gyro[0], 0.5, 0.02);
NEAR(m.out.gyro[2], 0, 0.01);
run(&m, HOS_MOTION_TAU, 0, 0, 0.01f, 1, 1, 0, 1);
NEAR(m.out.gyro[0], -1, 0.03);
/* ... and the orientation keeps a scaled or inverted tilt while held: 0.5 rad nose up at 200% stays at 1.0 for 3 s,
and inverted at -0.5 (the gravity pull acts on the real filter only; hckrman101). Compared against the matrix of
a pad really tilted that far, at 100% */
for (int c = 0; c < 2; c++) {
const float sens = c ? 1.0f : 2.0f, want = c ? -0.5f : 1.0f;
start(&m, FLAT);
run(&m, 0, 0, 0, 0.5f, 1, sens, 0, c);
run(&m, 1, 0, 0, 0.5f, 1, sens, 0, c);
run(&m, 0, 0, 0, 3, 1, sens, 0, c);
start(&n, FLAT);
run(&n, 0, 0, 0, 0.5f, 1, 1, 0, 0);
run(&n, want > 0 ? 1.0f : -1.0f, 0, 0, fabsf(want), 1, 1, 0, 0);
run(&n, 0, 0, 0, 3, 1, 1, 0, 0);
same_direction(&m, &n, 0.04);
}
/* a scaled turn while tilted keeps the handed-out orientation upright (the scaling in the world's frame,
hckrman101): 0.5 rad nose up at 200% (1.0 out), then a quarter turn about the vertical at 200% (a half turn out):
the world's up seen from the pad, its third column, is still a real 1.0 rad tilt's */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.5f, 2, 2, 0, 0);
run(&m, 1, 0, 0, 0.5f, 2, 2, 0, 0);
{ /* (the quarter turn: about the world's up, seen from the pad) */
const float u[3] = {su[0], su[1], su[2]};
run(&m, u[0] * 1.5708f, u[1] * 1.5708f, u[2] * 1.5708f, 1, 2, 2, 0, 0);
}
run(&m, 0, 0, 0, 1, 2, 2, 0, 0);
start(&n, FLAT);
run(&n, 0, 0, 0, 0.5f, 1, 1, 0, 0);
run(&n, 1, 0, 0, 1, 1, 1, 0, 0);
run(&n, 0, 0, 0, 1.5f, 1, 1, 0, 0);
{
const float a3[3] = {m.out.direction[2], m.out.direction[5], m.out.direction[8]};
const float b3[3] = {n.out.direction[2], n.out.direction[5], n.out.direction[8]};
NEAR(dot3(a3, b3), 1, 2e-3);
}
/* a repeat of the last reading is no reading (its time goes into the next), and a stall is no step */
start(&m, FLAT);
run(&m, 0, 0, 0, 0.2f, 1, 1, 0, 0);
{
const float a[3] = {0, HOS_MOTION_G, 0}, g0[3] = {0, 0, 0}, g1[3] = {0, HOS_MOTION_TAU, 0};
NEAR(hos_motion_update(&m, a, g0, 0.004f, 1, 1, 0, 0), 1, 0);
NEAR(hos_motion_update(&m, a, g0, 0.004f, 1, 1, 0, 0), 0, 0);
const float r0 = m.out.rotation[2];
NEAR(hos_motion_update(&m, a, g1, 0.004f, 1, 1, 0, 0), 1, 0);
NEAR(m.out.rotation[2] - r0, 0.008, 2e-4);
const float r1 = m.out.rotation[2];
hos_motion_update(&m, a, g0, 1.0f, 1, 1, 0, 0);
NEAR(m.out.rotation[2], r1, 1e-6);
}
if (failures) return 1;
puts("motion: ok");
return 0;
}

251
tests/native_suatom.cpp Normal file
View File

@@ -0,0 +1,251 @@
// Independently authored encoding fields; no shader dump or game program is used.
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
using Shader::Ampere::Instruction;
using Shader::Ampere::LoweredProgram;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
static void field(Instruction& i, unsigned at, unsigned count, uint32_t value) {
CHECK(count <= 32 && (count == 32 || value < (uint64_t{1} << count)));
for (unsigned bit = 0; bit < count; ++bit) {
auto& word = i.words[(at + bit) / 32];
const uint32_t mask = uint32_t{1} << ((at + bit) % 32);
word = (word & ~mask) | ((value >> bit & 1) ? mask : 0);
}
}
static Instruction op(uint32_t code, uint32_t pred = 7) {
Instruction i{};
field(i, 0, 12, code); field(i, 12, 4, pred);
return i;
}
static Instruction suatom(uint32_t d = 13, uint32_t a = 9, uint32_t b = 17,
uint32_t bank = 3, uint32_t offset = 0x24, uint32_t pred = 7) {
auto i = op(0x593, pred);
field(i, 16, 8, d); field(i, 24, 8, a); field(i, 32, 8, b);
field(i, 40, 14, offset); field(i, 54, 5, bank);
field(i, 77, 4, 7); // Strong(GPU), not acquire/release
field(i, 81, 3, 7); // no fault-predicate destination
field(i, 84, 3, 1); // normal eviction; U32 ADD, 1D, sample addressing, IGN are zero
return i;
}
static Instruction exit_op() {
auto i = op(0x94d); field(i, 87, 3, 7); return i;
}
static LoweredProgram lower(Instruction i) {
return Shader::Ampere::LowerToMaxwell(std::vector<Instruction>{i, exit_op()});
}
static uint64_t address(unsigned index) { return 0x50 + index * 8 + (index + 1) / 3 * 8; }
static void lowering_checks() {
const auto r = lower(suatom());
CHECK(r);
CHECK(r.required_texture_buffer == 3 && r.instructions.size() == 2);
const uint64_t w = r.instructions[0];
CHECK(w >> 55 == 0xea00000000000000ULL >> 55);
CHECK((w & 255) == 13 && (w >> 8 & 255) == 9 && (w >> 20 & 255) == 17);
CHECK((w >> 36 & 0x1fff) == 0x24 && (w >> 54 & 1) == 0);
CHECK((w >> 29 & 15) == 0 && (w >> 33 & 7) == 0 && (w >> 49 & 31) == 0);
for (uint32_t p : {2U, 10U, 7U}) {
const auto predicated = lower(suatom(13, 9, 17, 3, 0x24, p));
CHECK(predicated && (predicated.instructions[0] >> 16 & 15) == p);
}
CHECK(lower(suatom(255, 255, 255, 17, 0x1fff)));
for (auto [at, count, value] : std::vector<std::array<uint32_t, 3>>{
{0,12,0x793}, {0,12,0xf93}, {54,5,18}, {40,14,0x2000},
{59,2,1}, {61,3,1}, {64,8,1}, {72,1,1}, {73,3,1}, {76,1,1},
{77,4,0}, {77,4,5}, {77,4,10}, {81,3,2}, {84,3,0}, {87,4,1},
{91,1,1}, {95,1,1}, {96,1,1}, {104,1,1}, {126,1,1}, {127,1,1}}) {
auto bad = suatom(); field(bad, at, count, value);
const auto refused = lower(bad);
CHECK(!refused && refused.instructions.empty() && refused.error_offset == 0);
}
auto scheduled = suatom(); field(scheduled, 105, 21, 0x155555);
CHECK(lower(scheduled).instructions == r.instructions);
const auto same = Shader::Ampere::LowerToMaxwell(std::vector<Instruction>{suatom(), suatom(7), exit_op()});
CHECK(same && same.required_texture_buffer == 3);
const auto conflict = Shader::Ampere::LowerToMaxwell(std::vector<Instruction>{suatom(), suatom(7,9,17,2), exit_op()});
CHECK(!conflict && conflict.error == "native surface descriptor buffer conflict" && conflict.error_offset == 16);
// The addend alone is high: it must participate in REGMAP discovery, and has scalar width.
const auto high = lower(suatom(13, 9, 250));
CHECK(high && high.register_map_words > 0);
CHECK(Shader::Ampere::RenameableFields(0x593) == 7);
CHECK(Shader::Ampere::FieldSpan(0x593, 0) == 1 && Shader::Ampere::FieldSpan(0x593, 1) == 1 &&
Shader::Ampere::FieldSpan(0x593, 2) == 1 && Shader::Ampere::FieldSpan(0x593, 3) == 0);
// A conditional BRA crosses an instruction expanded by high-register mapping.
auto branch = op(0x947, 2); field(branch, 32, 32, 16); field(branch, 87, 3, 7);
const auto relocated = Shader::Ampere::LowerToMaxwell(std::vector<Instruction>{branch, suatom(250,250,250), exit_op()});
CHECK(relocated && relocated.register_map_words > 0);
const auto delta = static_cast<int32_t>((relocated.instructions[0] >> 20 & 0xffffff) << 8) >> 8;
CHECK(address(0) + 8 + delta == address(relocated.source_instruction_offsets[2]));
// Explicit MEMBAR.SC.GPU remains a separate instruction; SUATOM adds no fences.
auto barrier = op(0x992); field(barrier, 76, 3, 2);
const auto barriers = Shader::Ampere::LowerToMaxwell(std::vector<Instruction>{barrier, suatom(), barrier, exit_op()});
CHECK(barriers && barriers.instructions.size() == 4);
CHECK(barriers.instructions[0] >> 48 == 0xef98 && barriers.instructions[2] == barriers.instructions[0]);
std::puts("PASS: direct SUATOM fields, predicates, scalar REGMAP, relocation, descriptor bounds/conflicts and mode refusals");
}
#ifndef SUATOM_LOWERING_ONLY
#include <filesystem>
#include <fstream>
#include <map>
#include <set>
#include <spirv/unified1/spirv.hpp>
#include "shader_recompiler/backend/spirv/emit_spirv.h"
#include "shader_recompiler/frontend/maxwell/translate_program.h"
#include "shader_recompiler/host_translate_info.h"
struct Environment final : Shader::Environment {
std::vector<uint64_t> code;
uint32_t bank, offset;
Environment(const LoweredProgram& lowered, uint32_t o) : bank(lowered.required_texture_buffer), offset(o) {
stage = Shader::Stage::Compute;
code.resize(0x50 / 8);
for (auto word : lowered.instructions) {
if (code.size() % 4 == 0) code.push_back(0);
code.push_back(word);
}
}
uint64_t ReadInstruction(uint32_t at) override { CHECK(at / 8 < code.size()); return code[at/8]; }
uint32_t ReadCbufValue(uint32_t b, uint32_t o) override {
CHECK(b == bank && o == offset); return 0x1234;
}
Shader::TextureType ReadTextureType(uint32_t h) override { CHECK(h == 0x1234); return Shader::TextureType::Color1D; }
Shader::TexturePixelFormat ReadTexturePixelFormat(uint32_t) override { return Shader::TexturePixelFormat::R32_UINT; }
bool IsTexturePixelFormatInteger(uint32_t h) override { CHECK(h == 0x1234); return true; }
uint32_t ReadViewportTransformState() override { return 0; }
uint32_t TextureBoundBuffer() const override { return bank; }
uint32_t LocalMemorySize() const override { return 0; }
uint32_t SharedMemorySize() const override { return 4; }
std::array<uint32_t,3> WorkgroupSize() const override { return {32,1,1}; }
bool HasHLEMacroState() const override { return false; }
std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(uint32_t,uint32_t) override { return std::nullopt; }
void Dump(uint64_t,uint64_t) override {}
};
static Instruction move(uint32_t reg, uint32_t value) {
auto i = op(0x802); field(i,16,8,reg); field(i,32,32,value); field(i,72,4,15); return i;
}
static void module_check(const std::filesystem::path& directory, const char* name,
uint32_t d, uint32_t a, uint32_t b, uint32_t pred = 7,
bool branch = false, bool explicit_barriers = false) {
std::vector<Instruction> source;
if (d == 239) {
auto uniform = op(0x882); field(uniform,32,32,42); source.push_back(uniform);
}
// Initialize old destination, coordinate and addend, respecting input aliases.
source.push_back(move(d, 99)); source.push_back(move(a, 3));
if (b != a) source.push_back(move(b, 11));
if (pred != 7 || branch) {
auto tid = op(0x919); field(tid,16,8,1); field(tid,72,8,33); source.push_back(tid);
auto compare = op(0x20c); field(compare,24,8,1); field(compare,32,8,255);
field(compare,76,3,5); field(compare,81,3,2); field(compare,84,3,7); field(compare,87,3,7);
source.push_back(compare); // P2 = threadIdx.x != 0, so its value is not constant
}
if (branch) {
auto jump = op(0x947,2); field(jump,32,32,16); field(jump,87,3,7); source.push_back(jump);
}
auto barrier = op(0x992); field(barrier,76,3,2);
if (explicit_barriers) source.push_back(barrier);
source.push_back(suatom(d,a,b,3,0x24,pred));
if (explicit_barriers) source.push_back(barrier);
// A side effect consumes the old value returned by the actual SUATOM translation.
auto store = op(0x388); field(store,24,8,255); field(store,32,8,d); field(store,73,3,4);
source.push_back(store); source.push_back(exit_op());
const auto lowered = Shader::Ampere::LowerToMaxwell(source); CHECK(lowered);
if (d == 239) CHECK(lowered.register_map_words > 0);
Environment env(lowered,0x90);
Shader::ObjectPool<Shader::IR::Inst> insts;
Shader::ObjectPool<Shader::IR::Block> blocks;
Shader::ObjectPool<Shader::Maxwell::Flow::Block> flow_blocks;
Shader::Maxwell::Flow::CFG cfg{env,flow_blocks,0x50};
Shader::HostTranslateInfo host{}; host.support_int64 = host.support_float64 = host.support_float16 = true;
host.support_conditional_barrier = true;
auto program = Shader::Maxwell::TranslateProgram(insts,blocks,env,cfg,host);
CHECK(program.info.image_descriptors.size() == 1);
const auto& descriptor = program.info.image_descriptors[0];
CHECK(descriptor.cbuf_index == 3 && descriptor.cbuf_offset == 0x90 && descriptor.is_read && descriptor.is_written);
CHECK(descriptor.format == Shader::ImageFormat::R32_UINT && descriptor.type == Shader::TextureType::Color1D);
Shader::Profile profile{}; profile.supported_spirv = 0x00010600;
profile.support_int64 = true; profile.support_descriptor_aliasing = true;
const auto spirv = Shader::Backend::SPIRV::EmitSPIRV(profile,program);
std::map<uint32_t,uint32_t> constants;
std::map<uint32_t,std::vector<uint32_t>> phis;
std::vector<uint32_t> stored;
uint32_t atomic = 0, atomic_count = 0, barriers = 0, selects = 0;
bool texel_pointer = false;
for (size_t at = 5; at < spirv.size();) {
const uint32_t count = spirv[at] >> 16; CHECK(count && at + count <= spirv.size());
const auto opcode = static_cast<spv::Op>(spirv[at] & 0xffff);
const auto* w = &spirv[at+1];
if (opcode == spv::Op::OpConstant && count == 4) constants[w[1]] = w[2];
if (opcode == spv::Op::OpImageTexelPointer) {
CHECK(constants.at(w[3]) == 3); texel_pointer = true;
}
if (opcode == spv::Op::OpAtomicIAdd) {
CHECK(count == 7 && constants.at(w[3]) == uint32_t(spv::ScopeDevice) && constants.at(w[4]) == 0);
CHECK(constants.at(w[5]) == (a == b ? 3U : 11U));
atomic = w[1]; ++atomic_count;
}
if (opcode == spv::Op::OpMemoryBarrier) {
CHECK(constants.at(w[0]) == uint32_t(spv::ScopeDevice));
CHECK(constants.at(w[1]) & uint32_t(spv::MemorySemanticsImageMemoryMask));
CHECK(constants.at(w[1]) & uint32_t(spv::MemorySemanticsAcquireReleaseMask));
++barriers;
}
if (opcode == spv::Op::OpSelectionMerge) ++selects;
if (opcode == spv::Op::OpPhi) {
for (uint32_t n = 2; n + 1 < count - 1; n += 2) phis[w[1]].push_back(w[n]);
}
if (opcode == spv::Op::OpStore) stored.push_back(w[1]);
at += count;
}
CHECK(atomic_count == 1 && texel_pointer);
CHECK(barriers == (explicit_barriers ? 2U : 0U));
CHECK((pred == 7 && !branch) || selects > 0);
std::set<uint32_t> values{atomic};
bool changed;
do {
changed = false;
for (const auto& [result,inputs] : phis)
for (auto input : inputs) if (values.contains(input)) changed |= values.insert(result).second;
} while (changed);
bool consumed = false;
for (auto value : stored) consumed |= values.contains(value);
CHECK(consumed == (d != 255)); // discarding the result must still retain the atomic side effect
std::filesystem::create_directories(directory);
std::ofstream out(directory / (std::string(name) + ".spv"),std::ios::binary);
out.write(reinterpret_cast<const char*>(spirv.data()),static_cast<std::streamsize>(spirv.size()*4)); CHECK(out.good());
std::printf("PASS: %s actual lowering/Maxwell/IR/SPIR-V, descriptor and atomic result contract\n",name);
}
#endif
int main(int argc, char** argv) {
if (argc == 2 && std::string(argv[1]) == "--expect-original-rejection") {
const auto r = lower(suatom());
CHECK(!r && r.error == "unsupported native opcode" && r.error_offset == 0);
std::puts("PASS: original production lowering rejects the authored direct SUATOM"); return 0;
}
lowering_checks();
#ifndef SUATOM_LOWERING_ONLY
CHECK(argc == 2);
const std::filesystem::path directory{argv[1]};
module_check(directory,"separate",13,9,17);
module_check(directory,"dest-coordinate-alias",9,9,17);
module_check(directory,"dest-addend-alias",17,9,17);
module_check(directory,"all-alias",9,9,9);
module_check(directory,"high-addend",13,9,250);
module_check(directory,"high-all-alias",250,250,250);
module_check(directory,"reserved239-alias",239,239,239);
module_check(directory,"predicate",13,9,17,2);
module_check(directory,"inverted-predicate",13,9,17,10);
module_check(directory,"relocated-branch",250,250,250,7,true);
module_check(directory,"explicit-barriers",13,9,17,7,false,true);
module_check(directory,"discard-old-value",255,9,17);
#endif
}

View File

@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.20)
project(native_suatom LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT DEFINED REPO_ROOT OR NOT DEFINED SPIRV_HEADERS_ROOT)
message(FATAL_ERROR "REPO_ROOT and SPIRV_HEADERS_ROOT are required")
endif()
set(SHADER "${REPO_ROOT}/gpu/vk/eden/shader_recompiler")
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
"${SHADER}/backend/spirv/*.cpp"
"${SHADER}/frontend/ir/*.cpp"
"${SHADER}/frontend/maxwell/*.cpp"
"${SHADER}/ir_opt/*.cpp"
"${REPO_ROOT}/gpu/vk/sirit/src/*.cpp")
add_executable(native_suatom "${REPO_ROOT}/tests/native_suatom.cpp" ${SOURCES})
target_include_directories(native_suatom PRIVATE
"${REPO_ROOT}/gpu/vk/compat" "${REPO_ROOT}/gpu/compat"
"${REPO_ROOT}/gpu/vk/eden" "${REPO_ROOT}/gpu/third_party/suyu"
"${REPO_ROOT}/gpu/vk/sirit/include" "${REPO_ROOT}/gpu/vk/sirit/src"
"${SPIRV_HEADERS_ROOT}/include")
target_compile_definitions(native_suatom PRIVATE NDEBUG MKWVK_INCLUDES=2 VK_NO_PROTOTYPES)
if(MSVC)
target_compile_options(native_suatom PRIVATE /W0 /O1 /EHsc)
else()
target_compile_options(native_suatom PRIVATE -w -O1)
endif()

View File

@@ -0,0 +1,34 @@
# Direct surface atomic regression
The fields in `../native_suatom.cpp` are independently authored. No game shader,
dump, cache or private SDK instruction is used. The small Python check compiles
the production lowering header; the CMake target additionally runs the same
instructions through the actual Maxwell CFG/translator, optimization passes and
SPIR-V emitter. Every emitted module is validated in the existing Linux SPIR-V
job. No GPU or game executes.
The accepted form is direct-descriptor `0x593`, 1D U32 ADD, sample addressing,
IGN, Strong(GPU), normal eviction, and no fault-predicate destination. Other
forms and modifiers are refused. Registers and descriptor fields are authored
separately, with alias/high-register, predication, branch relocation, offset/bank
boundary and conflict cases. The full path checks the descriptor bank and byte
offset, atomic coordinate/addend, and a store consuming the returned old value.
Strong(GPU) is not an acquire/release qualifier. Mesa emits image atomics with
`MemOrder::Strong(MemScope::GPU)` and handles explicit barriers separately. The
existing image atomic backend emits a Device-scoped relaxed atomic; no fences
are added by this lowering. A case with explicit native MEMBARs verifies those
barriers survive independently.
Encoding references:
- [Independent decoder](https://github.com/seedds/nxnative/blob/7f50222003e4270e3655d4f798e85744d390edf0/dm/third_party/ryujinx/src/Ryujinx.Graphics.Shader/Instructions/InstEmitAmpereMem.cs#L529): direct descriptor bank/word offset and SUATOM operands.
- [Mesa encoder](https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/b1fe47e94409d81924c8663f58fac97e66ab4527/src/nouveau/compiler/nak/sm70_encode.rs): shared atomic/dimension/type/order fields. Mesa encodes its register-handle form, not this direct-descriptor opcode.
- [Mesa NIR translation](https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/b1fe47e94409d81924c8663f58fac97e66ab4527/src/nouveau/compiler/nak/from_nir.rs): Strong(GPU) image atomics and separate barriers.
- [Public SM87 encoding tables](https://mbuchel.github.io/sass-encoding-lab/): `0x593` and IGN. The opcode table SHA-256 is `559048b6a840e5548ba5fa577e31230f0a217f370d50c7c378e12997d96dd747`.
Configure with Clang C++20 and `-DREPO_ROOT=<repo>` plus
`-DSPIRV_HEADERS_ROOT=<SPIRV-Headers>`, then build `native_suatom`. Run it with an
authored output directory and validate its `.spv` files using `spirv-val
--target-env vulkan1.3`. These checks establish translation and module validity,
not driver execution, general surface-atomic coverage or a visible game fix.

View File

@@ -0,0 +1,546 @@
// A parallel replay's new images in queue order: the production upload command buffer state, create()'s initialization,
// bc_scratch_layout, the texture segment hooks, replay_handle's segment jobs and submits and the init pools' startup,
// included from the init_*.inc files tests/test_parallel_init_order.py cuts from the source. Vulkan is a spy: commands
// are recorded per command buffer (never into one not begun, ended or submitted), and the submitted ones are played in
// queue order, the image layouts tracked as the GPU would see them. The workers run on threads of their own: one after
// another in the order a case sets (the one that makes an image first need not be the first segment in the queue), or
// all at once, held at fixed points by latches (a worker waits for another to be inside its init recording, or for an
// earlier segment's submit). A command buffer can be made to fail its begin or its end; replay_handle's part after the
// segments (begin_recording) is modelled as a recording of its own.
#define VK_NO_PROTOTYPES
#include <vulkan/vulkan_core.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <thread>
#include <vector>
struct Op {
char kind; // F: full barrier, T: UNDEFINED -> GENERAL, B: another image barrier, C: clear, U: a use (copy, dispatch, draw)
VkImage image;
};
struct VkCommandBuffer_T {
std::string name;
std::vector<Op> ops;
bool begun = false, ended = false, submitted = false;
};
std::vector<VkCommandBuffer_T*> all_cbs, queue;
std::vector<std::unique_ptr<VkCommandBuffer_T>> owned_cbs;
std::set<int> published_slots; // draw-stream CPU writes published before a worker slot reaches the GPU
std::atomic<int> failures{0};
#define CHECK(v) do { if (!(v)) { std::printf("FAIL:%d %s\n", __LINE__, #v); std::fflush(stdout); std::_Exit(1); } } while (0)
struct Latch {
std::mutex mu;
std::condition_variable cv;
bool set = false;
void post() {
{
std::lock_guard l(mu);
set = true;
}
cv.notify_all();
}
void wait() {
std::unique_lock l(mu);
CHECK(cv.wait_for(l, std::chrono::seconds(10), [&] { return set; }));
}
};
Latch latch[128]; // (by letter. x: a worker is inside the texture's initialization, y: let it go on, z: segment 0 submitted)
const VkImage kTex = reinterpret_cast<VkImage>(uintptr_t(0x10)), kScratch = reinterpret_cast<VkImage>(uintptr_t(0x20)),
kScratch2 = reinterpret_cast<VkImage>(uintptr_t(0x30));
const char* image_name(VkImage i) {
return i == kTex ? "t" : i == kScratch ? "s" : i == kScratch2 ? "s2" : "?";
}
bool hold_in_init; // (close_race: the texture's initialization waits there, its lock held, for the other worker)
void record(VkCommandBuffer cb, char kind, VkImage image = VK_NULL_HANDLE) {
CHECK(cb && cb->begun && !cb->ended && !cb->submitted);
cb->ops.push_back({kind, image});
}
void vkCmdPipelineBarrier2(VkCommandBuffer cb, const VkDependencyInfo* di) {
for (uint32_t i = 0; i < di->imageMemoryBarrierCount; i++) {
const VkImageMemoryBarrier2& b = di->pImageMemoryBarriers[i];
const bool first = b.oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && b.newLayout == VK_IMAGE_LAYOUT_GENERAL;
record(cb, first ? 'T' : 'B', b.image);
if (first && b.image == kTex && hold_in_init) {
latch['x'].post();
latch['y'].wait();
std::this_thread::sleep_for(std::chrono::milliseconds(20)); // (the other worker closing its segment meanwhile)
}
}
}
void vkCmdClearColorImage(VkCommandBuffer cb, VkImage i, VkImageLayout, const VkClearColorValue*, uint32_t, const VkImageSubresourceRange*) {
record(cb, 'C', i);
}
void vkCmdClearDepthStencilImage(VkCommandBuffer cb, VkImage i, VkImageLayout, const VkClearDepthStencilValue*, uint32_t,
const VkImageSubresourceRange*) {
record(cb, 'C', i);
}
VkResult vkResetCommandPool(VkDevice, VkCommandPool, VkCommandPoolResetFlags) {
return VK_SUCCESS;
}
std::string fail_begin, fail_end; // (this command buffer cannot be begun / ended)
VkResult vkBeginCommandBuffer(VkCommandBuffer cb, const VkCommandBufferBeginInfo*) {
CHECK(cb);
if (cb->name == fail_begin) return VK_ERROR_OUT_OF_HOST_MEMORY;
cb->ops.clear(), cb->begun = true, cb->ended = false;
return VK_SUCCESS;
}
VkResult vkEndCommandBuffer(VkCommandBuffer cb) {
CHECK(cb && cb->begun && !cb->ended);
if (cb->name == fail_end) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
cb->ended = true;
return VK_SUCCESS;
}
VkResult vkQueueSubmit2(VkQueue, uint32_t n, const VkSubmitInfo2* si, VkFence) {
for (uint32_t s = 0; s < n; s++)
for (uint32_t i = 0; i < si[s].commandBufferInfoCount; i++) {
VkCommandBuffer cb = si[s].pCommandBufferInfos[i].commandBuffer;
CHECK(cb && cb->ended && !cb->submitted);
if (cb->name.rfind("cmd_", 0) == 0)
CHECK(published_slots.count(std::stoi(cb->name.substr(4))) == 1);
cb->submitted = true;
queue.push_back(cb);
if (cb->name == "cmd_0") latch['z'].post();
}
return VK_SUCCESS;
}
std::mutex cbs_mu;
VkCommandBuffer make_cb(const char* what, int k) {
auto cb = std::make_unique<VkCommandBuffer_T>();
cb->name = std::string(what) + "_" + std::to_string(k);
auto* result = cb.get();
std::lock_guard l(cbs_mu);
all_cbs.push_back(result);
owned_cbs.push_back(std::move(cb));
return result;
}
// (the init pools' startup: each pool creation and allocation can be made to fail, its output left as garbage)
const uintptr_t kGarbage = 0xBAD0;
int alloc_calls, fail_at;
std::set<uintptr_t> live_pools;
uintptr_t next_pool = 0x1000;
VkResult vkCreateCommandPool(VkDevice, const VkCommandPoolCreateInfo*, const VkAllocationCallbacks*, VkCommandPool* p) {
if (++alloc_calls == fail_at) {
*p = reinterpret_cast<VkCommandPool>(kGarbage);
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
live_pools.insert(next_pool);
*p = reinterpret_cast<VkCommandPool>(next_pool++);
return VK_SUCCESS;
}
VkResult vkAllocateCommandBuffers(VkDevice, const VkCommandBufferAllocateInfo* ai, VkCommandBuffer* out) {
CHECK(ai->commandBufferCount == 1 && live_pools.count(reinterpret_cast<uintptr_t>(ai->commandPool)));
if (++alloc_calls == fail_at) {
*out = reinterpret_cast<VkCommandBuffer>(kGarbage);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
*out = make_cb("pool", alloc_calls);
return VK_SUCCESS;
}
void vkDestroyCommandPool(VkDevice, VkCommandPool p, const VkAllocationCallbacks*) {
CHECK(p && live_pools.erase(reinterpret_cast<uintptr_t>(p))); // (only a live one, once)
}
namespace mkwvk {
std::mutex log_mu;
std::string log_text;
std::function<void(const std::string&)> on_log; // (init_end_fail: looks at the state when the replay stops)
void logf(const char* f, ...) {
char line[512];
va_list a;
va_start(a, f);
std::vsnprintf(line, sizeof line, f, a);
va_end(a);
{
std::lock_guard l(log_mu);
log_text += std::string(line) + "\n";
}
if (on_log) on_log(line);
}
static const struct FixtureHost { void (*log_flush)(); } fixture_host{}, *host = &fixture_host; // (replay_fatal's)
#include "init_fatal.inc"
void full_barrier(VkCommandBuffer cmd, const char* = "") {
record(cmd, 'F');
}
void memory_table_begin() {}
bool memory_table_flush() { return false; }
bool memory_rb_refresh(VkCommandBuffer) { return false; }
void memory_rb_recording_begun() {}
VkCommandBuffer last_barrier_cmd = VK_NULL_HANDLE;
thread_local VkCommandBuffer last_pass_end_cmd = VK_NULL_HANDLE;
bool is_bc(VkFormat) { return false; }
bool is_astc(VkFormat) { return false; }
#include "init_upload.inc"
#include "init_hooks.inc"
// ---- create()'s initialization of a new image ----
struct Texture {
VkImage image;
};
std::vector<std::unique_ptr<Texture>> textures;
std::mutex par_lock; // (texture_get's ParLock: an image is made once, by whichever worker gets there first)
Texture* create(VkImage image) {
struct { VkImage image; VkFormat format; VkImageAspectFlags aspect; } x{image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT};
const bool depth = false;
auto tex = std::make_unique<Texture>(Texture{image});
#include "init_create.inc"
textures.push_back(std::move(tex));
return p;
}
void texture_use() { // (texture_get, and texture_touch's upload again: a copy into it in this thread's uploads)
std::lock_guard lk(par_lock);
if (textures.empty()) create(kTex);
record(upload_cmd(), 'U', kTex);
}
// ---- the BC3 scratch, and the second one (an upload at another size) ----
std::mutex bc_mu;
std::chrono::steady_clock::time_point bc_used_at;
uint64_t bc_last_value = 0;
std::atomic<bool> bc_have{false};
constexpr uint32_t kBcLayers = 8;
VkImage bc_scratch = VK_NULL_HANDLE, bc_scratch2 = VK_NULL_HANDLE;
bool bc_scratch_fresh = false, bc_scratch2_fresh = false;
bool bc_scratch_make() {
bc_scratch = kScratch, bc_scratch_fresh = true, bc_have = true;
return true;
}
bool bc_scratch2_make() {
bc_scratch2 = kScratch2, bc_scratch2_fresh = true;
return true;
}
#include "init_scratch.inc"
void scratch_use(bool resized) { // (an ASTC/BC3 upload: decoded into the scratch (and blitted into the second), in this thread's uploads)
std::lock_guard lk(par_lock);
CHECK(bc_scratch_layout(upload_cmd(), resized));
record(upload_cmd(), 'U', kScratch);
if (resized) record(upload_cmd(), 'U', kScratch2);
}
// ---- replay_handle's segments ----
#include "init_recording.inc"
struct Segment {
// t: the texture (made if new, uploaded, drawn with), s: the scratch, S: both scratches, -: nothing,
// x / z: wait for latch x (a worker inside the texture's initialization) / z (segment 0 submitted), d: 100 ms more,
// L: the initialization holds the segments' init lock (seg_init_mu) while it records; then latch y lets it go on
std::string script;
const uint8_t* end = nullptr; // (kStop: to the command buffer's end, every segment's commands in order)
};
const uint8_t* const kStop = reinterpret_cast<const uint8_t*>("stop");
const std::vector<Segment>* cur_segs;
void record_segment(VkCommandBuffer& cmd, const Segment& g) {
std::string script = g.script;
if (g.end == kStop)
for (size_t k = 1; k < cur_segs->size(); k++) script += (*cur_segs)[k].script;
for (char a : script) {
if (a == 't') texture_use(), record(cmd, 'U', kTex);
if (a == 's' || a == 'S') scratch_use(a == 'S');
if (a == 'x' || a == 'z') latch[(int)a].wait();
if (a == 'd') std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (a == 'L') {
#if HAVE_SEG_INIT
if (seg_init_mu.try_lock()) {
seg_init_mu.unlock();
std::printf(" FAIL: the init lock is free while another worker records a new image's initialization\n");
failures++;
}
#endif
latch['y'].post();
}
}
}
void draw_begin_recording(int, VkCommandBuffer, uint64_t) {}
void draw_flush_recording(int slot) { CHECK(slot >= 0 && slot < 8); published_slots.insert(slot); }
void queue_flush_locked() {}
void checkpoints_report() {}
[[noreturn]] void device_lost() { std::_Exit(3); } // (vk_replay.cpp: the report, then the process ends)
struct {
VkDevice device = VK_NULL_HANDLE;
VkQueue queue = VK_NULL_HANDLE;
std::mutex present_mu;
} dev;
std::atomic<int64_t> par_lock_wait_ns{0}, par_locks{0};
std::atomic<bool> par_active{false};
std::vector<int> run_order; // (the order the workers run in, one after another; empty: all at once)
std::vector<std::thread> workers;
bool jobs_started;
std::atomic<int> running{0}; // (workers still recording)
struct RunJobs {
void notify_all();
};
struct {
std::mutex mu;
std::vector<std::function<void()>> jobs;
int pending = 0;
RunJobs cv;
std::condition_variable done;
} par_pool;
void RunJobs::notify_all() {
jobs_started = true;
std::vector<std::function<void()>> jobs;
{
std::lock_guard lk(par_pool.mu);
jobs.swap(par_pool.jobs);
}
if (run_order.empty()) { // (at once, as par_worker_main runs them; the replay thread submits meanwhile)
for (auto& job : jobs) {
running++;
workers.emplace_back([job] {
job();
running--;
{
std::lock_guard lk(par_pool.mu);
par_pool.pending--;
}
par_pool.done.notify_all();
});
}
return;
}
CHECK(jobs.size() == run_order.size());
for (int k : run_order) std::thread(jobs[k]).join();
std::lock_guard lk(par_pool.mu);
par_pool.pending = 0;
}
Recording recordings[8];
int recording_count = 0;
uint64_t rec_cur_value = 1;
Recording& begin_segment_recording(uint64_t) {
const int k = recording_count++;
published_slots.erase(k);
Recording& r = recordings[k];
r.cmd = make_cb("cmd", k), r.upload = make_cb("upload", k);
[k](auto& x) { // (the fixed code's own command buffer for its new images)
if constexpr (requires { x.init; }) x.init = make_cb("init", k);
}(r);
vkBeginCommandBuffer(r.cmd, nullptr); // (as begin_segment_recording does; the job begins the uploads)
r.value = 100 + k, r.slot = k;
return r;
}
// (replay_handle's part after the segments: the replay thread's next recording, submitted later as [its uploads, it])
Recording post;
Recording* rec_cur = nullptr;
int rec_parts = 0;
Recording& begin_recording(uint64_t value) {
post.cmd = make_cb("post", 0), post.upload = make_cb("postupload", 0);
vkBeginCommandBuffer(post.cmd, nullptr);
vkBeginCommandBuffer(post.upload, nullptr);
post.value = value;
last_pass_end_cmd = post.cmd;
texture_begin_recording(post.upload, value);
CHECK(last_pass_end_cmd == VK_NULL_HANDLE);
draw_begin_recording(post.slot, post.cmd, value);
return post;
}
void submit_post() {
VkCommandBufferSubmitInfo cb[2]{{VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO}, {VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO}};
uint32_t n = 0;
if (texture_uploads_recorded()) cb[n++].commandBuffer = post.upload;
cb[n++].commandBuffer = post.cmd;
vkEndCommandBuffer(post.upload);
vkEndCommandBuffer(post.cmd);
VkSubmitInfo2 si{VK_STRUCTURE_TYPE_SUBMIT_INFO_2};
si.commandBufferInfoCount = n, si.pCommandBufferInfos = cb;
vkQueueSubmit2(VK_NULL_HANDLE, 1, &si, VK_NULL_HANDLE);
}
/// replay_handle from its segments on (the dry pass, the part before and the slots' order are the caller's)
void submit_segments(std::vector<Segment> seg, std::vector<int> order) {
run_order = order;
cur_segs = &seg;
VkCommandBuffer cmd = VK_NULL_HANDLE;
const uint8_t* const stop = kStop;
uint64_t seq = 10;
const uint64_t base = 1;
[&]() -> const uint8_t* {
#include "init_jobs.inc"
return nullptr;
}();
for (auto& w : workers) w.join();
}
#include "init_pools.inc"
} // namespace mkwvk
std::string ops_text(const VkCommandBuffer_T* cb) {
std::string s;
for (const Op& o : cb->ops) s += std::string(s.empty() ? "" : " ") + o.kind + (o.image ? image_name(o.image) : "");
return s;
}
std::string queue_text() {
std::string order;
for (auto* cb : queue) order += " " + cb->name + "[" + ops_text(cb) + "]";
return order;
}
/// The submitted command buffers in queue order: each image has one UNDEFINED -> GENERAL change, before every use
void play(const char* name) {
std::printf("%s: queue%s\n", name, queue_text().c_str());
std::map<VkImage, int> made, early;
for (auto* cb : queue)
for (const Op& o : cb->ops) {
if (!o.image) continue;
if (o.kind == 'T') {
if (made[o.image]++)
std::printf(" FAIL: image %s made UNDEFINED again in %s (what came before is discarded)\n", image_name(o.image),
cb->name.c_str()), failures++;
} else if (!made[o.image] && !early[o.image]++) {
std::printf(" FAIL: %s uses image %s (%c) while it is still UNDEFINED in the queue (VUID-vkCmdDraw-None-09600)\n",
cb->name.c_str(), image_name(o.image), o.kind), failures++;
}
}
for (auto [image, n] : made)
if (n != 1) std::printf(" FAIL: image %s changed from UNDEFINED %d times\n", image_name(image), n), failures++;
for (auto* cb : all_cbs)
for (const Op& o : cb->ops)
if (o.kind != 'F' && !cb->submitted) {
std::printf(" FAIL: %s has commands (%c%s) and was never submitted\n", cb->name.c_str(), o.kind, image_name(o.image));
failures++;
break;
}
}
VkCommandBuffer_T* named(const char* name) {
for (auto* cb : all_cbs)
if (cb->name == name) return cb;
return nullptr;
}
/// Which command buffer holds image i's UNDEFINED -> GENERAL change ("": none)
std::string init_of(VkImage i) {
for (auto* cb : all_cbs)
for (const Op& o : cb->ops)
if (o.kind == 'T' && o.image == i) return cb->name;
return "";
}
int main(int argc, char** argv) {
using namespace mkwvk;
const std::string c = argc > 1 ? argv[1] : "";
if (c == "later_makes") { // (segment 1's worker makes the texture, then segment 0's uploads it again and draws with it)
submit_segments({{"t"}, {"t"}}, {1, 0});
play(c.c_str());
} else if (c == "three") { // (segment 0 is done; 2 makes it while 1 still records, and 1 uses it)
submit_segments({{"-"}, {"t"}, {"t"}}, {0, 2, 1});
play(c.c_str());
} else if (c == "scratch") { // (the BC3 scratch: made by segment 1's worker, used by segment 0's too)
submit_segments({{"s"}, {"s"}}, {1, 0});
play(c.c_str());
} else if (c == "scratch2") { // (segment 1 makes the scratch, then the second one; segment 0 uses both; then both fresh at once)
submit_segments({{"S"}, {"sS"}}, {1, 0});
play(c.c_str());
queue.clear(), all_cbs.clear(), recording_count = 0;
bc_scratch = bc_scratch2 = VK_NULL_HANDLE, bc_have = false;
submit_segments({{"S"}, {"S"}}, {1, 0});
play("scratch2 (both fresh)");
} else if (c == "in_order") { // (made by the first segment, as serial replay would)
submit_segments({{"t"}, {"t"}}, {0, 1});
play(c.c_str());
} else if (c == "serial") { // (no segments: the replay thread's uploads get exactly the calls they always did)
VkCommandBuffer up = make_cb("upload", 0);
vkBeginCommandBuffer(up, nullptr);
texture_begin_recording(up, 1);
texture_use();
scratch_use(false);
scratch_use(true);
CHECK(texture_uploads_recorded());
vkEndCommandBuffer(up);
const std::string got = ops_text(up), want = "F Tt Ct Bt Ut Ts Us Ts2 Us Us2 F";
std::printf("serial: %s[%s]\n", up->name.c_str(), got.c_str());
if (got != want) std::printf(" FAIL: serial uploads changed, want [%s]\n", want.c_str()), failures++;
for (auto* cb : all_cbs)
if (cb != up && !cb->ops.empty()) std::printf(" FAIL: serial replay recorded into %s\n", cb->name.c_str()), failures++;
} else if (c == "close_race") { // (at once: segment 1 records the texture's initialization into segment 0's init command
// buffer, holding the lock, while segment 0's worker finishes and closes it)
hold_in_init = true;
submit_segments({{"xL"}, {"t"}}, {});
play(c.c_str());
if (HAVE_SEG_INIT && init_of(kTex) != "init_0")
std::printf(" FAIL: the texture's initialization is in %s, not init_0\n", init_of(kTex).c_str()), failures++;
} else if (c == "after_submit") { // (at once: segment 1 makes the texture only after segment 0 was submitted)
submit_segments({{"-"}, {"zt"}}, {});
play(c.c_str());
if (named("init_0") && !named("init_0")->ops.empty())
std::printf(" FAIL: init_0 (submitted) was recorded into\n"), failures++;
} else if (c == "init_begin_fail") { // (segment 1's init command buffer cannot be begun: no worker starts, and the
// whole command buffer is recorded in order on the replay thread, after the segments)
#if HAVE_SEG_INIT
fail_begin = "init_1";
submit_segments({{"-"}, {"t"}, {"t"}}, {0, 2, 1});
if (jobs_started) std::printf(" FAIL: a segment's worker started\n"), failures++;
submit_post();
play(c.c_str());
for (auto* cb : queue)
if (cb->name.rfind("post", 0) != 0) std::printf(" FAIL: segment command buffer %s submitted\n", cb->name.c_str()), failures++;
if (init_of(kTex) != "postupload_0")
std::printf(" FAIL: the texture's initialization is in %s, not postupload_0\n", init_of(kTex).c_str()), failures++;
if (!named("post_0") || ops_text(named("post_0")) != "F Ut Ut") std::printf(" FAIL: not every segment's draw was recorded\n"), failures++;
#else
std::printf("init_begin_fail: no init command buffers in this code\n");
#endif
} else if (c == "init_end_fail") { // (at once: segments 1 and 2 wait for segment 0's submit, then make and use the texture,
// whose initialization goes to init_1; init_1 cannot be ended. The replay must stop
// for good (replay_fatal ends the process, exit code 3) after segment 2's worker is done,
// with nothing that uses the texture submitted; later work using it must never run)
#if HAVE_SEG_INIT
fail_end = "init_1";
on_log = [](const std::string& line) {
if (line.find("FATAL") == std::string::npos) return;
std::printf("init_end_fail: stopped: queue%s\n", queue_text().c_str());
if (running) std::printf(" FAIL: the replay stopped with a worker still recording\n");
CHECK(published_slots == std::set<int>{0}); // lost init and later slots were not published
for (auto* cb : queue)
for (const Op& o : cb->ops)
if (o.image == kTex) std::printf(" FAIL: %s, submitted, uses the texture whose initialization was lost\n", cb->name.c_str());
if (init_of(kTex) != "init_1") std::printf(" FAIL: the texture's initialization is in %s, not init_1\n", init_of(kTex).c_str());
std::fflush(stdout);
};
submit_segments({{"-"}, {"zt"}, {"ztd"}}, {});
// (the replay went on: the next recording (and frame) uses the texture again)
Recording& later = begin_recording(rec_cur_value);
texture_use();
record(later.cmd, 'U', kTex);
submit_post();
std::printf("%s: went on: queue%s\n", c.c_str(), queue_text().c_str());
std::printf(" FAIL: the replay went on after a new image's initialization was lost\n"), failures++;
#else
std::printf("init_end_fail: no init command buffers in this code\n");
#endif
} else if (c == "alloc_fail") { // (init_pools_make: each of its pool creations and allocations failing in turn)
#if HAVE_INIT_POOLS
const int n = (int)(sizeof(recordings) / sizeof(recordings[0]));
VkCommandPoolCreateInfo pi{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
for (int f = 1; f <= 2 * n + 1; f++) { // (the last: none fails)
alloc_calls = 0, fail_at = f <= 2 * n ? f : 0;
const bool ok = init_pools_make(pi);
if (ok != !fail_at) std::printf(" FAIL: step %d failing, init_pools_make returned %d\n", f, ok), failures++;
for (const Recording& r : recordings)
if (ok ? (!r.init_pool || !r.init) : (r.init_pool || r.init))
std::printf(" FAIL: step %d failing, a recording's init pool/buffer is %p/%p\n", f, (void*)r.init_pool, (void*)r.init),
failures++;
if (!ok && !live_pools.empty())
std::printf(" FAIL: step %d failing, %zu init pools left made\n", f, live_pools.size()), failures++;
}
if (live_pools.size() != (size_t)n) std::printf(" FAIL: %zu init pools made, want %d\n", live_pools.size(), n), failures++;
init_pools_destroy();
if (!live_pools.empty()) std::printf(" FAIL: init pools left after init_pools_destroy\n"), failures++;
std::printf("alloc_fail: each of the %d steps failing gives back every pool made and leaves no handle; none failing makes %d\n",
2 * n, n);
#else
std::printf("alloc_fail: no init pools in this code\n");
#endif
} else {
std::printf("unknown case: %s\n", c.c_str());
return 2;
}
std::fflush(stdout);
return failures ? 1 : 0;
}

View File

@@ -0,0 +1,297 @@
// The readback refresh (PR 430) in a parallel replay's submission: the production memory_rb_refresh and its bookkeeping,
// texture_begin_recording / texture_uploads_recorded, the write-back note at the end of gpu_buffer_bind and vk_draw.cpp's
// storage-descriptor binding, included from rb_actual.inc (tests/test_parallel_rb_refresh.py cuts them from the source).
// Vulkan is a spy: copies are recorded and run later, reading their source when they run, as the GPU reads guest memory.
// The submission's begin/end (epoch, write-back) and replay_handle's order of recordings are modelled here.
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <mutex>
#include <atomic>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include <algorithm>
struct FakeBuf {
const char* name;
std::vector<uint8_t> mem;
};
using VkBuffer = FakeBuf*;
using VkDeviceMemory = void*;
using VmaAllocation = void*;
using VkDeviceAddress = uint64_t;
#define VK_NULL_HANDLE nullptr
struct VkBufferCopy {
uint64_t srcOffset, dstOffset, size;
};
struct Cmd {
char op; // c: copy, a: add, s: store, r: read
VkBuffer src, dst;
std::vector<VkBufferCopy> r;
uint64_t off;
uint32_t val;
};
struct FakeCB {
std::string name;
std::vector<Cmd> cmds;
};
using VkCommandBuffer = FakeCB*;
void vkCmdCopyBuffer(VkCommandBuffer cb, VkBuffer s, VkBuffer d, uint32_t n, const VkBufferCopy* r) {
cb->cmds.push_back({'c', s, d, std::vector<VkBufferCopy>(r, r + n), 0, 0});
}
namespace mkwvk {
constexpr uint64_t kPageShift = 12;
void logf(const char*, ...) {}
struct GprofSpan {
int items = 0;
uint64_t bytes = 0;
GprofSpan(VkCommandBuffer, const char*) {}
};
void full_barrier(VkCommandBuffer, const char* = "") {}
struct Host { // (GPU addresses below 1 TB are guest addresses)
bool gpu_ptr(uint64_t, uint64_t* avail) const { return *avail = ~0ull, true; }
uint64_t gpu_guest(uint64_t g) const { return g; }
} const host_object;
const Host* host = &host_object;
std::atomic<bool> par_active{false};
void memory_table_begin() {}
bool memory_table_flush() { return false; }
void memory_protect_gpu(uint64_t, uint64_t) {}
bool slow_desc_timing() { return false; }
uint64_t texture_segment();
void memory_rb_recording_begun();
void memory_rb_segments_begin(uint64_t base, uint64_t n);
void memory_note_gpu_write(uint64_t gpu, uint64_t size);
bool memory_rb_refresh(VkCommandBuffer up);
#include "rb_bufferref.inc"
std::mutex pools_mu;
#include "rb_pool.inc"
std::map<uint64_t, Pool> imported;
uint64_t mirror_epoch = 0;
bool mirror_upload = false;
#include "rb_writeback.inc"
std::vector<WriteBack> writebacks;
void mirror_sync(Pool& p) { // (model: no CPU page is dirty here)
p.synced_epoch = mirror_epoch;
p.sync_seg = texture_segment();
}
#include "rb_refresh.inc"
/// gpu_buffer_bind: the pool's mirror when it has one (model of its front), then its actual last lines
BufferRef gpu_buffer_bind(uint64_t gpu, uint64_t size, bool written) {
std::lock_guard lk(pools_mu);
auto it = imported.upper_bound(gpu);
if (it == imported.begin()) return {};
Pool* p = &(--it)->second;
if (gpu + size > p->hi) return {};
const uint64_t off = gpu - p->lo;
if (!mirror_upload || p->mstate != Pool::MIRROR_ON) return {p->buffer, off, size, 0};
#include "rb_bindtail.inc"
return {p->mirror, off, size, 0};
}
uint64_t gpu_pool_end(uint64_t gpu) {
std::lock_guard lk(pools_mu);
auto it = imported.upper_bound(gpu);
return it == imported.begin() ? 0 : (--it)->second.hi;
}
thread_local VkCommandBuffer last_barrier_cmd = VK_NULL_HANDLE;
thread_local VkCommandBuffer last_pass_end_cmd = VK_NULL_HANDLE;
thread_local VkCommandBuffer up_cmd = VK_NULL_HANDLE;
uint64_t rec_value = 0;
thread_local bool up_used = false;
thread_local uint64_t tl_seg = 0;
uint32_t epoch = 0;
#include "rb_texture.inc"
/// A storage descriptor bound as vk_draw.cpp binds it (its actual code: the bare entries' guess, then the binding)
struct Desc {
uint32_t count;
bool is_written;
uint32_t cbuf_index, cbuf_offset;
};
BufferRef bind_storage(const Desc& d, uint64_t addr, uint64_t size, uint64_t* base_out) {
static const uint64_t bare_read = 64ull << 20; // (the device's reach)
const uint64_t salign = 16; // (minStorageBufferOffsetAlignment)
bool bare = false;
#include "rb_draw.inc"
(void)t0;
*base_out = base;
return b;
}
} // namespace mkwvk
using namespace mkwvk;
// ---- the world ----
constexpr uint64_t LO = 0x10000000, SZ = 0x10000, A = LO + 0x2000;
FakeBuf G{"G", std::vector<uint8_t>(SZ)}, M{"M", std::vector<uint8_t>(SZ)};
std::vector<FakeCB*> queue;
std::string stream; // (every copy the GPU ran, in order)
uint32_t& word(FakeBuf& b, uint64_t guest) { return *reinterpret_cast<uint32_t*>(b.mem.data() + (guest - LO)); }
void run_gpu() {
for (FakeCB* cb : queue)
for (Cmd& c : cb->cmds) {
uint32_t* w = c.op == 'c' ? nullptr : reinterpret_cast<uint32_t*>(c.dst->mem.data() + c.off);
if (c.op == 'c')
for (const VkBufferCopy& r : c.r) {
std::memcpy(c.dst->mem.data() + r.dstOffset, c.src->mem.data() + r.srcOffset, r.size);
char line[128];
std::snprintf(line, sizeof line, "%s: %s->%s +%#llx %#llx\n", cb->name.c_str(), c.src->name, c.dst->name,
(unsigned long long)r.srcOffset, (unsigned long long)r.size);
stream += line;
}
else if (c.op == 'a') *w += c.val;
else if (c.op == 's') *w = c.val;
else c.val = *w; // (a read: what it saw)
}
queue.clear();
}
void setup() {
Pool p;
p.lo = LO, p.hi = LO + SZ, p.buffer = &G, p.mirror = &M, p.mstate = Pool::MIRROR_ON;
imported.emplace(LO, p);
}
// (memory_begin_submission / memory_end_submission, modelled: the epoch, and the write-back of what was marked written)
void begin_submission() {
std::lock_guard lk(pools_mu);
mirror_epoch++, mirror_upload = true, writebacks.clear();
}
void end_submission(VkCommandBuffer cmd) {
std::lock_guard lk(pools_mu);
for (const WriteBack& w : writebacks) cmd->cmds.push_back({'c', &M, &G, {{w.lo, w.lo, w.hi - w.lo}}, 0, 0});
writebacks.clear(), mirror_upload = false;
}
struct Rec {
FakeCB *cmd, *up;
};
std::vector<FakeCB*> cbs;
Rec begin_recording(const char* name) { // (vk_replay.cpp begin_recording: then texture_begin_recording)
Rec r{new FakeCB{std::string(name) + ".cmd", {}}, new FakeCB{std::string(name) + ".up", {}}};
last_pass_end_cmd = r.cmd;
texture_begin_recording(r.up, 1);
if (last_pass_end_cmd != VK_NULL_HANDLE) {
std::fputs("a new recording kept the previous pass-end command buffer\n", stderr);
std::exit(1);
}
return r;
}
void submit(const Rec& r) { // (submit_recording: the uploads, the refresh last of them, ahead of the work)
if (texture_uploads_recorded()) queue.push_back(r.up);
queue.push_back(r.cmd);
}
void add(VkCommandBuffer cmd, uint64_t g, uint32_t v) { // (a draw adding into a word bound written)
const BufferRef b = gpu_buffer_bind(g, 16, true);
cmd->cmds.push_back({'a', nullptr, b.buffer, {}, b.offset, v});
}
/// replay_handle's parallel block: the part before submitted, the segments recorded on their threads (one after
/// another, in order) and submitted in order, then the recording after them, in the same submission
Rec parallel(Rec& r, const std::vector<std::function<void(VkCommandBuffer)>>& segs) {
submit(r);
static uint64_t seq = 0;
const uint64_t base = ++seq;
seq += segs.size();
#if HAVE_SEGMENTS_BEGIN
memory_rb_segments_begin(base, segs.size());
#endif
par_active = true;
for (size_t k = 0; k < segs.size(); k++) {
Rec s{new FakeCB{"seg" + std::to_string(k + 1) + ".cmd", {}}, new FakeCB{"seg" + std::to_string(k + 1) + ".up", {}}};
bool uploads = false;
std::thread([&] {
texture_set_segment(base + k);
texture_begin_recording(s.up, 1);
segs[k](s.cmd);
uploads = texture_uploads_recorded();
}).join();
if (uploads) queue.push_back(s.up);
queue.push_back(s.cmd);
}
par_active = false;
texture_set_segment(++seq);
return begin_recording("after");
}
int fails = 0;
void expect(const char* what, uint32_t got, uint32_t want) {
std::printf("%s %s: %u (expected %u)\n", got == want ? "ok " : "FAIL", what, got, want);
fails += got != want;
}
int main(int argc, char** argv) {
const std::string which = argc > 1 ? argv[1] : "";
setup();
memory_note_readback(A, 16); // (the CPU reads A back: OoT's exposure meter)
if (which == "parallel") {
// P1: the part before, 3 segments and the part after each add 1 into the meter, in one submission; the CPU's 0
// lands after the recording, before the GPU runs it
Rec r = begin_recording("before");
begin_submission();
add(r.cmd, A, 1);
auto one = [](VkCommandBuffer c) { add(c, A, 1); };
Rec after = parallel(r, {one, one, one});
add(after.cmd, A, 1);
end_submission(after.cmd);
submit(after);
word(G, A) = 0;
run_gpu();
expect("parallel: the meter in guest memory after the write-back (the GPU's sum)", word(G, A), 5);
} else if (which == "untracked") {
// P12: a bare table entry (no size: bound written=false, 1 MB guessed, only its first 64 bytes noted written) stores
// into A's page past that head in segment 1; segment 3 reads it back
const Desc table{2, true, 3, 0x40};
const uint64_t at = LO + 0x1000, w = A + 0x80;
Cmd* read = nullptr;
Rec r = begin_recording("before");
begin_submission();
Rec after = parallel(r, {[&](VkCommandBuffer c) {
uint64_t base = 0;
const BufferRef b = bind_storage(table, at, 0, &base);
c->cmds.push_back({'s', nullptr, b.buffer, {}, b.offset + (w - base), 7});
},
[](VkCommandBuffer) {},
[&](VkCommandBuffer c) {
uint64_t base = 0;
const BufferRef b = bind_storage(table, at, 0, &base);
c->cmds.push_back({'r', nullptr, b.buffer, {}, b.offset + (w - base), 0});
read = &c->cmds.back();
}});
end_submission(after.cmd);
submit(after);
run_gpu();
expect("untracked writer: segment 3 reads what segment 1 stored past the bare entry's head", read->val, 7);
} else if (which == "serial") {
// serial replay: one recording a submission, then one after its end (frame generation's); the CPU writes A
// between them. The copies the GPU runs must be today's, exactly
for (int f = 1; f <= 3; f++) {
Rec r = begin_recording(("serial" + std::to_string(f)).c_str());
begin_submission();
add(r.cmd, A, 2);
end_submission(r.cmd);
submit(r);
word(G, A) = 0;
run_gpu();
word(G, A) += 10;
Rec g = begin_recording(("framegen" + std::to_string(f)).c_str());
submit(g);
run_gpu();
}
static const char* const want =
"serial1.up: G->M +0x2000 0x1000\nserial1.cmd: M->G +0x2000 0x10\nframegen1.up: G->M +0x2000 0x1000\n"
"serial2.up: G->M +0x2000 0x1000\nserial2.cmd: M->G +0x2000 0x10\nframegen2.up: G->M +0x2000 0x1000\n"
"serial3.up: G->M +0x2000 0x1000\nserial3.cmd: M->G +0x2000 0x10\nframegen3.up: G->M +0x2000 0x1000\n";
if (stream != want) std::printf("FAIL serial: the copies differ from today's:\n%s", stream.c_str());
fails += stream != want;
expect("serial: the meter in guest memory", word(G, A), 12);
} else {
std::fprintf(stderr, "usage: parallel | untracked | serial\n");
return 2;
}
return fails ? 1 : 0;
}

View File

@@ -0,0 +1,255 @@
// The actual save function, with authored cache bytes and Vulkan/file API spies. No driver is loaded.
#include <atomic>
#include <chrono>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <limits>
#include <new>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
constexpr int MOVEFILE_REPLACE_EXISTING = 1;
void Sleep(unsigned ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }
#endif
// Arm only the allocating thread, after the query/copy API or immediately before thread construction.
// Real std::vector, std::string and std::thread allocations still run; nothing models their failure handling.
thread_local bool fail_next_new = false;
std::atomic<int> allocation_failures{0};
void* operator new(std::size_t n) {
if (fail_next_new) {
fail_next_new = false;
++allocation_failures;
throw std::bad_alloc();
}
if (void* p = std::malloc(n ? n : 1)) return p;
throw std::bad_alloc();
}
// Keep the replacement allocator boundary: GCC must not inline free into a caller of operator new.
[[gnu::noinline]] void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { ::operator delete(p); }
void* operator new[](std::size_t n) { return ::operator new(n); }
void operator delete[](void* p) noexcept { ::operator delete(p); }
void operator delete[](void* p, std::size_t) noexcept { ::operator delete(p); }
using VkResult = int;
constexpr VkResult VK_SUCCESS = 0, VK_ERROR_UNKNOWN = -13;
struct { int device = 1; } dev;
int pcache = 2;
std::string pcache_path;
std::atomic<int64_t> pcache_built_since_save{32};
std::atomic<std::size_t> pcache_saved_size{0};
// The size the driver gives for the cache's data; a case grows it for pipelines taken in between saves.
std::atomic<std::size_t> cache_size{64};
enum Fault { NONE, VECTOR_ALLOC, VECTOR_LENGTH, TMP_ALLOC, QUERY_FAIL, EMPTY, COPY_FAIL, SHORT_WRITE, BLOCK_COPY, REPLACE_FAIL };
std::atomic<Fault> fault{NONE};
std::atomic<int> queries{0}, copies{0}, writes{0}, replacements{0}, save_errors{0}, start_errors{0};
std::atomic<bool> copy_blocked{false}, allow_copy{false};
#define CHECK(c) do { if (!(c)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); return 1; } } while (0)
void logf(const char* fmt, ...) {
char line[512];
va_list args;
va_start(args, fmt);
std::vsnprintf(line, sizeof line, fmt, args);
va_end(args);
std::fprintf(stderr, "%s\n", line);
if (std::strstr(line, "pipeline cache save failed:")) ++save_errors;
if (std::strstr(line, "could not start pipeline cache save:")) ++start_errors;
}
VkResult vkGetPipelineCacheData(int device, int cache, std::size_t* n, void* data) {
if (device != dev.device || cache != pcache) std::abort();
if (!data) {
++queries;
if (fault == QUERY_FAIL) return VK_ERROR_UNKNOWN;
*n = fault == VECTOR_LENGTH ? (std::numeric_limits<std::size_t>::max)() : fault == EMPTY ? 0 : cache_size.load();
if (fault == VECTOR_ALLOC) fail_next_new = true;
} else {
++copies;
if (fault == COPY_FAIL) return VK_ERROR_UNKNOWN;
if (fault == BLOCK_COPY) {
copy_blocked = true;
while (!allow_copy.load()) Sleep(1);
}
std::memset(data, 'N', *n);
if (fault == TMP_ALLOC) fail_next_new = true;
}
return VK_SUCCESS;
}
std::size_t save_fwrite(const void* p, std::size_t size, std::size_t n, FILE* f) {
++writes;
if (fault == SHORT_WRITE) return 0;
return std::fwrite(p, size, n, f);
}
int save_replace(const char* from, const char* to, unsigned flags) {
if (flags != MOVEFILE_REPLACE_EXISTING || to != pcache_path) std::abort();
++replacements;
if (fault == REPLACE_FAIL) return 0;
#ifdef _WIN32
return MoveFileExA(from, to, flags);
#else
return std::rename(from, to) == 0;
#endif
}
#define fwrite save_fwrite
#define MoveFileExA save_replace
#include "pipeline_cache_save.inc"
#undef MoveFileExA
#undef fwrite
bool wait_until(const std::atomic<int>& n, int target) {
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(3);
while (n.load() < target && std::chrono::steady_clock::now() < end) Sleep(1);
return n.load() >= target;
}
bool old_file() {
FILE* f = std::fopen(pcache_path.c_str(), "rb");
if (!f) return false;
char data[4]{};
const auto n = std::fread(data, 1, sizeof data, f);
std::fclose(f);
return n == 3 && std::memcmp(data, "old", 3) == 0;
}
bool new_file() {
FILE* f = std::fopen(pcache_path.c_str(), "rb");
if (!f) return false;
char data[257]{};
const auto n = std::fread(data, 1, sizeof data, f);
std::fclose(f);
if (n != cache_size) return false;
for (std::size_t i = 0; i < n; ++i) if (data[i] != 'N') return false;
return true;
}
int run(const std::string& name) {
if (name == "success") {
pcache_built_since_save = 31;
pipeline_cache_save(false);
CHECK(queries == 0 && old_file());
pcache_built_since_save = 32;
pipeline_cache_save(false);
CHECK(wait_until(copies, 1));
cache_size = 96; // Pipelines taken in meanwhile: the forced save has new data to write.
pipeline_cache_save(true); // Wait for the worker, then preserve the existing forced-save behavior.
CHECK(queries == 2 && writes == 2 && replacements == 2 && new_file());
CHECK(save_errors == 0 && start_errors == 0);
pcache = 0;
pipeline_cache_save(true);
CHECK(queries == 2);
} else if (name == "force-vector" || name == "async-vector" || name == "force-length" ||
name == "force-string" || name == "async-string") {
fault = name == "force-length" ? VECTOR_LENGTH : name.find("string") != std::string::npos ? TMP_ALLOC : VECTOR_ALLOC;
const bool async = name.starts_with("async");
pipeline_cache_save(!async);
CHECK(wait_until(save_errors, 1));
CHECK(old_file() && replacements == 0 && start_errors == 0);
CHECK(allocation_failures == (fault == VECTOR_LENGTH ? 0 : 1));
fault = NONE;
pipeline_cache_save(true);
CHECK(new_file() && replacements == 1 && queries == 2); // No stuck busy flag after the failure.
} else if (name == "thread-start") {
fail_next_new = true;
pipeline_cache_save(false); // The real std::thread's state allocation, before it can run the worker.
CHECK(!fail_next_new && allocation_failures == 1 && start_errors == 1 && save_errors == 0);
CHECK(queries == 0 && old_file());
pipeline_cache_save(true);
CHECK(new_file() && queries == 1 && replacements == 1);
} else if (name == "api-results") {
for (Fault f : {QUERY_FAIL, EMPTY, COPY_FAIL}) {
fault = f;
pipeline_cache_save(true);
CHECK(old_file() && replacements == 0 && save_errors == 0 && start_errors == 0);
}
fault = NONE;
pipeline_cache_save(true);
CHECK(new_file() && replacements == 1);
} else if (name == "short-write") {
fault = SHORT_WRITE;
pipeline_cache_save(true);
CHECK(writes == 1 && replacements == 0 && old_file());
fault = NONE;
pipeline_cache_save(true);
CHECK(new_file() && replacements == 1);
} else if (name == "forced-wait") {
fault = BLOCK_COPY;
pipeline_cache_save(false);
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(3);
while (!copy_blocked.load() && std::chrono::steady_clock::now() < end) Sleep(1);
CHECK(copy_blocked);
std::atomic<bool> started{false}, finished{false};
std::thread waiter([&] { started = true; pipeline_cache_save(true); finished = true; });
while (!started.load()) Sleep(1);
Sleep(20);
const bool waited = !finished.load() && queries == 1;
cache_size = 96; // Taken in while the first save copied: the waiter has new data to write.
allow_copy = true;
waiter.join();
CHECK(waited && finished && queries == 2 && replacements == 2 && new_file());
} else if (name == "unchanged") {
pcache_saved_size = 64; // As pcache_init leaves it: the driver's size for the data read from the file.
pipeline_cache_save(true); // At exit, nothing taken in.
CHECK(queries == 1 && copies == 0 && writes == 0 && old_file());
pcache_built_since_save = 32; // In play: the precompile's pipelines, all found in the cache, still count.
pipeline_cache_save(false);
CHECK(wait_until(queries, 2));
pipeline_cache_save(true); // After the worker.
CHECK(queries == 3 && copies == 0 && writes == 0 && replacements == 0 && old_file());
cache_size = 96;
for (Fault f : {SHORT_WRITE, REPLACE_FAIL}) { // Not in place: tried again at the next save.
fault = f;
pipeline_cache_save(true);
CHECK(old_file() && pcache_saved_size == 64);
}
fault = NONE;
pipeline_cache_save(true);
CHECK(queries == 6 && copies == 3 && writes == 3 && replacements == 2 && new_file() && pcache_saved_size == 96);
pipeline_cache_save(true); // Written: nothing new since.
CHECK(queries == 7 && copies == 3 && writes == 3 && replacements == 2 && new_file());
cache_size = 80; // A different size of any kind is data the file lacks.
pipeline_cache_save(true);
CHECK(copies == 4 && replacements == 3 && new_file());
CHECK(save_errors == 0 && start_errors == 0);
} else {
CHECK(false);
}
std::printf("PASS: %s\n", name.c_str());
return 0;
}
int main(int argc, char** argv) {
// An old detached worker would terminate the process. The original-source negative runs in its own bounded child.
std::set_terminate([] { std::fputs("uncaught worker exception\n", stderr); std::_Exit(86); });
if (argc == 2 && std::strcmp(argv[1], "terminate-control") == 0) std::terminate();
if (argc == 2 && std::strcmp(argv[1], "uncaught-thread-control") == 0) {
#ifdef _MSC_VER
std::fputs("MSVC-target uncaught-thread control\n", stderr);
#endif
std::thread worker([] { throw std::bad_alloc(); });
worker.join();
return 2;
}
if (argc != 3) return 2;
pcache_path = std::string(argv[2]) + "/authored-pipeline-cache-with-a-path-longer-than-small-string-storage.bin";
FILE* f = std::fopen(pcache_path.c_str(), "wb");
if (!f) return 2;
std::fwrite("old", 1, 3, f);
std::fclose(f);
try {
return run(argv[1]);
} catch (const std::exception& e) {
std::fprintf(stderr, "uncaught caller exception: %s\n", e.what());
return 87;
}
}

View File

@@ -0,0 +1,136 @@
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <atomic>
#include <cerrno>
#include <cstdarg>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
static void need(bool ok,const char* why){if(!ok){std::fprintf(stderr,"FAIL: %s\n",why);std::exit(1);}}
std::mutex mu;
std::map<std::string,std::vector<uint8_t>> files;
struct Stream{std::string path;};
std::map<FILE*,std::unique_ptr<Stream>> live;
std::vector<std::string> messages;
std::string mode;
unsigned writes=0,closes=0,fail_write=0;
const std::vector<uint8_t> previous={'p','r','e','v','i','o','u','s'};
void logf(const char* format,...){char text[2048];va_list args;va_start(args,format);std::vsnprintf(text,sizeof text,format,args);va_end(args);std::lock_guard lk(mu);messages.emplace_back(text);}
FILE* png_open(const char* name,const char* access){
std::lock_guard lk(mu);
if(mode=="open-failure"){errno=EACCES;return nullptr;}
if(std::strchr(access,'x') && files.count(name)){errno=EEXIST;return nullptr;}
files[name].clear();auto s=std::make_unique<Stream>();s->path=name;
FILE* f=reinterpret_cast<FILE*>(s.get());live.emplace(f,std::move(s));return f;
}
size_t png_write(const void* data,size_t size,size_t count,FILE* f){
std::lock_guard lk(mu);auto& out=files.at(live.at(f)->path);writes++;
const bool fail=(fail_write && writes==fail_write);
size_t n=size*count;if(fail && n)n--;
auto p=static_cast<const uint8_t*>(data);out.insert(out.end(),p,p+n);
if(fail){errno=ENOSPC;return size?n/size:0;}return count;
}
int png_close(FILE* f){std::lock_guard lk(mu);need(live.erase(f)==1,"exactly one close per owned stream");closes++;if(mode=="close-failure"){errno=ENOSPC;return EOF;}return 0;}
int png_remove(const char* path){std::lock_guard lk(mu);if(mode=="cleanup-failure"){errno=EACCES;return -1;}if(!files.erase(path)){errno=ENOENT;return -1;}return 0;}
int png_rename(const char* from,const char* to){
std::lock_guard lk(mu);if(mode=="publish-failure"){errno=EACCES;return -1;}
files[to]=std::move(files.at(from));files.erase(from);return 0;
}
[[maybe_unused]] int png_move(const char* from,const char* to,unsigned){return png_rename(from,to)==0;}
[[maybe_unused]] unsigned png_last_error(){return 5;}
#ifndef MOVEFILE_REPLACE_EXISTING
#define MOVEFILE_REPLACE_EXISTING 1
#endif
#define fopen png_open
#define fwrite png_write
#define fclose png_close
#define remove png_remove
#define rename png_rename
#define MoveFileExA png_move
#define GetLastError png_last_error
/* PNG */
#undef fopen
#undef fwrite
#undef fclose
#undef remove
#undef rename
#undef MoveFileExA
#undef GetLastError
const uint32_t kShotW=257,kShotH=251;
/* ANNOUNCE */
static std::vector<uint8_t> pixels(unsigned seed){
std::vector<uint8_t> out(size_t(kShotW)*kShotH*4);
for(size_t i=0;i<out.size();i+=4){out[i]=uint8_t(seed);out[i+1]=73;out[i+2]=211;out[i+3]=127;}
return out;
}
static bool saved(const std::string& path,unsigned seed){auto rgba=pixels(seed);return write_png(path.c_str(),rgba.data(),kShotW,kShotH);}
static void emit(const char* directory){
unsigned n=0;
for(auto& [name,data]:files)if(name.ends_with(".png") && data!=previous){
const std::string path=std::string(directory)+"/"+std::to_string(n++)+".png";
FILE* f=std::fopen(path.c_str(),"wb");need(f!=nullptr,"fixture output open");need(std::fwrite(data.data(),1,data.size(),f)==data.size(),"fixture output write");need(std::fclose(f)==0,"fixture output close");
}
}
int main(int argc,char**argv){
need(argc==3,"mode/output directory");mode=argv[1];const std::string target="capture.png";files[target]=previous;
if(mode.starts_with("write-"))fail_write=unsigned(std::atoi(mode.c_str()+6));
if(mode=="cleanup-failure" || mode=="announce-failure")fail_write=1;
if(mode=="bounded-report"){
mode="open-failure";for(int i=0;i<30;i++)need(!saved(target,17),"every failed capture returns false");
need(!messages.empty() && messages.size()<=8,"failure diagnostics remain bounded");
need(files[target]==previous && live.empty(),"repeated failure preserves prior capture and handles");
mode="healthy";need(saved(target,19),"capture still recovers after report limit");emit(argv[2]);return 0;
}
if(mode=="invalid"){
uint8_t one[4]{};
need(!write_png(target.c_str(),one,0,1),"zero width rejected");
need(!write_png(target.c_str(),one,UINT32_MAX,1),"PNG width limit rejected");
need(!write_png(target.c_str(),one,1000000,1000000),"oversized IDAT rejected before source access");
need(!write_png(nullptr,one,1,1) && !write_png("",one,1,1) && !write_png(target.c_str(),nullptr,1,1),"missing input/path rejected");
need(files.size()==1 && files[target]==previous && !writes && !closes,"invalid input leaves destination untouched");return 0;
}
if(mode=="parallel" || mode=="same-target"){
std::atomic<unsigned> ready{0};std::atomic<bool> go{false};std::vector<std::thread> workers;
for(unsigned i=1;i<=12;i++)workers.emplace_back([&,i]{ready++;while(!go.load())std::this_thread::yield();need(saved(mode=="same-target"?target:std::to_string(i)+".png",i),"concurrent capture succeeds");});
while(ready.load()!=12)std::this_thread::yield();
go=true;
for(auto& t:workers)t.join();
need(live.empty(),"parallel files all closed");emit(argv[2]);return 0;
}
if(mode=="collisions" || mode=="collision-limit"){
unsigned count=mode=="collisions"?3:128;
for(unsigned i=0;i<count;i++)files[target+".part."+std::to_string(i)]=previous;
bool ok=saved(target,17);need(ok==(mode=="collisions"),"bounded exclusive creation");
for(unsigned i=0;i<count;i++)need(files[target+".part."+std::to_string(i)]==previous,"foreign collision preserved");
if(!ok){need(files[target]==previous,"collision failure preserves target");mode="healthy";need(saved(target,19),"next capture retries after collisions");}
emit(argv[2]);return 0;
}
if(mode=="announce-failure" || mode=="announce-success"){
auto px=std::make_shared<std::vector<uint8_t>>(pixels(17));announce(px,target,1);
bool success=mode=="announce-success";
unsigned said=0;for(auto& m:messages)said+=m.find("written to")!=std::string::npos;
need(said==(success?1u:0u),"success announcement matches actual publication");
need(success || files[target]==previous,"failed announced capture preserves target");emit(argv[2]);return 0;
}
const bool healthy=mode=="healthy";
bool ok=saved(target,17);need(ok==healthy,"publication status reflects write/close/replace result");
need(live.empty(),"all file handles closed on success and failure");
if(!healthy){
need(files[target]==previous,"failed capture preserves previous complete file");
need(!messages.empty() && messages.back().find("capture.png")!=std::string::npos,"failure identifies capture path");
need(files.size()==(mode=="cleanup-failure"?2u:1u),"only owned incomplete temporary may remain on cleanup failure");
if(mode=="cleanup-failure")return 0;
mode="healthy";fail_write=0;need(saved(target,19),"subsequent capture recovers without disabling output");
}
emit(argv[2]);return 0;
}

View File

@@ -0,0 +1,156 @@
// Synthetic transfer/readback fixture. test_present_aspect_vulkan.py inserts the
// production helpers/present block at the markers below; no game content is used.
#include <vulkan/vulkan.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <vector>
#include "vk_present_rect.h"
using namespace mkwvk;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr,"FAIL %d: %s\n",__LINE__,#x); std::exit(1); } } while(0)
#define OK(x) CHECK((x)==VK_SUCCESS)
VkDevice device;
VkPhysicalDevice physical;
VkPhysicalDeviceMemoryProperties memory_props;
struct { VkExtent2D swap_extent; } dev;
float display_aspect;
float get_aspect() { return display_aspect; }
struct Host { float (*aspect_ratio)(); } host_object{get_aspect};
Host* host = &host_object;
VkImage framegen_scale(VkCommandBuffer,VkImageView,VkImage,uint32_t,uint32_t,const int*,uint32_t,uint32_t) {
return VK_NULL_HANDLE; // This GPU fixture exercises direct blits; filtered routing is covered by L1.
}
// @PRODUCTION_HELPERS@
void record_actual(VkCommandBuffer command,VkImage src,uint32_t sw,uint32_t sh,const int* crop,VkImage dst) {
struct { VkCommandBuffer cmd; } f{command};
VkImageView view=VK_NULL_HANDLE;
// @PRODUCTION_PRESENT@
}
uint32_t memory_type(uint32_t bits,VkMemoryPropertyFlags flags) {
for(uint32_t i=0;i<memory_props.memoryTypeCount;i++)
if((bits&(1u<<i))&&(memory_props.memoryTypes[i].propertyFlags&flags)==flags) return i;
CHECK(false); return 0;
}
struct Buffer { VkBuffer object; VkDeviceMemory memory; };
Buffer buffer(VkDeviceSize size,VkBufferUsageFlags usage) {
Buffer b{}; VkBufferCreateInfo ci{}; ci.sType=VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
ci.size=size;ci.usage=usage;ci.sharingMode=VK_SHARING_MODE_EXCLUSIVE;
OK(vkCreateBuffer(device,&ci,nullptr,&b.object));
VkMemoryRequirements req{}; vkGetBufferMemoryRequirements(device,b.object,&req);
VkMemoryAllocateInfo allocation{};allocation.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocation.allocationSize=req.size;
allocation.memoryTypeIndex=memory_type(req.memoryTypeBits,VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT|VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
OK(vkAllocateMemory(device,&allocation,nullptr,&b.memory));
OK(vkBindBufferMemory(device,b.object,b.memory,0));return b;
}
struct Image { VkImage object;VkDeviceMemory memory; };
Image image(uint32_t width,uint32_t height) {
Image result{};VkImageCreateInfo ci{};ci.sType=VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
ci.imageType=VK_IMAGE_TYPE_2D;ci.format=VK_FORMAT_R8G8B8A8_UNORM;ci.extent={width,height,1};
ci.mipLevels=ci.arrayLayers=1;ci.samples=VK_SAMPLE_COUNT_1_BIT;ci.tiling=VK_IMAGE_TILING_OPTIMAL;
ci.usage=VK_IMAGE_USAGE_TRANSFER_SRC_BIT|VK_IMAGE_USAGE_TRANSFER_DST_BIT;ci.sharingMode=VK_SHARING_MODE_EXCLUSIVE;
OK(vkCreateImage(device,&ci,nullptr,&result.object));
VkMemoryRequirements req{};vkGetImageMemoryRequirements(device,result.object,&req);
VkMemoryAllocateInfo allocation{};allocation.sType=VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocation.allocationSize=req.size;allocation.memoryTypeIndex=memory_type(req.memoryTypeBits,VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
OK(vkAllocateMemory(device,&allocation,nullptr,&result.memory));
OK(vkBindImageMemory(device,result.object,result.memory,0));return result;
}
void release(Buffer b){vkDestroyBuffer(device,b.object,nullptr);vkFreeMemory(device,b.memory,nullptr);}
void release(Image i){vkDestroyImage(device,i.object,nullptr);vkFreeMemory(device,i.memory,nullptr);}
struct Case { const char* name;uint32_t width,height;float aspect;PresentRect expected;bool padded; };
int main() {
VkApplicationInfo app{};app.sType=VK_STRUCTURE_TYPE_APPLICATION_INFO;app.pApplicationName="Hookshot F01 synthetic presentation";
app.apiVersion=VK_API_VERSION_1_3;
VkInstanceCreateInfo ici{};ici.sType=VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;ici.pApplicationInfo=&app;
VkInstance instance{};OK(vkCreateInstance(&ici,nullptr,&instance));
uint32_t count=0;OK(vkEnumeratePhysicalDevices(instance,&count,nullptr));CHECK(count);
std::vector<VkPhysicalDevice> cards(count);OK(vkEnumeratePhysicalDevices(instance,&count,cards.data()));
physical=cards[0];
for(auto candidate:cards){VkPhysicalDeviceProperties props{};vkGetPhysicalDeviceProperties(candidate,&props);
if(props.deviceType==VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU){physical=candidate;break;}}
VkPhysicalDeviceProperties props{};vkGetPhysicalDeviceProperties(physical,&props);
std::printf("DEVICE: %s, api %u.%u, driver %u\n",props.deviceName,VK_VERSION_MAJOR(props.apiVersion),VK_VERSION_MINOR(props.apiVersion),props.driverVersion);
CHECK(props.apiVersion>=VK_API_VERSION_1_3);
vkGetPhysicalDeviceMemoryProperties(physical,&memory_props);
VkFormatProperties format{};vkGetPhysicalDeviceFormatProperties(physical,VK_FORMAT_R8G8B8A8_UNORM,&format);
const VkFormatFeatureFlags needed=VK_FORMAT_FEATURE_BLIT_SRC_BIT|VK_FORMAT_FEATURE_BLIT_DST_BIT|VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
CHECK((format.optimalTilingFeatures&needed)==needed);
uint32_t families=0;vkGetPhysicalDeviceQueueFamilyProperties(physical,&families,nullptr);
std::vector<VkQueueFamilyProperties> queues(families);vkGetPhysicalDeviceQueueFamilyProperties(physical,&families,queues.data());
uint32_t family=0;while(family<families&&!(queues[family].queueFlags&VK_QUEUE_GRAPHICS_BIT))++family;CHECK(family<families);
float priority=1;VkDeviceQueueCreateInfo qci{};qci.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
qci.queueFamilyIndex=family;qci.queueCount=1;qci.pQueuePriorities=&priority;
VkPhysicalDeviceVulkan13Features features{};features.sType=VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
features.synchronization2=VK_TRUE;
VkDeviceCreateInfo dci{};dci.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;dci.pNext=&features;dci.queueCreateInfoCount=1;dci.pQueueCreateInfos=&qci;
OK(vkCreateDevice(physical,&dci,nullptr,&device));VkQueue queue{};vkGetDeviceQueue(device,family,0,&queue);
VkCommandPoolCreateInfo pci{};pci.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;pci.queueFamilyIndex=family;
VkCommandPool pool{};OK(vkCreateCommandPool(device,&pci,nullptr,&pool));
VkCommandBufferAllocateInfo cai{};cai.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;cai.commandPool=pool;
cai.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY;cai.commandBufferCount=1;VkCommandBuffer command{};OK(vkAllocateCommandBuffers(device,&cai,&command));
const float wide=43.0f/18.0f;
const Case cases[]={
{"physical16:9",1600,1000,0,{0,50,1600,900},false},
{"wide matching",3440,1440,wide,{0,0,3440,1440},false},
{"maximize geometry",1920,1009,wide,{0,102,1920,804},false},
{"wide into16:9",1280,720,wide,{0,92,1280,536},false},
{"restore geometry",1720,720,wide,{0,0,1720,720},false},
{"padded active crop",1280,720,wide,{0,92,1280,536},true},
{"invalid override",1600,1000,std::numeric_limits<float>::infinity(),{0,50,1600,900},false}
};
for(const auto& test:cases){
const uint32_t sw=test.padded?80:64,sh=test.padded?50:36;
const int crop[4]={8,7,64,36};
Buffer upload=buffer(sw*sh*4,VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
Buffer download=buffer(VkDeviceSize(test.width)*test.height*4,VK_BUFFER_USAGE_TRANSFER_DST_BIT);
Image src=image(sw,sh),dst=image(test.width,test.height);
void* mapped=nullptr;OK(vkMapMemory(device,upload.memory,0,VK_WHOLE_SIZE,0,&mapped));
auto* pixels=static_cast<uint8_t*>(mapped);
for(uint32_t y=0;y<sh;y++)for(uint32_t x=0;x<sw;x++){
bool white=!test.padded||(x>=8&&x<72&&y>=7&&y<43);
const size_t at=(size_t(y)*sw+x)*4;
pixels[at]=255;pixels[at+1]=pixels[at+2]=white?255:0;pixels[at+3]=255;
}
vkUnmapMemory(device,upload.memory);
OK(vkResetCommandPool(device,pool,0));VkCommandBufferBeginInfo begin{};begin.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
OK(vkBeginCommandBuffer(command,&begin));
image_barrier(command,src.object,VK_IMAGE_LAYOUT_UNDEFINED,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkBufferImageCopy copy{};copy.imageSubresource={VK_IMAGE_ASPECT_COLOR_BIT,0,0,1};copy.imageExtent={sw,sh,1};
vkCmdCopyBufferToImage(command,upload.object,src.object,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,1,&copy);
image_barrier(command,src.object,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,VK_IMAGE_LAYOUT_GENERAL);
image_barrier(command,dst.object,VK_IMAGE_LAYOUT_UNDEFINED,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
const VkClearColorValue magenta{{1,0,1,1}};const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT,0,1,0,1};
vkCmdClearColorImage(command,dst.object,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,&magenta,1,&range);
image_barrier(command,dst.object,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
dev.swap_extent={test.width,test.height};display_aspect=test.aspect;
record_actual(command,src.object,sw,sh,test.padded?crop:nullptr,dst.object);
image_barrier(command,dst.object,VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
copy.imageExtent={test.width,test.height,1};
vkCmdCopyImageToBuffer(command,dst.object,VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,download.object,1,&copy);
OK(vkEndCommandBuffer(command));VkSubmitInfo submit{};submit.sType=VK_STRUCTURE_TYPE_SUBMIT_INFO;submit.commandBufferCount=1;submit.pCommandBuffers=&command;
OK(vkQueueSubmit(queue,1,&submit,VK_NULL_HANDLE));OK(vkQueueWaitIdle(queue));
OK(vkMapMemory(device,download.memory,0,VK_WHOLE_SIZE,0,&mapped));pixels=static_cast<uint8_t*>(mapped);
uint64_t checked=0;
for(uint32_t y=0;y<test.height;y++)for(uint32_t x=0;x<test.width;x++){
const auto r=test.expected;const bool inside=x>=r.x&&x<r.x+r.w&&y>=r.y&&y<r.y+r.h;
// Vulkan linear blits sample a half-source-texel footprint beyond a
// crop boundary. Exclude that scaled footprint only for the padded
// fixture; still check every bar pixel and all other content pixels.
const uint32_t margin_x=(r.w+127)/128+1,margin_y=(r.h+71)/72+1;
if(test.padded&&inside&&(x-r.x<margin_x||r.x+r.w-1-x<margin_x||
y-r.y<margin_y||r.y+r.h-1-y<margin_y))continue;
const size_t at=(size_t(y)*test.width+x)*4;const uint8_t want=inside?255:0;
if(pixels[at]!=want||pixels[at+1]!=want||pixels[at+2]!=want||pixels[at+3]!=255){
std::fprintf(stderr,"FAIL %s pixel %u,%u: %u,%u,%u,%u\n",test.name,x,y,pixels[at],pixels[at+1],pixels[at+2],pixels[at+3]);return 1;
}
++checked;
}
vkUnmapMemory(device,download.memory);release(src);release(dst);release(upload);release(download);
std::printf("PASS %s: %llu GPU readback pixels\n",test.name,static_cast<unsigned long long>(checked));
}
vkDestroyCommandPool(device,pool,nullptr);vkDestroyDevice(device,nullptr);vkDestroyInstance(instance,nullptr);
std::puts("PASS: seven offscreen GPU cases; no swapchain/window, no game assets");return 0;
}

View File

@@ -0,0 +1,136 @@
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <algorithm>
#include <atomic>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <string>
#include <vector>
#include "vk_present_rect.h"
#include "vk_present_trace.h"
using namespace mkwvk;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
using VkImage = uint64_t;
using VkImageView = uint64_t;
constexpr VkImage VK_NULL_HANDLE = 0;
constexpr int VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL = 1, VK_IMAGE_ASPECT_COLOR_BIT = 1;
struct VkClearColorValue { float r, g, b, a; };
struct VkImageSubresourceRange { int aspect, mip, levels, layer, layers; };
struct { struct { uint32_t width = 1720, height = 720; } swap_extent; bool hdr = false; } dev;
std::atomic<bool> present_native_pq{false};
struct Host { float (*aspect_ratio)(); };
float ratio = 43.0f / 18.0f;
int aspect_calls = 0;
float aspect_callback() { ++aspect_calls; return ratio; }
Host h{aspect_callback}; Host* host = &h;
struct { int upscale = 0, sharpness = 0, framegen = 0; } opts;
std::atomic<uint32_t> present_kind{0};
bool scaled = false;
bool encoded = false;
int hdr_calls = 0;
int blits = 0, clears = 0;
PresentRect drawn{};
std::vector<std::string> messages;
void logf(const char* format, ...) {
char b[1024]; va_list a; va_start(a, format); std::vsnprintf(b, sizeof b, format, a); va_end(a); messages.emplace_back(b);
}
void vkCmdClearColorImage(int, VkImage, int, const VkClearColorValue*, int, const VkImageSubresourceRange*) { ++clears; }
void image_barrier(int, VkImage, int, int) {}
void full_barrier(int) {}
VkImageView framegen_scaled_view() { return 19; }
VkImage framegen_hdr(int, VkImageView, uint32_t, uint32_t, const int*, uint32_t, uint32_t) {
++hdr_calls; return encoded ? 29 : VK_NULL_HANDLE;
}
VkImage framegen_scale(int, VkImageView, VkImage, uint32_t, uint32_t, const int*, uint32_t, uint32_t) { return scaled ? 9 : 0; }
void blit_crop_to_rect(int, VkImage, uint32_t, uint32_t, const int*, VkImage, int, PresentRect rect) { ++blits; drawn = rect; }
void present(VkImage src = 1, uint32_t sw = 3440, uint32_t sh = 1440, const int* crop = nullptr, VkImageView view = 2) {
struct { int cmd = 1; } f; VkImage dst = 3;
/* ACTUAL_DRAW */
/* ACTUAL_TRACE */
}
void unit_state() {
CHECK(PresentGeometryTrace::kLimit == 256);
PresentGeometryTrace t; PresentGeometrySample s;
CHECK(t.note(s) == PresentTraceDecision::Emit);
CHECK(t.note(s) == PresentTraceDecision::Quiet);
s.kind = 1; CHECK(t.note(s) == PresentTraceDecision::Emit);
for (int i = 0; i < 10000; ++i) { s.kind = uint32_t(i % 2); CHECK(t.note(s) == PresentTraceDecision::Quiet); }
s.kind = 2; CHECK(t.note(s) == PresentTraceDecision::Emit);
s.host_aspect(true, std::numeric_limits<double>::quiet_NaN()); CHECK(t.note(s) == PresentTraceDecision::Emit);
s.host_aspect(true, std::numeric_limits<double>::infinity()); CHECK(t.note(s) == PresentTraceDecision::Quiet);
s.host_aspect(true, -1); CHECK(t.note(s) == PresentTraceDecision::Quiet);
s.host_aspect(false, 0); CHECK(t.note(s) == PresentTraceDecision::Emit);
s.host_aspect(true, 43.0/18.0); CHECK(t.note(s) == PresentTraceDecision::Emit);
for (int i = 0; i < 10000; ++i) {
s.filter = i % 3; s.sharpness = i % 101; s.framegen = i % 6;
CHECK(t.note(s) == PresentTraceDecision::Quiet);
}
++s.output[0]; CHECK(t.note(s) == PresentTraceDecision::Emit);
PresentGeometryTrace bounded;
for (uint32_t i = 0; i < PresentGeometryTrace::kLimit; ++i) { s.output[0] = i; CHECK(bounded.note(s) == PresentTraceDecision::Emit); }
++s.output[0]; CHECK(bounded.note(s) == PresentTraceDecision::Limit);
for (int i = 0; i < 10000; ++i) { ++s.output[0]; CHECK(bounded.note(s) == PresentTraceDecision::Quiet); }
}
int main(int argc, char** argv) {
CHECK(argc == 2); const bool enabled = std::atoi(argv[1]) != 0; unit_state();
present(); CHECK(blits == 1 && drawn.w == 1720 && drawn.h == 720);
CHECK(aspect_calls == 1); // tracing reuses the actual fit input, including when the callback could change.
if (enabled) {
CHECK(messages.size() == 1);
CHECK(messages.back().find("kind=game source=3440x1440") != std::string::npos);
CHECK(messages.back().find("output=1720x720 destination=0,0 1720x720") != std::string::npos);
CHECK(messages.back().find("route=direct") != std::string::npos);
}
for (int i = 0; i < 100; ++i) present();
CHECK(messages.size() == size_t(enabled));
dev.swap_extent = {3440, 1369}; present();
if (enabled) CHECK(messages.size() == 2 && messages.back().find("output=3440x1369") != std::string::npos);
const int crop[] = {16, 8, 2560, 1440}; present(1, 4096, 2160, crop);
if (enabled) CHECK(messages.back().find("crop=supplied:16,8 2560x1440 content=2560x1440") != std::string::npos);
const size_t before_requests = messages.size();
opts.upscale = 2; opts.sharpness = 50; present(1, 4096, 2160, crop);
CHECK(messages.size() == before_requests);
scaled = true; present(1, 4096, 2160, crop);
if (enabled) CHECK(messages.back().find("route=scaled view=1 filter-request=2 sharpness-request=50") != std::string::npos);
dev.hdr = encoded = true; present(1, 4096, 2160, crop);
if (enabled) CHECK(messages.back().find("route=hdr") != std::string::npos);
encoded = false; present(1, 4096, 2160, crop);
if (enabled) CHECK(messages.back().find("route=scaled") != std::string::npos);
encoded = true; present_native_pq = true;
const int before_native_pq = hdr_calls;
present(1, 4096, 2160, crop); CHECK(hdr_calls == before_native_pq);
dev.hdr = false; present_native_pq = false; encoded = false;
opts.framegen = 2; present_kind = 1; present(1, 2560, 1440);
if (enabled) CHECK(messages.back().find("kind=made") != std::string::npos && messages.back().find("framegen-request=2") != std::string::npos);
const size_t n = messages.size();
present_kind = 0; present(1, 4096, 2160, crop);
present_kind = 1; present(1, 2560, 1440); CHECK(messages.size() == n);
// The game stream's request-only change and alternating frames add nothing.
for (int i = 0; i < 100; ++i) { present_kind = 0; present(1, 4096, 2160, crop); present_kind = 1; present(1, 2560, 1440); }
CHECK(messages.size() == n);
scaled = false;
const int before_startup = aspect_calls;
present(0, 0, 0, nullptr, 0);
CHECK(aspect_calls == before_startup);
if (enabled) CHECK(messages.back().find("kind=startup") != std::string::npos && messages.back().find("route=clear") != std::string::npos);
ratio = std::numeric_limits<float>::quiet_NaN(); present_kind = 0; present();
if (enabled) CHECK(messages.back().find("host-aspect=invalid:0") != std::string::npos);
const size_t invalid = messages.size(); present(); CHECK(messages.size() == invalid);
host = nullptr; present();
if (enabled) CHECK(messages.back().find("host-aspect=unavailable:0") != std::string::npos);
const size_t before_slider = messages.size();
for (int i = 0; i < 1000; ++i) { opts.sharpness = i % 101; present(); }
CHECK(messages.size() == before_slider);
++dev.swap_extent.width; present();
CHECK(messages.size() == before_slider + size_t(enabled));
for (uint32_t i = 0; i < 300; ++i) { dev.swap_extent.width = 1720 + i; present(); }
if (enabled) {
CHECK(messages.size() == 257);
CHECK(messages.back().find("limit of 256 changes reached") != std::string::npos);
}
if (!enabled) CHECK(messages.empty());
std::puts("PASS: effective geometry/route changes, independent frame kinds, invalid-aspect stability, opt-in and bounded output");
}

View File

@@ -0,0 +1,151 @@
// Production consumers inserted by test_present_source_crop.py; no GPU or game data.
#include <algorithm>
#include <atomic>
#include <climits>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <initializer_list>
#include <mutex>
#include <new>
#include <thread>
#include <utility>
#define VK_NO_PROTOTYPES
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
#include "vk_present_rect.h"
using namespace mkwvk;
using VmaAllocation = void*;
constexpr int kMaxMult = 8;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #x); std::exit(1); } } while (0)
/* ACTUAL_STATE */
struct { int framegen = 2, hdr_white = 200, hdr_peak = 1000; } opts;
struct { bool push_descriptor = true; std::mutex present_mu; } dev;
constexpr bool vkCmdPushDescriptorSetKHR = true;
constexpr uint32_t HDR_ENCODE_COMP_SPV[] = {0};
bool init_scale() { std::abort(); }
bool make_pipe(Pipe&, const uint32_t*, size_t, std::initializer_list<VkDescriptorType>, int) { std::abort(); }
void logf(const char*) { std::abort(); }
void device_wait_idle_locked() { std::abort(); }
void drop_img(Img&) { std::abort(); }
bool make_img(Img&, uint32_t, uint32_t, VkFormat, VkImageUsageFlags) { std::abort(); }
void to_general(VkCommandBuffer, VkImage) { std::abort(); }
float hdr_uv[4]{};
int blits = 0, clears = 0, initializations = 0, sizes = 0, dispatches = 0, copies = 0;
uint32_t allocated_w = 0, allocated_h = 0;
namespace fsr3fi { bool wanted = false; bool frames_wanted() { return wanted; } }
int fsr_calls = 0, fsr_result = 1;
int fsr3fi_frame(VkCommandBuffer, OutSet&, int, uint32_t w, uint32_t h, uint64_t sequence) {
CHECK(w == allocated_w && h == allocated_h && sequence > 0);
++fsr_calls; return fsr_result;
}
VkImageBlit last_blit{};
int32_t copy_pc[4]{};
void vkCmdBlitImage(VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout,
uint32_t count, const VkImageBlit* b, VkFilter filter) {
CHECK(count == 1 && filter == VK_FILTER_LINEAR); ++blits; last_blit = *b;
}
void vkCmdClearColorImage(VkCommandBuffer, VkImage, VkImageLayout, const VkClearColorValue* c,
uint32_t count, const VkImageSubresourceRange* range) {
CHECK(count == 1 && range->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
CHECK(c->float32[0] == 0 && c->float32[1] == 0 && c->float32[2] == 0 && c->float32[3] == 1); ++clears;
}
bool init() { ++initializations; return true; }
bool ensure_size(VkCommandBuffer, uint32_t w, uint32_t h) {
++sizes; allocated_w = w; allocated_h = h; return true;
}
int64_t now_ns() { return 1000000; }
int multiplier() { return 2; }
int display_refresh_mhz() { return 60000; }
int auto_slots(int64_t, int64_t, int64_t, int64_t&, float*, int64_t*, int) { return 0; }
void display_main() { std::abort(); } // Never started: isolated recorder sets thread_started.
void full_barrier(VkCommandBuffer) {}
void pass(VkCommandBuffer, const Pipe& p, std::initializer_list<std::pair<VkImageView, VkSampler>>,
uint32_t, uint32_t, const void* pc = nullptr, uint32_t bytes = 0) {
++dispatches;
if (&p == &fg.copy) { CHECK(bytes == sizeof copy_pc); std::memcpy(copy_pc, pc, bytes); }
if (&p == &hdr.pipe) { CHECK(bytes == 32); std::memcpy(hdr_uv, pc, sizeof hdr_uv); }
}
void vkCmdCopyImage(VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout,
uint32_t count, const VkImageCopy* c) {
CHECK(count == 1 && c->extent.width == allocated_w && c->extent.height == allocated_h); ++copies;
}
/* ACTUAL_FUNCTIONS */
void reset() {
fg.~State(); new (&fg) State;
fg.thread_started = true;
blits = clears = initializations = sizes = dispatches = copies = 0;
allocated_w = allocated_h = 0;
fsr3fi::wanted = false; fsr_calls = 0; fsr_result = 1;
std::memset(copy_pc, 0, sizeof copy_pc);
}
void check(uint32_t sw, uint32_t sh, const int* crop, uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
reset();
blit_crop_to_rect(nullptr, 1, sw, sh, crop, 2, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, {3, 5, 1920, 1080});
const bool valid = w && h;
CHECK(blits == int(valid) && clears == int(!valid));
if (valid) {
CHECK(last_blit.srcOffsets[0].x == int(x) && last_blit.srcOffsets[0].y == int(y));
CHECK(last_blit.srcOffsets[1].x == int(x + w) && last_blit.srcOffsets[1].y == int(y + h));
CHECK(last_blit.dstOffsets[0].x == 3 && last_blit.dstOffsets[1].x == 1923);
}
blits = clears = 0;
blit_crop(nullptr, 1, sw, sh, crop, 2, VK_IMAGE_LAYOUT_GENERAL, 320, 180);
CHECK(blits == int(valid) && clears == int(!valid));
if (valid) CHECK(last_blit.dstOffsets[0].x == 0 && last_blit.dstOffsets[1].x == 320);
const bool recorded = framegen_record(nullptr, 1, sw, sh, crop);
CHECK(recorded == valid && initializations == int(valid) && sizes == int(valid));
if (valid) {
CHECK(allocated_w == w && allocated_h == h && dispatches == 2 && copies == 1);
CHECK(copy_pc[0] == int(x) && copy_pc[1] == int(y) && copy_pc[2] == int(w) && copy_pc[3] == int(h));
// Newly merged FSR3 receives the same validated extent; fallback still uses the shared crop.
fsr3fi::wanted = true;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 1 && fg.recorded->made == 1);
CHECK(dispatches == 4 && copies == 2);
fsr_result = 0;
CHECK(framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 2 && fg.recorded->made == 1);
CHECK(dispatches == 9 && copies == 3);
CHECK(copy_pc[0] == int(x) && copy_pc[1] == int(y) && copy_pc[2] == int(w) && copy_pc[3] == int(h));
} else {
CHECK(dispatches == 0 && copies == 0 && !fg.recorded);
fsr3fi::wanted = true; fg.have_prev = true;
CHECK(!framegen_record(nullptr, 1, sw, sh, crop) && fsr_calls == 0);
CHECK(dispatches == 0 && copies == 0 && !fg.recorded);
}
// Execute the actual HDR consumer, keeping resources ready to isolate source validation.
hdr.init = true; hdr.failed = false; hdr.w = 1920; hdr.h = 1080; hdr.out.image = 77;
const int before = dispatches;
CHECK(framegen_hdr(nullptr, 1, sw, sh, crop, 1920, 1080) == (valid ? 77 : VK_NULL_HANDLE));
CHECK(dispatches == before + int(valid));
if (valid) {
CHECK(hdr_uv[0] == x / float(sw) && hdr_uv[1] == y / float(sh));
CHECK(hdr_uv[2] == (x + w) / float(sw) && hdr_uv[3] == (y + h) / float(sh));
}
}
int main() {
check(3440, 1440, nullptr, 0, 0, 3440, 1440);
const int ordinary[] = {160, 90, 2240, 1260}; check(2560, 1440, ordinary, 160, 90, 2240, 1260);
const int clipped[] = {2500, 1400, 100, 100}; check(2560, 1440, clipped, 2500, 1400, 60, 40);
const int overflow[] = {1, 1, INT_MAX, INT_MAX}; check(640, 480, overflow, 1, 1, 639, 479);
const int bad[][4] = {{-1, 0, 40, 40}, {0, -1, 40, 40}, {INT_MIN, 0, INT_MAX, 40},
{640, 0, 1, 1}, {0, 480, 1, 1}, {INT_MAX, INT_MAX, 1, 1}};
for (const auto& c : bad) check(640, 480, c, 0, 0, 0, 0);
const int disabled[][4] = {{INT_MIN, INT_MIN, 0, 10}, {-1, -1, 10, -1}, {20, 30, -1, -1}};
for (const auto& c : disabled) check(640, 480, c, 0, 0, 640, 480);
check(0, 480, nullptr, 0, 0, 0, 0);
check(640, 0, nullptr, 0, 0, 0, 0);
check(uint32_t(INT_MAX) + 1, 480, nullptr, 0, 0, 0, 0);
check(640, uint32_t(INT_MAX) + 1, nullptr, 0, 0, 0, 0);
uint32_t seed = 42;
for (int i = 0; i < 4096; ++i) {
seed = seed * 1664525u + 1013904223u; const int x = int(seed % 640);
seed = seed * 1664525u + 1013904223u; const int y = int(seed % 480);
const int c[] = {x, y, INT_MAX, INT_MAX};
check(640, 480, c, uint32_t(x), uint32_t(y), uint32_t(640-x), uint32_t(480-y));
}
reset(); blit_crop_to_rect(nullptr, 1, 640, 480, nullptr, 2, VK_IMAGE_LAYOUT_GENERAL, {0, 0, 0, 100});
CHECK(blits == 0 && clears == 0);
std::puts("PASS: presentation/capture/interpolation source crops agree; invalid input clears or skips, no signed overflow");
}

View File

@@ -0,0 +1,158 @@
// Instrument Vulkan, not the loader: the runner compiles the actual Stage/Program
// declarations, serializer, Reader, make_module and program_cache_load functions.
// Info is a minimal stand-in with the cache's separately serialized containers;
// this test covers module ownership, not Shader::Info layout or SPIR-V validity.
#include <array>
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#ifdef _WIN32
#include <io.h>
#else
#define _fseeki64 fseeko
#define _ftelli64 ftello
#endif
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
using VkShaderModule = uintptr_t;
using VkDescriptorSetLayout = uintptr_t;
using VkPipelineLayout = uintptr_t;
using VkPipeline = uintptr_t;
using VkDescriptorType = uint32_t;
using VkShaderStageFlagBits = uint32_t;
using VkShaderStageFlags = uint32_t;
using VkPrimitiveTopology = uint32_t;
constexpr uintptr_t VK_NULL_HANDLE = 0;
constexpr uint32_t VK_SHADER_STAGE_VERTEX_BIT = 1, VK_SHADER_STAGE_FRAGMENT_BIT = 16;
constexpr uint32_t VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST = 3;
constexpr size_t kIdxEntry = 20;
constexpr int VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO = 15;
struct VkShaderModuleCreateInfo {
int sType;
size_t codeSize = 0;
const uint32_t* pCode = nullptr;
};
namespace Shader {
namespace IR { enum class Attribute : uint64_t {}; }
struct Descriptor { // (a descriptor's array size, which get_info checks)
uint32_t count;
Descriptor(uint32_t c = 1) : count(c) {}
};
struct Info {
uint32_t marker = 0;
std::vector<Descriptor> constant_buffer_descriptors, storage_buffers_descriptors,
texture_buffer_descriptors, image_buffer_descriptors, texture_descriptors, image_descriptors;
std::map<IR::Attribute, IR::Attribute> legacy_stores_mapping;
std::vector<std::vector<uint32_t>> handle_programs;
};
}
#include "cache_types.inc"
struct { uintptr_t device = 123; } dev;
std::map<VkShaderModule, unsigned> destroyed;
unsigned attempts = 0, created = 0, releases = 0, fail_at = 0;
int vkCreateShaderModule(uintptr_t device, const VkShaderModuleCreateInfo* info, const void* allocator,
VkShaderModule* module) {
CHECK(device == dev.device && allocator == nullptr);
CHECK(info->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO && info->codeSize && info->pCode);
++attempts;
*module = VK_NULL_HANDLE;
if (attempts == fail_at) return -1;
*module = ++created;
CHECK(destroyed.emplace(*module, 0).second);
return 0;
}
void vkDestroyShaderModule(uintptr_t device, VkShaderModule module, const void* allocator) {
CHECK(device == dev.device && allocator == nullptr && module != VK_NULL_HANDLE);
auto it = destroyed.find(module);
CHECK(it != destroyed.end() && it->second == 0);
++it->second;
++releases;
}
void disk_init() {}
void logf(const char*, ...) {}
bool disk_on = true, disk_listing = true;
FILE* disk_in = nullptr;
FILE* disk_out = nullptr;
FILE* disk_idx = nullptr;
std::mutex disk_mu;
std::unordered_map<uint64_t, std::vector<DiskRecord>> disk_records;
std::atomic<int64_t> st_cache_hits{0};
#define PROF_EXACT() // (vk_prof.h's exact-timing scope, since #989: nothing to time here)
#include "cache_functions.inc"
void save(uint64_t key, bool empty_second = false, int bad_answer = 0) {
Program program;
program.key = key;
program.stages.emplace_back();
program.stages.emplace_back();
program.stages[0].nvn_stage = 0;
program.stages[0].vk_stage = VK_SHADER_STAGE_VERTEX_BIT;
program.stages[1].nvn_stage = 1;
program.stages[1].vk_stage = VK_SHADER_STAGE_FRAGMENT_BIT;
program.stages[1].info.handle_programs = {{1, 2}};
program.stages[1].info.texture_descriptors = {0};
program.answers.push_back({1, 0, 1, false}); // (a fitting answer: stage 1's one texture)
// (the file is passed around: an answer naming a stage or slot the program does not have is refused)
if (bad_answer == 1) program.answers.push_back({7, 0, 1, false});
if (bad_answer == 2) program.answers.push_back({1, 3, 1, false});
std::vector<std::vector<uint32_t>> code{{0x07230203}, {0x07230203}};
if (empty_second) code[1].clear();
program_cache_save(program, code);
CHECK(disk_records.at(key).size() > 0);
}
void release_accepted(std::vector<std::unique_ptr<Program>>& programs) {
for (auto& p : programs) for (auto& stage : p->stages) {
CHECK(stage.module && destroyed.at(stage.module) == 0);
vkDestroyShaderModule(dev.device, stage.module, nullptr);
stage.module = VK_NULL_HANDLE;
}
programs.clear();
}
int main(int argc, char** argv) {
CHECK(argc == 2);
disk_in = disk_out = std::tmpfile();
CHECK(disk_in);
const std::string scenario = argv[1];
save(7, scenario == "empty", scenario == "answer_stage" ? 1 : scenario == "answer_desc" ? 2 : 0);
if (scenario == "first") fail_at = 1;
if (scenario == "second" || scenario == "mixed") fail_at = 2;
if (scenario == "truncated") --disk_records.at(7).back().size;
if (scenario == "mixed") {
save(7); // A valid variant after the failing variant, under the same key.
save(9); // An independent key must also remain usable.
}
std::vector<std::unique_ptr<Program>> programs;
program_cache_load(7, programs);
unsigned accepted = scenario == "valid" || scenario == "mixed" ? 1 : 0;
CHECK(programs.size() == accepted && st_cache_hits == accepted);
std::printf("%s: created=%u destroyed=%u accepted_modules=%u\n", scenario.c_str(), created, releases, accepted * 2);
CHECK(created - releases == accepted * 2);
if (scenario == "first") CHECK(created == 0 && releases == 0);
if (scenario == "second" || scenario == "empty") CHECK(created == 1 && releases == 1);
if (scenario == "truncated") CHECK(created == 0 && releases == 0);
if (scenario == "answer_stage" || scenario == "answer_desc") CHECK(created == 2 && releases == 2);
if (scenario == "valid") CHECK(created == 2 && releases == 0);
if (scenario == "mixed") {
CHECK(created == 3 && releases == 1);
program_cache_load(9, programs);
CHECK(programs.size() == 2 && st_cache_hits == 2 && created == 5 && releases == 1);
program_cache_load(7, programs); // Consumed records cannot be cleaned up twice.
CHECK(programs.size() == 2 && created == 5 && releases == 1);
}
for (const auto& p : programs) CHECK(p->ready.load() && p->stages.size() == 2);
release_accepted(programs);
CHECK(created == releases);
for (const auto& [module, count] : destroyed) { (void)module; CHECK(count == 1); }
CHECK(std::fclose(disk_in) == 0);
std::puts("PASS: no live, invalid or double-destroyed module");
}

View File

@@ -0,0 +1,257 @@
#define _CRT_SECURE_NO_WARNINGS
#include <vulkan/vulkan.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <set>
#include <string>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
void require(bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", why); std::exit(1); }
}
bool fail_stream_commit = false, fail_pool_commit = false;
// Replace only the container allocator boundary, so actual ownership-commit code can fail.
template<class T> struct FaultVector : std::vector<T> {
using Base = std::vector<T>;
using typename Base::iterator;
bool failing() const { return std::is_same_v<T, VkDescriptorPool> ? fail_pool_commit : fail_stream_commit; }
iterator insert(iterator at, const T& value) {
if (failing()) throw std::bad_alloc();
return Base::insert(at, value);
}
void push_back(const T& value) {
if (failing()) throw std::bad_alloc();
Base::push_back(value);
}
};
struct Allocation { std::vector<uint8_t> bytes; };
using VmaAllocation = Allocation*;
struct { VkDevice device{}; VkPhysicalDeviceProperties props{}; } dev;
std::map<VkBuffer, Allocation> buffers;
std::set<VkDescriptorPool> pools;
std::map<VkDescriptorSet, VkDescriptorPool> sets;
uintptr_t serial = 100;
template<class T> T handle() { return reinterpret_cast<T>(++serial); }
bool fail_buffer = false, fail_map = false, fail_pool = false;
int fail_sets = 0, allocation_calls = 0, invalid_pool_calls = 0, destroys = 0, updates = 0, binds = 0;
uint64_t largest_request = 0;
std::mutex par_mutex;
struct ParLock { std::lock_guard<std::mutex> lock{par_mutex}; };
void logf(const char*, ...) {}
bool create_buffer(uint64_t bytes, VkBufferUsageFlags, bool, VkBuffer& buffer, VmaAllocation& allocation, void** map, bool = false) {
++allocation_calls; largest_request = std::max(largest_request, bytes);
if (fail_buffer || bytes > (16ull << 20)) return false;
buffer = handle<VkBuffer>(); auto& a = buffers[buffer]; a.bytes.assign((size_t)bytes, 0);
allocation = &a; *map = fail_map ? nullptr : a.bytes.data(); return true;
}
void destroy_buffer(VkBuffer buffer, VmaAllocation allocation) {
require(buffer && buffers.count(buffer) && &buffers.at(buffer) == allocation, "buffer ownership/double free");
buffers.erase(buffer); ++destroys;
}
VKAPI_ATTR VkResult VKAPI_CALL vkCreateDescriptorPool(VkDevice, const VkDescriptorPoolCreateInfo*, const VkAllocationCallbacks*, VkDescriptorPool* pool) {
// Vulkan output handles must not be trusted after a failing creation result.
if (fail_pool) { *pool = reinterpret_cast<VkDescriptorPool>(uintptr_t(0xdead)); return VK_ERROR_OUT_OF_HOST_MEMORY; }
*pool = handle<VkDescriptorPool>(); pools.insert(*pool); return VK_SUCCESS;
}
void drop_sets(VkDescriptorPool pool) {
for (auto it = sets.begin(); it != sets.end();) {
if (it->second == pool) it = sets.erase(it); else ++it;
}
}
VKAPI_ATTR VkResult VKAPI_CALL vkResetDescriptorPool(VkDevice, VkDescriptorPool pool, VkDescriptorPoolResetFlags) {
require(pools.count(pool), "reset invalid pool"); drop_sets(pool); return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL vkDestroyDescriptorPool(VkDevice, VkDescriptorPool pool, const VkAllocationCallbacks*) {
require(pools.erase(pool) == 1, "pool ownership/double free"); drop_sets(pool);
}
VKAPI_ATTR VkResult VKAPI_CALL vkAllocateDescriptorSets(VkDevice, const VkDescriptorSetAllocateInfo* info, VkDescriptorSet* set) {
if (!pools.count(info->descriptorPool)) { ++invalid_pool_calls; return VK_ERROR_UNKNOWN; }
if (fail_sets > 0) { --fail_sets; return VK_ERROR_OUT_OF_POOL_MEMORY; }
*set = handle<VkDescriptorSet>(); sets[*set] = info->descriptorPool; return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL vkUpdateDescriptorSets(VkDevice, uint32_t n, const VkWriteDescriptorSet* writes, uint32_t, const VkCopyDescriptorSet*) {
for (uint32_t i = 0; i < n; ++i) require(sets.count(writes[i].dstSet), "update failed/destroyed set");
++updates;
}
VKAPI_ATTR void VKAPI_CALL vkCmdBindDescriptorSets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t n, const VkDescriptorSet* bound, uint32_t, const uint32_t*) {
require(n == 1 && sets.count(bound[0]), "bind failed/destroyed set"); ++binds;
}
/* RES */
std::deque<RecordingRes> res;
thread_local RecordingRes* cur = nullptr;
const uint64_t kStream = 1024;
constexpr VkBufferUsageFlags kStreamUse = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
std::atomic<uint64_t> stream_extra{0}, stream_grown{0}, stream_peak{0};
bool grow_off = false;
bool stream_grow_off() { return grow_off; }
struct BufferRef { VkBuffer buffer{}; uint64_t offset = 0, size = 0, address = 0; };
/* POOL */
/* STREAM */
thread_local VkCommandBuffer rec_cmd{};
thread_local uint64_t rec_value = 0, targets_serial = 0;
// Global defaults are outside this per-recording allocation contract.
VkBuffer zero_buffer = reinterpret_cast<VkBuffer>(uintptr_t(99)); VmaAllocation zero_alloc{};
struct Pass {}; Pass& pass() { static thread_local Pass p; return p; }
bool work_since_barrier = false, last_dyn_valid = false, last_desc_valid = false, last_vb_valid = false;
VkPipeline last_pipeline{};
struct { bool on = false; } vertex_set, last_ib;
void descriptors_disturbed() {}
std::unordered_map<int, int>& handle_hits() { static thread_local std::unordered_map<int, int> h; return h; }
struct Program {};
constexpr int kNvnStages = 8;
/* BANK_CACHE */
std::array<std::array<BankCache, kNvnStages>, 2>& bank_cache() { static thread_local std::array<std::array<BankCache, kNvnStages>, 2> b; return b; }
void make_dummies() {}
/* BEGIN */
#define FINE_SCOPE(...) ((void)0)
#define FINE_N(...) ((void)0)
struct NvnState { uint64_t bind_gen = 1, tex_gen[kNvnStages]{}, ssbo_gen[kNvnStages]{}; uint32_t grid[3]{1, 1, 1}; uint8_t value = 17; };
struct Stage { int nvn_stage = 0; };
thread_local bool grid_patch_want = false;
thread_local uint64_t grid_patch_at = UINT64_MAX;
thread_local VkBuffer grid_patch_buf{};
thread_local int bank_fills = 0;
void driver_bank_fill(const NvnState& s, int, int, void* out, size_t n) { ++bank_fills; std::memset(out, s.value, n); }
uint32_t driver_bank_word(const NvnState& s, int, int, uint32_t) { return uint32_t(s.value) * 0x01010101u; }
VkDescriptorBufferInfo cached_bank(const NvnState& s, const Stage& st) {
const uint32_t index = 0, used = 16;
const uint64_t align = 256;
VkDescriptorBufferInfo bi{VK_NULL_HANDLE, 0, VK_WHOLE_SIZE};
/* BANK_BRANCH */
return bi;
}
struct Writes { std::vector<VkWriteDescriptorSet> writes{VkWriteDescriptorSet{}}; } writes;
Writes& dw() { return writes; }
bool bind_tail() {
VkCommandBuffer cmd{}; VkPipelineBindPoint bp = VK_PIPELINE_BIND_POINT_GRAPHICS;
struct { VkDescriptorSetLayout set_layout{}; VkPipelineLayout layout{}; } p;
/* DESCRIPTORS */
}
void begin(int slot = 0, uint64_t value = 1) { draw_begin_recording(slot, VkCommandBuffer{}, value); }
void cleanup() {
for (auto& r : res) {
for (auto& s : r.streams) destroy_buffer(s.buffer, s.alloc);
for (auto p : r.extra) vkDestroyDescriptorPool(dev.device, p, nullptr);
if (r.pool) vkDestroyDescriptorPool(dev.device, r.pool, nullptr);
}
require(buffers.empty() && pools.empty() && sets.empty(), "owned resources leaked");
}
void copied(size_t bytes, uint8_t value) {
std::vector<uint8_t> input(bytes, value);
const auto b = stream_copy(input.data(), input.size());
require(b.buffer && b.size == bytes && b.offset <= buffers.at(b.buffer).bytes.size() - bytes, "copy binding bounds");
require(std::equal(input.begin(), input.end(), buffers.at(b.buffer).bytes.begin() + b.offset), "CPU copy data");
}
int main(int argc, char** argv) {
require(argc == 2, "case argument"); const std::string mode = argv[1];
dev.props.limits.minUniformBufferOffsetAlignment = 256;
dev.props.limits.minStorageBufferOffsetAlignment = 64;
if (mode == "no-recording") {
require(stream_alloc(16, 256) == UINT64_MAX && allocation_calls == 0, "absent recording authorizes write");
cleanup(); return 0;
}
if (mode == "initial-buffer" || mode == "initial-map" || mode == "initial-commit") {
fail_buffer = mode == "initial-buffer"; fail_map = mode == "initial-map"; fail_stream_commit = mode == "initial-commit";
begin();
require(res[0].streams.empty() && !cur->stream && !cur->mapped && cur->size == 0, "incomplete initial stream published");
require(stream_alloc(16, 256) == UINT64_MAX && buffers.empty(), "missing initial stream authorizes write/leaks");
fail_buffer = fail_map = fail_stream_commit = false; begin(0, 2); copied(31, 17);
require(res[0].streams.size() == 1, "initial retry must create exactly one owned stream");
} else if (mode == "pool-retry") {
fail_pool = true; begin(); const auto first = cur->stream;
require(!bind_tail() && !invalid_pool_calls, "null initial pool reached Vulkan");
begin(0, 2); require(res[0].streams.size() == 1 && cur->stream == first, "pool retry duplicated/replaced stream");
fail_pool = false; begin(0, 3); require(bind_tail(), "pool retry did not recover"); copied(63, 51);
} else if (mode == "parallel") {
// Actual TLS selection and serialized resource admission, without concurrent growth of fixture maps.
std::thread a([] { begin(0); std::vector<uint8_t> input(31, 17); require(stream_copy(input.data(), input.size()).buffer != VK_NULL_HANDLE, "worker0 copy"); });
std::thread b([] { begin(1); std::vector<uint8_t> input(63, 51); require(stream_copy(input.data(), input.size()).buffer != VK_NULL_HANDLE, "worker1 copy"); });
a.join(); b.join(); require(res.size() == 2 && res[0].stream != res[1].stream, "worker slot ownership");
} else {
begin(); copied(127, 23);
const auto old = cur->stream; const auto used = cur->used; const auto count = buffers.size();
if (mode == "bank-same") {
NvnState state; state.ssbo_gen[0] = 1;
const auto first = cached_bank(state, Stage{});
state.ssbo_gen[0] = 2; state.bind_gen = 2; state.grid[0] = 7;
const auto second = cached_bank(state, Stage{}); const int fills = bank_fills;
const auto third = cached_bank(state, Stage{});
require(first.buffer == second.buffer && first.offset == second.offset && third.buffer == first.buffer &&
bank_fills == fills, "identical bank must retain its binding and cache the new generation");
} else if (mode == "bank-retry") {
NvnState state; state.ssbo_gen[0] = 1;
const auto first = cached_bank(state, Stage{});
require(first.buffer, "initial shader bank");
// Fill the remaining stream so the changed bank must grow it.
const auto remaining = cur->size - ((cur->used + 255) & ~255ull);
if (remaining) copied((size_t)remaining, 91);
state.ssbo_gen[0] = 2; state.bind_gen = 2; state.value = 29;
fail_buffer = true;
require(!cached_bank(state, Stage{}).buffer, "failed shader-bank allocation accepted");
fail_buffer = false;
const auto retry = cached_bank(state, Stage{});
require(retry.buffer, "shader bank retry did not recover");
const auto& bytes = buffers.at(retry.buffer).bytes;
require(std::all_of(bytes.begin() + retry.offset, bytes.begin() + retry.offset + retry.range,
[](uint8_t v) { return v == 29; }), "bank retry returned stale shader bytes");
require(buffers.at(first.buffer).bytes[first.offset] == 17, "old shader bank binding lost");
} else if (mode == "growth-buffer" || mode == "growth-map" || mode == "growth-commit") {
fail_buffer = mode == "growth-buffer"; fail_map = mode == "growth-map"; fail_stream_commit = mode == "growth-commit";
std::vector<uint8_t> input(4096, 9);
require(!stream_copy(input.data(), input.size()).buffer, "failed growth accepted");
require(cur->stream == old && cur->used == used && cur->si == 0 && buffers.size() == count && stream_extra == 0 && stream_grown == 0, "failed growth changed ownership/cursor/counters");
fail_buffer = fail_map = fail_stream_commit = false; copied(4096, 9); copied(1100000, 7);
require(cur->si == 2 && buffers.count(old) && stream_grown == 2, "growth retry retains old bindings");
} else if (mode == "overflow" || mode == "rounding" || mode == "alignment-zero" || mode == "alignment-three") {
const auto calls = allocation_calls;
uint64_t n = mode == "overflow" ? UINT64_MAX : mode == "rounding" ? UINT64_MAX - 1024 : 16;
uint64_t align = mode == "alignment-zero" ? 0 : mode == "alignment-three" ? 3 : 256;
require(stream_alloc(n, align) == UINT64_MAX && cur->stream == old && cur->used == used && allocation_calls == calls, "invalid/overflow range changed or admitted");
} else if (mode == "grow-off") {
grow_off = true; require(!stream_reserve(4096, 256), "disabled growth accepted");
require(cur->stream == old && cur->used == used && buffers.size() == count, "disabled growth changed current stream");
} else if (mode == "retention" || mode == "reuse") {
copied(4096, 91); const auto grown = cur->stream;
begin(0, 2); require(cur->stream == old && cur->used == 0 && cur->streams.size() == 2, "recording reuse reset");
copied(4096, 73); require(cur->stream == grown && buffers.size() == count + 1, "cached grown stream reuse");
if (mode == "retention") {
begin(0, 3); for (int i = 0; i < 120; ++i) begin(0, uint64_t(i + 4));
require(cur->streams.size() == 1 && !buffers.count(grown) && stream_extra == 0, "idle grown stream retirement");
}
} else if (mode == "descriptor-retained") {
fail_sets = 1; require(bind_tail() && cur->extra.size() == 1, "first extra pool");
const auto retained = cur->extra[0]; const auto count_before = pools.size();
fail_sets = 2; require(!bind_tail(), "second pool allocation failure");
require(cur->extra.size() == 1 && cur->extra[0] == retained && pools.count(retained) && pools.size() == count_before,
"rollback destroyed or displaced a retained pool");
require(bind_tail() && cur->extra.size() == 1, "retained pool could not retry");
} else if (mode == "descriptor-pool" || mode == "descriptor-set" || mode == "descriptor-commit") {
const auto pool_count = pools.size(); fail_sets = mode == "descriptor-set" ? 2 : 1;
fail_pool = mode == "descriptor-pool"; fail_pool_commit = mode == "descriptor-commit";
require(!bind_tail() && !updates && !binds && !invalid_pool_calls, "failed descriptor allocation updated/bound/invalid pool");
require(cur->extra.empty() && pools.size() == pool_count, "failed descriptor pool ownership retained/leaked");
fail_pool = fail_pool_commit = false; fail_sets = 1;
require(bind_tail() && cur->extra.size() == 1 && updates == 1 && binds == 1, "descriptor retry did not recover");
} else if (mode == "healthy") {
require(bind_tail() && cur->extra.empty() && updates == 1 && binds == 1, "healthy primary descriptor binding");
copied(17, 51); require(cur->stream == old && cur->used == 273, "alignment/prefix preservation");
} else require(false, "unknown case");
}
require(!invalid_pool_calls, "invalid pool call escaped"); cleanup();
std::printf("PASS %s actual allocation/ownership/retry\n", argv[1]);
}

391
tests/run_all.py Normal file
View File

@@ -0,0 +1,391 @@
"""Every game-free check in one run: each tests/test_*.py and nx2/test_*.py in its own process, the C++ unit tests under
gpu/vk/tests and a byte-compile of every tracked Python file. All of them run even when one fails; the failures are
printed whole at the end, with a table of every check (slowest first), also to $GITHUB_STEP_SUMMARY when it is set.
Exits 1 when anything failed.
python -I -B tests/run_all.py [--jobs N] [--timeout SECONDS] [--strict] [--no-skip] [name ...]
A name picks the checks whose path contains it. --strict: a check that needs a tool this PC lacks (a compiler, the
SPIR-V or Vulkan headers, spirv-val) fails instead of being skipped (CI installs them all). --no-skip runs the checks a
test skips on this OS too. Standard library only: no package is installed for the runner itself.
A test says what it needs in its own file, with up to three top-level assignments of plain literals (after its
docstring and imports). The runner reads them with ast and never imports the test; a test that declares none runs with
no arguments and its own defaults.
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"] # its command-line arguments
RUN_ALL_MODULES = ["zstandard"] # Python packages it imports
RUN_ALL_SKIP = {"windows": "needs spirv-val: checked on Linux"} # OS ("linux", "windows") -> why it is skipped
{name} in an argument is a tool or folder find_tools looks for (cxx, vulkan_headers, spirv_headers, spirv_val, glslang,
dotnet); a check whose tool, compiler (the word after --cc or --cxx) or module is missing here is skipped, or fails
with --strict. A check skipped on both OSes (a GPU, the game) names both. A declaration that is not a plain literal of
that shape, names an unknown placeholder or OS, or any other RUN_ALL_ name, stops the run with the file and line; so
does one set or changed anywhere but its own top-level line (inside an if or a try, .append(...), +=, a subscript).
Only the tests a run picks (by name) are read: a bad declaration elsewhere does not stop it. A test that does not parse
fails, on every OS, without running.
"""
import argparse
import ast
import concurrent.futures
import importlib.util
import os
import shutil
import subprocess
import sys
import tempfile
import time
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
WINDOWS = os.name == "nt"
OS = "windows" if WINDOWS else "linux"
# What a check needs is declared in the test file itself, as top-level literals the runner reads with ast (the test is
# never imported): see the docstring above and read_declarations below.
DECLARATIONS = ("RUN_ALL_ARGS", "RUN_ALL_MODULES", "RUN_ALL_SKIP")
# (a list's or a dict's methods that change it in place: a declaration changed that way is not what the runner reads)
MUTATORS = {"append", "extend", "insert", "remove", "pop", "clear", "update", "setdefault", "popitem", "sort",
"reverse", "__setitem__", "__delitem__", "__iadd__", "__ior__"}
OSES = ("linux", "windows")
# Retired: the central tables these declarations replaced. They stay, empty, so that an entry a branch from before the
# move adds here stops the run and says where it belongs, instead of being dropped without a word. Do not add to them.
ARGS = {}
MODULES = {}
SKIP = {}
def which(*names):
for n in names:
p = shutil.which(n)
if p:
return p
return None
def header_dir(env, cpm_name, probe):
"""A headers folder: the environment variable, else the CPM cache nx2/build.py reads (RX_CPM, build-linux/deps/cpm)"""
if os.environ.get(env):
return os.environ[env]
for cpm in (os.environ.get("RX_CPM"), os.path.join(ROOT, "build-linux", "deps", "cpm")):
d = os.path.join(cpm or "", cpm_name)
if cpm and os.path.isdir(d) and os.listdir(d):
inc = os.path.join(d, sorted(os.listdir(d))[-1], "include")
if os.path.exists(os.path.join(inc, probe)):
return inc
return None
def find_tools():
return {
"spirv_headers": header_dir("HOOKSHOT_SPIRV_HEADERS", "spirv-headers", os.path.join("spirv", "unified1", "spirv.h")),
"vulkan_headers": header_dir("HOOKSHOT_VULKAN_HEADERS", "vulkanheaders", os.path.join("vulkan", "vulkan.h")),
"spirv_val": which("spirv-val"),
"glslang": which("glslangValidator", "glslang"),
"dotnet": which("dotnet"),
"cxx": which(os.environ.get("CXX", ""), "clang++", "g++", "c++"),
}
class Check:
def __init__(self, name, run, why_skip=None):
self.name, self.run, self.why_skip = name, run, why_skip
self.status, self.secs, self.output = "skip", 0.0, ""
class DeclarationError(Exception):
pass
def read_declarations(path, rel, tools):
"""A test's RUN_ALL_ARGS, RUN_ALL_MODULES and RUN_ALL_SKIP (see the docstring), from its top-level assignments,
checked against the placeholders in tools and the OSes in OSES. {"args": [...], "modules": [...], "skip": {os:
why}}, empty where the test declares nothing. A file that does not parse declares nothing and says why in
"unparsed" (its check fails with that, on every OS, without running); a bad declaration raises DeclarationError naming
the file and line."""
found = {"args": [], "modules": [], "skip": {}, "unparsed": None}
with open(path, "rb") as f:
source = f.read()
try:
tree = ast.parse(source, rel)
except (SyntaxError, ValueError) as e:
found["unparsed"] = "%s: does not parse (%s), so its RUN_ALL_ declarations cannot be read" % (rel, e)
return found
seen = set()
def bad(node, what):
return DeclarationError("%s:%d: %s" % (rel, node.lineno, what))
def declared(node):
return isinstance(node, ast.Name) and node.id.startswith("RUN_ALL_")
# (at module level but not a top-level NAME = literal: inside an if or a try, a RUN_ALL_ARGS.append(...), a del. The
# runner reads only the top level, so these would be ignored without a word. The top level's own forms are checked
# below; a function's or a class's own names are not declarations and are left alone)
top_targets = {id(n) for top in tree.body if isinstance(top, (ast.Assign, ast.AnnAssign, ast.AugAssign))
for t in (top.targets if isinstance(top, ast.Assign) else [top.target]) for n in ast.walk(t)}
def module_level(node):
yield node
for child in ast.iter_child_nodes(node):
if not isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda)):
yield from module_level(child)
for node in module_level(tree):
if id(node) in top_targets:
continue
if declared(node) and not isinstance(node.ctx, ast.Load):
raise bad(node, "%s must be assigned at the top level of the file, as NAME = literal" % node.id)
elif isinstance(node, (ast.Subscript, ast.Attribute)) and declared(node.value) \
and not isinstance(node.ctx, ast.Load):
raise bad(node, "%s must be assigned once, alone, as NAME = literal" % node.value.id)
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and declared(node.func.value) \
and node.func.attr in MUTATORS:
raise bad(node, "%s is changed after it is set (.%s): write the whole literal in one assignment"
% (node.func.value.id, node.func.attr))
for node in tree.body:
if isinstance(node, ast.Assign):
targets, value = node.targets, node.value
elif isinstance(node, (ast.AnnAssign, ast.AugAssign)):
targets, value = [node.target], node.value
else:
continue
names = [n.id for t in targets for n in ast.walk(t) if isinstance(n, ast.Name) and n.id.startswith("RUN_ALL_")]
if not names:
continue
name = names[0]
if name not in DECLARATIONS:
raise bad(node, "%s is not a run_all.py declaration (%s)" % (name, ", ".join(DECLARATIONS)))
if not isinstance(node, (ast.Assign, ast.AnnAssign)) or value is None or len(targets) != 1 \
or not isinstance(targets[0], ast.Name):
raise bad(node, "%s must be assigned once, alone, as NAME = literal" % name)
if name in seen:
raise bad(node, "%s is assigned twice" % name)
seen.add(name)
try:
v = ast.literal_eval(value)
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
raise bad(node, "%s must be a plain literal (no names, calls or expressions)" % name) from None
if name == "RUN_ALL_SKIP":
if not isinstance(v, dict) or not all(isinstance(k, str) and isinstance(w, str) and w.strip()
for k, w in v.items()):
raise bad(node, 'RUN_ALL_SKIP must be a dict of OS -> reason, e.g. {"windows": "why"}')
unknown = sorted(k for k in v if k not in OSES)
if unknown:
raise bad(node, "RUN_ALL_SKIP names an unknown OS %s (known: %s)" % (", ".join(map(repr, unknown)),
", ".join(OSES)))
found["skip"] = dict(v)
continue
if not isinstance(v, list) or not all(isinstance(x, str) for x in v):
raise bad(node, "%s must be a list of strings" % name)
if name == "RUN_ALL_ARGS":
unknown = sorted({x.strip("{}") for x in v if x.startswith("{")} - set(tools))
if unknown:
raise bad(node, "RUN_ALL_ARGS names an unknown placeholder %s (known: %s)"
% (", ".join("{%s}" % u for u in unknown), ", ".join(sorted(tools))))
found["args"] = list(v)
else:
found["modules"] = list(v)
return found
def stale_tables():
"""Entries a branch from before the move still adds to the retired ARGS, MODULES or SKIP: what to declare instead"""
out = []
for table, decl in ((ARGS, "RUN_ALL_ARGS"), (MODULES, "RUN_ALL_MODULES"), (SKIP, "RUN_ALL_SKIP")):
for rel, v in table.items():
if decl == "RUN_ALL_SKIP" and isinstance(v, tuple) and len(v) == 2:
v = {o: v[1] for o in sorted(v[0])}
out.append("%s: declare it in the test: %s = %r" % (rel, decl, v))
return out
def python_check(rel, decl, tools, timeout):
args, missing = [], []
for a in decl["args"]:
if a.startswith("{"):
v = tools.get(a.strip("{}"))
if not v:
missing.append(a.strip("{}"))
args.append(v or "")
else:
args.append(a)
# (a compiler named by the arguments, or the test's own default one, must be on PATH)
for i, a in enumerate(args[:-1]):
if a in ("--cc", "--cxx") and not which(args[i + 1]):
missing.append(args[i + 1])
missing += [m for m in decl["modules"] if importlib.util.find_spec(m) is None]
def run():
return subprocess.run([sys.executable, "-I", "-B", os.path.join(ROOT, rel)] + args, cwd=ROOT, timeout=timeout,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
return run, missing
def cpp_checks(tools, timeout):
"""gpu/vk/tests: the presentation rectangle (all of it), the lowering harness built and run over no programs
(the programs it reads are dumped from the game, so here it only shows the lowering still compiles), and the
lowering's register-marker regression"""
cxx = tools["cxx"]
out = []
def build_and_run(src, flags, args):
def run():
with tempfile.TemporaryDirectory() as tmp:
exe = os.path.join(tmp, "t.exe")
r = subprocess.run([cxx, "-std=c++20", *flags, os.path.join(ROOT, src), "-o", exe], cwd=ROOT,
timeout=timeout, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
if r.returncode:
return r
return subprocess.run([exe] + [a.replace("{tmp}", tmp) for a in args], cwd=ROOT, timeout=timeout,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
return run
rect = "gpu/vk/tests/present_rect_test.cpp"
out.append((rect, build_and_run(rect, ["-Wall", "-Wextra", "-Werror"], [])))
lower = "gpu/vk/tests/lower_test.cpp"
eden, compat = os.path.join(ROOT, "gpu", "vk", "eden"), os.path.join(ROOT, "gpu", "vk", "compat")
out.append((lower, build_and_run(lower, ["-O1", "-Wno-everything", "-I" + eden, "-I" + compat], ["{tmp}"])))
regmap = "gpu/vk/tests/regmap_marker_test.cpp" # (the lowering's renamed-register markers: no number mapped twice)
out.append((regmap, build_and_run(regmap, ["-O1", "-Wno-everything", "-I" + eden, "-I" + compat], [])))
return out, ([] if cxx else ["a C++ compiler"])
class Result:
def __init__(self, returncode, stdout):
self.returncode, self.stdout = returncode, stdout
def compile_all():
"""Every tracked .py file byte-compiled (in memory: nothing written), so a syntax error anywhere fails"""
r = subprocess.run(["git", "ls-files", "-z", "--", "*.py"], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if r.returncode:
return Result(r.returncode, r.stdout)
files = [f for f in r.stdout.decode("utf-8").split("\0") if f]
bad = []
for f in files:
try:
with open(os.path.join(ROOT, f), "rb") as src:
compile(src.read(), f, "exec", dont_inherit=True)
except (SyntaxError, ValueError) as e:
bad.append("%s: %s" % (f, e))
text = "%d files byte-compiled\n" % len(files) + "".join(b + "\n" for b in bad)
return Result(1 if bad else 0, text.encode())
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("names", nargs="*")
p.add_argument("--jobs", type=int, default=os.cpu_count() or 2)
p.add_argument("--timeout", type=int, default=600, help="seconds per check (default 600)")
p.add_argument("--strict", action="store_true", help="a missing tool fails the check instead of skipping it")
p.add_argument("--no-skip", action="store_true", help="also run the checks on the skip list")
a = p.parse_args()
tools = find_tools()
checks = []
tests = sorted(d + "/" + f for d in ("tests", "nx2") for f in os.listdir(os.path.join(ROOT, d))
if f.startswith("test_") and f.endswith(".py"))
stale = stale_tables()
if stale:
print("tests/run_all.py: ARGS, MODULES and SKIP are retired: each test declares what it needs in its own file"
" (see the docstring at the top of tests/run_all.py)", file=sys.stderr)
for line in stale:
print(" " + line, file=sys.stderr)
return 2
# (only the tests asked for: a bad declaration elsewhere does not stop a run of one named test)
if a.names:
tests = [rel for rel in tests if any(n in rel for n in a.names)]
decls, errors = {}, []
for rel in tests:
try:
decls[rel] = read_declarations(os.path.join(ROOT, rel), rel, tools)
except DeclarationError as e:
errors.append(str(e))
if errors:
print("tests/run_all.py: bad declarations, nothing run:", file=sys.stderr)
for e in errors:
print(" " + e, file=sys.stderr)
return 2
for rel in tests:
reason = decls[rel]["skip"].get(OS)
run, missing = python_check(rel, decls[rel], tools, a.timeout)
c = Check(rel, run)
if decls[rel]["unparsed"]: # (no skip to read: the same failure on every OS, not run)
c.run = lambda why=decls[rel]["unparsed"]: Result(1, (why + "\n").encode())
elif reason and not a.no_skip and not any(n in rel for n in a.names):
c.why_skip = reason
elif missing and not a.strict:
c.why_skip = "not found here: " + ", ".join(missing)
elif missing:
c.run = lambda m=missing: Result(1, ("not found: %s (--strict)\n" % ", ".join(m)).encode())
checks.append(c)
cpp, missing = cpp_checks(tools, a.timeout)
for name, run in cpp:
c = Check(name, run)
if missing:
c.why_skip = "not found here: " + ", ".join(missing)
if a.strict:
c.why_skip = None
c.run = lambda m=missing: Result(1, ("not found: %s (--strict)\n" % ", ".join(m)).encode())
checks.append(c)
checks.append(Check("(byte-compile every tracked .py)", compile_all))
if a.names:
checks = [c for c in checks if any(n in c.name for n in a.names)]
def one(c):
t = time.time()
try:
r = c.run()
c.status = "pass" if r.returncode == 0 else "FAIL"
c.output = r.stdout.decode("utf-8", "replace")
except subprocess.TimeoutExpired as e:
c.status = "TIMEOUT"
c.output = (e.stdout or b"").decode("utf-8", "replace") + "\n(stopped after %d s)\n" % e.timeout
except Exception as e: # (a check that could not even start is a failure, not a crash of the runner)
c.status = "FAIL"
c.output = "%s: %s\n" % (type(e).__name__, e)
c.secs = time.time() - t
return c
to_run = [c for c in checks if not c.why_skip]
t0 = time.time()
print("%d checks, %d skipped, %d at once (%s)" % (len(to_run), len(checks) - len(to_run), a.jobs, OS), flush=True)
with concurrent.futures.ThreadPoolExecutor(max(1, a.jobs)) as ex:
for c in concurrent.futures.as_completed([ex.submit(one, c) for c in to_run]):
c = c.result()
print("%-7s %6.1fs %s" % (c.status, c.secs, c.name), flush=True)
wall = time.time() - t0
failed = [c for c in to_run if c.status != "pass"]
gh = os.environ.get("GITHUB_ACTIONS") == "true"
for c in failed:
print(("::group::" if gh else "\n==== ") + "%s %s" % (c.status, c.name), flush=True)
print(c.output.rstrip())
if gh:
print("::endgroup::")
print("::error title=%s %s::see the step's log for its output" % (c.status, c.name))
order = sorted(to_run, key=lambda c: -c.secs) + sorted((c for c in checks if c.why_skip), key=lambda c: c.name)
passed = sum(c.status == "pass" for c in to_run)
head = "%d passed, %d failed, %d skipped in %.0f s (%s, %d at once)" % (passed, len(failed), len(checks) - len(to_run),
wall, OS, a.jobs)
print("\n%-8s %8s %s" % ("result", "time", "check"))
for c in order:
print("%-8s %8s %s%s" % (c.status, "" if c.why_skip else "%.1fs" % c.secs, c.name,
" (%s)" % c.why_skip if c.why_skip else ""))
print(head, flush=True)
summary = os.environ.get("GITHUB_STEP_SUMMARY")
if summary:
mark = {"pass": "pass", "FAIL": "**FAIL**", "TIMEOUT": "**TIMEOUT**", "skip": "skip"}
lines = ["### Tests (%s): %s" % (OS, head), "", "| Result | Time | Check | Note |", "|---|---:|---|---|"]
for c in order:
note = c.why_skip or ""
if c.status in ("FAIL", "TIMEOUT"):
last = [l for l in c.output.strip().splitlines() if l.strip()][-1:] or [""]
note = "`%s`" % last[0].replace("|", "\\|").replace("`", "'")[:160]
lines.append("| %s | %s | `%s` | %s |" % (mark[c.status], "" if c.why_skip else "%.1f s" % c.secs, c.name, note))
with open(summary, "a", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n\n")
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())

View File

@@ -0,0 +1,283 @@
#define VK_NO_PROTOTYPES
#include <vulkan/vulkan.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <memory>
#include <mutex>
#include <new>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace fixture {
enum Cache { None = -1, Owner, Seen, Mine, Failure };
std::atomic<int> fail_cache{None};
std::atomic<unsigned> allocation_faults{0};
template<class T, int Tag> struct FaultAllocator : std::allocator<T> {
using value_type = T;
FaultAllocator() = default;
template<class U> FaultAllocator(const FaultAllocator<U, Tag>&) {}
template<class U> struct rebind { using other = FaultAllocator<U, Tag>; };
T* allocate(size_t n) {
int expected = Tag;
if (fail_cache.compare_exchange_strong(expected, None)) { allocation_faults++; throw std::bad_alloc(); }
return std::allocator<T>{}.allocate(n);
}
};
template<class K, class V, int Tag> using FaultMap = std::unordered_map<K, V, std::hash<K>, std::equal_to<K>, FaultAllocator<std::pair<const K, V>, Tag>>;
template<class K, int Tag> using FaultSet = std::unordered_set<K, std::hash<K>, std::equal_to<K>, FaultAllocator<K, Tag>>;
/* PARAMS */
/* ENUMS */
struct {
VkDevice device{}; VkPhysicalDeviceProperties props{}; VkPhysicalDeviceFeatures features{};
bool custom_border_color = false;
} dev;
FaultMap<uint64_t, VkSampler, Owner> samplers;
std::mutex replay_mu;
struct ParLock { std::lock_guard<std::mutex> lock; ParLock() : lock(replay_mu) {} };
uint64_t generation = 1, frame_no = 1;
unsigned creates = 0, destroyed = 0;
VkResult create_result = VK_SUCCESS;
bool garbage = false, arm_owner_commit = false, arm_failure_commit = false, cache_off = false;
std::string policy;
std::string descriptor_policy;
float registration_lod_max = 8;
struct Saved {
VkSamplerCreateInfo ci{};
bool custom = false, reduced = false;
VkSamplerReductionMode reduction{};
float border[4]{};
};
std::unordered_map<VkSampler, Saved> live;
uint64_t mix(uint64_t a, uint64_t b) { return (a ^ b) * 0x9e3779b97f4a7c15ull; }
uint64_t texture_frame_no() { return frame_no; }
bool sampler_cache_on() { return !cache_off; }
uint64_t sampler_registrations_generation() { return generation; }
uint32_t sampler_slot_version(int) { return (uint32_t)generation; }
bool sampler_of(int id, nvn_samp_par* p) {
if (id < 0) return false;
*p = {};
p->minf = NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR; p->magf = NVN_MAG_FILTER_LINEAR;
p->aniso = 1; p->lod_max = registration_lod_max; p->lod_bias = id * 0.25f;
if (policy == "nearest" || policy == "force-linear" || policy.find("compare") == 0) p->minf = NVN_MIN_FILTER_NEAREST, p->magf = NVN_MAG_FILTER_NEAREST;
if (policy == "linear") p->minf = NVN_MIN_FILTER_LINEAR;
if (policy == "compare-explicit") p->cmp_mode = 1, p->cmp_func = 1;
if (policy == "custom-border" || policy == "custom-reduction") { p->border[0] = 0.25f; p->border[1] = 0.5f; p->border[2] = 0.75f; p->border[3] = 1; }
if (policy == "standard-border") std::fill(std::begin(p->border), std::end(p->border), 1.0f);
if (policy == "reduction" || policy == "compare-reduction" || policy == "custom-reduction") p->reduction = 1;
if (policy == "wrap") p->ws = NVN_WRAP_MODE_REPEAT, p->wt = NVN_WRAP_MODE_MIRRORED_REPEAT, p->wr = NVN_WRAP_MODE_CLAMP_TO_BORDER;
if (policy == "lod-clamp") p->lod_bias = 100, p->lod_min = -3, p->lod_max = -1;
return true;
}
const char* getenv(const char* name) {
if (!std::strcmp(name, "RX_VK_PUSH_STATE_OLD") && descriptor_policy == "descriptor-old") return "1";
if (!std::strcmp(name, "RX_VK_PUSH_ALL") && descriptor_policy == "descriptor-whole") return "1";
if (!std::strcmp(name, "RX_VK_SAMPLER_CACHE") && cache_off) return "0";
if (!std::strcmp(name, "RX_VK_SHADOW_PCF") && policy == "compare-pcf-off") return "0";
if (!std::strcmp(name, "RX_VK_ANISO") && policy == "anisotropy") return "16";
if (!std::strcmp(name, "RX_VK_FORCE_LINEAR") && policy == "force-linear") return "1";
return nullptr;
}
void logf(const char*, ...) {}
VkResult vkCreateSampler(VkDevice, const VkSamplerCreateInfo* ci, const VkAllocationCallbacks*, VkSampler* out) {
creates++;
*out = garbage ? reinterpret_cast<VkSampler>(uintptr_t(0xdead)) : VK_NULL_HANDLE;
if (create_result != VK_SUCCESS) {
if (arm_failure_commit) { arm_failure_commit = false; fail_cache = Failure; }
return create_result;
}
*out = reinterpret_cast<VkSampler>(uintptr_t(100 + creates));
Saved saved; saved.ci = *ci; saved.ci.pNext = nullptr;
for (auto* next = static_cast<const VkBaseInStructure*>(ci->pNext); next; next = next->pNext) {
if (next->sType == VK_STRUCTURE_TYPE_SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT) {
auto* border = reinterpret_cast<const VkSamplerCustomBorderColorCreateInfoEXT*>(next);
saved.custom = true; std::copy(std::begin(border->customBorderColor.float32), std::end(border->customBorderColor.float32), saved.border);
}
if (next->sType == VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO) {
saved.reduced = true; saved.reduction = reinterpret_cast<const VkSamplerReductionModeCreateInfo*>(next)->reductionMode;
}
}
live.emplace(*out, saved);
// Arm after the resource exists: target the operation, not an STL-dependent nth allocation.
if (arm_owner_commit) { arm_owner_commit = false; fail_cache = Owner; }
return VK_SUCCESS;
}
void vkDestroySampler(VkDevice, VkSampler sampler, const VkAllocationCallbacks*) {
if (!sampler) return; // (Vulkan permits null destruction; a non-null error output is still undefined)
assert(live.erase(sampler) == 1); destroyed++; // (not undefined error output or an already-owned sampler)
}
/* ACTUAL */
struct Program { bool push = true; VkPipelineLayout layout{}; VkDescriptorSetLayout set_layout{}; };
struct DescriptorWrites { std::vector<VkWriteDescriptorSet> writes; } descriptor_writes;
DescriptorWrites& dw() { return descriptor_writes; }
struct Pushed;
std::array<Pushed, 2>& pushed();
struct Recording { VkDescriptorPool pool{}; std::vector<VkDescriptorPool> extra; } recording;
Recording* cur = &recording;
bool g_fine = false;
enum { FC_PUSH_FULL, FC_PUSH_WRITES, FC_PUSH_DESCS, FC_PUSH_DELTA, FC_POOL_SETS, FC_POOL_DESCS };
int64_t fine_n[6]{};
#define FINE_SCOPE(x) (void)0
#define FINE_N(x, n) (void)0
unsigned descriptor_allocs = 0, descriptor_updates = 0, descriptor_binds = 0, descriptor_pushes = 0;
void check_writes(uint32_t n, const VkWriteDescriptorSet* writes) {
for (uint32_t i = 0; i < n; i++)
if (writes[i].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
for (uint32_t k = 0; k < writes[i].descriptorCount; k++)
assert(live.count(writes[i].pImageInfo[k].sampler));
}
void vkCmdPushDescriptorSetKHR(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t n, const VkWriteDescriptorSet* writes) {
check_writes(n, writes); descriptor_pushes++;
}
VkDescriptorPool make_pool() { return reinterpret_cast<VkDescriptorPool>(uintptr_t(11)); }
void vkDestroyDescriptorPool(VkDevice, VkDescriptorPool pool, const VkAllocationCallbacks*) { assert(pool); }
VkResult vkAllocateDescriptorSets(VkDevice, const VkDescriptorSetAllocateInfo*, VkDescriptorSet* out) {
descriptor_allocs++; *out = reinterpret_cast<VkDescriptorSet>(uintptr_t(12)); return VK_SUCCESS;
}
void vkUpdateDescriptorSets(VkDevice, uint32_t n, const VkWriteDescriptorSet* writes, uint32_t, const VkCopyDescriptorSet*) {
check_writes(n, writes); descriptor_updates++;
}
void vkCmdBindDescriptorSets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t, const VkDescriptorSet*, uint32_t, const uint32_t*) {
descriptor_binds++;
}
/* DESCRIPTORS */
std::array<Pushed, 2>& pushed() { static std::array<Pushed, 2> state; return state; }
std::string route;
VkSampler get(int id = 0, bool compare = false) {
return route == "make" ? sampler_make(id, compare) : sampler_for(id, compare);
}
void valid(VkSampler sampler) { assert(sampler && live.count(sampler)); }
void retry(const std::string& test) {
create_result = test == "host-oom" ? VK_ERROR_OUT_OF_HOST_MEMORY : test == "unknown" ? VK_ERROR_UNKNOWN : VK_ERROR_OUT_OF_DEVICE_MEMORY;
garbage = test == "error-output";
assert(!get()); assert(!get()); assert(creates == 1 && live.empty() && destroyed == 0);
create_result = VK_SUCCESS;
assert(!get()); assert(creates == 1); // (bounded until a later present)
frame_no++;
if (test == "generation-retry") generation++;
VkSampler next{};
if (test == "thread-retry") { std::thread worker([&] { next = get(); }); worker.join(); }
else next = get();
valid(next); assert(creates == 2 && get() == next);
}
void run(const std::string& test) {
cache_off = route == "cache-off";
dev.props.limits.maxSamplerLodBias = 16; dev.props.limits.maxSamplerAnisotropy = 8;
dev.features.samplerAnisotropy = VK_TRUE; dev.custom_border_color = true;
if (test.find("descriptor-") == 0) {
descriptor_policy = test;
recording.pool = make_pool(); // A live recording already owns its initial pool before binding.
Program p; p.push = test != "descriptor-pool";
p.layout = reinterpret_cast<VkPipelineLayout>(uintptr_t(20));
const auto bp = test == "descriptor-compute" ? VK_PIPELINE_BIND_POINT_COMPUTE : VK_PIPELINE_BIND_POINT_GRAPHICS;
const auto cmd = reinterpret_cast<VkCommandBuffer>(uintptr_t(21));
auto first = get(); valid(first);
VkDescriptorImageInfo images[2]{{first, {}, VK_IMAGE_LAYOUT_GENERAL}, {first, {}, VK_IMAGE_LAYOUT_GENERAL}};
VkWriteDescriptorSet w{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
w.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; w.descriptorCount = 2; w.pImageInfo = images;
dw().writes = {w};
create_result = VK_ERROR_OUT_OF_DEVICE_MEMORY; images[1].sampler = get(1);
assert(!images[1].sampler && !bind_descriptors(cmd, bp, p));
assert(!descriptor_allocs && !descriptor_updates && !descriptor_binds && !descriptor_pushes);
create_result = VK_SUCCESS; frame_no++; images[1].sampler = get(1); valid(images[1].sampler);
assert(bind_descriptors(cmd, bp, p));
auto before = pushed()[bp == VK_PIPELINE_BIND_POINT_COMPUTE];
const auto allocs = descriptor_allocs, updates = descriptor_updates, binds = descriptor_binds, pushes = descriptor_pushes;
create_result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
images[1].sampler = get(2); assert(!images[1].sampler);
assert(!bind_descriptors(cmd, bp, p));
assert(descriptor_allocs == allocs && descriptor_updates == updates && descriptor_binds == binds && descriptor_pushes == pushes);
const auto& after = pushed()[bp == VK_PIPELINE_BIND_POINT_COMPUTE];
assert(after.cmd == before.cmd && after.layout == before.layout && after.n == before.n && after.words == before.words && after.writes == before.writes && after.at == before.at);
create_result = VK_SUCCESS; frame_no++; images[1].sampler = get(2); valid(images[1].sampler);
assert(bind_descriptors(cmd, bp, p));
assert(p.push ? descriptor_pushes == pushes + 1 : descriptor_updates == updates + 1 && descriptor_binds == binds + 1);
// Storage images do not consume a sampler: their null sampler remains legal.
dw().writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; images[0].sampler = images[1].sampler = VK_NULL_HANDLE;
assert(bind_descriptors(cmd, bp, p));
return;
}
if (test == "host-oom" || test == "device-oom" || test == "unknown" || test == "error-output" ||
test == "generation-retry" || test == "thread-retry") { retry(test); return; }
if (test == "independent") {
create_result = VK_ERROR_OUT_OF_HOST_MEMORY; assert(!get()); create_result = VK_SUCCESS;
auto second = get(1); valid(second); assert(creates == 2 && !get() && get(1) == second); return;
}
if (test == "owner-commit") {
arm_owner_commit = true; assert(!get()); assert(live.empty() && destroyed == 1 && allocation_faults == 1);
assert(!get() && creates == 1); frame_no++; valid(get()); assert(creates == 2); return;
}
if (test == "concurrent") {
std::vector<std::thread> workers;
for (int i = 0; i < 4; i++) workers.emplace_back([] { for (int j = 0; j < 50; j++) assert(get()); });
for (auto& worker : workers) worker.join();
assert(creates == 1 && live.size() == 1); return;
}
if (test == "seen-commit") {
auto first = get(); valid(first);
// A distinct ID forces the parameter acceleration insertion.
// Use sampler_make directly to test this cache even when the outer ID table is bypassed.
if (!cache_off) {
fail_cache = Seen; auto next = sampler_make(1, false); valid(next); assert(allocation_faults == 1);
assert(live.at(next).ci.mipLodBias == 0.25f && live.at(next).ci.maxLod == 8);
} else valid(get(1));
assert(destroyed == 0); return;
}
if (test == "mine-commit") {
auto first = sampler_make(0, false); valid(first);
fail_cache = Mine; assert(sampler_make(0, false) == first);
assert(allocation_faults == 1 && creates == 1 && destroyed == 0 && live.size() == 1); return;
}
if (test == "valid-after-failure" || test == "failure-commit") {
auto first = get(); valid(first); create_result = VK_ERROR_OUT_OF_DEVICE_MEMORY;
arm_failure_commit = test == "failure-commit"; assert(!get(1)); assert(get() == first);
create_result = VK_SUCCESS;
if (test == "failure-commit") { assert(allocation_faults == 1 && !get(2) && creates == 2); }
else valid(get(2));
assert(!get(1)); frame_no++; valid(get(1)); assert(get() == first && destroyed == 0); return;
}
if (test == "registration") {
auto first = get(); valid(first); generation++; registration_lod_max = 3;
auto second = get(); valid(second); assert(second != first && creates == 2 && live.at(second).ci.maxLod == 3); return;
}
if (test == "shared-thread") {
auto first = get(); valid(first); VkSampler other{};
std::thread worker([&] { other = get(); }); worker.join();
assert(other == first && creates == 1); return;
}
policy = test;
const bool compare = test.find("compare") == 0;
const int id = test == "unknown-id" ? -1 : 0;
auto sampler = get(id, compare); valid(sampler);
const auto& saved = live.at(sampler); const auto& ci = saved.ci;
assert(get(id, compare) == sampler && creates == 1);
if (test == "nearest") assert(ci.minFilter == VK_FILTER_NEAREST && ci.magFilter == VK_FILTER_NEAREST && ci.maxLod == 0.25f);
if (test == "linear") assert(ci.minFilter == VK_FILTER_LINEAR && ci.maxLod == 0.25f);
if (test == "unknown-id" || test == "force-linear") assert(ci.minFilter == VK_FILTER_LINEAR && ci.magFilter == VK_FILTER_LINEAR && ci.maxLod == 0.25f);
if (test == "wrap") assert(ci.addressModeU == VK_SAMPLER_ADDRESS_MODE_REPEAT && ci.addressModeV == VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT && ci.addressModeW == VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER);
if (test == "lod-clamp") assert(ci.mipLodBias == 16 && ci.minLod == 0 && ci.maxLod == 0);
if (compare) {
assert(ci.compareEnable && ci.compareOp == (test == "compare-explicit" ? VK_COMPARE_OP_NEVER : VK_COMPARE_OP_LESS_OR_EQUAL));
assert(ci.minFilter == (test == "compare-pcf-off" ? VK_FILTER_NEAREST : VK_FILTER_LINEAR));
}
if (test == "custom-border" || test == "custom-reduction") assert(saved.custom && ci.borderColor == VK_BORDER_COLOR_FLOAT_CUSTOM_EXT && saved.border[0] == 0.25f && saved.border[3] == 1);
if (test == "standard-border") assert(!saved.custom && ci.borderColor == VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE);
if (test == "reduction" || test == "custom-reduction") assert(saved.reduced && saved.reduction == VK_SAMPLER_REDUCTION_MODE_MIN);
if (test == "compare-reduction") assert(!saved.reduced);
if (test == "anisotropy") assert(ci.anisotropyEnable && ci.maxAnisotropy == 8);
}
} // namespace fixture
int main(int argc, char** argv) {
assert(argc == 3); fixture::route = argv[1]; fixture::run(argv[2]); return 0;
}

183
tests/screensaver_test.c Normal file
View File

@@ -0,0 +1,183 @@
/* All APIs are mocks: no window, screen saver, security or power setting is accessed. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef void *HWND;
typedef void *HRAWINPUT;
typedef unsigned UINT;
typedef int BOOL;
typedef int32_t LONG;
typedef uintptr_t WPARAM;
typedef intptr_t LPARAM;
typedef intptr_t LRESULT;
#define CALLBACK
#define TRUE 1
#define FALSE 0
#define LOWORD(v) ((unsigned)(uintptr_t)(v) & 0xffff)
#define HIWORD(v) (((unsigned)(uintptr_t)(v) >> 16) & 0xffff)
enum {
WM_TIMER = 0x113, WM_SYSCOMMAND = 0x112, WM_INPUT = 0xff, WM_LBUTTONDOWN = 0x201,
WM_KEYDOWN = 0x100, WM_SYSKEYDOWN = 0x104, WM_SYSCHAR = 0x106, WM_APP = 0x8000,
WM_DPICHANGED = 0x2e0, WM_KILLFOCUS = 8, WM_CANCELMODE = 0x1f, WM_SETCURSOR = 0x20,
WM_MOVE = 3, WM_SIZE = 5, WM_PAINT = 0xf, WM_ERASEBKGND = 0x14, WM_CLOSE = 0x10,
VK_ESCAPE = 0x1b, VK_F11 = 0x7a, VK_RETURN = 0xd, HTCLIENT = 1,
VK_SHIFT = 0x10, VK_CONTROL = 0x11, VK_MENU = 0x12,
SC_SCREENSAVE = 0xf140, SCF_ISSECURE = 1, SPI_GETSCREENSAVESECURE = 0x76,
SC_CLOSE = 0xf060, SC_MONITORPOWER = 0xf170, SC_MAXIMIZE = 0xf030,
SWP_NOZORDER = 4, SWP_NOACTIVATE = 0x10, RID_INPUT = 0x10000003,
RIM_TYPEMOUSE = 0, MOUSE_MOVE_ABSOLUTE = 1
};
typedef struct { LONG left, top, right, bottom; } RECT;
typedef struct { UINT dwType; } RAWINPUTHEADER;
typedef struct {
RAWINPUTHEADER header;
struct { struct { unsigned short usFlags; LONG lLastX, lLastY; } mouse; } data;
} RAWINPUT;
#define CHECK(name, condition) do { if (!(condition)) { \
fprintf(stderr, "FAIL: %s (line %d)\n", name, __LINE__); exit(1); } } while (0)
static HWND game = (HWND)(uintptr_t)1, foreground;
static int renderer, fullscreen, mouse_captured;
static volatile LONG mouse_dx, mouse_dy;
static int iconic, query_ok, query_writes, policy_secure, query_calls, forwarded, cases;
static UINT forwarded_msg;
static WPARAM forwarded_wp;
static LPARAM forwarded_lp;
static const LRESULT default_result = 1234;
HWND GetForegroundWindow(void) { return foreground; }
short GetKeyState(int vkey) { (void)vkey; return 0; }
BOOL IsIconic(HWND w) { CHECK("iconic query targets game", w == game); return iconic; }
/* (#1532: the display's idle timer reset on WM_TIMER while the game is in front) */
typedef unsigned long long ULONGLONG;
typedef unsigned EXECUTION_STATE;
#define ES_SYSTEM_REQUIRED 0x1u
#define ES_DISPLAY_REQUIRED 0x2u
static ULONGLONG tick = 100000;
static int awake_calls;
static EXECUTION_STATE awake_flags;
ULONGLONG GetTickCount64(void) { return tick; }
EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE s) { awake_calls++, awake_flags = s; return 0; }
BOOL SystemParametersInfoA(UINT action, UINT parameter, void *value, UINT flags) {
CHECK("only read the secure screen saver policy", action == SPI_GETSCREENSAVESECURE);
CHECK("no profile write/notification flags", parameter == 0 && flags == 0 && value != NULL);
query_calls++;
if (query_writes) *(BOOL *)value = policy_secure;
return query_ok;
}
static LRESULT DefWindowProcA(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
CHECK("forward original window", w == game);
forwarded++;
forwarded_msg = msg; forwarded_wp = wp; forwarded_lp = lp;
return default_result;
}
/* The complete procedure compiles, but these unrelated branches must never run in this fixture. */
static void unexpected(void) { fputs("FAIL: unrelated window action executed\n", stderr); abort(); }
static UINT GetRawInputData(HRAWINPUT h, UINT command, void *data, UINT *size, UINT header) {
(void)h; (void)command; (void)data; (void)size; (void)header; unexpected(); return (UINT)-1;
}
static LONG InterlockedExchangeAdd(volatile LONG *target, LONG value) {
(void)target; (void)value; unexpected(); return 0;
}
static void mouse_capture(HWND w, int on) { (void)w; (void)on; unexpected(); }
static void set_fullscreen(HWND w, int on) { (void)w; (void)on; unexpected(); }
static BOOL SetWindowPos(HWND w, HWND after, int x, int y, int width, int height, UINT flags) {
(void)w; (void)after; (void)x; (void)y; (void)width; (void)height; (void)flags; unexpected(); return FALSE;
}
static void *SetCursor(void *cursor) { (void)cursor; unexpected(); return NULL; }
static void rx_resize(unsigned width, unsigned height) { (void)width; (void)height; unexpected(); }
static BOOL ValidateRect(HWND w, const RECT *rect) { (void)w; (void)rect; unexpected(); return FALSE; }
static void hos_relaunch_ask(void) { unexpected(); } /* (host/hos_relaunch.c: WM_APP + 9 and WM_CLOSE end the process) */
static void hos_window_closed_exit(void) { unexpected(); }
static void rx_close_asked(void) { unexpected(); } /* (host/rx_backend.c: WM_CLOSE, not one of the checked messages) */
void hos_vk_note_key_press(unsigned key) { (void)key; } /* (host/rx_backend.c: the menu notes key presses; nothing here) */
/* (PR 946: the Insert menu takes the mouse while it is open; these branches never run here either) */
enum {
WM_MOUSEMOVE = 0x200, WM_MOUSELEAVE = 0x2a3, WM_MOUSEWHEEL = 0x20a, WM_LBUTTONUP = 0x202,
WM_RBUTTONDOWN = 0x204, WM_RBUTTONUP = 0x205, WM_MBUTTONDOWN = 0x207, WM_MBUTTONUP = 0x208,
MK_LBUTTON = 1, MK_RBUTTON = 2, MK_MBUTTON = 0x10, TME_LEAVE = 2,
HOS_MOUSE_LEFT = 1, HOS_MOUSE_RIGHT = 2, HOS_MOUSE_MIDDLE = 4
};
typedef struct { UINT cbSize; UINT dwFlags; HWND hwndTrack; UINT dwHoverTime; } TRACKMOUSEEVENT;
#define GET_WHEEL_DELTA_WPARAM(v) ((short)HIWORD(v))
static volatile LONG ptr_x, ptr_y, ptr_w, ptr_h, ptr_in, ptr_wheel;
static int menu_open;
static BOOL TrackMouseEvent(TRACKMOUSEEVENT *t) { (void)t; unexpected(); return FALSE; }
static HWND GetCapture(void) { unexpected(); return NULL; }
static BOOL ReleaseCapture(void) { unexpected(); return FALSE; }
static void menu_press(HWND w, LPARAM lp, LONG bit) { (void)w; (void)lp; (void)bit; unexpected(); }
static void menu_mouse(HWND w, int open) { (void)w; (void)open; unexpected(); }
/* (the game's own menus take the mouse while one of them is shown (host/oot_menu.c); never here either) */
typedef int64_t LONG64;
typedef struct { LONG x, y; } POINT;
static int game_menu_open;
static volatile LONG64 game_menu_seen_ns;
static volatile LONG gm_wheel, play_wheel;
static volatile LONG64 play_wheel_ns;
static LONG gm_mx, gm_my;
static LONG64 InterlockedCompareExchange64(volatile LONG64 *target, LONG64 value, LONG64 compare) {
const LONG64 was = *target;
if (was == compare) *target = value;
return was;
}
static int64_t hos_now_ns(void) { return 0; }
static void gm_mouse_used(void) { unexpected(); }
static int gm_cursor_shown(void) { unexpected(); return 1; }
static void game_menu_press(LPARAM lp, LONG bit) { (void)lp; (void)bit; unexpected(); }
static void menus_mouse(HWND w, int insert_open, int game_open) { (void)w; (void)insert_open; (void)game_open; unexpected(); }
static void gm_cursor_off(HWND w) { (void)w; unexpected(); }
/* ACTUAL_WNDPROC */
static void reset(void) {
renderer = 1; foreground = game; iconic = 0;
query_ok = 1; query_writes = 1; policy_secure = FALSE;
query_calls = forwarded = 0;
}
static void call(const char *name, UINT msg, WPARAM wp, int handled, int queries) {
const LPARAM lp = 0x5678;
const LRESULT result = wndproc(game, msg, wp, lp);
CHECK(name, result == (handled ? 0 : default_result));
CHECK("forwarding count", forwarded == !handled);
CHECK("policy query count", query_calls == queries);
if (!handled) CHECK("forward original message and parameters",
forwarded_msg == msg && forwarded_wp == wp && forwarded_lp == lp);
cases++;
}
int main(void) {
reset(); call("foreground nonsecure saver is handled", WM_SYSCOMMAND, SC_SCREENSAVE, 1, 1);
reset(); call("secure request is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE | SCF_ISSECURE, 0, 0);
reset(); call("secure request flag survives other low bits", WM_SYSCOMMAND, SC_SCREENSAVE | SCF_ISSECURE | 8, 0, 0);
reset(); policy_secure = TRUE; call("password policy is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 1);
reset(); policy_secure = 2; call("any true policy is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 1);
reset(); query_ok = 0; call("failed policy query is forwarded even if it wrote false", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 1);
reset(); query_ok = 0; query_writes = 0; call("unknown policy is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 1);
reset(); query_writes = 0; call("unwritten policy stays secure", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 1);
reset(); renderer = 0; call("no renderer is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 0);
reset(); foreground = (HWND)(uintptr_t)2; call("background game is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 0);
reset(); foreground = NULL; call("unknown foreground is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 0);
reset(); iconic = 1; call("minimized game is forwarded", WM_SYSCOMMAND, SC_SCREENSAVE, 0, 0);
/* (#1532) the display kept on: in front, at most once in 30 s, not minimized, not in the background, no ES_CONTINUOUS */
reset(); awake_calls = 0;
CHECK("timer handled", wndproc(game, WM_TIMER, 1, 0) == 0);
CHECK("foreground timer keeps the display on", awake_calls == 1 && awake_flags == (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED));
wndproc(game, WM_TIMER, 1, 0);
CHECK("at most once in 30 s", awake_calls == 1);
tick += 30000; iconic = 1; wndproc(game, WM_TIMER, 1, 0);
CHECK("not while minimized", awake_calls == 1);
iconic = 0; foreground = NULL; wndproc(game, WM_TIMER, 1, 0);
CHECK("not in the background", awake_calls == 1);
foreground = game; wndproc(game, WM_TIMER, 1, 0);
CHECK("again after 30 s in front", awake_calls == 2);
reset(); call("close command is forwarded", WM_SYSCOMMAND, SC_CLOSE, 0, 0);
reset(); call("monitor power command is forwarded", WM_SYSCOMMAND, SC_MONITORPOWER, 0, 0);
reset(); call("maximize command is forwarded", WM_SYSCOMMAND, SC_MAXIMIZE, 0, 0);
reset(); call("unknown command is forwarded", WM_SYSCOMMAND, 0xf2e0, 0, 0);
reset(); call("zero command is forwarded", WM_SYSCOMMAND, 0, 0, 0);
reset(); call("unrelated message is forwarded", WM_APP + 99, 0xabc, 0, 0);
printf("PASS: %d actual-wndproc screensaver/secure-policy/forwarding cases; all APIs mocked\n", cases);
return 0;
}

139
tests/seed_lowering.cpp Normal file
View File

@@ -0,0 +1,139 @@
// Runs the shader lowering over every stage in triforce_shaders.bin (HKSEED01: every shader the game ships), exactly as
// vk_shader.cpp's parse_code does: the new lowering first, the old REGMAP one when the new one rejects a stage. Prints each
// stage that is not a plain "new ok", then totals. A stage neither lowering takes is a program that is never drawn.
// clang++ -std=c++20 -O2 -w -I gpu/vk/eden -I gpu/vk/compat tests/seed_lowering.cpp -o seed_lowering.exe
// seed_lowering <game folder>/triforce_shaders.bin [mma] [<file: one line per lowered stage, for a before/after diff>]
// 11-10: 26 stages failed (36 programs); 3 after the register-pressure forms, USHF.R.U64 and the guarded 64-bit uniform adds.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include "shader_recompiler/frontend/ampere/lower_to_maxwell.h"
#include "shader_recompiler/frontend/ampere/lower_to_maxwell_regmap.h"
#include "shader_recompiler/frontend/ampere/program_capture.h"
static uint64_t hash_bytes(const void* p, size_t n, uint64_t h = 0xcbf29ce484222325ull) {
const uint8_t* b = (const uint8_t*)p;
for (size_t i = 0; i < n; i++) h = (h ^ b[i]) * 0x100000001b3ull;
return h;
}
constexpr uint32_t kControlMagic = 0x98761234, kControlSize = 0xd8, kAmpereHeader = 128;
int main(int argc, char** argv) {
if (argc < 2) return 2;
const bool mma = argc > 2 && !strcmp(argv[2], "mma");
FILE* f = fopen(argv[1], "rb");
if (!f) return 3;
_fseeki64(f, 0, SEEK_END);
std::vector<uint8_t> blob((size_t)_ftelli64(f));
_fseeki64(f, 0, SEEK_SET);
if (fread(blob.data(), 1, blob.size(), f) != blob.size() || memcmp(blob.data(), "HKSEED01", 8)) return 4;
fclose(f);
const uint32_t nprograms = *(uint32_t*)(blob.data() + 8), nstages = *(uint32_t*)(blob.data() + 12);
size_t at = 16;
unsigned long long all_hash = 1469598103934665603ull;
FILE* list_ok = argc > 3 ? fopen(argv[3], "w") : nullptr; // (each lowered stage's own hash, for a before/after diff)
int n_ok = 0, n_old = 0, n_fail = 0, n_hdr = 0, n_bad = 0, n_invalid = 0;
std::map<std::string, int> why_old, why_fail;
int by_stage_total[8]{}, by_stage_old[8]{}, by_stage_fail[8]{};
std::vector<int> outcome(nstages, 0); // 0 ok, 1 old, 2 fail, 3 not a program
std::vector<uint64_t> keys(nstages, 0);
std::vector<uint32_t> stage_of(nstages, 0);
for (uint32_t s = 0; s < nstages; s++) {
const uint8_t* h = blob.data() + at;
const uint32_t csize = *(uint32_t*)(h + 12), bytes = *(uint32_t*)(h + 16);
const size_t control = at + 20, code = control + csize;
at = code + bytes;
if (at > blob.size()) { fprintf(stderr, "truncated\n"); return 5; }
const uint8_t* ctl = blob.data() + control;
uint32_t magic, len, c1o, end;
uint16_t st;
memcpy(&magic, ctl, 4), memcpy(&len, ctl + 0x38, 4), memcpy(&c1o, ctl + 0x40, 4), memcpy(&end, ctl + 0x44, 4), memcpy(&st, ctl + 0x56, 2);
if (csize < kControlSize || magic != kControlMagic || !len || end > (16u << 20) || c1o > end || len > end || st > 7 || end > bytes) {
n_bad++, outcome[s] = 3;
continue;
}
const uint8_t* area = blob.data() + code;
const uint64_t key = hash_bytes(area, end, hash_bytes(ctl, kControlSize));
keys[s] = key, stage_of[s] = st;
by_stage_total[st]++;
const bool graphics = st != 5;
const uint32_t hdr = graphics ? kAmpereHeader : 0;
if (graphics && len <= hdr + 16) { n_hdr++; continue; }
using Shader::Ampere::Instruction;
std::vector<Instruction> native;
Shader::Ampere::ProgramCaptureState capture;
bool invalid = false;
for (uint32_t i = 0; hdr + (i + 1) * sizeof(Instruction) <= len; i++) {
Instruction& insn = native.emplace_back();
memcpy(&insn, area + hdr + i * sizeof(Instruction), sizeof insn);
const auto r = capture.Observe(insn, i);
if (r == Shader::Ampere::ProgramCaptureState::Result::InvalidTarget) { invalid = true; break; }
if (r == Shader::Ampere::ProgramCaptureState::Result::Complete) break;
}
if (invalid) {
n_invalid++, n_fail++, outcome[s] = 2, by_stage_fail[st]++;
why_fail["capture: invalid branch target"]++;
printf("%u %016llx FAIL capture: invalid branch target\n", st, (unsigned long long)key);
continue;
}
Shader::Ampere::native_matrix_multiply = Shader::AmpereRegmap::native_matrix_multiply = mma && !graphics;
const auto lowered = Shader::Ampere::LowerToMaxwell(native);
if (lowered) {
n_ok++;
unsigned long long h = 1469598103934665603ull;
for (auto w : lowered.instructions) h = (h ^ w) * 1099511628211ull;
all_hash = (all_hash ^ h ^ key) * 1099511628211ull;
if (list_ok) fprintf(list_ok, "%u %016llx %zu %016llx\n", st, (unsigned long long)key, lowered.instructions.size(), h);
continue;
}
const uint32_t ia = lowered.error_offset / 16;
const unsigned op = ia < native.size() ? (unsigned)native[ia].Bits(0, 12) : 0xffffffffu;
char why[200];
snprintf(why, sizeof why, "%s (op %#x)", std::string(lowered.error).c_str(), op);
char words[96] = "";
if (ia < native.size())
snprintf(words, sizeof words, " @%u/%zu %08x %08x %08x %08x", ia, native.size(), native[ia].words[0], native[ia].words[1],
native[ia].words[2], native[ia].words[3]);
const auto old = Shader::AmpereRegmap::LowerToMaxwell(
std::span{reinterpret_cast<const Shader::AmpereRegmap::Instruction*>(native.data()), native.size()});
if (old) {
n_old++, outcome[s] = 1, by_stage_old[st]++;
why_old[why]++;
printf("%u %016llx OLD %s%s%s\n", st, (unsigned long long)key, why, old.register_map_words ? " [old: extended registers]" : "", words);
} else {
const uint32_t oa = old.error_offset / 16;
const unsigned oop = oa < native.size() ? (unsigned)native[oa].Bits(0, 12) : 0xffffffffu;
n_fail++, outcome[s] = 2, by_stage_fail[st]++;
why_fail[why]++;
printf("%u %016llx FAIL %s [old: %s (op %#x)]%s\n", st, (unsigned long long)key, why, std::string(old.error).c_str(), oop, words);
}
}
// programs (sets of stages): how many have a stage that fails, or one that takes the old lowering
int p_fail = 0, p_old = 0;
for (uint32_t p = 0; p < nprograms; p++) {
const uint32_t n = *(uint32_t*)(blob.data() + at);
const uint32_t* list = (const uint32_t*)(blob.data() + at + 4);
at += 4 + (size_t)n * 4;
bool fl = false, ol = false;
for (uint32_t k = 0; k < n; k++)
if (list[k] < nstages) fl |= outcome[list[k]] == 2, ol |= outcome[list[k]] == 1;
p_fail += fl, p_old += !fl && ol;
}
printf("\n== %u stages, %u programs (native matrix multiplies %s)\n", nstages, nprograms, mma ? "on" : "off");
printf("== stages: %d lowered by the new lowering, %d only by the old one, %d by neither; %d header-only, %d not programs (%d bad branch targets)\n",
n_ok, n_old, n_fail, n_hdr, n_bad, n_invalid);
if (list_ok) fclose(list_ok);
printf("== hash of everything the new lowering made: %016llx\n", all_hash);
printf("== programs: %d have a stage that does not lower at all (never drawn), %d more use the old lowering for a stage\n", p_fail, p_old);
printf("== by NVN stage (0 vertex, 1 fragment, 5 compute, 6 task, 7 mesh): total / old / fail\n");
for (int k = 0; k < 8; k++)
if (by_stage_total[k]) printf(" stage %d: %d / %d / %d\n", k, by_stage_total[k], by_stage_old[k], by_stage_fail[k]);
printf("== neither lowering takes it, by the new lowering's reason:\n");
for (const auto& [w, n] : why_fail) printf(" %4d %s\n", n, w.c_str());
printf("== only the old lowering takes it, by the new lowering's reason:\n");
for (const auto& [w, n] : why_old) printf(" %4d %s\n", n, w.c_str());
return 0;
}

View File

@@ -0,0 +1,239 @@
/* Authored process/Win32 doubles; no archive, symlink, installer or user data. */
#include <assert.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#ifdef REAL_CHILD
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#define GetLastError mock_GetLastError
#define CloseHandle mock_CloseHandle
#define CreatePipe mock_CreatePipe
#define SetHandleInformation mock_SetHandleInformation
#define CreateFileW mock_CreateFileW
#define CreateProcessW mock_CreateProcessW
#define ReadFile mock_ReadFile
#define WaitForSingleObject mock_WaitForSingleObject
#define GetExitCodeProcess mock_GetExitCodeProcess
#define MultiByteToWideChar mock_MultiByteToWideChar
#define GetSystemDirectoryW mock_GetSystemDirectoryW
typedef unsigned long DWORD;
typedef int BOOL;
typedef void *HANDLE;
#define TRUE 1
#define FALSE 0
#define MAX_PATH 260
#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)
#define STARTF_USESHOWWINDOW 1
#define STARTF_USESTDHANDLES 256
#define SW_HIDE 0
#define CREATE_NO_WINDOW 0x08000000
#define HANDLE_FLAG_INHERIT 1
#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define FILE_SHARE_READ 1
#define FILE_SHARE_WRITE 2
#define OPEN_EXISTING 3
#define INFINITE 0xffffffff
#define CP_UTF8 65001
#define ERROR_BROKEN_PIPE 109
#define WAIT_OBJECT_0 0
typedef struct { DWORD nLength; void *lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES;
typedef struct { DWORD cb, dwFlags; unsigned short wShowWindow; HANDLE hStdInput, hStdOutput, hStdError; } STARTUPINFOW;
typedef struct { HANDLE hProcess, hThread; DWORD dwProcessId, dwThreadId; } PROCESS_INFORMATION;
static struct {
DWORD code, error, pipe_error, inherit_error, null_error, launch_error, query_error;
unsigned open, violation;
int capturing, inherited, launched;
size_t offset, length, converted_length;
wchar_t expected_zip[MAX_PATH], expected_destination[MAX_PATH];
char data[20000];
} state;
#define BAD_IF(c) do { if (c) state.violation++; } while (0)
static HANDLE opened(unsigned bit) { BAD_IF(state.open & bit); state.open |= bit; return (HANDLE)(uintptr_t)bit; }
DWORD GetLastError(void) { return state.error; }
BOOL CloseHandle(HANDLE handle) {
unsigned bit = (unsigned)(uintptr_t)handle;
BAD_IF(!(state.open & bit)); state.open &= ~bit; return TRUE;
}
BOOL CreatePipe(HANDLE *rd, HANDLE *wr, SECURITY_ATTRIBUTES *sa, DWORD size) {
BAD_IF(size || sa->nLength != sizeof *sa || !sa->bInheritHandle || sa->lpSecurityDescriptor);
if (state.pipe_error) { state.error = state.pipe_error; return FALSE; }
*rd = opened(1); *wr = opened(2); return TRUE;
}
BOOL SetHandleInformation(HANDLE h, DWORD mask, DWORD flags) {
BAD_IF(h != (HANDLE)(uintptr_t)1 || mask != HANDLE_FLAG_INHERIT || flags != 0);
if (state.inherit_error) { state.error = state.inherit_error; return FALSE; }
state.inherited = 1; return TRUE;
}
HANDLE CreateFileW(const wchar_t *name, DWORD access, DWORD share, SECURITY_ATTRIBUTES *sa, DWORD create, DWORD flags, HANDLE template) {
BAD_IF(wcscmp(name, L"NUL") || access != (GENERIC_READ | GENERIC_WRITE) || share != (FILE_SHARE_READ | FILE_SHARE_WRITE)
|| !sa->bInheritHandle || create != OPEN_EXISTING || flags || template);
if (state.null_error) { state.error = state.null_error; return INVALID_HANDLE_VALUE; }
return opened(16);
}
BOOL CreateProcessW(const wchar_t *app, wchar_t *cmd, void *a, void *b, BOOL inherit, DWORD flags, void *env, const wchar_t *dir,
STARTUPINFOW *si, PROCESS_INFORMATION *pi) {
BAD_IF(app || a || b || env || flags != CREATE_NO_WINDOW || si->cb != sizeof *si || si->wShowWindow != SW_HIDE);
wchar_t expected[2 * MAX_PATH + 64];
swprintf(expected, 2 * MAX_PATH + 64, L"\"C:\\Windows\\System32\\tar.exe\" -xf \"%ls\" -C \"%ls\"", state.expected_zip, state.expected_destination);
BAD_IF(wcscmp(cmd, expected));
BAD_IF(wcscmp(dir, state.expected_destination));
state.capturing = !!(si->dwFlags & STARTF_USESTDHANDLES);
if (state.capturing) {
BAD_IF(!inherit || !state.inherited || si->hStdInput != (HANDLE)(uintptr_t)16 || si->hStdOutput != si->hStdInput || si->hStdError != (HANDLE)(uintptr_t)2);
} else BAD_IF(inherit);
if (state.launch_error) { state.error = state.launch_error; return FALSE; }
state.launched = 1; pi->hProcess = opened(4); pi->hThread = opened(8); return TRUE;
}
BOOL ReadFile(HANDLE h, void *buf, DWORD cap, DWORD *n, void *overlapped) {
BAD_IF(h != (HANDLE)(uintptr_t)1 || overlapped || (state.open & 2));
if (state.offset == state.length) { *n = 0; state.error = ERROR_BROKEN_PIPE; return FALSE; }
size_t amount = state.length - state.offset;
if (amount > cap) amount = cap;
memcpy(buf, state.data + state.offset, amount); state.offset += amount; *n = (DWORD)amount; return TRUE;
}
DWORD WaitForSingleObject(HANDLE h, DWORD timeout) {
BAD_IF(h != (HANDLE)(uintptr_t)4 || timeout != INFINITE);
if (state.capturing) BAD_IF(state.offset != state.length || (state.open & (1|2|16)));
return WAIT_OBJECT_0;
}
BOOL GetExitCodeProcess(HANDLE h, DWORD *code) {
BAD_IF(h != (HANDLE)(uintptr_t)4);
if (state.query_error) { state.error = state.query_error; return FALSE; }
*code = state.code; return TRUE;
}
int MultiByteToWideChar(unsigned cp, DWORD flags, const char *text, int size, wchar_t *out, int cap) {
BAD_IF(cp != CP_UTF8 || flags || size != -1);
size_t n = strlen(text); if (n + 1 > (size_t)cap) return 0;
state.converted_length = n;
for (size_t i = 0; i <= n; i++) out[i] = (unsigned char)text[i];
return (int)n + 1;
}
#endif
static jmp_buf failed;
static wchar_t message[1024];
static int failure_seen;
static void fail(const wchar_t *fmt, ...) {
va_list ap; va_start(ap, fmt);
int count = vswprintf(message, 1024, fmt, ap);
va_end(ap);
assert(count >= 0 && count < 1024);
failure_seen = 1;
longjmp(failed, 1);
}
/* ACTUAL_HELPER */
#ifdef REAL_CHILD
static wchar_t fake_system[MAX_PATH];
static DWORD fake_system_directory(wchar_t *out, DWORD n) {
assert(wcslen(fake_system) < n); wcscpy(out, fake_system); return (DWORD)wcslen(out);
}
#define GetSystemDirectoryW fake_system_directory
#else
DWORD GetSystemDirectoryW(wchar_t *out, DWORD n) {
assert(n > wcslen(L"C:\\Windows\\System32")); wcscpy(out, L"C:\\Windows\\System32"); return (DWORD)wcslen(out);
}
#endif
static void extract_part(const wchar_t *target, const wchar_t *archive) {
wchar_t dest[MAX_PATH], zip[MAX_PATH];
wchar_t sys[MAX_PATH], tarcmd[2 * MAX_PATH + 64];
int i = 0, nparts = 8;
wcscpy(dest, target);
wcscpy(zip, archive);
#ifndef REAL_CHILD
wcscpy(state.expected_destination, target), wcscpy(state.expected_zip, archive);
#endif
/* ACTUAL_CALL */
}
#define CHECK(c, why) do { if (!(c)) { fprintf(stderr, "FAIL: %s\n", why); return 1; } } while (0)
#ifdef REAL_CHILD
int main(int argc, char **argv) {
wchar_t self[MAX_PATH]; GetModuleFileNameW(NULL, self, MAX_PATH);
const wchar_t *name = wcsrchr(self, L'\\'); name = name ? name + 1 : self;
if (!_wcsicmp(name, L"tar.exe")) {
DWORD n, flags;
HANDLE in = GetStdHandle(STD_INPUT_HANDLE), out = GetStdHandle(STD_OUTPUT_HANDLE), err = GetStdHandle(STD_ERROR_HANDLE);
const char *prefix = GetHandleInformation(in, &flags) && GetHandleInformation(out, &flags)
? "valid-standard-handles; authored real tar diagnostic: " : "invalid-standard-handles; authored real tar diagnostic: ";
WriteFile(err, prefix, (DWORD)strlen(prefix), &n, NULL);
char bytes[1000]; memset(bytes, 'x', sizeof bytes);
for (int i = 0; i < 20; i++) WriteFile(err, bytes, sizeof bytes, &n, NULL);
return 23;
}
CHECK(argc == 2, "temporary folder argument");
CHECK(MultiByteToWideChar(CP_ACP, 0, argv[1], -1, fake_system, MAX_PATH), "temporary path conversion");
wchar_t child[MAX_PATH]; swprintf(child, MAX_PATH, L"%ls\\tar.exe", fake_system);
CHECK(CopyFileW(self, child, TRUE), "authored fake tar copy");
HANDLE saved_in = GetStdHandle(STD_INPUT_HANDLE), saved_out = GetStdHandle(STD_OUTPUT_HANDLE);
SetStdHandle(STD_INPUT_HANDLE, NULL); SetStdHandle(STD_OUTPUT_HANDLE, NULL);
if (!setjmp(failed)) extract_part(fake_system, L"C:\\authored cache\\part.zip");
SetStdHandle(STD_INPUT_HANDLE, saved_in); SetStdHandle(STD_OUTPUT_HANDLE, saved_out);
CHECK(failure_seen, "real child exit must fail extraction");
CHECK(wcsstr(message, L"exited with code 23"), "actual real child exit status");
CHECK(wcsstr(message, fake_system), "actual real destination");
CHECK(wcsstr(message, L"valid-standard-handles; authored real tar diagnostic:"), "GUI/no-console valid handles and real stderr");
CHECK(!wcsstr(message, L"invalid-standard-handles"), "reject invalid inherited standard handles");
CHECK(wcslen(message) < 1024, "bounded real message");
puts("PASS real child, long stderr, no parent standard handles");
return 0;
}
#else
int main(int argc, char **argv) {
CHECK(argc == 2, "case argument");
const char *test = argv[1];
wchar_t target[MAX_PATH] = L"Q:\\authored destination\\version.part";
wchar_t archive[MAX_PATH] = L"C:\\authored cache\\part.zip";
state.code = 23;
strcpy(state.data, "authored tar diagnostic"); state.length = strlen(state.data);
if (!strcmp(test, "launch")) state.launch_error = 5;
else if (!strcmp(test, "long")) { memset(state.data, 'x', sizeof state.data); memcpy(state.data, "authored tar diagnostic", 23); state.length = sizeof state.data; }
else if (!strcmp(test, "success")) state.code = 0, state.length = 0;
else if (!strcmp(test, "warning")) state.code = 0;
else if (!strcmp(test, "pipe_fallback")) state.code = 0, state.pipe_error = 87;
else if (!strcmp(test, "inherit_fallback")) state.code = 0, state.inherit_error = 5;
else if (!strcmp(test, "null_fallback")) state.code = 0, state.null_error = 5;
else if (!strcmp(test, "empty")) state.length = 0;
else if (!strcmp(test, "high_exit")) state.code = 0xc0000005UL;
else if (!strcmp(test, "query")) state.query_error = 6;
else if (!strcmp(test, "capture_error")) state.pipe_error = 87;
else if (!strcmp(test, "max_paths")) {
for (int i = 3; i < MAX_PATH - 1; i++) target[i] = L'd', archive[i] = L'a';
target[MAX_PATH - 1] = archive[MAX_PATH - 1] = 0;
memset(state.data, 'x', sizeof state.data); memcpy(state.data, "authored tar diagnostic", 23); state.length = sizeof state.data;
}
else CHECK(!strcmp(test, "failure"), "known case");
if (!setjmp(failed)) extract_part(target, archive);
CHECK(state.violation == 0 && state.open == 0, "command/flags/inheritance/drain order/handle cleanup unchanged");
CHECK(state.launched == !state.launch_error, "actual child launch");
if (!strcmp(test, "success") || !strcmp(test, "warning") || strstr(test, "fallback")) {
CHECK(!failure_seen, "successful extractor stays successful even without capture");
} else {
CHECK(failure_seen && wcslen(message) < 1024, "bounded failure message");
CHECK(wcsstr(message, target), "actual destination retained");
CHECK(wcsstr(message, archive), "actual archive retained");
CHECK(!wcsstr(message, L"space on C:"), "do not invent a full C drive");
if (!strcmp(test, "launch")) CHECK(wcsstr(message, L"Could not start") && wcsstr(message, L"Windows error 5"), "launch error is not child exit");
else if (!strcmp(test, "query")) CHECK(wcsstr(message, L"Could not read") && wcsstr(message, L"Windows error 6"), "unknown exit status is not invented");
else {
CHECK(wcsstr(message, !strcmp(test, "high_exit") ? L"exited with code 3221225477" : L"exited with code 23"), "actual unsigned child exit status");
if (!strcmp(test, "capture_error")) {
CHECK(wcsstr(message, L"Stderr unavailable") && wcsstr(message, L"Windows error 87"), "capture failure label and OS reason");
CHECK(!wcsstr(message, L"Tar stderr excerpt"), "capture error is not captured child stderr");
} else if (strcmp(test, "empty")) CHECK(wcsstr(message, L"authored tar diagnostic"), "bounded child stderr retained");
if (!strcmp(test, "long") || !strcmp(test, "max_paths")) CHECK(state.converted_length == 511, "511-byte capture unchanged");
if (!strcmp(test, "max_paths")) CHECK(wcsstr(message, L"excerpt") && wcslen(message) == 1023, "full paths with bounded displayed excerpt");
}
}
printf("PASS %s\n", test); return 0;
}
#endif

View File

@@ -0,0 +1,443 @@
// (triforce) The start-up screen drawn with no game, no window and no GPU: gpu/vk/vk_startup_screen.h's own drawing through
// Dear ImGui's draw lists into a picture, by a small software rasterizer (triangles with a colour and a texture coordinate a
// corner, blended as the window's 8-bit image blends them). tests/startup_screen_preview.py builds and runs it: the previews
// of every state of the screen, and tests/test_startup_screen.py's checks.
// startup_screen_preview <out folder> <romfs folder or -> <fonts folder> [--only name] [--size WxH] [--before]
// The fonts are the game's own, read from the player's romfs in memory as the game does (never written anywhere), else
// Libertinus from the fonts folder. Each picture is written as <name>.rgba: width, height (32-bit), then RGBA bytes.
#define IMGUI_DEFINE_MATH_OPERATORS
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <string>
#include <vector>
#include "imgui.h"
#include "imgui_internal.h"
#include "vk_game_font.h"
#include "vk_progress.h"
#include "vk_startup_screen.h"
#if __has_include("hookshot_logo.h")
#include "hookshot_logo.h"
#endif
using namespace mkwvk;
// ---- what vk_imgui.cpp gives the drawing in the game ----
static ImTextureData* crest = nullptr;
static float crest_aspect = 1;
namespace mkwvk {
void logf(const char*, ...) {}
}
namespace mkwvk::ui {
ImTextureData* logo(float* aspect) {
*aspect = crest_aspect;
return crest;
}
}
// ---- the rasterizer ----
struct Picture {
int w = 0, h = 0;
std::vector<unsigned char> px; // (RGBA, alpha 255)
};
struct Texel { float r, g, b, a; };
static Texel sample(const ImTextureData* t, float u, float v) {
if (!t || !t->Pixels) return {1, 1, 1, 1};
const float x = u * t->Width - 0.5f, y = v * t->Height - 0.5f;
const int x0 = (int)std::floor(x), y0 = (int)std::floor(y);
const float fx = x - x0, fy = y - y0;
const auto at = [&](int xx, int yy) -> Texel {
xx = xx < 0 ? 0 : xx >= t->Width ? t->Width - 1 : xx, yy = yy < 0 ? 0 : yy >= t->Height ? t->Height - 1 : yy;
const unsigned char* p = t->Pixels + ((size_t)yy * t->Width + xx) * t->BytesPerPixel;
if (t->BytesPerPixel == 1) return {1, 1, 1, p[0] / 255.0f};
return {p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, p[3] / 255.0f};
};
const Texel a = at(x0, y0), b = at(x0 + 1, y0), c = at(x0, y0 + 1), e = at(x0 + 1, y0 + 1);
const auto mix = [&](float p, float q, float r, float s) { return (p * (1 - fx) + q * fx) * (1 - fy) + (r * (1 - fx) + s * fx) * fy; };
return {mix(a.r, b.r, c.r, e.r), mix(a.g, b.g, c.g, e.g), mix(a.b, b.b, c.b, e.b), mix(a.a, b.a, c.a, e.a)};
}
static void triangle(Picture& pic, const ImDrawVert& A, const ImDrawVert& B0, const ImDrawVert& C0, const ImTextureData* tex, const ImVec4& clip) {
const ImDrawVert* b = &B0;
const ImDrawVert* c = &C0;
const auto fx = [](float v) { return (int64_t)std::llround((double)v * 256.0); };
int64_t ax = fx(A.pos.x), ay = fx(A.pos.y), bx = fx(b->pos.x), by = fx(b->pos.y), cx = fx(c->pos.x), cy = fx(c->pos.y);
int64_t area = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
if (!area) return;
if (area < 0) std::swap(b, c), std::swap(bx, cx), std::swap(by, cy), area = -area;
const int x0 = std::max((int)std::floor(std::min({A.pos.x, b->pos.x, c->pos.x})), std::max(0, (int)clip.x)),
x1 = std::min((int)std::ceil(std::max({A.pos.x, b->pos.x, c->pos.x})), std::min(pic.w, (int)clip.z)),
y0 = std::max((int)std::floor(std::min({A.pos.y, b->pos.y, c->pos.y})), std::max(0, (int)clip.y)),
y1 = std::min((int)std::ceil(std::max({A.pos.y, b->pos.y, c->pos.y})), std::min(pic.h, (int)clip.w));
// (the top-left rule: a pixel on an edge two triangles share is drawn once)
const auto bias = [](int64_t dx, int64_t dy) { return (dy == 0 && dx > 0) || dy < 0 ? 0 : -1; };
const int64_t b_ab = bias(bx - ax, by - ay), b_bc = bias(cx - bx, cy - by), b_ca = bias(ax - cx, ay - cy);
const auto col = [](ImU32 v, int shift) { return (float)(v >> shift & 255) / 255.0f; };
for (int y = y0; y < y1; y++)
for (int x = x0; x < x1; x++) {
const int64_t px = (int64_t)x * 256 + 128, py = (int64_t)y * 256 + 128;
const int64_t e_ab = (bx - ax) * (py - ay) - (by - ay) * (px - ax), e_bc = (cx - bx) * (py - by) - (cy - by) * (px - bx),
e_ca = (ax - cx) * (py - cy) - (ay - cy) * (px - cx);
if (e_ab + b_ab < 0 || e_bc + b_bc < 0 || e_ca + b_ca < 0) continue;
const float wa = (float)e_bc / area, wb = (float)e_ca / area, wc = (float)e_ab / area;
const Texel t = sample(tex, A.uv.x * wa + b->uv.x * wb + c->uv.x * wc, A.uv.y * wa + b->uv.y * wb + c->uv.y * wc);
float rgba[4];
const int shifts[4] = {IM_COL32_R_SHIFT, IM_COL32_G_SHIFT, IM_COL32_B_SHIFT, IM_COL32_A_SHIFT};
const float tv[4] = {t.r, t.g, t.b, t.a};
for (int k = 0; k < 4; k++) rgba[k] = (col(A.col, shifts[k]) * wa + col(b->col, shifts[k]) * wb + col(c->col, shifts[k]) * wc) * tv[k];
unsigned char* o = &pic.px[((size_t)y * pic.w + x) * 4];
for (int k = 0; k < 3; k++) o[k] = (unsigned char)std::lround(std::clamp(rgba[k] * rgba[3] * 255.0f + o[k] * (1 - rgba[3]), 0.0f, 255.0f));
}
}
/// The textures ImGui wants made or let go, as the game's backend answers them (their pixels are read where they are)
static void textures() {
for (ImTextureData* t : ImGui::GetPlatformIO().Textures) {
if (t->Status == ImTextureStatus_WantCreate || t->Status == ImTextureStatus_WantUpdates) {
t->SetTexID((ImTextureID)(intptr_t)t);
t->SetStatus(ImTextureStatus_OK);
} else if (t->Status == ImTextureStatus_WantDestroy) {
t->SetTexID(ImTextureID_Invalid);
t->SetStatus(ImTextureStatus_Destroyed);
}
}
}
static size_t last_vertices = 0;
static void rasterize(Picture& pic, const ImDrawData* dd) {
last_vertices = 0;
for (const ImDrawList* list : dd->CmdLists) {
last_vertices += (size_t)list->VtxBuffer.Size;
for (const ImDrawCmd& cmd : list->CmdBuffer) {
if (cmd.UserCallback) continue;
const ImTextureData* tex = (const ImTextureData*)(intptr_t)cmd.GetTexID();
const ImDrawIdx* idx = list->IdxBuffer.Data + cmd.IdxOffset;
const ImDrawVert* vtx = list->VtxBuffer.Data + cmd.VtxOffset;
for (unsigned i = 0; i + 2 < cmd.ElemCount; i += 3) triangle(pic, vtx[idx[i]], vtx[idx[i + 1]], vtx[idx[i + 2]], tex, cmd.ClipRect);
}
}
}
static bool write(const Picture& pic, const std::string& path) {
FILE* f = fopen(path.c_str(), "wb");
if (!f) return false;
const uint32_t wh[2] = {(uint32_t)pic.w, (uint32_t)pic.h};
fwrite(wh, 4, 2, f);
fwrite(pic.px.data(), 1, pic.px.size(), f);
fclose(f);
return true;
}
// ---- the fonts, as vk_imgui.cpp loads them ----
static ui::Fonts fonts;
static bool load_fonts(const std::string& romfs, const std::string& bundled) {
ImFontAtlas* atlas = ImGui::GetIO().Fonts;
const auto game = [&](game_font::Reader& r, const char* name) -> ImFont* {
std::vector<unsigned char> bytes = r.read(name);
if (bytes.empty()) return nullptr;
void* font = IM_ALLOC(bytes.size());
memcpy(font, bytes.data(), bytes.size());
ImFontConfig cfg;
snprintf(cfg.Name, sizeof cfg.Name, "%s", name);
return atlas->AddFontFromMemoryTTF(font, (int)bytes.size(), 18.0f, &cfg);
};
static game_font::Reader reader;
if (romfs != "-" && reader.open(romfs)) {
fonts.body = game(reader, "ClarimoUDPE-Regular");
fonts.strong = fonts.body ? game(reader, "ClarimoUDPE-DemiBold") : nullptr;
fonts.heading = fonts.strong ? game(reader, "CetraDisplayPro-Medium") : nullptr;
if (fonts.heading) return true;
atlas->Clear();
}
fonts.body = atlas->AddFontFromFileTTF((bundled + "/LibertinusSans-Regular.ttf").c_str(), 18.0f);
fonts.strong = atlas->AddFontFromFileTTF((bundled + "/LibertinusSans-Bold.ttf").c_str(), 18.0f);
fonts.heading = atlas->AddFontFromFileTTF((bundled + "/LibertinusSerif-Semibold.ttf").c_str(), 18.0f);
return false;
}
// ---- the screen as it was before the redesign (gpu/vk/vk_menu.cpp draw_startup and vk_progress.h startup_view at 2290d02),
// for the pictures that set the two side by side ----
struct OldView {
std::string version, step, title, pct, left, right, hint;
double fraction = -1;
};
static OldView old_view(const StartupState& s) {
OldView v;
if (s.stopping) {
v.title = "Closing", v.left = "Keeping what is prepared for the next start";
return v;
}
if (s.finished) {
if (s.secs >= 1.0) v.title = "Starting the game";
return v;
}
if (s.secs < 1.0) return v;
const bool pipelines = s.stage > 0;
if (s.prepared) v.step = pipelines ? "STEP 2 OF 2" : "STEP 1 OF 2";
v.title = !pipelines ? "Preparing shaders" : s.prepared ? "Building pipelines" : "Loading pipelines";
if (s.prepared) v.hint = "This happens once after an update. Closing the window keeps what is done.";
if (!s.total) {
v.left = !pipelines ? "Reading the game's shaders" : s.stage == 1 ? "Reading the pipeline notes" : "Loading the shader cache";
return v;
}
v.fraction = (double)std::min(s.done, s.total) / (double)s.total;
v.pct = percent_text(s.done, s.total);
v.left = with_commas(s.done) + " / " + with_commas(s.total) + (pipelines ? " pipelines" : " shaders");
v.right = left_text(s.left);
return v;
}
static void draw_old(ui::Ctx& c, const OldView& v, float fade) {
const float s = c.s, w = c.w, h = c.h, cx = w * 0.5f, k = s * 0.86f;
c.d->AddRectFilled(ImVec2(0, 0), ImVec2(w, h), ui::rgba(14, 10, 6));
const float lh = 112 * s, top = h * 0.5f - 176 * s;
for (int i = 40; i >= 1; i--) c.d->AddCircleFilled(ImVec2(cx, top + lh * 0.5f + 70 * s), h * 0.021f * i, ui::rgba(46, 33, 18, 4), 128);
float aspect = 1;
ImTextureData* logo = ui::logo(&aspect);
if (logo) c.d->AddImage(logo->GetTexRef(), ImVec2(cx - lh * aspect * 0.5f, top), ImVec2(cx + lh * aspect * 0.5f, top + lh));
float y = top + lh + 16 * s;
ui::spaced(c, c.f.heading, 42 * k, ImVec2(cx, y), ui::rgba(246, 232, 204), "HOOKSHOT", 6 * k);
ui::flourish(c, cx - 190 * k, cx + 190 * k, y + 66 * k, ui::kAccent);
ui::text_centre(c, c.f.body, 14.5f * k, ImVec2(cx, y + 78 * k), ui::kDim, v.version.c_str());
if (!v.title.empty()) {
y += 120 * k;
if (!v.step.empty()) {
std::string step = v.step;
for (size_t i = 1; i < step.size(); i++) step[i] = (char)tolower((unsigned char)step[i]);
ui::text_centre(c, c.f.body, 15 * k, ImVec2(cx, y), ui::kAccent, step.c_str());
}
ui::text_centre(c, c.f.heading, 24 * k, ImVec2(cx, y + 20 * k), ui::kInk, v.title.c_str());
const float x0 = cx - 270 * k, x1 = cx + 270 * k, by = y + 64 * k;
float under = by;
if (v.fraction >= 0) {
const float bh = 26 * k, in = 4 * k, fx = x0 + in + (x1 - x0 - 2 * in) * (float)std::min(v.fraction, 1.0);
c.d->AddRectFilled(ImVec2(x0, by), ImVec2(x1, by + bh), ui::kCellDark);
c.d->AddRect(ImVec2(x0, by), ImVec2(x1, by + bh), ui::kEdge, 0, std::max(1.0f, s));
if (fx > x0 + in) ui::gold(c, ImVec2(x0 + in, by + in), ImVec2(fx, by + bh - in));
const float split = std::max(fx, x0 + in);
c.d->PushClipRect(ImVec2(x0, by), ImVec2(split, by + bh), true);
ui::text_mid_centre(c, c.f.strong, 15 * k, cx, by + bh * 0.5f, ui::kGoldInk, v.pct.c_str());
c.d->PopClipRect();
c.d->PushClipRect(ImVec2(split, by), ImVec2(x1, by + bh), true);
ui::text_mid_centre(c, c.f.strong, 15 * k, cx, by + bh * 0.5f, ui::kInk, v.pct.c_str());
c.d->PopClipRect();
under = by + bh;
}
const float px = 16.5f * k;
ui::text(c, c.f.body, px, ImVec2(v.fraction >= 0 ? x0 : cx - ui::size_of(c.f.body, px, v.left.c_str()).x * 0.5f, under + 12 * k), ui::kSoft,
v.left.c_str());
if (!v.right.empty()) ui::text(c, c.f.body, px, ImVec2(x1 - ui::size_of(c.f.body, px, v.right.c_str()).x, under + 12 * k), ui::kSoft, v.right.c_str());
if (!v.hint.empty()) ui::text_centre(c, c.f.body, 15.5f * k, ImVec2(cx, by + 92 * k), ui::kDim, v.hint.c_str());
}
if (fade > 0) c.d->AddRectFilled(ImVec2(0, 0), ImVec2(w, h), ui::rgba(0, 0, 0, (int)(std::min(fade, 1.0f) * 255 + 0.5f)));
}
// ---- a start played through: what the preparation reports as time passes (vk_main.cpp's painter, with no workers) ----
struct Step {
int stage, pass;
size_t total;
double wait, secs; // (seconds with no count yet, then seconds counting)
};
struct Story {
std::vector<Step> steps;
bool prepared = false, compile = false; // (compile: the driver's part of the shaders is on, vk_draw.cpp prepare_compiles)
double close_at = -1; // (the window closed then)
};
struct Moment {
StartupState st;
float fade = 0;
};
static Moment moment_of(const Story& story, double t, ProgressClock& eta, int& eta_key) {
Moment m;
m.st.secs = t, m.st.prepared = false, m.st.compile = story.compile;
double at = 0;
bool found = false;
for (size_t i = 0; i < story.steps.size(); i++) {
const Step& s = story.steps[i];
if (s.stage == 0) m.st.prepared = true;
if (found) continue;
if (t < at + s.wait + s.secs || (story.close_at >= 0 && story.close_at < at + s.wait + s.secs)) {
const double tt = story.close_at >= 0 ? std::min(t, story.close_at) : t;
m.st.stage = s.stage, m.st.pass = s.pass;
if (tt >= at + s.wait) {
m.st.total = s.total;
// (the programs differ: a slow stretch in the middle, as a few heavy ones make)
double f = (tt - at - s.wait) / s.secs;
f = f < 0.4 ? f * 0.9 : f < 0.6 ? 0.36 + (f - 0.4) * 0.6 : 0.48 + (f - 0.6) * 1.3;
m.st.done = (size_t)(std::clamp(f, 0.0, 1.0) * s.total);
}
found = true;
}
at += s.wait + s.secs;
}
if (story.close_at >= 0 && t >= story.close_at) m.st.stopping = true;
if (!found) {
m.st.finished = true, m.st.stage = 2;
m.fade = (float)std::clamp((t - at - 0.35) / 0.4, 0.0, 1.0); // (held a moment here so its last picture can be taken)
}
const int key = m.st.stage * 2 + m.st.pass;
if (key != eta_key) eta.reset(), eta_key = key;
if (m.st.total) eta.note(t, m.st.done, m.st.total);
m.st.left = m.st.total ? eta.left() : -1;
return m;
}
struct Shot {
const char* name;
const Story* story;
double at;
};
int main(int argc, char** argv) {
if (argc < 4) {
fprintf(stderr, "usage: startup_screen_preview <out folder> <romfs folder or -> <fonts folder> [--only name] [--size WxH] [--before] [--large] [--linear]\n");
return 2;
}
const std::string out = argv[1], romfs = argv[2], bundled = argv[3];
std::string only;
int W = 1920, H = 1080;
bool before = false, large = false, linear = false, no_crest = false, frames = false;
for (int i = 4; i < argc; i++) {
if (!strcmp(argv[i], "--only") && i + 1 < argc) only = argv[++i];
else if (!strcmp(argv[i], "--size") && i + 1 < argc) sscanf(argv[++i], "%dx%d", &W, &H);
else if (!strcmp(argv[i], "--before")) before = true;
else if (!strcmp(argv[i], "--large")) large = true;
else if (!strcmp(argv[i], "--linear")) linear = true;
else if (!strcmp(argv[i], "--no-crest")) no_crest = true;
else if (!strcmp(argv[i], "--frames")) frames = true; // (every frame's size checked, a line each tenth of a second)
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasVtxOffset;
io.Fonts->TexDesiredFormat = ImTextureFormat_RGBA32;
const bool game_fonts = load_fonts(romfs, bundled);
printf("fonts: %s\n", game_fonts ? "the game's own, from its files" : "Libertinus");
#ifdef HOOKSHOT_LOGO
if (!no_crest) {
crest = IM_NEW(ImTextureData)();
crest->Create(ImTextureFormat_RGBA32, (int)hookshot_logo_w, (int)hookshot_logo_h);
memcpy(crest->GetPixels(), hookshot_logo_rgba, (size_t)hookshot_logo_w * hookshot_logo_h * 4);
crest->UseColors = true;
ImGui::RegisterUserTexture(crest);
crest_aspect = (float)hookshot_logo_w / hookshot_logo_h;
}
#endif
// (a first start as things are, the driver's part of the shaders off: two steps, their proportions as measured on an
// RTX 4070 SUPER (translation 37 s, the predicted pipelines 6 to 44 s, the noted ones a fifth of a second) and shortened:
// the shaders translated (2.2 to 26.2 s), the predicted pipelines (to 38.6 s), the noted ones (to 39.1 s))
Story first;
first.steps = {{0, 0, 14179, 2.2, 24}, {1, 0, 12513, 0.4, 12}, {2, 0, 3721, 0.1, 0.4}};
Story three = first; // (where the driver's part is switched on: three steps, it from 26.2 to 38.7 s)
three.compile = true;
three.steps = {{0, 0, 14179, 2.2, 24}, {0, 1, 31840, 0.5, 12}, {1, 0, 12513, 0.4, 9}, {2, 0, 3721, 0.1, 0.4}};
Story warm; // (any other start: the caches opened for three seconds, the noted pipelines read back in a moment)
warm.steps = {{2, 0, 4028, 3.0, 0.25}};
Story closed = first; // (the window closed while the shaders are translated)
closed.close_at = 10.3;
Story nothing; // (nothing to prepare, yet slow enough to show: the caches opened)
nothing.steps = {{2, 0, 0, 1.8, 0}};
const std::vector<Shot> shots = {
{"01-opening", &first, 0.50},
{"01b-opening-gleam", &first, 1.75},
{"02-reading", &first, 2.05},
{"03-translating", &first, 10.0},
{"04-first-stone-ignites", &first, 26.44},
{"05-building-pipelines", &first, 30.0},
{"06-second-stone-ignites", &first, 34.4},
{"06b-last-stone-ignites", &first, 38.85},
{"07-starting-the-game", &first, 39.4},
{"08-fade", &first, 39.65},
{"09-warm-start", &warm, 1.02},
{"09b-warm-start-stones-lit", &warm, 2.4},
{"10-warm-start-done", &warm, 3.55},
{"11-closing", &closed, 11.1},
{"12-nothing-to-prepare", &nothing, 1.5},
{"13-with-the-drivers-part", &three, 34.0},
};
int failed = 0;
for (const Shot& shot : shots) {
if (!only.empty() && only != shot.name) continue;
start_screen::motion = start_screen::Motion{};
ProgressClock eta;
int eta_key = -1;
Picture pic;
const double step = 1.0 / 60;
size_t most_vertices = 0;
for (double t = 0;; t += step) {
const bool last = t + step > shot.at;
const Moment m = moment_of(*shot.story, last ? shot.at : t, eta, eta_key);
io.DisplaySize = ImVec2((float)W, (float)H);
io.DeltaTime = (float)step;
ImGui::NewFrame();
ui::Ctx c{ImGui::GetForegroundDrawList(), fonts, 1, (float)W, (float)H, 0, false};
c.s = ui::unit((float)H, large ? 3 : 2, false, false, &c.safe);
if (before) {
OldView v = old_view(m.st);
v.version = "v2.0.11 dev";
draw_old(c, v, m.fade);
} else {
StartupView v = startup_view(m.st);
v.version = "v2.0.11 dev", v.device = "NVIDIA GeForce RTX 4070 SUPER", v.linear = linear;
start_screen::draw(c, v, m.fade, 100.0 + (last ? shot.at : t));
}
ImGui::Render();
textures();
const ImDrawData* dd = ImGui::GetDrawData();
size_t vertices = 0;
for (const ImDrawList* list : dd->CmdLists) {
vertices += (size_t)list->VtxBuffer.Size;
// (every corner inside the window, a little over for the soft edges: nothing is laid out off it)
for (const ImDrawVert& vtx : list->VtxBuffer)
if (!(vtx.pos.x > -W && vtx.pos.x < 2 * W && vtx.pos.y > -H && vtx.pos.y < 2 * H)) failed++;
}
most_vertices = std::max(most_vertices, vertices);
if (last) {
pic.w = W, pic.h = H;
pic.px.assign((size_t)W * H * 4, 255);
for (size_t i = 0; i < pic.px.size(); i += 4) pic.px[i] = pic.px[i + 1] = pic.px[i + 2] = 0;
rasterize(pic, dd);
break;
}
}
if (!write(pic, out + "/" + shot.name + ".rgba")) {
fprintf(stderr, "cannot write into %s\n", out.c_str());
return 1;
}
printf("%s: %dx%d at %.2f s, at most %zu vertices a frame\n", shot.name, W, H, shot.at, most_vertices);
if (frames) printf(" (frames checked every 1/60 s up to it)\n");
}
if (!before) { // (the end: the textures let go inside a frame that draws none of them, as the game does)
start_screen::art_release();
ImGui::NewFrame();
ui::Ctx c{ImGui::GetForegroundDrawList(), fonts, 1, (float)W, (float)H, 0, false};
c.s = ui::unit((float)H, 2, false, false, &c.safe);
start_screen::draw(c, StartupView{}, 1, 1000);
ImGui::Render();
for (const ImDrawList* list : ImGui::GetDrawData()->CmdLists)
for (const ImDrawCmd& cmd : list->CmdBuffer)
if (cmd.ElemCount && cmd.TexRef._TexData && cmd.TexRef._TexData != io.Fonts->TexData) failed++, fprintf(stderr, "a released texture is drawn\n");
textures();
// (and they stay gone: ImGui asks again for a texture whose destroy it does not know to be meant, at the next frame)
for (int k = 0; k < 3; k++) {
ImGui::NewFrame();
ImGui::Render();
textures();
}
int gone = 0;
for (ImTextureData* t : ImGui::GetPlatformIO().Textures) {
if (t == crest || t == io.Fonts->TexData) continue;
if (t->Status != ImTextureStatus_Destroyed || t->Pixels) failed++, fprintf(stderr, "a released texture is back (status %d)\n", (int)t->Status);
else gone++;
}
if (gone != (crest ? 6 : 1)) failed++, fprintf(stderr, "%d textures released, %d expected\n", gone, crest ? 6 : 1);
}
if (failed) {
fprintf(stderr, "FAIL: %d\n", failed);
return 1;
}
printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,119 @@
"""Pictures of the start-up screen (gpu/vk/vk_startup_screen.h) with no game, no window and no GPU: builds
tests/startup_screen_preview.cpp (the screen's own drawing code, Dear ImGui, a software rasterizer) and writes a PNG for every
state of the screen, and with --before the screen as it was before the redesign, drawn by the same rasterizer.
python -B tests/startup_screen_preview.py --out <folder> [--romfs <the game's romfs>] [--size 1920x1080] [--before]
[--only 03-translating] [--large] [--linear] [--no-crest] [--cxx clang++]
--romfs: the player's own game files, read in memory for the game's menu font exactly as the game reads them (nothing of them
is written anywhere but into the pictures); without it the bundled Libertinus is used, as in a game folder that has no font.
Needs Pillow for the PNGs (else the raw .rgba files are left: width, height, RGBA bytes).
"""
import argparse
import os
import shutil
import struct
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
IMGUI = ROOT / "third_party" / "imgui"
ZSTD = ROOT / "third_party" / "zstd" / "lib"
ZSTD_SOURCES = ("common/debug.c", "common/entropy_common.c", "common/error_private.c", "common/fse_decompress.c", "common/xxhash.c",
"common/zstd_common.c", "decompress/huf_decompress.c", "decompress/zstd_ddict.c", "decompress/zstd_decompress.c",
"decompress/zstd_decompress_block.c")
def header_dir(name, probe, given=""):
"""A package's include folder: the one given, the CPM cache (RX_CPM), HOOKSHOT_VULKAN_HEADERS, or the Vulkan SDK"""
roots = [given] if given else []
for cache in (os.environ.get("RX_CPM"), str(ROOT / "cpm")):
if cache and os.path.isdir(os.path.join(cache, name)):
roots += [os.path.join(cache, name, d) for d in sorted(os.listdir(os.path.join(cache, name)), reverse=True)]
for env in ("HOOKSHOT_VULKAN_HEADERS", "VULKAN_SDK"):
if os.environ.get(env):
roots.append(os.environ[env])
for root in roots:
for inc in ("include", "Include", ""):
if os.path.isfile(os.path.join(root, inc, *probe.split("/"))):
return os.path.join(root, inc)
raise SystemExit("no %s headers (%s): set RX_CPM to a CPM cache" % (name, probe))
def build(cxx, work, headers="", opt="-O2"):
"""The preview program, its objects kept in work (an object is made again when its source is newer). headers: the
Vulkan headers (gpu/vk/vk_ui.h reaches them through vk_imgui.h)"""
import logo_rgba
work.mkdir(parents=True, exist_ok=True)
logo_rgba.write(str(work / "hookshot_logo.h"))
win = os.name == "nt"
target = ["--target=x86_64-pc-windows-msvc", "-fms-extensions", "-D_CRT_SECURE_NO_WARNINGS"] if win and "clang" in cxx else []
includes = ["-I" + str(ROOT / "gpu" / "vk"), "-I" + str(IMGUI), "-I" + str(ZSTD), "-I" + str(work),
"-I" + header_dir("vulkanheaders", "vulkan/vulkan.h", headers)]
if os.name != "nt": # (the Win32 names the back end's headers use, as its own build has them on POSIX)
includes += ["-I" + str(ROOT / "host" / "posix"), "-D_GNU_SOURCE"]
cxxflags = target + ["-std=c++20", opt, "-g0", "-DNDEBUG", "-Wno-everything", "-DVK_NO_PROTOTYPES", "-DIMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS",
"-DIMGUI_DISABLE_WIN32_FUNCTIONS", "-DIMGUI_DISABLE_DEMO_WINDOWS", "-DIMGUI_DISABLE_DEBUG_TOOLS",
"-include", "algorithm", "-include", "cstdio", "-include", "cstdlib", "-include", "cstring", "-include", "functional",
"-include", "string", "-include", "vector"] + includes
cflags = target + [opt, "-g0", "-DNDEBUG", "-Wno-everything", "-DZSTD_DISABLE_ASM", "-DZSTD_LEGACY_SUPPORT=0", "-DZSTD_MULTITHREAD=0"]
cc = cxx.replace("clang++", "clang").replace("g++", "gcc")
objects = []
jobs = [(cxx, IMGUI / f, cxxflags, []) for f in ("imgui.cpp", "imgui_draw.cpp", "imgui_tables.cpp", "imgui_widgets.cpp")]
jobs += [(cc, ZSTD / f, cflags, []) for f in ZSTD_SOURCES]
ours = [ROOT / "gpu" / "vk" / f for f in ("vk_startup_screen.h", "vk_progress.h", "vk_ui.h", "vk_game_font.h")] + [work / "hookshot_logo.h"]
jobs.append((cxx, ROOT / "tests" / "startup_screen_preview.cpp", cxxflags, ours))
for compiler, src, flags, deps in jobs:
obj = work / (src.name.rsplit(".", 1)[0] + ".o")
objects.append(str(obj))
newest = max(os.path.getmtime(p) for p in [src] + deps)
if obj.exists() and os.path.getmtime(obj) >= newest:
continue
subprocess.run([compiler] + flags + ["-c", str(src), "-o", str(obj)], check=True)
exe = work / ("startup_screen_preview" + (".exe" if win else ""))
subprocess.run([cxx] + target + objects + ["-o", str(exe)] + ([] if win else ["-lpthread"]), check=True)
return exe
def to_png(folder):
try:
from PIL import Image
except ImportError:
print("no Pillow: the pictures stay as .rgba (width, height, RGBA bytes)")
return
for raw in sorted(folder.glob("*.rgba")):
data = raw.read_bytes()
w, h = struct.unpack_from("<II", data, 0)
Image.frombytes("RGBA", (w, h), data[8:8 + w * h * 4]).convert("RGB").save(raw.with_suffix(".png"), optimize=True)
raw.unlink()
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--out", required=True, type=Path)
p.add_argument("--work", type=Path, help="where the program is built (default: <out>/.build)")
p.add_argument("--romfs", default="-")
p.add_argument("--size", default="1920x1080")
p.add_argument("--only", default="")
p.add_argument("--cxx", default=shutil.which("clang++") or "clang++")
p.add_argument("--headers", default="", help="the Vulkan headers' folder (default: the CPM cache, RX_CPM)")
for flag in ("before", "large", "linear", "no-crest"):
p.add_argument("--" + flag, action="store_true")
a = p.parse_args()
exe = build(a.cxx, a.work or a.out / ".build", a.headers)
a.out.mkdir(parents=True, exist_ok=True)
cmd = [str(exe), str(a.out), a.romfs, str(ROOT / "third_party" / "fonts" / "libertinus"), "--size", a.size]
if a.only:
cmd += ["--only", a.only]
for flag in ("before", "large", "linear", "no-crest"):
if getattr(a, flag.replace("-", "_")):
cmd.append("--" + flag)
subprocess.run(cmd, check=True)
to_png(a.out)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,46 @@
"""3D images used by sampled2D views carry the enabled extension's creation flag.
python -I -B tests/test_3d_view_create_flags.py [--cxx clang++]
MSVC requires a configured developer prompt and --cxx cl. No game or GPU.
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--source',type=Path,default=ROOT)
parser.add_argument('--cxx',default='clang++')
a=parser.parse_args()
source=(a.source/'gpu/vk/vk_texture.cpp').read_text(encoding='utf-8')
start=source.index(' if (x.type == VK_IMAGE_TYPE_3D) flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;') if ' if (x.type == VK_IMAGE_TYPE_3D) flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;' in source else source.index(' if (x.type == VK_IMAGE_TYPE_3D) {', source.index('VkImageCreateFlags flags = 0;'))
fragment=source[start:source.index(' VkImageCreateInfo ci',start)]
fixture=r"""
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#define CHECK(x) do {if(!(x)){std::fprintf(stderr,"FAIL %d %s\n",__LINE__,#x);std::exit(1);}}while(0)
enum{VK_IMAGE_TYPE_2D=1,VK_IMAGE_TYPE_3D=2,VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT=32,VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT=128};
struct{bool view_2d_of_3d;}dev;
unsigned run(int type,bool enabled,unsigned prior){struct{int type;}x{type};dev.view_2d_of_3d=enabled;unsigned flags=prior;
"""+fragment+r"""
return flags;}
int main(){
for(unsigned prior:{0u,1u,2u,4u,7u}) {
for(bool enabled:{false,true}){
CHECK(run(VK_IMAGE_TYPE_2D,enabled,prior)==prior);
CHECK(run(VK_IMAGE_TYPE_3D,enabled,prior)==(prior|VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT|(enabled?VK_IMAGE_CREATE_2D_VIEW_COMPATIBLE_BIT_EXT:0)));
}
}
std::puts("PASS: actual3D allocation flags preserve existing bits and gate2D-view capability");}
"""
fixture='#include <initializer_list>\n'+fixture
with tempfile.TemporaryDirectory(prefix='3d-view-flags-') as tmp:
folder=Path(tmp);cpp=folder/'fixture.cpp';exe=folder/'fixture.exe';cpp.write_text(fixture,encoding='utf-8')
if Path(a.cxx).name.lower() in ('cl','cl.exe','clang-cl','clang-cl.exe'):
cmd=[a.cxx,'/nologo','/std:c++17','/EHsc',str(cpp),'/Fo'+str(folder/'fixture.obj'),'/Fe'+str(exe)]
else:cmd=[a.cxx,'-std=c++17','-Wall','-Wextra','-Werror',str(cpp),'-o',str(exe)]
subprocess.run(cmd,cwd=folder,check=True);subprocess.run([str(exe)],check=True)
if __name__=='__main__':main()

96
tests/test_alt_enter.py Normal file
View File

@@ -0,0 +1,96 @@
"""Alt+Enter toggles fullscreen and presses no button (Enter is Plus by default, and any button can be bound to it; issue #390).
The bound keys' helper (host/nn_hid.c's kmap_down) is compiled alone against fake key states; the window
procedures (host/display.c, host/posix/display_sdl.c) are checked to toggle on Alt+Enter, once per press, and to
leave other Alt keys (Alt+F4) to Windows.
python -I -B tests/test_alt_enter.py [--cc gcc]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def case(text, label):
"""The body of one `case label:` of a switch, up to the next case"""
start = text.index("case %s:" % label)
end = text.index(" case ", start + 1)
return text[start:end]
def check_windows():
src = (ROOT / "host" / "display.c").read_text(encoding="utf-8")
sys_down = case(src, "WM_SYSKEYDOWN")
for need, why in (("VK_RETURN", "Enter"), ("1 << 29", "the Alt bit"), ("1 << 30", "a held key repeating"),
("set_fullscreen(w, !fullscreen)", "the toggle"), ("break;", "other Alt keys to DefWindowProc")):
if need not in sys_down:
raise SystemExit("display.c: WM_SYSKEYDOWN lacks %s (%s)" % (need, why))
if "'\\r'" not in case(src, "WM_SYSCHAR"):
raise SystemExit("display.c: Alt+Enter's WM_SYSCHAR is not taken (Windows beeps)")
def check_linux():
src = (ROOT / "host" / "posix" / "display_sdl.c").read_text(encoding="utf-8")
if "scancode == 40 && (mod & 0x300)" not in src:
raise SystemExit("display_sdl.c: Alt+Enter no longer toggles fullscreen")
FIXTURE = r'''#include <stdio.h>
#include <stdlib.h>
#define VK_RETURN 0x0D
#define VK_MENU 0x12
#define VK_TAB 0x09
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
static int held[256];
static int down(int vk) { return held[vk & 255]; }
@KMAP_DOWN@
int main(void) {
held[VK_RETURN] = 1;
CHECK(kmap_down(VK_RETURN, down)); /* Enter alone is Plus */
held[VK_MENU] = 1;
CHECK(!kmap_down(VK_RETURN, down)); /* Alt+Enter is not */
held[VK_TAB] = 1;
CHECK(kmap_down(VK_TAB, down)); /* Alt does not hide the other keys */
held[VK_RETURN] = 0;
CHECK(!kmap_down(VK_RETURN, down));
held[VK_MENU] = 0;
CHECK(!kmap_down(VK_MENU, down) && kmap_down(VK_TAB, down));
printf("PASS: Alt+Enter is fullscreen, not Plus\n");
return 0;
}
'''
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cc", default="gcc")
a = p.parse_args()
check_windows()
check_linux()
hid = (ROOT / "host" / "nn_hid.c").read_text(encoding="utf-8")
fn = block(hid, "static int kmap_down(")
if "kmap_down(key_bind[i][k], key)" not in hid:
raise SystemExit("nn_hid.c: the bound keys no longer go through kmap_down")
with tempfile.TemporaryDirectory(prefix="alt-enter-") as tmp:
c = Path(tmp) / "fixture.c"
exe = Path(tmp) / "fixture"
c.write_text(FIXTURE.replace("@KMAP_DOWN@", fn), encoding="utf-8")
subprocess.run([a.cc, "-std=c11", "-Wall", "-Wextra", "-Werror", str(c), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,80 @@
"""The translator is told the truth about a GPU whose graphics stages run wider than NVN's 32-lane warps.
AMD's GPUs pin only compute to 32 lanes (requiredSubgroupSizeStages 0x20 on an RX 5500M): a fragment stage runs at 64, so the
profile asks for the long path that keeps each 32-lane guest warp to its half (warp_size_potentially_larger_than_guest), and
the broadcast fallbacks walk all the host's lanes. On AMD's proprietary driver fragment-stage shuffles take the broadcast
fallback (Lichii #273: a basket in Link's house drew as a white blob), and a depth-compare texture is never an integer image
(#273: the shadow samples of the lighting). The profile's choices are in the program cache's stamp.
The long path is per stage: a graphics stage the GPU pins to 32 lanes as well (RDNA2/RDNA3 pin fragment, task and mesh:
requiredSubgroupSizeStages 0xf0) keeps main's short path, so only the stages that really run at 64 lanes change.
python -I -B tests/test_amd_fragment_warp.py
"""
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
SPIRV = VK / "eden" / "shader_recompiler" / "backend" / "spirv"
def main():
shader = (VK / "vk_shader.cpp").read_text(encoding="utf-8")
profile = shader[shader.index("Shader::Profile make_profile()"):shader.index("Shader::HostTranslateInfo host_info()")]
for needle, why in (
("dev.subgroup_size > 32 && (dev.sg32_stages & VK_SHADER_STAGE_ALL_GRAPHICS)",
"a graphics stage running wider than 32 lanes is not told to the translator"),
("p.max_subgroup_size = p.warp_size_potentially_larger_than_guest ? dev.subgroup_max : 32",
"the broadcast fallbacks do not walk the host's upper lanes"),
("p.has_broken_spirv_fragment_shuffle = dev.driver_id == VK_DRIVER_ID_AMD_PROPRIETARY",
"AMD's proprietary driver keeps native fragment-stage shuffles"),
):
if needle not in profile:
raise SystemExit(why)
# (stages pinned to 32 lanes keep the short path: the long one in every compute program was new, longer SPIR-V that AMD
# compiled for hours, and they need none of it)
if "EmitSPIRV(profile_pinned(), rt, t->program, bindings)" not in shader:
raise SystemExit("compute programs take the long warp path though they are pinned to 32 lanes")
if shader.count("EmitSPIRV(fallback ? profile_pinned() : profile_graphics(") != 2:
raise SystemExit("the mesh fallback's compute stages take the long warp path though they are pinned to 32 lanes")
# (per graphics stage: pinned ones keep the short path, as on main; RX_VK_WARP64 still sets them all)
graphics = shader[shader.index("const Shader::Profile& profile_graphics("):]
graphics = graphics[:graphics.index("\n}\n")]
for needle, why in (
("q.warp_size_potentially_larger_than_guest = false, q.max_subgroup_size = 32", "a pinned graphics stage takes the long path"),
("!forced && (dev.sg32_stages & stage) ? pinned : profile()", "the stage's pinning does not pick its path"),
('getenv("RX_VK_WARP64")', "RX_VK_WARP64 no longer sets every graphics stage"),
):
if needle not in graphics:
raise SystemExit(why)
if "EmitSPIRV(profile()," in shader:
raise SystemExit("a stage is translated with the shared profile, not its own stage's")
for stage, n in (("VK_SHADER_STAGE_VERTEX_BIT", 2), ("VK_SHADER_STAGE_FRAGMENT_BIT", 2), ("VK_SHADER_STAGE_TASK_BIT_EXT", 1),
("VK_SHADER_STAGE_MESH_BIT_EXT", 1)):
if shader.count("profile_graphics(%s)" % stage) != n:
raise SystemExit("the %s programs do not take their stage's profile" % stage)
stamp = shader[shader.index("uint64_t disk_stamp()"):]
if "profile().warp_size_potentially_larger_than_guest" not in stamp.split("return")[0]:
raise SystemExit("programs translated for another GPU's warps are kept in the program cache")
if "(uint32_t)dev.sg32_stages" not in stamp.split("return")[0]:
raise SystemExit("programs translated for another GPU's pinned stages are kept in the program cache")
main_cpp = (VK / "vk_main.cpp").read_text(encoding="utf-8")
if "dev.driver_id = drv.driverID" not in main_cpp or "dev.subgroup_size = sg.subgroupSize" not in main_cpp:
raise SystemExit("the driver or its subgroup size is not read")
warp = (SPIRV / "emit_spirv_warp.cpp").read_text(encoding="utf-8")
if "ctx.profile.has_broken_spirv_fragment_shuffle && ctx.stage == Stage::Fragment" not in warp:
raise SystemExit("fragment-stage shuffles do not take the broadcast fallback")
if warp.count("UseShuffleFallback(ctx)") < 3:
raise SystemExit("absolute, relative and butterfly shuffles do not all take the fallback")
# (not the quad swap: a program with quad shuffles alone has no lane id input nor ballot capability for it)
quad = warp[warp.index("Id EmitQuadSwap("):]
if "UseShuffleFallback(ctx)" in quad[:quad.index("\n}\n")]:
raise SystemExit("the quad swap takes the broadcast fallback, which its programs cannot have")
texture = (VK / "eden" / "shader_recompiler" / "ir_opt" / "texture_pass.cpp").read_text(encoding="utf-8")
if "!flags.is_depth && is_texture_pixel_format_integer(cbuf)" not in texture:
raise SystemExit("a depth-compare texture can be an integer image")
print("AMD fragment warps, shuffles and depth-compare textures ok")
if __name__ == "__main__":
main()

96
tests/test_antilag.py Normal file
View File

@@ -0,0 +1,96 @@
"""AMD Anti-Lag 2 (gpu/vk/vk_antilag.cpp): compiles its actual frame calls against the Vulkan headers with a recording
vkAntiLagUpdateAMD, and checks that the device and the present path call them. No GPU or driver required.
python -I -B tests/test_antilag.py --headers <Vulkan-Headers/include> [--cxx g++]
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
ROOT = Path(__file__).resolve().parents[1]
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--headers", type=Path, required=True)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (ROOT / "gpu" / "vk" / "vk_antilag.cpp").read_text(encoding="utf-8")
def block(text, marker):
"""From marker to the brace closing its first one"""
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
# the wiring: the device asks for the extension and its feature, the present gives the present stage before
# vkQueuePresentKHR, and the input stage comes after the present (with Reflex's sleep)
main = (ROOT / "gpu" / "vk" / "vk_main.cpp").read_text(encoding="utf-8")
for call in ("antilag_device_extensions(want, exts);", "antilag_device_features(tail);", "antilag_device_created();"):
if call not in main:
raise SystemExit("vk_main.cpp no longer calls " + call)
if main.index("antilag_device_features(tail);") > main.index("vkGetPhysicalDeviceFeatures2(dev.physical, &f);"):
raise SystemExit("vk_main.cpp: Anti-Lag 2's feature joins the chain after the features were queried")
replay = (ROOT / "gpu" / "vk" / "vk_replay.cpp").read_text(encoding="utf-8")
present = block(replay, "void present_image(")
if "antilag_present();" not in present or present.index("antilag_present();") > present.index("vkQueuePresentKHR("):
raise SystemExit("present_image: Anti-Lag 2's present stage is not given before vkQueuePresentKHR")
if "reflex_sleep(); antilag_input();" not in block(replay, "void do_present("):
raise SystemExit("do_present: Anti-Lag 2's input stage is not given after the present")
functions = "\n".join(block(source, m) + (";" if m.startswith("struct") else "") for m in (
"struct AntiLag {", "AntiLag& al()", "void update(", "bool on()", "void stage(", "void antilag_present()",
"void antilag_input()"))
fixture = r'''
#include <vulkan/vulkan_core.h>
#include <cstdio>
#include <mutex>
#include <vector>
struct {VkDevice device=VK_NULL_HANDLE;} dev;
bool fg=false;int want=1;
bool framegen_on(){return fg;}
bool wanted(){return want>0;}
template<class... T> void logf(const char*,T...){}
struct Call{VkAntiLagModeAMD mode;int stage;uint64_t frame;};
std::vector<Call> calls;
void VKAPI_CALL record(VkDevice,const VkAntiLagDataAMD* d){
calls.push_back({d->mode,d->pPresentationInfo?(int)d->pPresentationInfo->stage:-1,
d->pPresentationInfo?d->pPresentationInfo->frameIndex:0});
}
''' + functions + r'''
#define CHECK(c) do{if(!(c)){std::printf("FAIL line %d: %s\n",__LINE__,#c);return 1;}}while(0)
int main(){
// not available: nothing is ever called
antilag_input();antilag_present();CHECK(calls.empty());
al().ready=true;al().update=record;
// a present before the first input stage gives nothing
antilag_present();CHECK(calls.empty());
// a frame: its input stage, then its present stage, with one index, mode On
antilag_input();antilag_present();
CHECK(calls.size()==2&&calls[0].stage==VK_ANTI_LAG_STAGE_INPUT_AMD&&calls[1].stage==VK_ANTI_LAG_STAGE_PRESENT_AMD);
CHECK(calls[0].frame==calls[1].frame&&calls[0].mode==VK_ANTI_LAG_MODE_ON_AMD&&calls[1].mode==VK_ANTI_LAG_MODE_ON_AMD);
antilag_input();CHECK(calls.size()==3&&calls[2].frame==calls[0].frame+1);
// frame generation on: the driver told Off once, then nothing
calls.clear();fg=true;antilag_input();antilag_present();antilag_input();
CHECK(calls.size()==1&&calls[0].mode==VK_ANTI_LAG_MODE_OFF_AMD&&calls[0].stage==-1);
// on again: the present waits for an input stage
calls.clear();fg=false;antilag_present();CHECK(calls.empty());
antilag_input();antilag_present();CHECK(calls.size()==2&&calls[1].stage==VK_ANTI_LAG_STAGE_PRESENT_AMD);
// the row off: Off once, then nothing
calls.clear();want=0;antilag_input();antilag_present();antilag_input();
CHECK(calls.size()==1&&calls[0].mode==VK_ANTI_LAG_MODE_OFF_AMD);
std::puts("PASS: Anti-Lag 2's stages in order with one index a frame, Off told once, nothing while off");
}
'''
with tempfile.TemporaryDirectory(prefix="antilag-") as tmp:
cpp, exe = Path(tmp) / "fixture.cpp", Path(tmp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-missing-field-initializers",
"-pthread", "-I" + str(a.headers), str(cpp), "-o", str(exe)], check=True)
raise SystemExit(subprocess.run([str(exe)]).returncode)

View File

@@ -0,0 +1,54 @@
"""Game-free: every gpu/ header the host's sources include is one the Linux AppImage copies (scripts/linux/appimage.sh).
v2.0.6's AppImage lacked gpu/vk/oot_cheat_items.h, which host/hos_main.c includes, and no Linux build could finish (issue 990).
python -I tests/test_appimage_headers.py
"""
import re
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main():
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
included = set()
for src in sorted((ROOT / "host").rglob("*.[ch]")):
for m in re.finditer(r'#include "\.\./gpu/((?:vk|render)/[^"]+)"', src.read_text(encoding="utf-8", errors="replace")):
included.add(m.group(1))
# headers those include, inside gpu/ (one level is all there is today; walk until nothing new)
todo = list(included)
while todo:
rel = todo.pop()
path = ROOT / "gpu" / rel
if not path.is_file():
raise SystemExit("host/ includes gpu/%s, which does not exist" % rel)
for m in re.finditer(r'#include "([^"]+)"', path.read_text(encoding="utf-8", errors="replace")):
dep = (Path(rel).parent / m.group(1)).as_posix()
dep = str(Path(dep)).replace("\\", "/")
parts = []
for piece in dep.split("/"):
if piece == "..":
parts.pop()
elif piece != ".":
parts.append(piece)
dep = "/".join(parts)
if (ROOT / "gpu" / dep).is_file() and dep not in included:
included.add(dep)
todo.append(dep)
copied_render = "cp -a gpu/render/*.h" in script
copied_vk_loop = "grep -rhoE 'gpu/vk/[A-Za-z0-9_]+[.]h' host" in script
missing = []
for rel in sorted(included):
if rel.startswith("render/") and rel.endswith(".h") and copied_render:
continue
if rel.startswith("vk/") and ("cp -a gpu/vk/%s" % rel[3:] in script or copied_vk_loop):
continue
missing.append(rel)
if missing:
raise SystemExit("the AppImage does not ship gpu/" + ", gpu/".join(missing) + " (host/ includes them)")
print("ok: the AppImage ships the %d gpu headers host/ includes" % len(included))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,55 @@
"""The AppImage carries the licences of the third-party parts compiled into hookshot_vk.so: Dear ImGui (MIT), zstd (BSD) and
the Libertinus fonts (OFL), each with its PROVENANCE.md. Runs the real copy block of scripts/linux/appimage.sh into a temporary
folder; nothing is built.
python -I -B tests/test_appimage_licenses.py
"""
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
WANT = {
"licenses/imgui/LICENSE.txt": "third_party/imgui/LICENSE.txt",
"licenses/imgui/PROVENANCE.md": "third_party/imgui/PROVENANCE.md",
"licenses/zstd/LICENSE": "third_party/zstd/LICENSE",
"licenses/zstd/PROVENANCE.md": "third_party/zstd/PROVENANCE.md",
"licenses/libertinus/OFL.txt": "third_party/fonts/libertinus/OFL.txt",
"licenses/libertinus/PROVENANCE.md": "third_party/fonts/PROVENANCE.md",
}
class AppImageLicences(unittest.TestCase):
# (the AppImage is made on Linux; Windows runners have no usable bash for the script's block)
@unittest.skipUnless(sys.platform.startswith("linux"), "the AppImage is made on Linux")
def test_copied(self):
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
start = script.index("# ---- the licences of the third-party parts compiled into hookshot_vk.so")
end = script.index("# ---- end of the licences ----", start)
with tempfile.TemporaryDirectory(prefix="hookshot-appimage-licences-") as folder:
env = dict(os.environ, T=folder)
result = subprocess.run(["bash", "-eu", "-o", "pipefail", "-c", script[start:end]], cwd=ROOT, env=env,
capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
for shipped, source in WANT.items():
self.assertEqual((Path(folder) / shipped).read_bytes(), (ROOT / source).read_bytes(), shipped)
def test_block_after_the_deps_licences(self):
# (cp -a of the deps' licences into an existing folder would nest it: this block must come after)
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
self.assertLess(script.index('cp -a "$DEPS/licenses" "$T/licenses"'),
script.index("# ---- the licences of the third-party parts compiled into hookshot_vk.so"))
def test_vk_third_party_covered(self):
# (every third_party folder the Vulkan back end compiles in, from nx2/build.py, has its licence shipped)
build = (ROOT / "nx2/build.py").read_text(encoding="utf-8") + (ROOT / "nx2/ui_fonts.py").read_text(encoding="utf-8")
for part, licence in (("imgui", "licenses/imgui/"), ("zstd", "licenses/zstd/"), ("fonts", "licenses/libertinus/")):
if '"third_party", "%s"' % part in build:
self.assertTrue(any(k.startswith(licence) for k in WANT), part)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,111 @@
"""Execute the real AppImage package bootstrap with failing apt/PPA commands.
No network or package installation. The command stand-ins record ordering and
inject transient or persistent failures at each bootstrap stage under sh -e.
"""
import argparse
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest
# (Linux only)
RUN_ALL_SKIP = {"windows": "the AppImage package bootstrap is POSIX shell; exercised on Linux"}
ROOT = Path(__file__).resolve().parents[1]
STAGES = ("update", "prerequisites", "ppa", "toolchain", "dependencies")
STUB = r'''#!/usr/bin/env python3
import json,os,sys
from pathlib import Path
name=Path(sys.argv[0]).name
if name=='sleep':stage='sleep'
elif name=='add-apt-repository':stage='ppa'
elif sys.argv[1]=='update':stage='update'
elif 'software-properties-common' in sys.argv:stage='prerequisites'
elif 'g++-13' in sys.argv:stage='toolchain'
elif 'libx11-dev' in sys.argv:stage='dependencies'
else:raise SystemExit('unexpected package command')
state=Path(os.environ['BOOTSTRAP_STATE'])
data=json.loads(state.read_text())
data['calls'].append({'stage':stage,'argv':sys.argv[1:]})
fail=stage==os.environ.get('BOOTSTRAP_FAIL_STAGE') and data['failures']<int(os.environ.get('BOOTSTRAP_FAIL_COUNT','0'))
if fail:data['failures']+=1
state.write_text(json.dumps(data))
raise SystemExit(100 if fail else 0)
'''
def bootstrap(root):
source = (root / ".github/workflows/linux-appimage.yml").read_text(encoding="utf-8")
start = source.index(" - name: System packages")
start = source.index(" run: |\n", start) + len(" run: |\n")
lines = []
for line in source[start:].splitlines():
if line and not line.startswith(" "):
break
lines.append(line[10:] if line else "")
return "\n".join(lines) + "\n"
@unittest.skipIf(os.name == "nt", "POSIX AppImage package bootstrap")
class PackageRetry(unittest.TestCase):
def run_bootstrap(self, stage="", failures=0):
shell = shutil.which("sh")
self.assertIsNotNone(shell)
with tempfile.TemporaryDirectory(prefix="appimage-packages-") as temp:
folder = Path(temp)
tools = folder / "bin"
tools.mkdir()
for command in ("apt-get", "add-apt-repository", "sleep"):
path = tools / command
path.write_text(STUB, encoding="utf-8")
path.chmod(0o755)
state = folder / "state.json"
state.write_text(json.dumps({"calls": [], "failures": 0}), encoding="utf-8")
script = folder / "bootstrap.sh"
script.write_text(bootstrap(ROOT), encoding="utf-8")
env = dict(os.environ, PATH=str(tools) + os.pathsep + os.environ["PATH"],
BOOTSTRAP_STATE=str(state), BOOTSTRAP_FAIL_STAGE=stage,
BOOTSTRAP_FAIL_COUNT=str(failures), DEBIAN_FRONTEND="noninteractive")
result = subprocess.run([shell, "-e", str(script)], capture_output=True, text=True, env=env, timeout=30)
self.assertFalse(result.stderr, result.stderr)
return result.returncode, json.loads(state.read_text(encoding="utf-8"))["calls"]
def test_success_keeps_the_five_bootstrap_stages(self):
code, calls = self.run_bootstrap()
self.assertEqual(code, 0)
self.assertEqual([c["stage"] for c in calls], list(STAGES))
def test_each_transient_failure_restarts_with_fresh_indexes(self):
for stage in STAGES:
with self.subTest(stage=stage):
code, calls = self.run_bootstrap(stage, 1)
self.assertEqual(code, 0)
failed_prefix = list(STAGES[:STAGES.index(stage) + 1])
self.assertEqual([c["stage"] for c in calls], failed_prefix + ["sleep"] + list(STAGES))
self.assertEqual([c["argv"] for c in calls if c["stage"] == "sleep"], [["20"]])
def test_two_index_failures_can_recover_on_the_last_attempt(self):
code, calls = self.run_bootstrap("update", 2)
self.assertEqual(code, 0)
self.assertEqual([c["stage"] for c in calls], ["update", "sleep", "update", "sleep"] + list(STAGES))
def test_persistent_failures_stop_after_three_attempts_with_the_original_status(self):
for stage in STAGES:
with self.subTest(stage=stage):
code, calls = self.run_bootstrap(stage, 10)
self.assertEqual(code, 100)
prefix = list(STAGES[:STAGES.index(stage) + 1])
self.assertEqual([c["stage"] for c in calls], prefix + ["sleep"] + prefix + ["sleep"] + prefix)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source", type=Path, default=ROOT)
args, remaining = parser.parse_known_args()
ROOT = args.source
unittest.main(argv=[sys.argv[0]] + remaining)

View File

@@ -0,0 +1,100 @@
"""Game-free: a player's build starts from the tools the Linux AppImage carries. Runs the real tools-copy block of
scripts/linux/appimage.sh into a temporary folder, then nx2/build.py --standalone --dry-run from there, as the AppImage runs
it (HOOKSHOT_VK_PREBUILT: the Vulkan back end comes built), with stand-ins for the compiler, sysroot and FFmpeg it brings.
The dry run goes up to the compile, where the lifted game is first needed, and fails on any other file the compile would
read that is not in the tree. 2026-11-10: nx2/build.py wrote the menu's fonts from third_party/fonts at every start, the
AppImage does not carry that folder, and every Linux player's build stopped (#1567); CI had packaged the AppImage and never
started a build from it. The AppImage workflow does the same with the real image: scripts/linux/check_player_build.sh.
python -I -B tests/test_appimage_player_build.py
"""
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
def package(tools):
"""appimage.sh's own copy of the tools into <tools> (its T). Without -e: the native helpers it copies (zbic.so) are
built by appimage.sh first, and what the build needs of the tree is the dry run's to say"""
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
start = script.index("# ---- the tools (the sources")
end = script.index("# ---- what it builds with", start)
subprocess.run(["bash", "-u", "-c", script[start:end]], cwd=ROOT, env=dict(os.environ, T=str(tools), VERSION="test"),
capture_output=True, text=True)
def dry_run(tools, lib, work):
"""nx2/build.py as nx2/make_game.py runs it in the AppImage (AppRun's variables), with nothing else of this PC's"""
env = {"PATH": "/usr/bin:/bin", "HOME": str(work / "home"), "LANG": "C.UTF-8", "HOOKSHOT_TOOLS": str(tools),
"EMSDK": str(lib / "emsdk"), "HOOKSHOT_SYSROOT": str(lib / "sysroot"), "HOOKSHOT_FFMPEG": str(lib / "ffmpeg"),
"HOOKSHOT_VK_PREBUILT": str(lib / "hookshot_vk.so"), "OOT_WORK": str(work / "work"), "RX_RELEASE": "1",
"A64_HOT_CHUNK": "1", "RX_MARCH": "x86-64-v3"}
return subprocess.run([sys.executable, "-I", "-B", "-u", "-X", "utf8", "nx2/build.py", "--standalone", "--jobs", "1",
"--dry-run"], cwd=tools, env=env, capture_output=True, text=True, timeout=300)
@unittest.skipUnless(sys.platform.startswith("linux"), "the AppImage is made on Linux")
class PlayerBuildFromTheAppImage(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory(prefix="hookshot-appimage-build-")
self.addCleanup(temp.cleanup)
self.root = Path(temp.name)
self.tools, self.lib = self.root / "AppDir/usr/share/hookshot", self.root / "AppDir/usr/lib/hookshot"
self.tools.mkdir(parents=True)
package(self.tools)
# (stand-ins for what appimage.sh builds or brings: a compiler that is never run, the sysroot, FFmpeg, the back end)
clang = self.lib / "emsdk/upstream/bin/clang"
clang.parent.mkdir(parents=True)
clang.write_text("#!/bin/sh\nexit 1\n")
clang.chmod(0o755)
for d in ("sysroot", "ffmpeg/include", "ffmpeg/lib"):
(self.lib / d).mkdir(parents=True)
(self.lib / "hookshot_vk.so").write_bytes(b"")
(self.root / "home").mkdir()
def test_starts_from_the_packaged_tree(self):
r = dry_run(self.tools, self.lib, self.root)
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertIn("as built", r.stdout) # (the prebuilt back end: nothing of gpu/vk compiled)
self.assertRegex(r.stdout, r"dry run: [1-9]\d* files to compile besides the lift's, 0 missing")
self.assertFalse(list(self.tools.rglob("__pycache__"))) # (the image is read-only on a player's PC)
def test_a_file_the_image_lacks_fails(self):
first = (self.tools / "third_party/opus/sources.txt").read_text().split()[0]
(self.tools / "third_party/opus" / first).unlink()
r = dry_run(self.tools, self.lib, self.root)
self.assertEqual(r.returncode, 1, r.stdout + r.stderr)
self.assertIn("missing: " + str(self.tools / "nx2" / ".." / "third_party" / "opus" / first), r.stdout)
class TheChecksAreWired(unittest.TestCase):
def test_the_workflow_checks_the_image_it_made(self):
flow = (ROOT / ".github/workflows/linux-appimage.yml").read_text(encoding="utf-8")
self.assertLess(flow.index("run: scripts/linux/appimage.sh"), flow.index("run: scripts/linux/check_player_build.sh"))
def test_apprun_sets_up_before_python(self):
# (check_player_build.sh takes AppRun's environment from its lines before PY=)
apprun = (ROOT / "builder/linux/AppRun").read_text(encoding="utf-8")
head = apprun[:apprun.index("\nPY=")]
self.assertRegex(head, r"\nexport HOOKSHOT_VK_PREBUILT=")
self.assertNotRegex(head, r"\n\s*(exec|exit)\b")
script = (ROOT / "scripts/linux/check_player_build.sh").read_text(encoding="utf-8")
self.assertIn("sed '/^PY=/,$d'", script)
self.assertIn("--dry-run", script)
def test_the_test_sets_what_apprun_sets(self):
apprun = (ROOT / "builder/linux/AppRun").read_text(encoding="utf-8")
exported = set(re.findall(r"^export (HOOKSHOT_\w+|EMSDK)=", apprun, re.MULTILINE))
source = Path(__file__).read_text(encoding="utf-8")
used = set(re.findall(r'"(HOOKSHOT_\w+|EMSDK)":', source))
self.assertEqual(exported - used - {"HOOKSHOT_SDL2", "HOOKSHOT_BUILDER_EXE"}, set()) # (those two: the game's, after the build)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,29 @@
"""Game-free: the Linux AppImage carries the controller fonts the button prompts are drawn from (third_party/kenney_input,
nx2/prompt_assets.py KENNEY). Without them make_game.py says the prompts "were not made" and the game keeps the Switch's icons,
even with fontTools installed.
python -I tests/test_appimage_prompt_fonts.py
"""
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main():
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
assets = (ROOT / "nx2/prompt_assets.py").read_text(encoding="utf-8")
m = re.search(r'KENNEY = os\.path\.join\(os\.path\.dirname\(HERE\), "third_party", "([^"]+)"\)', assets)
if not m:
raise SystemExit("FAIL: nx2/prompt_assets.py has no KENNEY under third_party")
folder = "third_party/" + m.group(1)
if not (ROOT / folder).is_dir():
raise SystemExit("FAIL: %s does not exist" % folder)
if not re.search(r'^cp -a %s "\$T/third_party/"$' % re.escape(folder), script, re.M):
raise SystemExit("FAIL: scripts/linux/appimage.sh does not copy %s" % folder)
print("ok: the AppImage copies %s" % folder)
if __name__ == "__main__":
sys.exit(main())

View File

@@ -0,0 +1,42 @@
"""The AppImage carries scripts/, so NVIDIA DLSS (nx2/make_game.py's ngx_script(), scripts/ngx_sdk.py) can be fetched from a
release AppImage and not only from a source checkout. Runs the real copy block of scripts/linux/appimage.sh into a
temporary folder; nothing is built.
Issue: the comment above the copy ("the sources the player's build compiles, and the scripts") already said scripts
belonged here, but the cp -a line itself left scripts out, so HOOKSHOT_NGX_SDK/--fetch-ngx-sdk on a released AppImage
failed with FileNotFoundError on scripts/ngx_sdk.py.
python -I -B tests/test_appimage_scripts.py
"""
import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
WANT = {
"scripts/ngx_sdk.py": "scripts/ngx_sdk.py",
"scripts/linux/ngx-sdk.sh": "scripts/linux/ngx-sdk.sh",
}
class AppImageScripts(unittest.TestCase):
# (the AppImage is made on Linux; Windows runners have no usable bash for the script's block)
@unittest.skipUnless(sys.platform.startswith("linux"), "the AppImage is made on Linux")
def test_copied(self):
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
start = script.index("# ---- the tools (the sources")
end = script.index("cp -a third_party/opus", start)
with tempfile.TemporaryDirectory(prefix="hookshot-appimage-scripts-") as folder:
env = dict(os.environ, T=folder, VERSION="authored-test")
result = subprocess.run(["bash", "-eu", "-o", "pipefail", "-c", script[start:end]], cwd=ROOT, env=env,
capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
for shipped, source in WANT.items():
self.assertEqual((Path(folder) / shipped).read_bytes(), (ROOT / source).read_bytes(), shipped)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,89 @@
"""The real AppImage source-copy block preserves repository-local quoted header dependencies.
No AppImage, toolchain, generated files or game is built. Bash runs only the source
copy block before the separately bundled third-party dependencies.
"""
from pathlib import Path
import os
import re
import subprocess
import tempfile
import unittest
# (Linux only)
RUN_ALL_SKIP = {"windows": "the AppImage source-copy block uses POSIX Bash and cp"}
ROOT = Path(__file__).resolve().parents[1]
INCLUDE = re.compile(r'^\s*#\s*include\s*"([^"\n]+)"', re.MULTILINE)
def missing_headers(source, packaged, entries):
"""Follow literal includes relative to their source, including transitive edges/cycles.
System/include-path/generated dependencies are outside this local-header check.
Inactive preprocessor branches are checked too: the package supports every host route.
"""
pending = list(entries)
visited, missing = set(), {}
while pending:
relative = pending.pop()
if relative in visited:
continue
visited.add(relative)
path = source / relative
for name in INCLUDE.findall(path.read_text(encoding="utf-8")):
included = (path.parent / name).resolve()
if not included.is_relative_to(source) or not included.is_file():
continue
target = included.relative_to(source).as_posix()
if not (packaged / target).is_file():
missing.setdefault(target, relative)
pending.append(target)
return missing
class AppImageHeaders(unittest.TestCase):
def test_actual_packaged_host_headers(self):
script = (ROOT / "scripts/linux/appimage.sh").read_text(encoding="utf-8")
# Execute production commands, stopping before binaries/dependencies and all native build steps.
start = script.index("# ---- the tools (the sources")
end = script.index("cp -a third_party/opus", start)
with tempfile.TemporaryDirectory(prefix="hookshot-appimage-headers-") as folder:
packaged = Path(folder) / "tools"
packaged.mkdir()
env = dict(os.environ, T=str(packaged), VERSION="authored-test")
result = subprocess.run(["bash", "-eu", "-o", "pipefail", "-c", script[start:end]],
cwd=ROOT, env=env, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
entries = [p.relative_to(ROOT).as_posix() for p in (ROOT / "host").rglob("*")
if p.suffix in (".c", ".h")]
entries += [p.relative_to(ROOT).as_posix() for p in (ROOT / "gpu/render").glob("*.h")]
self.assertEqual(missing_headers(ROOT, packaged, entries), {},
"AppImage source is missing a repository-local header (target: includer)")
def test_transitive_missing_header_and_include_cycle(self):
with tempfile.TemporaryDirectory(prefix="hookshot-local-includes-") as folder:
source, packaged = Path(folder) / "source", Path(folder) / "packaged"
files = {
"host/main.c": '#include "../gpu/vk/top.h"\n',
"gpu/vk/top.h": '#include "deep/leaf.h"\n',
"gpu/vk/deep/leaf.h": '#include "../top.h"\n#include <external.h>\n',
}
for name, text in files.items():
path = source / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
if name != "gpu/vk/deep/leaf.h":
target = packaged / name
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(text, encoding="utf-8")
self.assertEqual(missing_headers(source, packaged, ["host/main.c"]),
{"gpu/vk/deep/leaf.h": "gpu/vk/top.h"})
target = packaged / "gpu/vk/deep/leaf.h"
target.parent.mkdir()
target.write_text(files["gpu/vk/deep/leaf.h"], encoding="utf-8")
self.assertEqual(missing_headers(source, packaged, ["host/main.c"]), {})
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,30 @@
// astc_bc_policy::all_by_vram (gpu/vk/astc_bc_policy.h): the cards that keep every large ASTC texture as BC3.
// tests/test_astc_bc_policy.py
#include "../gpu/vk/astc_bc_policy.h"
#include <cstdio>
#include <cstdlib>
static int failures = 0;
static void expect(bool ok, const char* what) {
if (!ok) {
std::printf("FAIL: %s\n", what);
failures++;
}
}
int main() {
const uint64_t MB = 1ull << 20;
using astc_bc_policy::all_by_vram;
expect(!all_by_vram(0), "unknown: as before (the finer ones as RGBA8)");
expect(all_by_vram(3379 * MB), "a 4 GB laptop card (3.3 GB allowed, issue 519)");
expect(all_by_vram(5900 * MB), "a 6 GB card");
expect(all_by_vram(7133 * MB), "an 8 GB RX 7700S (7.1 GB allowed)");
expect(all_by_vram(7700 * MB), "an 8 GB card allowed more");
expect(!all_by_vram(9400 * MB), "a 10 GB card");
expect(!all_by_vram(11200 * MB), "a 12 GB card");
expect(!all_by_vram(15000 * MB), "a 16 GB card");
if (failures) return EXIT_FAILURE;
std::printf("astc bc policy: all checks passed\n");
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,34 @@
"""Compile and run the rule for the cards that keep every large ASTC texture as BC3 (gpu/vk/astc_bc_policy.h; no game/GPU)."""
import argparse
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
def check_source():
"""The texture setup still asks the rule, and the switch that overrides it is still read"""
src = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
for needle in ('#include "astc_bc_policy.h"', "astc_bc_policy::all_by_vram(allowed)", 'getenv("RX_VK_ASTC_BC_ALL")',
"(bw * bh >= 36 || all) ? low : 64ull << 20"):
if needle not in src:
raise SystemExit("vk_texture.cpp no longer has: " + needle)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="astc-bc-policy-") as tmp:
exe = Path(tmp) / "astc-bc-policy"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror",
str(ROOT / "tests/test_astc_bc_policy.cpp"), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,73 @@
"""A stall under a few seconds is mixed out, so the sound catches the picture.
A wake more than 100 ms late used to move the mixer's deadline to now. Those frames were never mixed, and the
sound stayed behind by that much.
python -I -B tests/test_audio_deadline.py [--cxx g++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (ROOT / "host" / "nn_audio.c").read_text(encoding="utf-8")
loop = source[source.index("static DWORD WINAPI renderer_main"):source.index("/* ---- renderer lifecycle")]
if "audio_mix_deadline(" not in loop or "100000000" in loop:
raise SystemExit("the mixer still drops the frames it woke late for")
fn = block(source, "int64_t audio_mix_deadline(")
fixture = r'''#include <cstdint>
#include <cstdio>
#include <cstdlib>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
''' + fn + r'''
/* frames mixed before the deadline is in the future again, with `now` held still */
static int caught_up(int64_t late) {
const int64_t period = 5000000, start = 1000000000, now = start + late;
int64_t next = start;
int n = 0;
while (next <= now && n < 10000) {
next = audio_mix_deadline(now, next, period);
n++;
}
return n;
}
int main() {
const int64_t period = 5000000, start = 1000000000;
/* 40 ms late: still on the old grid, nine frames (the last one lands on now) */
CHECK(audio_mix_deadline(start + 40000000, start, period) == start + period);
CHECK(caught_up(40000000) == 9);
/* 150 ms late: the old 100 ms jump mixed one frame and left the sound behind */
CHECK(caught_up(150000000) == 31);
/* a four-second pause is not mixed out */
CHECK(caught_up(4000000000LL) == 1);
CHECK(audio_mix_deadline(start + 4000000000LL, start, period) == start + 4000000000LL + period);
std::printf("PASS: a hitch is mixed out, a long pause is not\n");
}
'''
with tempfile.TemporaryDirectory(prefix="audio-deadline-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / "fixture"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

411
tests/test_audio_in.py Normal file
View File

@@ -0,0 +1,411 @@
"""Check the microphone's input streams (host/nn_audio_in.c) as the game drives them: one device listed, streams opened on
the game's format and started (state 2), silence at their rate with the microphone off, one microphone opened for both
streams and its samples given to each, the newest kept past a stream's capacity, a microphone that stops sending or does
not open (silence, tries again after 3 s, then twice as long each time, said once), a named one by its id (of two with one
name, the one chosen), the default standing in for an unplugged one until it is back under another id, purge, stop and
close; and the menu's row keeping its choice by name. SDL, the menu and the clock are stand-ins. No GPU or game files.
python -I -B tests/test_audio_in.py [--cc gcc] [--cxx g++]
"""
import argparse
import re
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cc", "gcc", "--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
HOS_H = r'''
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef struct { uint64_t x[32]; uint64_t vreg[32][2]; } Ctx;
#define HLE(sym) void imp_##sym(Ctx *c)
#define ARG(n) (c->x[n])
#define RET(v) (c->x[0] = (uint64_t)(v))
#define GP(a) ((void *)(uintptr_t)(a))
#define G64(a) (*(uint64_t *)GP(a))
#define G32(a) (*(uint32_t *)GP(a))
#define HOS_WRITTEN(p, n) ((void)0)
static inline void *hos_guest_range(uint64_t a, uint64_t n) { (void)n; return a ? GP(a) : (void *)0; } /* (the test's addresses are host ones) */
void hos_log(const char *fmt, ...);
int64_t hos_now_ns(void);
'''
WINDOWS_H = r'''
#pragma once
typedef int SRWLOCK;
#define SRWLOCK_INIT 0
static inline void AcquireSRWLockExclusive(SRWLOCK *l) { (void)l; }
static inline void ReleaseSRWLockExclusive(SRWLOCK *l) { (void)l; }
static inline void AcquireSRWLockShared(SRWLOCK *l) { (void)l; }
static inline void ReleaseSRWLockShared(SRWLOCK *l) { (void)l; }
'''
FIXTURE = r'''
#include <stdarg.h>
#include <stdlib.h>
#include "nn_audio_in.c"
static int64_t clock_ns = 1000000000;
int64_t hos_now_ns(void) { return clock_ns; }
static int logged;
void hos_log(const char *fmt, ...) { (void)fmt; logged++; }
const char *hos_sdl_error(void) { return "no device"; }
/* SDL's microphones: "Test microphone", two "Twin" and a long name, as ids id_base.. ; the default (id 0) opens unless
no_default */
static int listed = 1, no_default, opens, opened, closed, mic_bytes, next_sample, last_format, last_rate, last_channels;
static uint32_t id_base = 7, last_id = 99;
#define LONG_NAME "Microphone Array (Intel\xc2\xae Smart Sound Technology for Digital Microphones)"
int hos_sdl_mic_list(uint32_t *ids, char (*names)[128], int max) {
static const char *const all[4] = {"Test microphone", "Twin", "Twin", LONG_NAME};
int n = 0;
for (; n < listed && n < max && n < 4; n++) ids[n] = id_base + (uint32_t)n, strcpy(names[n], all[n]);
return n;
}
void *hos_sdl_mic_open(uint32_t id, int freq, int channels, uint16_t format) {
opens++;
if (id ? id < id_base || id >= id_base + (uint32_t)listed : no_default) return NULL;
opened++, last_id = id, last_rate = freq, last_channels = channels, last_format = format;
return &opened;
}
int hos_sdl_mic_available(void *mic) { return mic ? mic_bytes : 0; }
int hos_sdl_mic_read(void *mic, void *dst, int bytes) {
(void)mic;
if (bytes > mic_bytes) bytes = mic_bytes;
for (int i = 0; i < bytes / 2; i++) ((int16_t *)dst)[i] = (int16_t)(next_sample++);
mic_bytes -= bytes;
return bytes;
}
void hos_sdl_mic_clear(void *mic) { (void)mic; mic_bytes = 0; }
void hos_sdl_mic_close(void *mic) { (void)mic; closed++; }
/* an HLE function called with its arguments in x0.. as the lifted code makes the call: the result in x0 */
static Ctx c;
static uint64_t call(void (*fn)(Ctx *), int n, ...) {
va_list ap;
va_start(ap, n);
memset(&c, 0, sizeof c);
for (int i = 0; i < n; i++) c.x[i] = va_arg(ap, uint64_t);
va_end(ap);
fn(&c);
return c.x[0];
}
#define NARGS(...) (int)(sizeof((uint64_t[]){__VA_ARGS__}) / sizeof(uint64_t))
#define CALL(fn, ...) call(fn, NARGS(__VA_ARGS__), __VA_ARGS__)
#define FAIL(n, ...) do { fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); return n; } while (0)
#define U(p) ((uint64_t)(uintptr_t)(p))
#define OPEN(s, name) CALL(imp__ZN2nn5audio15OpenInputStreamEPNS0_15InputStreamTypeEPvmRKNS0_15AudioDeviceNameERKNS0_12StreamFormatEi, \
U(s), U(work), (uint64_t)sizeof work, U(name), U(format), (uint64_t)4800)
#define START(s) CALL(imp__ZN2nn5audio16StartInputStreamEPNS0_15InputStreamTypeE, U(s))
#define STATE(s) CALL(imp__ZN2nn5audio19GetInputStreamStateEPKNS0_15InputStreamTypeE, U(s))
#define FILLED(s) (int)CALL(imp__ZN2nn5audio30GetInputStreamFilledFrameCountEPKNS0_15InputStreamTypeE, U(s))
#define READ(s, buf, n) (int)CALL(imp__ZN2nn5audio15ReadInputStreamEPNS0_15InputStreamTypeEPvi, U(s), U(buf), (uint64_t)(n))
#define PURGE(s) CALL(imp__ZN2nn5audio16PurgeInputStreamEPNS0_15InputStreamTypeE, U(s))
#define MS(n) (clock_ns += (int64_t)(n) * 1000000)
int main(void) {
int cases = 0;
static uint8_t names[8][256], raw[256], work[0x4000];
static uint64_t stream[0x360 / 8], other[0x360 / 8], never[0x360 / 8];
static const uint32_t format[4] = {2, 1, 48000, 0}; /* {PcmInt16, 1 channel, 48000 Hz}: the game's */
static int16_t buf[4800], buf2[4800];
if (CALL(imp__ZN2nn5audio23ListVirtualInputDevicesEPNS0_15AudioDeviceNameEi, U(names), (uint64_t)8) != 1 ||
strcmp((char *)names[0], "MainAudioIn"))
FAIL(1, "the device list: one device");
if (CALL(imp__ZN2nn5audio23ListVirtualInputDevicesEPNS0_15AudioDeviceNameEi, U(names), (uint64_t)0) != 0) FAIL(2, "a list of none");
CALL(imp__ZN2nn5audio26GetTrueRawAudioInputDeviceEPNS0_15AudioDeviceNameE, U(raw));
if (OPEN(stream, names[0]) != 0 || (stream[0] >> 32) != 0x4D494349u) FAIL(3, "the stream did not open");
if (OPEN(other, raw) != 0 || stream[0] == other[0]) FAIL(4, "the raw stream did not open apart");
if (STATE(stream) == 2 || FILLED(stream) != 0) FAIL(5, "an opened stream is not started, and has nothing");
if (START(never) == 0 || STATE(never) == 2 || FILLED(never)) FAIL(6, "a stream never opened does not start");
hos_mic_select(0, "", -1); /* (the menu: Off) */
if (START(stream) != 0 || STATE(stream) != 2) FAIL(7, "the started state is 2: the game's check");
if (opens) FAIL(8, "the microphone opened while the menu has it off");
cases += 8;
/* off: silence at 48 kHz, at most the capacity */
MS(10);
if (FILLED(stream) != 480) FAIL(9, "10 ms of silence: %d frames, not 480", FILLED(stream));
memset(buf, 0x55, sizeof buf);
if (READ(stream, buf, 256) != 256 || buf[0] || buf[255] || FILLED(stream) != 224) FAIL(10, "silence read");
MS(5000);
if (FILLED(stream) != 4800) FAIL(11, "silence held to the capacity");
START(other);
MS(10);
if (FILLED(other) != 480 || FILLED(stream) != 4800) FAIL(12, "the second stream: its own silence from its start");
cases += 4;
/* the default microphone: opened once for both streams, in their format, its samples given to each */
PURGE(stream), PURGE(other);
hos_mic_select(1, "", -1), mic_bytes = 1000;
if (FILLED(stream) != 500 || FILLED(other) != 500 || opened != 1 || last_id != 0 || last_rate != 48000 ||
last_channels != 1 || last_format != 0x8010)
FAIL(13, "one microphone for both, 48 kHz mono S16: %d and %d frames, opened %d, id %u, %d Hz %d ch fmt %#x",
FILLED(stream), FILLED(other), opened, last_id, last_rate, last_channels, last_format);
if (READ(stream, buf, 256) != 256 || READ(other, buf2, 256) != 256 || buf[0] != 0 || buf[255] != 255 ||
memcmp(buf, buf2, 512))
FAIL(14, "both streams read the same samples");
mic_bytes = 20000; /* the game stopped reading: only the newest capacity's worth is kept */
if (FILLED(stream) != 4800 || mic_bytes != 0) FAIL(15, "past the capacity: %d frames", FILLED(stream));
if (READ(stream, buf, 10000) != 4800 || buf[0] != 5700 || buf[4799] != 10499) FAIL(16, "the newest kept: %d .. %d", buf[0], buf[4799]);
cases += 4;
/* it stops sending: 2 s of nothing, then silence; tried again after 3 s, then 6, 12: said once */
MS(1000);
if (FILLED(stream) != 0 || closed) FAIL(17, "a second of nothing is not yet a lost microphone");
MS(1500);
if (FILLED(stream) != 0 || closed != 1) FAIL(18, "2.5 s of nothing: closed (%d)", closed);
MS(10);
if (FILLED(stream) != 480) FAIL(19, "silence once it stopped sending: %d", FILLED(stream));
no_default = 1;
const int said = logged, tried = opens;
MS(3000), (void)FILLED(stream);
if (opens != tried + 1 || FILLED(stream) != 4800) FAIL(20, "a failed try after 3 s: still silence (%d)", opens - tried);
MS(3000), (void)FILLED(stream);
if (opens != tried + 1) FAIL(21, "the next wait is 6 s, not 3");
MS(3000), (void)FILLED(stream);
MS(11000), (void)FILLED(stream);
if (opens != tried + 2) FAIL(22, "then 12 s: %d tries", opens - tried);
MS(1000), (void)FILLED(stream);
if (opens != tried + 3 || logged != said) FAIL(23, "then: tried again, and nothing more said (%d tries, %d lines)", opens - tried, logged - said);
no_default = 0;
MS(24000), (void)FILLED(stream);
PURGE(stream), mic_bytes = 200;
if (opened != 2 || FILLED(stream) != 100) FAIL(24, "the default back: %d opens, %d frames", opened, FILLED(stream));
cases += 8;
/* one that opens but never sends: closed and opened again, the waits growing, not every 5 s */
const int quiet_from = opens;
MS(2500), (void)FILLED(stream); /* (it gave sound: the waits started over at 3 s) */
MS(3000), (void)FILLED(stream);
MS(2500), (void)FILLED(stream);
MS(3000), (void)FILLED(stream);
if (opens != quiet_from + 1) FAIL(25, "a quiet one: the second wait is 6 s (%d opens)", opens - quiet_from);
MS(3000), (void)FILLED(stream);
if (opens != quiet_from + 2) FAIL(26, "then opened again (%d)", opens - quiet_from);
cases += 2;
/* a named one, by the id SDL lists it under; of two with one name, the one chosen */
hos_mic_count();
if (strcmp(hos_mic_name(0), "Test microphone") || hos_mic_name(1)[0]) FAIL(27, "the menu's list");
hos_mic_select(2, "Test microphone", 0);
(void)FILLED(stream);
if (last_id != 7) FAIL(28, "the named microphone: id %u", last_id);
const int before = opens;
hos_mic_select(2, "Test microphone", 0);
(void)FILLED(stream);
if (opens != before) FAIL(29, "the same choice again: not opened again");
listed = 3, hos_mic_count();
hos_mic_select(2, "Twin", 2);
(void)FILLED(stream);
if (last_id != 9) FAIL(30, "the second of two named alike: id %u", last_id);
cases += 4;
/* stalled (it stops sending): the default stands in at once, the chosen one tried again; plugged in again under another
id, found in a list made at the retry */
listed = 1, hos_mic_count(), hos_mic_select(2, "Test microphone", 0);
(void)FILLED(stream);
if (last_id != 7) FAIL(31, "the named one first (%u)", last_id);
listed = 0, mic_bytes = 0; /* (pulled out: the menu's list still has it, as 7) */
MS(2500), (void)FILLED(stream);
if (last_id != 0) FAIL(32, "the default stands in once it stops sending (id %u)", last_id);
listed = 1, id_base = 20; /* (plugged in again: another id; the menu's list still says 7) */
MS(3000), (void)FILLED(stream);
if (last_id != 20) FAIL(33, "back under its new id, found in a list made at the retry (%u)", last_id);
hos_mic_count();
const int reopened = opens;
hos_mic_select(2, "Test microphone", 0); /* (the menu, opened since: the same one, under its new id) */
(void)FILLED(stream);
if (opens != reopened) FAIL(41, "the menu's word of the id it has now: not opened again");
cases += 4;
/* pulled out and still sending zeros (SDL3 on Windows): no longer listed, so the default stands in within 5 s */
listed = 0;
for (int i = 0; i < 6; i++) mic_bytes = 9600, MS(1000), (void)FILLED(stream);
if (last_id != 0) FAIL(42, "a pulled-out one sending zeros: the default stands in (%u)", last_id);
listed = 1;
MS(4000), (void)FILLED(stream);
if (last_id != 20) FAIL(43, "and it is back on the next try (%u)", last_id);
listed = 0, hos_mic_select(2, "Test microphone", -1); /* (the menu sees it unplugged: looked for at once) */
mic_bytes = 9600, (void)FILLED(stream);
if (last_id != 0) FAIL(44, "the menu's word that it is unplugged: the default at once (%u)", last_id);
cases += 3;
/* a name past the ini's 60 bytes: the menu's kept name (cut) finds it, as does its whole name */
listed = 4, id_base = 7, hos_mic_count();
char kept[64] = {0};
memcpy(kept, LONG_NAME, 60); /* (the menu's kept name: its first 60 bytes) */
hos_mic_select(2, kept, -1);
MS(10), (void)FILLED(stream);
if (last_id != 10) FAIL(45, "the long name by its first 60 bytes (%u)", last_id);
hos_mic_select(2, LONG_NAME, 3);
const int long_opens = opens;
(void)FILLED(stream);
if (opens != long_opens || last_id != 10) FAIL(46, "its whole name: the same one, not opened again");
cases += 2;
hos_mic_select(0, "", -1);
const int closes = closed;
(void)FILLED(stream);
if (closed != closes + 1) FAIL(34, "off: closed");
PURGE(stream);
MS(10);
if (FILLED(stream) != 480) FAIL(35, "off: silence from then");
cases += 2;
memset(buf, 0, sizeof buf);
c.x[8] = U(buf);
buf[4] = 77;
imp__ZN2nn5audio24GetInputStreamStatisticsEPNS0_15InputStreamTypeEb(&c);
if (buf[4]) FAIL(36, "the statistics: nothing lost");
cases++;
hos_mic_select(1, "", -1);
(void)FILLED(stream);
const int open_now = closed;
CALL(imp__ZN2nn5audio15StopInputStreamEPNS0_15InputStreamTypeE, U(stream));
if (STATE(stream) == 2 || FILLED(stream) != 0 || closed != open_now) FAIL(37, "one stopped: the other still has the microphone");
CALL(imp__ZN2nn5audio15StopInputStreamEPNS0_15InputStreamTypeE, U(other));
if (closed != open_now + 1) FAIL(38, "both stopped: the microphone closed");
CALL(imp__ZN2nn5audio16CloseInputStreamEPNS0_15InputStreamTypeE, U(stream));
if (stream[0] || READ(stream, buf, 16) != 0) FAIL(39, "closed: the handle gone");
if (OPEN(stream, names[0]) != 0) FAIL(40, "a closed stream's slot is used again");
cases += 4;
/* a frame count from the game past 10 s is held to 10 s (its ring's byte offsets are ints: 0x20000001 stereo
frames overflowed them) */
CALL(imp__ZN2nn5audio16CloseInputStreamEPNS0_15InputStreamTypeE, U(stream));
CALL(imp__ZN2nn5audio16CloseInputStreamEPNS0_15InputStreamTypeE, U(other));
hos_mic_select(0, "", -1);
static uint64_t big[0x360 / 8];
static const uint32_t stereo[4] = {2, 2, 48000, 0};
if (CALL(imp__ZN2nn5audio15OpenInputStreamEPNS0_15InputStreamTypeEPvmRKNS0_15AudioDeviceNameERKNS0_12StreamFormatEi,
U(big), U(work), (uint64_t)sizeof work, U(names[0]), U(stereo), (uint64_t)0x20000001) != 0)
FAIL(41, "a stream of a huge frame count did not open");
START(big);
for (int k = 0; k < 12; k++) MS(1000), (void)FILLED(big); /* (silence comes a second at most a poll) */
if (FILLED(big) != 480000) FAIL(42, "a huge frame count: %d frames held, not 10 s", FILLED(big));
cases += 2;
printf("PASS: %d input stream cases (device list, open, start, silence, a shared microphone, capacity, a lost one and its"
" retries, a quiet one, named ones, a stand-in, stop, close)\n", cases);
return 0;
}
'''
MENU_FIXTURE = r"""#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
struct Opts { int mic = 1; } opts;
std::vector<std::string> names; /* the host's microphones */
struct Told { int mode; std::string name; int index; } told{-9, "", -9};
struct Host { int (*mic_count)(); const char* (*mic_name)(int); void (*mic_select)(int, const char*, int); };
int count() { return (int)names.size(); }
const char* name(int i) { return i >= 0 && i < (int)names.size() ? names[i].c_str() : ""; }
void select_mic(int mode, const char* n, int index) { told = {mode, n, index}; }
Host h{count, name, select_mic}, *host = &h;
struct Row { int* value; int hi; } rows[] = {{&opts.mic, 18}};
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); return 1; } } while (0)
@TEXT@
@REFRESH@
int main() {
names = {"USB Mic", "USB Mic", "Webcam"};
refresh_mics();
CHECK(rows[0].hi == 4 && opts.mic == 1 && mic_want.empty() && told.mode == 1);
opts.mic = 3, mic_chosen(); /* the second of two of one name */
CHECK(told.mode == 2 && told.name == "USB Mic" && told.index == 1);
refresh_mics();
CHECK(opts.mic == 3 && mic_want == "USB Mic" && told.index == 1); /* stays the second */
names = {"Webcam"}; /* unplugged: an entry of its own, the host told by name */
refresh_mics();
CHECK(opts.mic == 3 && rows[0].hi == 3 && std::strstr(tx_mic(3), "USB Mic (not connected)") && told.mode == 2 &&
told.name == "USB Mic" && told.index == -1);
names = {"Webcam", "USB Mic"}; /* back, elsewhere in the list */
refresh_mics();
CHECK(opts.mic == 3 && rows[0].hi == 3 && !std::strcmp(tx_mic(3), "USB Mic") && told.index == 1);
opts.mic = 1, mic_chosen(); /* the default chosen: the name goes */
refresh_mics();
CHECK(opts.mic == 1 && mic_want.empty() && !std::strcmp(tx_mic(1), "Default") && !std::strcmp(tx_mic(0), "Off") &&
told.mode == 1);
opts.mic = 0, mic_chosen();
CHECK(told.mode == 0);
names = {std::string(100, 'x'), "Webcam"}; /* a long name: kept short, matched by as much of it */
opts.mic = 2, mic_chosen();
CHECK(mic_want.size() == kMicNameKept && std::strlen("microphone_device=") + mic_want.size() + 1 < 80);
names = {"Webcam", std::string(100, 'x')};
refresh_mics();
CHECK(opts.mic == 3 && told.name == std::string(100, 'x') && told.index == 1); /* (the whole name to the host) */
names = {"Webcam"};
refresh_mics();
CHECK(told.name == std::string(60, 'x') && told.index == -1); /* (unplugged: the kept one, cut) */
names = {std::string(59, 'x') + "\xc3\xa9" + "yy"}; /* cut at a character's start, not inside the 2-byte one */
opts.mic = 2, mic_chosen();
CHECK(mic_want == std::string(59, 'x'));
std::puts("PASS: the Microphone row keeps its choice by name, and tells the host");
return 0;
}
"""
def block(text, start, end):
i = text.index(start)
return text[i:text.index(end, i)]
def through_function(text, start, last):
"""text from start to the end of the function that begins with last (its braces counted)"""
i = text.index(start)
j = text.index('{', text.index(last, i))
depth = 0
while True:
depth += text[j] == '{'
depth -= text[j] == '}'
j += 1
if depth == 0:
return text[i:j] + '\n'
def check(cond, what):
if not cond:
raise SystemExit("FAIL: " + what)
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--cc', default='gcc')
p.add_argument('--cxx', default='g++')
a = p.parse_args()
source = (ROOT / 'host' / 'nn_audio_in.c').read_text(encoding='utf-8')
sdl_h = (ROOT / 'host' / 'hos_sdl.h').read_text(encoding='utf-8')
# (the menu's row reaches the host: hos_mic_select, in the table it is given)
check('"Microphone", &opts.mic, 0,' in (ROOT / 'gpu' / 'vk' / 'vk_menu.cpp').read_text(encoding='utf-8'),
'the Insert menu has no Microphone row')
check(re.search(r'hos_mic_count,\s*hos_mic_name,\s*hos_mic_select', (ROOT / 'host' / 'rx_backend.c').read_text(encoding='utf-8')),
'the menu is not given the microphones')
check('if (!mic_want.empty()) refresh_mics();' in (ROOT / 'gpu' / 'vk' / 'vk_menu.cpp').read_text(encoding='utf-8'),
'the start lists the microphones (and starts SDL audio) even with none chosen')
with tempfile.TemporaryDirectory(prefix='audio-in-') as t:
folder = Path(t)
for name, text in (('hos.h', HOS_H), ('windows.h', WINDOWS_H), ('hos_sdl.h', sdl_h), ('nn_audio_in.c', source),
('fixture.c', FIXTURE)):
(folder / name).write_text(text, encoding='utf-8')
exe = folder / 'fixture'
subprocess.run([a.cc, '-std=gnu11', '-Wall', '-Wextra', '-Werror', '-I', str(folder), str(folder / 'fixture.c'),
'-o', str(exe)], check=True)
subprocess.run([str(exe)], check=True)
# (the menu's side: the row's choice by name, gpu/vk/vk_menu.cpp)
menu = (ROOT / 'gpu' / 'vk' / 'vk_menu.cpp').read_text(encoding='utf-8')
fixture = MENU_FIXTURE.replace('@TEXT@', through_function(menu, 'constexpr int kMaxMics', 'const char* tx_mic('))
fixture = fixture.replace('@REFRESH@', block(menu, 'void mic_tell_host()', 'void save()'))
(folder / 'menu.cpp').write_text(fixture, encoding='utf-8')
menu_exe = folder / 'menu'
subprocess.run([a.cxx, '-std=c++17', '-Wall', '-Wextra', '-Werror', str(folder / 'menu.cpp'), '-o', str(menu_exe)],
check=True)
subprocess.run([str(menu_exe)], check=True)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,55 @@
"""Compile the real output section with synthetic audio/device spies, without opening a device.
python -I -B tests/test_audio_output_errors.py [--cc clang]
"""
import argparse
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[1]
CASES = ("normal", "busy", "prepare", "write", "unprepare", "open", "bounded", "stall", "surround", "fallback")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cc", default="cc")
parser.add_argument("--source", type=Path, default=ROOT / "host/nn_audio.c")
parser.add_argument("--case", choices=CASES)
args = parser.parse_args()
process_flags = (subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS) if os.name == "nt" else 0
def run(command):
result = subprocess.run(command, creationflags=process_flags, capture_output=True,
text=True, encoding="utf-8", errors="replace")
print(result.stdout, end="")
print(result.stderr, end="", file=sys.stderr)
result.check_returncode()
source = args.source.read_text(encoding="utf-8")
definitions = []
for name in ("FRAME", "RATE"):
matches = re.findall(r"^#define " + name + r"\s+\d+[^\n]*", source, re.M)
assert len(matches) == 1, name
definitions.extend(matches)
start, end = source.index("#define NBLOCKS"), source.index("/* ---- parameter updates")
actual = "\n".join(definitions) + "\n" + source[start:end]
temporary = tempfile.TemporaryDirectory(prefix="audio-output-")
folder = Path(temporary.name).resolve()
try:
assert folder.parent == Path(tempfile.gettempdir()).resolve() and folder.name.startswith("audio-output-")
(folder / "audio_output.inc").write_text(actual, encoding="utf-8")
executable = folder / "fixture.exe"
run([args.cc, "-std=c11", "-Wall", "-Wextra", "-Werror", "-I" + str(folder),
str(ROOT / "tests/audio_output_errors_test.c"), "-o", str(executable)])
for case in ((args.case,) if args.case else CASES):
run([str(executable), case])
finally:
assert folder.parent == Path(tempfile.gettempdir()).resolve() and folder.name.startswith("audio-output-")
temporary.cleanup()
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,134 @@
"""The Insert menu's Audio output (Auto, Stereo, 5.1) reaches the speakers the game is told about (host/nn_audio.c).
The real au_speakers and au_menu_output from host/nn_audio.c and the menu's ini_key, tx_audio_output and hn_audio_output
from gpu/vk/vk_menu.cpp are compiled together: the fixture saves the row's choice under the key the menu would write, then
asks the host. No device or game is opened: the default output's channel count is a parameter of each case.
python -I -B tests/test_audio_output_menu.py [--cxx clang++]
"""
import argparse
import os
from pathlib import Path
import re
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = r"""
#include <cassert>
#include <cctype>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using DWORD = unsigned long;
constexpr int MAX_PATH = 260;
static DWORD hos_game_path_a(char *path, DWORD size) { return snprintf(path, size, "./hookshot.exe"); }
static char logged[256];
static void hos_log(const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(logged, sizeof logged, format, args);
va_end(args);
}
static void au_lock(void) {}
static void au_unlock(void) {}
static int device_channels, device_asked;
static int au_device_channels(void) { device_asked++; return device_channels; }
HOST
struct Row { int tab; const char* name; int* value; };
struct { int output_res, resolution, fps_cap, upscale, dlss, audio_output; } opts;
MENU
int main(int argc, char **argv) {
assert(argc == 7);
const char *saved = argv[2];
device_channels = atoi(argv[3]);
const int want = atoi(argv[4]), want_asked = atoi(argv[5]);
/* (plain values: the menu adds " (after restart)" to a restart row's label itself) */
assert(!strcmp(tx_audio_output(0), "Auto") && !strcmp(tx_audio_output(1), "Stereo"));
assert(!strcmp(tx_audio_output(2), "5.1") && !strcmp(tx_audio_output(3), "Auto") && !strcmp(tx_audio_output(-1), "Auto"));
for (int v = 0; v <= 2; v++) assert(hn_audio_output(v, -1) && hn_audio_output(v, 0) && !hn_audio_output(v, 1));
if (strcmp(saved, "-")) { /* (the lines as the menu's save writes them: the row's key, around others) */
const Row row = {1, argv[1], &opts.audio_output};
FILE *f = fopen("hookshot_options.ini", "w");
assert(f);
fprintf(f, "output_res=1\n%s=%s\nmicrophone=1\noptions_version=3\n", ini_key(row), saved);
fclose(f);
}
const int n = au_speakers();
assert(au_speakers() == n); /* (asked once: the second answer is the first) */
if (n != want || device_asked != want_asked || !strstr(logged, argv[6])) {
fprintf(stderr, "FAIL: saved %s, device %d: %d speakers (want %d), device asked %d (want %d), log \"%s\" (want \"%s\")\n",
saved, device_channels, n, want, device_asked, want_asked, logged, argv[6]);
return 1;
}
return 0;
}
"""
# (saved audio_output or "-" for no hookshot_options.ini, HOS_AUDIO_CHANNELS or None, the default output's channels,
# the speakers the game is told, whether the device was asked, what the log says)
CASES = (
("-", None, 6, 6, 1, "the default output has 6 channels"),
("-", None, 2, 2, 1, "the default output has 2 channels"),
("0", None, 8, 6, 1, "the default output has 8 channels"),
("0", None, 2, 2, 1, "the default output has 2 channels"),
("1", None, 6, 2, 0, "stereo for the game (the Insert menu's Audio output)"),
("2", None, 2, 6, 0, "5.1 for the game (the Insert menu's Audio output)"),
("3", None, 2, 6, 0, "5.1 for the game (the Insert menu's Audio output)"), # (the menu shows a hand-edited 3 as 5.1)
("-1", None, 6, 6, 1, "the default output has 6 channels"),
("2", "2", 6, 2, 0, "stereo for the game (HOS_AUDIO_CHANNELS)"),
("1", "6", 2, 6, 0, "5.1 for the game (HOS_AUDIO_CHANNELS)"),
("-", "6", 2, 6, 0, "5.1 for the game (HOS_AUDIO_CHANNELS)"),
)
def between(text, start, end):
i = text.index(start)
return text[i:text.index(end, i)]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
audio = (ROOT / "host/nn_audio.c").read_text(encoding="utf-8")
menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8")
# (tab, name, value, lo, hi, step, text, help, then hint, restart: read once at the start, preset_exempt)
row = re.search(r'\{TAB_(\w+), "([^"]+)", &opts\.audio_output, (-?\d+), (-?\d+), (\d+), (\w+),\s*"[^{}]*?", '
r'(\w+), (true|false), (true|false)\},', menu)
assert row, "the Insert menu has no Audio output row"
tab, name, lo, hi, step, text, hint, restart, exempt = row.groups()
assert (tab, lo, hi, step, text, hint, restart, exempt) == \
("AUDIO", "0", "2", "1", "tx_audio_output", "hn_audio_output", "true", "true"), row.groups()
host = between(audio, "/* (hookshot) The Insert menu's Audio output", "/* The device's format,")
hints = re.search(r'MENU_HINTS\(hn_audio_output,.*?\)\n(?=\S)', menu, re.S)
assert hints, "no MENU_HINTS(hn_audio_output, ...)"
menu_part = between(menu, "/// A row's hints from a table", "const char* tx_framegen(") + hints.group(0) + \
between(menu, "const char* tx_audio_output(int v) {", "const char* tx_robust(") + \
between(menu, "const char* ini_key(const Row& r) {", "// (hookshot) the controls, kept by the host")
with tempfile.TemporaryDirectory(prefix="audio-output-menu-") as tmp:
work = Path(tmp)
source, exe = work / "fixture.cpp", work / "fixture.exe"
source.write_text(FIXTURE.replace("HOST", host).replace("MENU", menu_part), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++17", "-Wall", "-Wextra", "-Werror", str(source), "-o", str(exe)], check=True)
for saved, env_channels, device, want, asked, log in CASES:
ini = work / "hookshot_options.ini"
if ini.exists():
ini.unlink()
env = os.environ.copy()
env.pop("HOS_AUDIO_CHANNELS", None)
if env_channels is not None:
env["HOS_AUDIO_CHANNELS"] = env_channels
subprocess.run([str(exe), name, saved, str(device), str(want), str(asked), log], cwd=work, env=env, check=True)
print("PASS: Audio output: a restart row; Auto follows the device, Stereo and 5.1 are told to the game, HOS_AUDIO_CHANNELS "
"over them")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,78 @@
"""Compile actual voice-filter setters, queued updates and the render_voice biquad loop.
Inputs and sample buffers are authored. No game, SDK, output device or resampler
is used. --source can select the prior release: its missing float import calls
the actual public runtime fallback, then fails the same queued-filter invariant.
"""
import argparse
import importlib.util
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
RUN_ALL_ARGS = ["--cc", "gcc"]
ROOT = Path(__file__).resolve().parents[1]
FLOAT = "_ZN2nn5audio29SetVoiceBiquadFilterParameterEPNS0_9VoiceTypeEiRKNS0_22TBiquadFilterParameterIfEE"
LEGACY = "_ZN2nn5audio29SetVoiceBiquadFilterParameterEPNS0_9VoiceTypeEiRKNS0_21BiquadFilterParameterE"
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cc", default="cc")
parser.add_argument("--source", type=Path, default=ROOT / "host/nn_audio.c")
args = parser.parse_args()
spec = importlib.util.spec_from_file_location("audio_block_helper", ROOT / "tests/test_audio_deadline.py")
helper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helper)
source = args.source.read_text(encoding="utf-8")
constants = []
for name in ("FRAME", "RATE", "AU_MAGIC", "MAX_VOICES", "MAX_MIXES", "MAX_SPLITTERS", "MAX_SPLIT_DESTS",
"MAX_CH", "MAX_BUF", "MAX_WB", "MAX_REL"):
matches = re.findall(r"^#define " + name + r"\s+[^\n]+", source, re.M)
assert len(matches) == 1, name
constants.extend(matches)
types = source[source.index("enum { K_VOICE"):source.index("static CRITICAL_SECTION lock;")]
queue = source[source.index("enum { OP_VOICE_RELEASE"):source.index("static int64_t last_update_ns;")]
runtime = (ROOT / "third_party/a64-kit/rt/a64_rt.c").read_text(encoding="utf-8")
actual = ["\n".join(constants), types, """
static Voice voices[MAX_VOICES];
static Mix mixes[MAX_MIXES];
static Splitter splitters[MAX_SPLITTERS];
static int sink_mix = -1;
static void release_wb(Voice *voice, uint64_t buffer) { (void)voice; (void)buffer; }
"""]
actual.extend(helper.block(source, marker) for marker in (
"static void put_handle(", "static int get_handle(", "static int voice_index(", "static Voice *voice_of("))
actual.extend([queue, helper.block(source, "static void apply("), helper.block(source, "static void submit("),
helper.block(source, "static void commit("), helper.block(runtime, "A64_WEAK void a64_unimplemented(")])
actual.append(helper.block(source, "HLE(" + LEGACY + ")"))
try:
actual.append(helper.block(source, "HLE(" + FLOAT + ")"))
except ValueError:
# An authored import dispatch for the absent HLE, using the real runtime fallback (not a replacement DSP/setter).
actual.append('HLE(' + FLOAT + ') { static int seen; a64_unimplemented(c, "' + FLOAT + '", &seen); }')
render = helper.block(source, "static void render_voice(")
loop = helper.block(render, "for (int f = 0; f < 2; f++)")
actual.append("static void filter_samples(Voice *v, float out[MAX_CH][FRAME]) {\n" + loop + "\n}")
flags = subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
with tempfile.TemporaryDirectory(prefix="audio-voice-biquad-") as temporary:
folder = Path(temporary)
(folder / "actual_audio_biquad.inc").write_text("\n\n".join(actual), encoding="utf-8")
executable = folder / "fixture.exe"
command = [args.cc, "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-I" + str(folder),
str(ROOT / "tests/audio_voice_biquad_test.c"), "-lm", "-o", str(executable)]
for command in (command, [str(executable)]):
result = subprocess.run(command, creationflags=flags, capture_output=True, text=True,
encoding="utf-8", errors="replace", timeout=30)
print(result.stdout, end="")
print(result.stderr, end="", file=sys.stderr)
if result.returncode:
raise SystemExit(result.returncode)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,75 @@
"""Compile the actual wave-buffer calls, queued updates and the mixer's buffer consumption around an authored pass.
Ocarina's sound thread feeds six mono voices one after another: it reads each one's GetWaveBufferCount and appends
when one is short. The mixer finishing a buffer between two of those reads made one voice look short, the game
appended it an extra buffer, then one more each pass, until its queue was full and that speaker went silent. A
finished buffer must reach the game at the next RequestUpdateAudioRenderer, as on the console, never in between.
No game, SDK or output device is used. --source can select another nn_audio.c (the prior release fails the check).
"""
import argparse
import importlib.util
import os
from pathlib import Path
import re
import subprocess
import sys
import tempfile
ROOT = Path(__file__).resolve().parents[1]
APPEND = "_ZN2nn5audio16AppendWaveBufferEPNS0_9VoiceTypeEPKNS0_10WaveBufferE"
RELEASED = "_ZN2nn5audio21GetReleasedWaveBufferEPNS0_9VoiceTypeE"
COUNT = "_ZN2nn5audio18GetWaveBufferCountEPKNS0_9VoiceTypeE"
STATE = "_ZN2nn5audio17SetVoicePlayStateEPNS0_9VoiceTypeENS1_9PlayStateE"
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cc", default="cc")
parser.add_argument("--source", type=Path, default=ROOT / "host/nn_audio.c")
args = parser.parse_args()
spec = importlib.util.spec_from_file_location("audio_block_helper", ROOT / "tests/test_audio_deadline.py")
helper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(helper)
source = args.source.read_text(encoding="utf-8")
constants = []
for name in ("FRAME", "RATE", "AU_MAGIC", "MAX_VOICES", "MAX_MIXES", "MAX_SPLITTERS", "MAX_SPLIT_DESTS",
"MAX_CH", "MAX_BUF", "MAX_WB", "MAX_REL"):
matches = re.findall(r"^#define " + name + r"\s+[^\n]+", source, re.M)
assert len(matches) == 1, name
constants.extend(matches)
types = source[source.index("enum { K_VOICE"):source.index("static CRITICAL_SECTION lock;")]
queue = source[source.index("enum { OP_VOICE_RELEASE"):source.index("static int64_t last_update_ns;")]
actual = ["\n".join(constants), types, """
static Voice voices[MAX_VOICES];
static Mix mixes[MAX_MIXES];
static Splitter splitters[MAX_SPLITTERS];
static int sink_mix = -1;
/* (the samples are not looked at here: only which buffer the mixer is on) */
static void source_frame(Voice *v, const WB *w, int64_t s, float *out) { (void)v; (void)w; (void)s; out[0] = 0; }
static void adpcm_seek(Voice *v, const WB *w) { (void)v; (void)w; }
static int16_t adpcm_sample(Voice *v, const WB *w, int64_t s) { (void)v; (void)w; (void)s; return 0; }
"""]
actual.extend(helper.block(source, marker) for marker in (
"static void put_handle(", "static int get_handle(", "static int voice_index(", "static Voice *voice_of(",
"static WB read_wb(", "static void release_wb(", "static int next_source_frame("))
actual.extend([queue, helper.block(source, "static void apply("), helper.block(source, "static void submit("),
helper.block(source, "static void commit(")])
actual.extend(helper.block(source, "HLE(" + name + ")") for name in (APPEND, RELEASED, COUNT, STATE))
flags = subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
with tempfile.TemporaryDirectory(prefix="audio-wave-buffer-release-") as temporary:
folder = Path(temporary)
(folder / "actual_audio_wave_buffers.inc").write_text("\n\n".join(actual), encoding="utf-8")
executable = folder / "fixture.exe"
command = [args.cc, "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-I" + str(folder),
str(ROOT / "tests/audio_wave_buffer_release_test.c"), "-lm", "-o", str(executable)]
for command in (command, [str(executable)]):
result = subprocess.run(command, creationflags=flags, capture_output=True, text=True,
encoding="utf-8", errors="replace", timeout=30)
print(result.stdout, end="")
print(result.stderr, end="", file=sys.stderr)
if result.returncode:
raise SystemExit(result.returncode)
if __name__ == "__main__":
main()

60
tests/test_bank_gen.py Normal file
View File

@@ -0,0 +1,60 @@
"""A graphics stage's driver banks are kept by the generation of the bindings they are made from.
c2 of a graphics stage holds its texture, sampler and image handles and c0 its storage buffer table, so a uniform buffer's bind (nearly every
draw) does not make the bank stale: tex_gen[stage] and ssbo_gen[stage] are what bind_gen was when those last changed. Compute, task and
mesh stages (c0 holds a grid, push constants, the pointer table) stay on bind_gen.
python -I -B tests/test_bank_gen.py
"""
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def main():
vk = ROOT / "gpu" / "vk"
bank = (vk / "vk_bank.cpp").read_text(encoding="utf-8")
replay = (vk / "vk_replay.cpp").read_text(encoding="utf-8")
draw = (vk / "vk_draw.cpp").read_text(encoding="utf-8")
fill = block(bank, "void driver_bank_fill(")
c2 = block(fill, "if (bank == 2) {")
if set(re.findall(r"\bs\.(\w+)", c2)) - {"tex", "img", "samp"}:
raise SystemExit("c2 is made from more than tex_gen covers: " + str(set(re.findall(r"\bs\.(\w+)", c2))))
tail = fill[fill.index("static constexpr uint32_t kSsboTable"):]
if set(re.findall(r"\bs\.(\w+)", tail)) - {"ssbo"} or "pointer_table(s, stage)" not in block(tail, "if ((stage == 6 || stage == 7)"):
raise SystemExit("a graphics stage's c0 is made from more than ssbo_gen covers")
if "stage == 5" not in fill[: fill.index("static constexpr uint32_t kSsboTable")]:
raise SystemExit("compute's c0 is no longer apart from the graphics stages'")
# (every place the handles or storage buffers change moves their generation with bind_gen)
for marker, field in (("case NOP_BIND_TEX:", "tex_gen"), ("case NOP_SET_TEX_POOL:", "tex_gen")):
at = replay.index(marker)
if field not in replay[at: at + 1400]:
raise SystemExit(f"{marker} does not set {field}")
if replay.count("ssbo_gen[b.stage] = s.bind_gen") != 2:
raise SystemExit("a storage buffer bind does not set ssbo_gen (NOP_BIND_BUFS and NOP_BIND_SSBO)")
if "s->tex_gen[k] = gen_base, s->ssbo_gen[k] = gen_base" not in block(replay, "void use_queue_state("):
raise SystemExit("another state's generations are not made afresh with bind_gen")
for name in ("s.tex[", "s.samp[", "s.img[", "s.ssbo["):
stores = [m for m in re.finditer(re.escape(name) + r"[^;\n]*\]\s*=[^=]", replay)]
if stores:
raise SystemExit(f"{name} is stored to outside the bind handlers' arrays")
uniform = block(draw, "VkDescriptorBufferInfo uniform_range(")
if "bank_stage > 4 ? s.bind_gen" not in uniform or "RX_VK_BANK_GEN_OFF" not in uniform:
raise SystemExit("compute, task and mesh banks are not kept by bind_gen, or the switch is missing")
if "c.bind_gen = gen;" not in uniform or "c.bind_gen == gen" not in uniform:
raise SystemExit("the bank cache is not kept and checked by the same generation")
print("PASS: a graphics stage's banks are kept by the generation of the bindings they are made from")
if __name__ == "__main__":
main()

32
tests/test_bc_levels.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include "../gpu/vk/bc_levels.h"
#include <cstdio>
#define CHECK(name, condition) do { if (!(condition)) { std::fprintf(stderr, "FAIL %s at line %d\n", name, __LINE__); return 1; } } while (0)
int main() {
using namespace mkwvk::bc_levels;
CHECK("blocks round up", blocks(1) == 1 && blocks(4) == 1 && blocks(5) == 2 && blocks(1365) == 342);
CHECK("powers of two are even", first_uneven(2048, 2048, 12) == 12 && first_uneven(512, 256, 10) == 10);
CHECK("1365 goes uneven at level 2", first_uneven(1365, 1365, 11) == 2);
CHECK("683 goes uneven at level 1", first_uneven(683, 683, 10) == 1);
CHECK("768 only at its 6-texel level", first_uneven(768, 768, 10) == 7);
CHECK("1536 only at its 6-texel level", first_uneven(1536, 1536, 11) == 8);
CHECK("either side can be uneven", first_uneven(1024, 1365, 11) == 2);
CHECK("1365 -> 1536", even_size(1365, 11) == 1536);
CHECK("683 -> 768", even_size(683, 10) == 768);
CHECK("768 and 1536 stay", even_size(768, 10) == 768 && even_size(1536, 11) == 1536);
CHECK("powers of two stay", even_size(2048, 12) == 2048 && even_size(256, 9) == 256);
CHECK("small sizes", even_size(171, 8) == 192 && even_size(12, 4) == 12);
CHECK("an even size is even down to 16 texels", first_uneven(even_size(1365, 11), even_size(1365, 11), 11) >= 8);
for (uint32_t w = 16; w <= 2048; w++) {
uint32_t levels = 1;
while ((w >> levels) > 0) levels++;
const uint32_t e = even_size(w, levels);
uint32_t big = 0;
while (big < levels && (e >> big) >= 16) big++;
CHECK("every size halves evenly down to 16 texels", e >= w && first_uneven(e, e, levels) >= big);
}
std::puts("bc levels: all checks passed");
}

22
tests/test_bc_levels.py Normal file
View File

@@ -0,0 +1,22 @@
"""Compile and run the block-compressed level helper (no game/GPU required)."""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
with tempfile.TemporaryDirectory(prefix="bc-levels-") as tmp:
exe = Path(tmp) / "bc-levels"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror",
str(ROOT / "tests/test_bc_levels.cpp"), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,71 @@
"""A BC texture can be rendered and cleared through a raw (one texel a block) view, sized in blocks, and OoT's noise pass runs
where those views can be made.
OoT renders the slices of two 64^3 BC4 noise volumes as RGBA16UI targets while an area loads (program 53509044401d78d2).
BC images had no colour attachment usage, so those views were not made; the pass ran over no target at the texels' 64 x 64,
and it was left out by default after it lost the device on first starts. The volumes stayed zero and the foliage's dithered
LOD crossfade, which reads them, left trees bare at mid distance (issue 1151).
python -I -B tests/test_bc_raw_targets.py
"""
from pathlib import Path
import re
import unittest
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
class BcRawTargetTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tex = (ROOT / "gpu" / "vk" / "vk_texture.cpp").read_text(encoding="utf-8")
cls.draw = (ROOT / "gpu" / "vk" / "vk_draw.cpp").read_text(encoding="utf-8")
cls.xfer = (ROOT / "gpu" / "vk" / "vk_xfer.cpp").read_text(encoding="utf-8")
def test_bc_images_take_colour_attachment_usage_asked_with_the_3d_flags(self):
raw = self.tex.index("Raw storage views address one compressed block per texel")
branch = self.tex[raw:self.tex.index("CUBE_COMPATIBLE_BIT", raw)]
self.assertIn("VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT", branch)
self.assertRegex(branch, r"x\.usage \|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;")
self.assertIn("VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT | (dev.view_2d_of_3d", branch)
def test_a_refused_bc_image_is_made_again_without_the_attachment_usage_first(self):
fallback = self.tex.index("// (a compressed image refused:")
begin = self.tex.rfind("} else if (", 0, fallback)
self.assertGreaterEqual(begin, 0)
chain = self.tex[begin:]
chain = chain[:chain.index("logf(\"vk: image %ux%ux%u")]
self.assertIn("!create_image(ci, x.image, x.allocation", chain)
attach = chain.index("ci.usage &= ~VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT")
storage = chain.index("ci.usage &= ~VK_IMAGE_USAGE_STORAGE_BIT")
self.assertLess(attach, storage)
def test_raw_views_of_bc_targets_are_sized_in_blocks(self):
targets = block(self.draw, "Targets targets(NvnState& s)")
self.assertRegex(targets, r"if \(bc\(tex->format\) && !bc\(f\)\) w = \(w \+ 3\) / 4, h = \(h \+ 3\) / 4;")
self.assertLess(targets.index("w = (w + 3) / 4"), targets.index("at.width = at.width ?"))
def test_clears_through_raw_views_are_sized_in_blocks(self):
clear = block(self.xfer, "void clear_tex(")
self.assertIn("raw ? (lw + 3) / 4 : lw", clear)
self.assertIn("ri.renderArea = {{0, 0}, {aw, ah}};", clear)
self.assertIn("const VkClearRect rect{{{rx, ry}, {rw, rh}}, 0, 1};", clear)
def test_the_noise_pass_is_left_out_only_while_its_targets_have_no_view(self):
line = re.search(r'const char\* list = .*RX_VK_SKIP_PROG.*;', self.draw).group(0)
self.assertNotIn("53509044401d78d2", line)
self.assertRegex(self.draw, r"default_list && cp->key == 0x53509044401d78d2ull && !at\.n")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,41 @@
"""A BC3 texture whose upload failed is tried again, not drawn as it was left.
Decoded ASTC is re-encoded to BC3 on the GPU, which needs staging memory for the ASTC blocks and the BC3 scratch. With the
VRAM full (a 4 GB card after an alt-tab: the textures evicted and made again) those could not be had, the upload failed,
and the texture was marked uploaded anyway: its uninitialized blocks were drawn as coloured noise and never made again
unless the game rewrote its memory. Now it stays not uploaded and is tried again 30 frames on, and the log says why.
python -I -B tests/test_bc_upload_retry.py
"""
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
def main():
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8")
header = (VK / "vk_texture.h").read_text(encoding="utf-8")
if "uint64_t bc_retry_frame = 0;" not in header:
raise SystemExit("a texture has no frame to try a failed BC3 upload again from")
failed = tex[tex.index("} else if (tex->bc) {"):]
failed = failed[:failed.index("} else {")]
if "tex->uploaded = true" in failed or "tex->bc_retry_frame = frame_no + 30;" not in failed:
raise SystemExit("a BC3 texture whose upload failed is marked uploaded (drawn as noise, never made again)")
if "if (tex->bc && !tex->uploaded && tex->bc_retry_frame > frame_no) return tex;" not in tex:
raise SystemExit("a failed BC3 upload is tried again every submission (it stages the whole ASTC source)")
upload = tex[tex.index("bool upload_astc_gpu(Texture& tex, uint64_t total, const Ranges* dirty) {"):]
upload = upload[:upload.index("\n}\n")]
if upload.count("return false;") != upload.count("astc_fail = \"") or "no memory for the BC3 scratch" not in upload:
raise SystemExit("a failed ASTC upload does not say why")
# (the scratch's output buffer too: only in VRAM it could not be made with the VRAM full, and every BC3 texture made
# again then failed, "no memory for the BC3 scratch" in the log)
scratch = tex[tex.index("bool bc_scratch_make() {"):]
scratch = scratch[:scratch.index("\n}\n")]
if "create_buffer(out, out_usage, false, bc_out, bc_out_alloc, nullptr, !pool)" not in scratch:
raise SystemExit("the BC3 scratch's output buffer cannot go to system memory when the VRAM is full")
print("failed BC3 uploads are tried again, with their cause logged")
if __name__ == "__main__":
main()

98
tests/test_bind_memo.py Normal file
View File

@@ -0,0 +1,98 @@
"""A repeated read-only buffer bind skips the pool lookup.
Draws bind the same guest ranges again: vertex streams, an index buffer, indirect
arguments, uniform buffers, storage-buffer entries. The binding stands until a pool
leaves, a mirror is made, or the next submission starts.
python -I -B tests/test_bind_memo.py [--cxx g++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
bind = block(source, "BufferRef gpu_buffer_bind(")
lock = bind.index("ParLock")
if bind.index("bind_memo_hit(") > lock:
raise SystemExit("a read bind takes the pool lock before checking the remembered one")
if "if (!written)" not in bind[: bind.index("bind_memo_hit(")]:
raise SystemExit("a written bind can be served from the remembered read")
store = bind.index("bind_memo[bind_slot(guest, size)] =")
if "gpu_bind_stamp()" not in bind[store : store + 160]:
raise SystemExit("a remembered bind keeps the stamp from before the lookup")
if "if (!p) return {};" not in bind:
raise SystemExit("a miss is remembered")
stamp = block(source, "uint64_t gpu_bind_stamp(")
for needle in ("pools_gen", "mirror_epoch"):
if needle not in stamp:
raise SystemExit(f"a remembered bind ignores {needle}")
made = block(source, "bool mirror_make(Pool& pool) {")
on = made.index("p->mstate = Pool::MIRROR_ON;")
nxt = made.index("p->synced_gen", on)
if "pools_gen" not in made[on:nxt]:
raise SystemExit("a new mirror does not drop remembered bindings")
slot = block(source, "uint32_t bind_slot(")
hit = block(source, "bool bind_memo_hit(")
fixture = f'''#include <cstdint>
#include <cstdio>
#include <cstdlib>
#define CHECK(x) do {{ if (!(x)) {{ std::fprintf(stderr, "FAIL:%d %s\\n", __LINE__, #x); std::exit(1); }} }} while (0)
{slot}
{hit}
int main() {{
CHECK(bind_slot(0x1000, 64) == bind_slot(0x1000, 64));
CHECK(bind_slot(0x1000, 64) != bind_slot(0x1000, 128));
CHECK(bind_slot(0, 64) != bind_slot(1ull << 40, 64));
int seen[256] = {{}};
int n = 0;
for (uint64_t i = 0; i < 64; i++) {{
const uint32_t s = bind_slot(i << 12, 64);
CHECK(s < 256);
if (!seen[s]) n++, seen[s] = 1;
}}
CHECK(n > 16);
int near[256] = {{}};
int n_near = 0;
for (uint64_t i = 0; i < 32; i++) {{
const uint32_t s = bind_slot(0x100000000ull + i * 16, 64);
CHECK(s < 256);
if (!near[s]) n_near++, near[s] = 1;
}}
CHECK(n_near > 8);
CHECK(bind_memo_hit(0x1000, 64, 7, 0x1000, 64, 7, 1));
CHECK(bind_memo_hit(0, 16, 1, 0, 16, 1, 1));
CHECK(!bind_memo_hit(0x1000, 64, 7, 0x1000, 64, 8, 1));
CHECK(!bind_memo_hit(0x1000, 32, 7, 0x1000, 64, 7, 1));
CHECK(!bind_memo_hit(0x2000, 64, 7, 0x1000, 64, 7, 1));
CHECK(!bind_memo_hit(0x1000, 64, 7, 0x1000, 64, 7, 0));
CHECK(!bind_memo_hit(0, 0, 1, 0, 0, 1, 1));
std::printf("PASS: a read bind is reused only for that range and stamp\\n");
}}
'''
with tempfile.TemporaryDirectory(prefix="bind-memo-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / "fixture"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

151
tests/test_bound_keys.py Normal file
View File

@@ -0,0 +1,151 @@
"""The keys-only controls (the left stick's and the camera's directions) and the mouse's buttons are bindings like
the Switch's buttons (host/nn_hid.c).
host/nn_hid.c's binding table and its bound_down are compiled alone, against fake key states and a fake
hookshot_options.ini: the defaults (WASD, the arrows, the mouse's left button Y and right button ZL), a mouse button
only while the mouse is captured and only once the click that captured it was let go, key_move_up from the file, a
pad_ line for a keys-only control ignored, a key taken from the control that had it, and Enter with Alt still no key.
python -I -B tests/test_bound_keys.py (needs a C compiler)
"""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
while text[end] in ";\n":
end += 1
if text[end - 1] == "\n":
break
return text[start:end]
def between(text, first, last):
"""from the line that holds first to the end of the block or statement that starts at last"""
start = text.rindex("\n", 0, text.index(first)) + 1
return text[start:text.index(last)] + block(text, last)
PRELUDE = r"""
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "vk_api.h"
typedef long LONG;
typedef unsigned long DWORD;
#define VK_LBUTTON 0x01
#define VK_RBUTTON 0x02
#define VK_MBUTTON 0x04
#define VK_XBUTTON1 0x05
#define VK_XBUTTON2 0x06
#define VK_TAB 0x09
#define VK_RETURN 0x0D
#define VK_SHIFT 0x10
#define VK_CONTROL 0x11
#define VK_MENU 0x12
#define VK_SPACE 0x20
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define MAX_PATH 260
#define NULL_MODULE 0
static LONG InterlockedExchange(volatile LONG *p, LONG v) { LONG o = *p; *p = v; return o; }
static LONG InterlockedCompareExchange(volatile LONG *p, LONG v, LONG c) { LONG o = *p; if (o == c) *p = v; return o; }
static void Sleep(int ms) { (void)ms; }
static void hos_log(const char *f, ...) { (void)f; }
static const char *ini_dir;
/* (the separator the system's own path has: nn_hid.c looks for a backslash first, so a mixed path finds the wrong folder) */
#ifdef _WIN32
#define EXE_SEP "\\"
#else
#define EXE_SEP "/"
#endif
static DWORD GetModuleFileNameA(void *m, char *b, DWORD n) { (void)m; return (DWORD)snprintf(b, n, "%s" EXE_SEP "hookshot.exe", ini_dir); }
#define hos_game_path_a(b,n) GetModuleFileNameA(0,b,n)
static int held[256];
static int key(int vk) { return held[vk & 255]; }
"""
MAIN = r"""
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
static void release(void) { memset(held, 0, sizeof held); }
int main(int argc, char **argv) {
(void)argc;
ini_dir = argv[1];
binds_load();
CHECK(hos_binding(HOS_BIND_KEY, HOS_KEY_MOVE_UP, 0) == 'Z'); /* from the file */
CHECK(hos_binding(HOS_BIND_KEY, HOS_KEY_MOVE_LEFT, 0) == 'A'); /* the default */
CHECK(hos_binding(HOS_BIND_PAD, HOS_KEY_MOVE_UP, 0) == -1); /* no gamepad input for a keys-only control */
CHECK(hos_binding(HOS_BIND_KEY, HOS_SW_Y, 0) == VK_LBUTTON && hos_binding(HOS_BIND_KEY, HOS_SW_ZL, 1) == VK_RBUTTON);
release(), held['Z'] = 1;
CHECK(bound_down(HOS_KEY_MOVE_UP, 0));
release(), held[VK_LEFT] = 1;
CHECK(bound_down(HOS_KEY_CAM_LEFT, 0));
/* the mouse's left button: held when the capture begins, it counts only after it was let go */
release(), held[VK_LBUTTON] = 1;
mouse_buttons_arm(1);
CHECK(!bound_down(HOS_SW_Y, 1));
release(), mouse_buttons_arm(1), held[VK_LBUTTON] = 1, mouse_buttons_arm(1);
CHECK(bound_down(HOS_SW_Y, 1));
CHECK(!bound_down(HOS_SW_Y, 0)); /* not while the mouse is not captured */
mouse_buttons_arm(0);
CHECK(!bound_down(HOS_SW_Y, 1)); /* a new capture arms again */
/* a key bound to a keys-only control is taken from the button that had it */
hos_bind(HOS_BIND_KEY, HOS_KEY_CAM_UP, 0, 'E');
CHECK(hos_binding(HOS_BIND_KEY, HOS_SW_A, 0) == 0 && hos_binding(HOS_BIND_KEY, HOS_KEY_CAM_UP, 0) == 'E');
hos_bind(HOS_BIND_PAD, HOS_KEY_CAM_UP, 0, HOS_PAD_SOUTH); /* no gamepad input for it: ignored */
CHECK(hos_binding(HOS_BIND_PAD, HOS_SW_B, 0) == HOS_PAD_SOUTH);
/* Enter with Alt is fullscreen, not Plus */
release(), held[VK_RETURN] = held[VK_MENU] = 1;
CHECK(!bound_down(HOS_SW_PLUS, 0));
hos_bind(HOS_BIND_KEY, -1, 0, 0);
CHECK(hos_binding(HOS_BIND_KEY, HOS_KEY_MOVE_UP, 0) == 'W' && hos_binding(HOS_BIND_KEY, HOS_SW_A, 0) == 'E');
if (!fails) printf("PASS\n");
return fails != 0;
}
"""
class BoundKeysTests(unittest.TestCase):
def test_bindings(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
hid = (ROOT / "host" / "nn_hid.c").read_text(encoding="utf-8")
code = [block(hid, "static int kmap_down("),
between(hid, "static const char *const bind_names[HOS_KEY_N]", "void hos_bind("),
between(hid, "static int mouse_vk_slot(", "static int bound_down(")]
code = "\n".join(code).replace("GetModuleFileNameA(NULL,", "GetModuleFileNameA(0,")
read = block(hid, "static void read_input(")
for needle in ("bound_down(HOS_KEY_MOVE_UP,", "bound_down(HOS_KEY_CAM_RIGHT,", "mouse_buttons_arm(", "bound_down(i, 1)"):
self.assertIn(needle, read, "read_input does not read the bound keys: " + needle)
self.assertNotIn("key('W')", read, "read_input still reads WASD itself")
with tempfile.TemporaryDirectory(prefix="hookshot-bound-keys-") as tmp:
tmp = Path(tmp)
(tmp / "hookshot_options.ini").write_text("key_move_up=90,0\npad_move_up=0\n", encoding="utf-8")
(tmp / "t.c").write_text(PRELUDE + code + MAIN, encoding="utf-8")
binary = tmp / ("t.exe" if sys.platform == "win32" else "t")
build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wno-unused-function", "-I", str(ROOT / "gpu" / "vk"),
"-I", str(ROOT / "gpu"), str(tmp / "t.c"), "-o", str(binary)], text=True, capture_output=True)
self.assertEqual(build.returncode, 0, build.stderr)
run = subprocess.run([str(binary), str(tmp)], text=True, capture_output=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

155
tests/test_build_log.py Normal file
View File

@@ -0,0 +1,155 @@
"""CLI build logs preserve live output and failures without a dump or compiler."""
import contextlib
import importlib.util
import io
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
SOURCE = Path(__file__).resolve().parents[1] / "nx2" / "make_game.py"
spec = importlib.util.spec_from_file_location("make_game_under_test", SOURCE)
make_game = importlib.util.module_from_spec(spec)
spec.loader.exec_module(make_game)
class BuildLogTests(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory(prefix="hookshot-log-")
self.addCleanup(temporary.cleanup)
self.root = Path(temporary.name)
self.log = self.root / "build log \u6f22.txt"
def invoke(self, action, log=True):
argv = [str(SOURCE), "--jobs", "1"]
if log:
argv += ["--log", str(self.log)]
stdout, stderr = io.StringIO(), io.StringIO()
with (mock.patch.object(sys, "argv", argv),
contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr),
mock.patch.object(make_game, "main", side_effect=action) as build):
code = 0
try:
make_game.cli()
except SystemExit as e:
code = e.code
self.assertIs(sys.stdout, stdout)
self.assertIs(sys.stderr, stderr)
return code, stdout.getvalue(), stderr.getvalue(), build
def child(self, code):
return make_game.PY + ["-c", code]
def test_live_output_unicode_and_child_streams(self):
self.log.write_text("stale run", encoding="utf-8")
def build(args):
self.assertEqual(args.log, str(self.log))
make_game.say("stage", "compile", "Compiling")
print("parent warning \u6f22", file=sys.stderr)
self.assertIn("parent warning \u6f22", self.log.read_text(encoding="utf-8"))
def on_line(line):
# The log is readable before the step (and the build) has finished.
self.assertIn(line, self.log.read_text(encoding="utf-8"))
make_game.run(self.child("import sys; print('child out \u6f22'); print('child err', file=sys.stderr)"),
on_line=on_line)
make_game.say("done", "fixture")
code, stdout, stderr, _ = self.invoke(build)
self.assertEqual(code, 0, stdout + stderr)
text = self.log.read_text(encoding="utf-8")
for line in ("@@stage compile Compiling", "child out \u6f22", "child err", "@@done fixture"):
self.assertIn(line, stdout)
self.assertIn(line, text)
self.assertEqual(stderr, "parent warning \u6f22\n")
self.assertNotIn("parent warning", stdout)
self.assertNotIn("stale run", text)
self.assertNotIn(b"\r\n", self.log.read_bytes())
def test_failed_child_keeps_error_and_nonzero_exit(self):
def build(args):
make_game.run(self.child("import sys; print('error: authored failure', file=sys.stderr); sys.exit(7)"))
self.fail("the failed step must stop the build")
code, stdout, stderr, _ = self.invoke(build)
self.assertEqual(code, 1)
self.assertIn("error: authored failure", stdout)
self.assertIn("@@error ", stdout)
self.assertEqual(stderr, "")
self.assertEqual(self.log.read_text(encoding="utf-8"), stdout)
def test_nonfatal_shader_output_stays_on_stderr_and_is_logged(self):
def build(args):
code = make_game.run(self.child("import sys; print('shader out'); print('shader err', file=sys.stderr); sys.exit(3)"),
check=False, output=sys.stderr)
self.assertEqual(code, 3)
make_game.say("done", "fixture")
code, stdout, stderr, _ = self.invoke(build)
self.assertEqual(code, 0, stdout + stderr)
self.assertEqual(stdout, "@@done fixture\n")
self.assertIn("shader out", stderr)
self.assertIn("shader err", stderr)
self.assertEqual(self.log.read_text(encoding="utf-8"), stderr + stdout)
def test_unexpected_exception_includes_traceback_and_error(self):
def build(args):
raise RuntimeError("authored exception")
code, stdout, stderr, _ = self.invoke(build)
self.assertEqual(code, 1)
self.assertIn("Traceback", stdout)
self.assertIn("RuntimeError: authored exception", stdout)
self.assertIn("@@error ", stdout)
self.assertEqual(self.log.read_text(encoding="utf-8"), stdout + stderr)
def test_system_exit_message_is_logged(self):
def build(args):
raise SystemExit("authored toolchain failure")
code, stdout, stderr, _ = self.invoke(build)
self.assertEqual(code, 1)
# (said in the builder's protocol, as #434 has it: a library's SystemExit text is an @@error line)
self.assertEqual(stdout, "@@error authored toolchain failure\n")
self.assertEqual(self.log.read_text(encoding="utf-8"), stdout + stderr)
def test_unwritable_destination_stops_before_build(self):
self.log = self.root / "missing parent" / "build.log"
code, stdout, stderr, build = self.invoke(lambda args: None)
self.assertEqual(code, 1)
self.assertIn("@@error Could not open build log", stdout)
build.assert_not_called()
self.assertFalse(self.log.exists())
def test_no_log_preserves_console_streams_and_creates_no_file(self):
def build(args):
self.assertIsNone(args.log)
self.assertIsInstance(sys.stdout, io.StringIO)
self.assertIsInstance(sys.stderr, io.StringIO)
print("console only")
code, stdout, stderr, _ = self.invoke(build, log=False)
self.assertEqual((code, stdout, stderr), (0, "console only\n", ""))
self.assertEqual(list(self.root.iterdir()), [])
def test_real_cli_logs_argument_validation_failure(self):
for jobs in ([], ["--jobs", "1"]):
with self.subTest(jobs=jobs):
ran = subprocess.run(make_game.PY + [str(SOURCE), "--log=" + str(self.log),
"--work", str(self.root / "work")] + jobs,
env=dict(os.environ, HOOKSHOT_JOBS=""),
capture_output=True, text=True, encoding="utf-8", timeout=30)
self.assertEqual(ran.returncode, 1, ran.stdout + ran.stderr)
self.assertIn("needs --out", ran.stdout)
if not jobs:
self.assertIn("compilers at once", ran.stdout)
self.assertEqual(self.log.read_text(encoding="utf-8"), ran.stdout + ran.stderr)
if __name__ == "__main__":
unittest.main()

144
tests/test_build_times.py Normal file
View File

@@ -0,0 +1,144 @@
"""The build's timer and countdown: make_game.py's build_times.json, and the estimates build.py makes from it.
Nothing here runs a compiler or needs the game.
python -I -B tests/test_build_times.py
"""
import importlib.util
import json
from pathlib import Path
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT / "nx2"), str(ROOT / "third_party" / "a64-kit" / "tools")]
def load(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
make_game = load("make_game_times", ROOT / "nx2" / "make_game.py")
build = load("build_times", ROOT / "nx2" / "build.py")
class TimesRecordTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix="hookshot-times-")
self.addCleanup(self.temporary.cleanup)
self.work = Path(self.temporary.name)
def record(self, files, compile_seconds=10.0, total_seconds=20.0, modules=3, kind="build"):
return make_game.record_times(str(self.work), files, compile_seconds, total_seconds, modules=modules, kind=kind)
def test_a_first_build_is_written_and_has_nothing_before_it(self):
self.assertIsNone(self.record({"code_001.c": 120.4}))
data = json.loads((self.work / "build_times.json").read_text(encoding="utf-8"))
self.assertEqual(data["format"], 1)
self.assertEqual(data["seconds"], {"code_001.c": 120.4})
self.assertEqual(len(data["builds"]), 1)
self.assertEqual(data["builds"][0]["compile_seconds"], 10.0)
self.assertEqual(data["builds"][0]["total_seconds"], 20.0)
self.assertEqual(data["builds"][0]["compiled"], 1)
self.assertEqual(data["builds"][0]["modules"], 3)
self.assertEqual(data["builds"][0]["kind"], "build")
def test_the_next_build_sees_the_last_one_and_keeps_the_old_modules(self):
self.record({"code_001.c": 120.4})
self.assertEqual(self.record({"code_002.c": 6.0}, 30.0, 40.0), 20.0)
data = make_game.load_times(str(self.work))
self.assertEqual(data["seconds"], {"code_001.c": 120.4, "code_002.c": 6.0})
self.assertEqual([b["total_seconds"] for b in data["builds"]], [20.0, 40.0])
def test_a_module_a_cache_held_keeps_its_time(self):
self.record({"code_001.c": 120.4})
self.record({}) # (nothing compiled: an update that only had to link)
self.assertEqual(make_game.load_times(str(self.work))["seconds"], {"code_001.c": 120.4})
def test_a_zero_is_not_a_measurement(self):
self.record({"code_001.c": 0.0, "code_002.c": 5.5})
self.assertEqual(make_game.load_times(str(self.work))["seconds"], {"code_002.c": 5.5})
def test_a_broken_or_foreign_file_is_started_again(self):
(self.work / "build_times.json").write_text("{not json", encoding="utf-8")
self.assertEqual(make_game.load_times(str(self.work))["builds"], [])
(self.work / "build_times.json").write_text('{"format": 99, "seconds": {"a": 1}}', encoding="utf-8")
self.assertEqual(make_game.load_times(str(self.work))["seconds"], {})
self.assertIsNone(self.record({"code_001.c": 3.0}))
def test_only_the_newest_builds_are_kept(self):
for i in range(make_game.KEEP_BUILDS + 5):
self.record({}, 1.0, float(i))
builds = make_game.load_times(str(self.work))["builds"]
self.assertEqual(len(builds), make_game.KEEP_BUILDS)
self.assertEqual(builds[-1]["total_seconds"], float(make_game.KEEP_BUILDS + 4))
class EstimateTests(unittest.TestCase):
def test_no_files_left_is_no_time(self):
self.assertEqual(build.eta_left([], 8), 0.0)
def test_the_work_spreads_over_the_compilers(self):
self.assertEqual(build.eta_left([100.0, 100.0, 100.0, 100.0], 4), 100.0)
self.assertEqual(build.eta_left([100.0, 100.0], 1), 200.0)
def test_the_longest_file_sets_the_floor(self):
self.assertEqual(build.eta_left([600.0, 60.0], 8), 600.0)
def test_measured_seconds_are_used_as_they_are(self):
with tempfile.TemporaryDirectory() as d:
src = Path(d) / "code_003.c"
src.write_bytes(b"x" * 1024)
jobs = [("cc", str(src), str(Path(d) / "code_003.o"), [], b"")]
self.assertEqual(build.per_file_seconds(jobs, {"code_003.c": 42.0}), [42.0])
def test_an_unmeasured_module_follows_the_measured_rate(self):
with tempfile.TemporaryDirectory() as d:
known = Path(d) / "code_001.c"
known.write_bytes(b"x" * 1024)
new = Path(d) / "code_002.c"
new.write_bytes(b"x" * 2048)
jobs = [("cc", str(known), "", [], b""), ("cc", str(new), "", [], b"")]
self.assertEqual(build.per_file_seconds(jobs, {"code_001.c": 10.0}), [10.0, 20.0])
def test_a_first_build_still_has_an_estimate_for_every_module(self):
with tempfile.TemporaryDirectory() as d:
jobs = []
for name in ("code_hot.c", "code_001.c", "host_main.c"):
src = Path(d) / name
src.write_bytes(b"x" * (1 << 20))
jobs.append(("cc", str(src), "", [], b""))
seconds = build.per_file_seconds(jobs, {})
self.assertEqual(len(seconds), 3)
self.assertTrue(all(seconds))
self.assertGreater(seconds[0], seconds[1]) # (the hot file's rate is the highest)
def test_a_broken_record_is_no_estimate(self):
with tempfile.TemporaryDirectory() as d:
path = Path(d) / "build_times.json"
self.assertEqual(build.times_from(str(path)), {})
path.write_text("{not json", encoding="utf-8")
self.assertEqual(build.times_from(str(path)), {})
path.write_text('{"seconds": {"code_001.c": 12.5, "odd": "x"}}', encoding="utf-8")
self.assertEqual(build.times_from(str(path)), {"code_001.c": 12.5})
def test_the_plan_leaves_out_what_a_cache_already_holds(self):
with tempfile.TemporaryDirectory() as d:
src = Path(d) / "code_001.c"
src.write_text("int f(void) { return 0; }\n", encoding="utf-8")
obj = Path(d) / "code_001.o"
job = ("cc", str(src), str(obj), ["-O1"], b"headers")
work, keys = build.plan_jobs([job])
self.assertEqual(work, [job])
key = keys[(str(src), str(obj))]
obj.write_bytes(b"")
Path(str(obj) + ".sha1").write_text(key, encoding="utf-8")
self.assertEqual(build.plan_jobs([job])[0], [])
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,40 @@
"""Run the Windows Builder's actual update decision without WPF, a game, downloads or NVIDIA's SDK."""
import argparse
from pathlib import Path
import shutil
import subprocess
import tempfile
RUN_ALL_ARGS = ["--dotnet", "{dotnet}"]
RUN_ALL_SKIP = {"linux": "Windows Builder update policy; .NET 8 fixture exercised on Windows"}
ROOT = Path(__file__).resolve().parents[1]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--source", type=Path, default=ROOT)
p.add_argument("--dotnet", default="dotnet")
a = p.parse_args()
dotnet = shutil.which(a.dotnet)
if not dotnet:
p.error(".NET 8 SDK is required for the Windows Builder update fixture")
source = (a.source / "builder/HookshotBuilder/MainWindow.xaml.cs").read_text(encoding="utf-8")
start = source.index(" internal void UpdateBuild(")
end = source.index(" // ================= the window", start)
fixture = (ROOT / "tests/builder_dlss_update_test.cs").read_text(encoding="utf-8")
with tempfile.TemporaryDirectory(prefix="builder-dlss-update-") as folder:
root = Path(folder)
(root / "Program.cs").write_text(fixture.replace("/* ACTUAL_UPDATE */", source[start:end]), encoding="utf-8")
for name in ("BuilderSettings.cs", "ShaderStep.cs"):
shutil.copyfile(a.source / "builder/HookshotBuilder" / name, root / name)
(root / "NuGet.Config").write_text("<configuration><packageSources><clear/></packageSources></configuration>", encoding="utf-8")
(root / "Fixture.csproj").write_text('''<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup>
<OutputType>Exe</OutputType><TargetFramework>net8.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable><TreatWarningsAsErrors>true</TreatWarningsAsErrors><NuGetAudit>false</NuGetAudit>
</PropertyGroup></Project>''', encoding="utf-8")
subprocess.run([dotnet, "run", "--project", str(root / "Fixture.csproj"), "--configuration", "Release"], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,69 @@
"""The Linux builder's "N GB free" line must measure the disk the game folder is on, also when the field holds ~/...
The field takes a path such as ~/Games/Hookshot (build() and apply_mode() expand the ~), but free_gb walked up the
unexpanded text, never found a folder, and reported the free space of / instead.
python -I -B tests/test_builder_free_space.py
"""
import importlib.util
import os
from pathlib import Path
import sys
import tempfile
import types
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
def load_builder():
for name in ("tkinter", "tkinter.filedialog", "tkinter.messagebox", "tkinter.ttk"):
if name not in sys.modules:
sys.modules[name] = types.ModuleType(name)
tk = sys.modules["tkinter"]
tk.Tk = type("Tk", (), {})
tk.filedialog = sys.modules["tkinter.filedialog"]
tk.messagebox = sys.modules["tkinter.messagebox"]
tk.ttk = sys.modules["tkinter.ttk"]
spec = importlib.util.spec_from_file_location("hookshot_builder_free_space", ROOT / "builder" / "linux" / "hookshot_builder.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
builder = load_builder()
def measured(path, home):
"""The folder free_gb asked the disk about"""
asked = []
def usage(where):
asked.append(where)
return types.SimpleNamespace(free=2 ** 30)
with mock.patch.dict(os.environ, {"HOME": home, "USERPROFILE": home}), mock.patch.object(builder.shutil, "disk_usage", usage):
gb = builder.free_gb(path)
return gb, asked
class FreeSpaceTests(unittest.TestCase):
def test_a_tilde_path_is_measured_under_the_home_folder(self):
with tempfile.TemporaryDirectory(prefix="hookshot-free-") as home:
gb, asked = measured("~/Games/Hookshot", home)
self.assertEqual(gb, 1.0)
self.assertEqual(asked, [home]) # (the nearest folder that exists, not "/")
def test_a_path_that_exists_is_measured_as_it_is(self):
with tempfile.TemporaryDirectory(prefix="hookshot-free-") as home:
gb, asked = measured(home, home)
self.assertEqual(asked, [home])
def test_an_empty_field_still_measures_something(self):
gb, asked = measured("", "/")
self.assertEqual(asked, ["/"])
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,29 @@
"""The builder calls the py launcher as `py -3 -I ...`, not `py -I -3 ...`.
The launcher's -3 has to come before Python's own -I: `py -I -3` hands -3 to Python, which stops with
"Unknown option: -3" and "Try 'python -h' for more information."
python -I -B tests/test_builder_python.py
"""
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUNNER = ROOT / "builder" / "HookshotBuilder" / "Runner.cs"
def main():
source = RUNNER.read_text(encoding="utf-8")
info = source[source.index("static ProcessStartInfo Info("):]
info = info[:info.index("return si;")]
if 'si.ArgumentList.Add("-I")' not in info:
raise SystemExit("Runner.Info does not run the tools isolated")
if "tools.PythonArgs" not in info:
raise SystemExit("Runner.Info drops the py launcher's -3")
if info.index("tools.PythonArgs") > info.index('si.ArgumentList.Add("-I")'):
raise SystemExit("py -I -3: Python sees -3 as one of its own options (Unknown option: -3)")
if 'new[] { "-3" }' not in source or "IsStoreShortcut" not in source:
raise SystemExit("the source-tree Python no longer avoids Windows' Store shortcut with py -3")
print("PASS: the py launcher gets -3 before -I")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,96 @@
"""The Linux builder must grow on a 1440p or 4K screen: Tk on X11 ignores the desktop's scale, so its text stays 12 px.
python -I -B tests/test_builder_scale.py
"""
import importlib.util
from pathlib import Path
import sys
import types
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
def load_builder():
for name in ("tkinter", "tkinter.filedialog", "tkinter.messagebox", "tkinter.ttk"):
if name not in sys.modules:
sys.modules[name] = types.ModuleType(name)
tk = sys.modules["tkinter"]
tk.Tk = type("Tk", (), {})
tk.filedialog = sys.modules["tkinter.filedialog"]
tk.messagebox = sys.modules["tkinter.messagebox"]
tk.ttk = sys.modules["tkinter.ttk"]
spec = importlib.util.spec_from_file_location("hookshot_builder_scale", ROOT / "builder" / "linux" / "hookshot_builder.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
builder = load_builder()
class FakeTcl:
"""Tk 8.6's X11 named fonts: pixel sizes, except one a theme gave in points"""
def __init__(self):
self.fonts = {"TkDefaultFont": -12, "TkFixedFont": -12, "TkHeadingFont": -12, "PointFont": 10}
self.scaling = None
def splitlist(self, value):
return value
def call(self, *args):
if args[:2] == ("tk", "scaling"):
self.scaling = args[2]
elif args == ("font", "names"):
return tuple(self.fonts)
elif args[:2] == ("font", "configure") and len(args) == 4:
return str(self.fonts[args[2]])
elif args[:2] == ("font", "configure"):
self.fonts[args[2]] = args[4]
def window(height, dpi):
win = types.SimpleNamespace(tk=FakeTcl(), winfo_screenheight=lambda: height)
with mock.patch.object(builder, "xft_dpi", return_value=dpi), mock.patch.dict(builder.os.environ, {}, clear=True):
builder.Builder.apply_scale(win)
return win
class ScaleTests(unittest.TestCase):
def test_a_1080p_screen_is_left_alone(self):
self.assertEqual(builder.ui_scale(1080, environ={}), 1.0)
win = window(1080, None)
self.assertIsNone(win.tk.scaling)
self.assertEqual(win.tk.fonts["TkDefaultFont"], -12)
def test_a_4k_screen_with_no_desktop_scale_doubles(self):
self.assertEqual(builder.ui_scale(2160, environ={}), 2.0)
self.assertAlmostEqual(builder.ui_scale(1440, environ={}), 1440 / 1080)
def test_xft_dpi_wins_over_the_screen_height(self):
self.assertEqual(builder.ui_scale(2160, dpi=144, environ={}), 1.5)
self.assertEqual(builder.ui_scale(2160, dpi=96, environ={}), 1.0)
def test_gdk_scale_of_one_is_not_an_answer(self):
self.assertEqual(builder.ui_scale(2160, environ={"GDK_SCALE": "1"}), 2.0)
self.assertEqual(builder.ui_scale(1080, environ={"GDK_SCALE": "2"}), 2.0)
self.assertEqual(builder.ui_scale(1080, environ={"QT_SCALE_FACTOR": "1.25"}), 1.25)
def test_hookshot_ui_scale_overrides_everything(self):
self.assertEqual(builder.ui_scale(2160, dpi=192, environ={"HOOKSHOT_UI_SCALE": "1", "GDK_SCALE": "2"}), 1.0)
self.assertEqual(builder.ui_scale(1080, environ={"HOOKSHOT_UI_SCALE": "junk"}), 1.0)
self.assertEqual(builder.ui_scale(1080, environ={"HOOKSHOT_UI_SCALE": "40"}), 4.0)
def test_pixel_fonts_grow_and_point_fonts_follow_tk_scaling(self):
win = window(2160, None)
self.assertEqual(win.tk.scaling, 2.0 * 96 / 72)
self.assertEqual(win.tk.fonts["TkDefaultFont"], -24)
self.assertEqual(win.tk.fonts["TkFixedFont"], -24)
self.assertEqual(win.tk.fonts["PointFont"], 10)
self.assertEqual(builder.Builder.px(win, 560), 1120)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,51 @@
"""The Linux builder shows an @@error sentence whole. The old split kept only what followed its second word.
python -I -B tests/test_builder_status.py
"""
import importlib.util
from pathlib import Path
import unittest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("make_game_status", ROOT / "nx2" / "make_game.py")
make_game = importlib.util.module_from_spec(spec)
spec.loader.exec_module(make_game)
MODULE = "@@error The file main in this dump is not a game module (it starts ab)."
STUDIO = "@@error Your Visual Studio is too old for this game's renderer."
def old_split(line):
return line[2:].split(" ", 2)[-1]
class BuilderStatusTests(unittest.TestCase):
def test_error_keeps_its_first_word(self):
self.assertEqual(old_split(MODULE), "file main in this dump is not a game module (it starts ab).")
self.assertEqual(make_game.reported(MODULE), "The file main in this dump is not a game module (it starts ab).")
self.assertEqual(old_split(STUDIO), "Visual Studio is too old for this game's renderer.")
self.assertEqual(make_game.reported(STUDIO), "Your Visual Studio is too old for this game's renderer.")
self.assertEqual(make_game.reported("@@error no output"), "no output")
def test_check_keeps_the_sentence_after_ok_or_bad(self):
ok = "@@check ok This is the version Hookshot supports (build e0f3ccfa)."
bad = "@@check bad This is build abcdef01. Hookshot supports build e0f3ccfa."
self.assertEqual(make_game.reported(ok), "This is the version Hookshot supports (build e0f3ccfa).")
self.assertEqual(make_game.reported(bad), "This is build abcdef01. Hookshot supports build e0f3ccfa.")
self.assertEqual(old_split(ok), make_game.reported(ok))
def test_other_lines_are_not_status(self):
self.assertIsNone(make_game.reported("@@stage compile Compiling"))
self.assertIsNone(make_game.reported("@@done /tmp/Hookshot"))
self.assertIsNone(make_game.reported("compiled in 10s"))
def test_the_builder_asks_make_game(self):
source = (ROOT / "builder" / "linux" / "hookshot_builder.py").read_text(encoding="utf-8")
start = source.index("def check_version")
body = source[start:source.index("\n def ", start + 1)]
self.assertIn("make_game.reported", body)
self.assertNotIn('.split(" ", 2)[-1]', body)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,152 @@
"""The capture's copy helpers: copy_pages and copy_pairs share them, and each call waits for its own parts only.
take_capture's texture shadow copies its pages on the helpers (copy_pairs), and a capture copies guest pages on them
(copy_pages), possibly from another thread at the same time. Each call must return with every one of its own copies
made, whatever the other caller has queued, and none may wait forever. A caller runs its own parts no helper has taken:
with every helper busy on another caller's copies, copy_pairs (pools_mu and shadow_mu held) still returns. The shadow pass itself is checked by its shape:
the pages copied now keep their slots and are copied through copy_pairs, the others written are erased.
python -I -B tests/test_capture_copy_helpers.py [--cxx g++]
"""
import argparse
import os
from pathlib import Path
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker, tail=""):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
if tail:
end = text.index(tail, end) + len(tail)
return text[start:end]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
take = block(source, "bool take_capture(")
for needle, why in (("copy_pairs(pairs.data()", "the shadow's copies do not go through the copy helpers"),
("c->slots[j] == i", "the pages copied now are erased before they are copied again"),
("shadow_erase(c->copied[i])", "a full shadow keeps the old copy of a page it could not replace")):
if needle not in take:
raise SystemExit("take_capture: " + why)
if take.index("copy_pairs(pairs.data()") > take.index("const auto tk1"):
raise SystemExit("take_capture: the shadow's copies are made after shadow_mu is let go")
parts = [source[source.index("struct CopyPair {"):source.index("\n", source.index("struct CopyPair {"))],
block(source, "struct CopyPart {", ";"),
block(source, "void copy_part(const CopyPart& w)"),
block(source, "struct CopyHelpers {", ";"),
block(source, "void copy_helper_main()"),
block(source, "int capture_copy_helpers()"),
block(source, "void copy_split(CopyPart whole"),
block(source, "void copy_pages(const uint32_t* pages"),
block(source, "void copy_pairs(const CopyPair* pairs")]
fixture = r'''#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <mutex>
#include <thread>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::fflush(stderr); std::_Exit(1); } } while (0)
typedef size_t SIZE_T;
constexpr uint64_t kPageShift = 12;
static std::vector<uint8_t> guest(4096 * 4096); // (16 MB of "guest memory")
struct Host { void* guest_base() { return guest.data(); } uint64_t guest_size() { return guest.size(); } } host_obj, *host = &host_obj;
static std::atomic<bool> hold_guest{false}; // (copy_pages' guest copies wait while it is set: the helpers kept busy)
static std::atomic<int> held{0};
static bool guarded_copy(void* dst, const void* src, size_t n) {
if (hold_guest.load()) {
held++;
while (hold_guest.load()) std::this_thread::yield();
}
std::memcpy(dst, src, n);
return true;
}
static void copy_fence() {} // (#1619's fence after a page-by-page retry: the stub's copies are plain memcpy)
static int GetCurrentThread() { return 0; }
static void SetThreadDescription(int, const wchar_t*) {}
''' + "\n".join(parts) + r'''
static void fill(uint8_t* p, uint32_t seed) { for (int i = 0; i < 4096; i += 4) { uint32_t v = seed * 2654435761u + i; std::memcpy(p + i, &v, 4); } }
int main() {
for (uint32_t pg = 0; pg < 4096; pg++) fill(guest.data() + (uint64_t)pg * 4096, pg + 1);
std::atomic<bool> stop{false};
// (another thread keeps the helpers busy with captures of guest pages, as the replay thread's would)
std::thread other([&] {
std::vector<uint32_t> pages(3000);
std::vector<uint8_t> out(pages.size() * 4096), ok(pages.size());
for (int round = 0; !stop.load(); round++) {
for (size_t i = 0; i < pages.size(); i++) pages[i] = (uint32_t)((i * 7 + round) % 4096);
copy_pages(pages.data(), (int64_t)pages.size(), out.data(), ok.data());
for (size_t i = 0; i < pages.size(); i++) {
CHECK(ok[i]);
CHECK(std::memcmp(out.data() + i * 4096, guest.data() + (uint64_t)pages[i] * 4096, 4096) == 0);
}
}
});
// (this thread: the shadow's pairs, of every size around the helpers' threshold, each checked as soon as its call returns)
std::vector<uint8_t> src(2500 * 4096), dst(2500 * 4096);
for (int round = 0; round < 200; round++) {
static const int sizes[] = {1, 127, 128, 129, 500, 2500};
const int n = sizes[round % 6];
for (int i = 0; i < n; i++) fill(src.data() + (uint64_t)i * 4096, (uint32_t)(round * 10007 + i));
std::memset(dst.data(), 0, (size_t)n * 4096);
std::vector<CopyPair> pairs;
for (int i = 0; i < n; i++) pairs.push_back({dst.data() + (uint64_t)(n - 1 - i) * 4096, src.data() + (uint64_t)i * 4096});
copy_pairs(pairs.data(), (int64_t)pairs.size());
for (int i = 0; i < n; i++) CHECK(std::memcmp(dst.data() + (uint64_t)(n - 1 - i) * 4096, src.data() + (uint64_t)i * 4096, 4096) == 0);
}
stop = true;
other.join();
{ // (every helper (RX_VK_CAPTURE_THREADS=2) and the other caller held in copy_pages' guest copies: copy_pairs runs its
// own parts and returns)
hold_guest = true;
std::vector<uint32_t> pages(3000);
for (size_t i = 0; i < pages.size(); i++) pages[i] = (uint32_t)i;
std::vector<uint8_t> out(pages.size() * 4096), ok(pages.size());
std::thread held_caller([&] { copy_pages(pages.data(), (int64_t)pages.size(), out.data(), ok.data()); });
while (held.load() < 3) std::this_thread::yield();
const int n = 2500;
for (int i = 0; i < n; i++) fill(src.data() + (uint64_t)i * 4096, (uint32_t)(77 + i));
std::vector<CopyPair> pairs;
for (int i = 0; i < n; i++) pairs.push_back({dst.data() + (uint64_t)i * 4096, src.data() + (uint64_t)i * 4096});
copy_pairs(pairs.data(), n);
CHECK(std::memcmp(dst.data(), src.data(), (size_t)n * 4096) == 0);
hold_guest = false;
held_caller.join();
for (size_t i = 0; i < pages.size(); i++) CHECK(ok[i] && std::memcmp(out.data() + i * 4096, guest.data() + i * 4096, 4096) == 0);
}
std::printf("PASS: copy_pairs and copy_pages share the helpers; each call returns with its own copies made, "
"its own parts run by itself when the helpers are busy\n");
std::fflush(stdout);
// (the helpers are detached and wait on copy_helpers' condition variable for good: running its destructor at exit would
// wait for them, as the game never does)
std::_Exit(0);
}
'''
with tempfile.TemporaryDirectory(prefix="capture-copy-helpers-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / ("fixture.exe" if os.name == "nt" else "fixture")
cpp.write_text(fixture, encoding="utf-8")
# (_CRT_SECURE_NO_WARNINGS: the MSVC CRT's getenv deprecation, as the game's own Windows build passes)
subprocess.run([a.cxx, "-std=c++20", "-O2", "-pthread", "-Wall", "-Wextra", "-Werror", "-D_CRT_SECURE_NO_WARNINGS",
str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True, timeout=120, env={**os.environ, "RX_VK_CAPTURE_THREADS": "2"})
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,105 @@
"""The capture's non-temporal copy (gpu/vk/vk_memory.cpp copy_nt) copies every byte and nothing past them.
copy_nt copies a capture's pages with streaming stores in whole 64-byte lines of the destination, and plain copies for
the head up to the destination's next line and for the tail. This compiles it out of the source, with SSE2 and with
AVX2 where the compiler takes -mavx2, and checks it against memcpy for sizes 0-9000 at every alignment of source and
destination within a line, with guard bytes on both sides of the destination. It also checks that the guarded copy
uses it, and that copy_part fences after its page-by-page retry (a copy that left at a fault did not reach its own).
python -I -B tests/test_capture_copy_nt.py --cxx <compiler>
The fixture is retained in the ignored work directory. Skipped on hosts that are not x86-64.
"""
import argparse
import os
from pathlib import Path
import platform
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
def function(source, marker):
start = source.index(marker)
end = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[end] == "{") - (source[end] == "}")
end += 1
return source[start:end]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
source = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
if "capture_nt() ? copy_nt : std::memcpy" not in source:
raise SystemExit("the guarded copy does not use copy_nt")
part = function(source, "void copy_part(const CopyPart& w)")
retry = part[part.index("for (int64_t q = i; q < j; q++)"):]
if "copy_fence();" not in retry[:retry.index("i = j;")]:
raise SystemExit("copy_part does not fence after its page-by-page retry")
if platform.machine().lower() not in ("amd64", "x86_64", "x64"):
print("SKIP: not an x86-64 host")
return
copy = function(source, "__attribute__((noinline)) void* copy_nt(")
fixture = r'''
#include <immintrin.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr,"FAIL:%d %s\n",__LINE__,#x); std::exit(1); } } while (0)
''' + copy + r'''
int main() {
std::vector<uint8_t> src(9000 + 128), dst(9000 + 256), want(9000 + 256);
for (size_t i = 0; i < src.size(); i++) src[i] = (uint8_t)(i * 131 + 7);
uint8_t* const sbase = (uint8_t*)(((uintptr_t)src.data() + 63) & ~(uintptr_t)63);
uint8_t* const dbase = (uint8_t*)(((uintptr_t)dst.data() + 63) & ~(uintptr_t)63);
uint8_t* const wbase = want.data() + (dbase - dst.data());
long checked = 0;
for (size_t n = 0; n <= 9000; n += n < 600 ? 1 : 61)
for (size_t so = 0; so < 64; so += 7)
for (size_t dof = 0; dof < 64; dof += 5) {
if (dof + n + 64 > (size_t)(dst.data() + dst.size() - dbase) || so + n > (size_t)(src.data() + src.size() - sbase))
continue;
std::memset(dst.data(), 0xA5, dst.size());
std::memset(want.data(), 0xA5, want.size());
std::memcpy(wbase + dof, sbase + so, n);
CHECK(copy_nt(dbase + dof, sbase + so, n) == dbase + dof);
_mm_sfence();
CHECK(std::memcmp(dst.data(), want.data(), dst.size()) == 0);
checked++;
}
std::printf("PASS copy_nt: %ld copies (%s)\n", checked,
#ifdef __AVX2__
"AVX2"
#else
"SSE2"
#endif
);
}
'''
work = ROOT / "work"
work.mkdir(exist_ok=True)
out = Path(tempfile.mkdtemp(prefix="capture-copy-nt-", dir=work))
cpp = out / "fixture.cpp"
cpp.write_text(fixture, encoding="utf-8")
env = dict(os.environ)
compiler = Path(args.cxx)
if compiler.is_file():
env["PATH"] = str(compiler.resolve().parent) + os.pathsep + env.get("PATH", "")
for name, flags in (("sse2", []), ("avx2", ["-mavx2"])):
exe = out / ("fixture-%s.exe" % name)
cmd = [args.cxx, "-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror"] + flags + [str(cpp), "-o", str(exe)]
subprocess.run(cmd, check=True)
subprocess.run([str(exe)], check=True, env=env)
print("Retained:", out)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,111 @@
"""Captured mirror regions preserve bytes when adjacent runs are combined.
python -I -B tests/test_capture_copy_runs.py --cxx <compiler>
The fixture is retained in the ignored work directory.
"""
import argparse
import os
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
def function(source, marker):
start = source.index(marker)
end = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[end] == "{") - (source[end] == "}")
end += 1
return source[start:end]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
source = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
helper = function(source, "void append_capture_copy(")
sync = function(source, "void mirror_sync(Pool& p)")
if "append_capture_copy(cap_runs[b->second.buffer]," not in sync:
raise SystemExit("the captured-page path does not use the helper")
fixture = r'''
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>
struct VkBufferCopy { uint64_t srcOffset, dstOffset, size; };
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr,"FAIL:%d %s\n",__LINE__,#x); std::exit(1); } } while (0)
''' + helper + r'''
static void expect_separate(VkBufferCopy a, VkBufferCopy b) {
std::vector<VkBufferCopy> v; append_capture_copy(v,a); append_capture_copy(v,b); CHECK(v.size()==2);
}
int main() {
std::vector<VkBufferCopy> v;
append_capture_copy(v,{16,32,4}); append_capture_copy(v,{20,36,8});
CHECK(v.size()==1 && v[0].srcOffset==16 && v[0].dstOffset==32 && v[0].size==12);
expect_separate({0,0,4},{5,4,4}); expect_separate({0,0,4},{4,5,4});
expect_separate({0,0,4},{2,2,4}); expect_separate({4,4,4},{0,0,4});
expect_separate({0,0,0},{0,0,4}); expect_separate({0,0,4},{4,4,0});
expect_separate({UINT64_MAX-1,0,2},{0,2,4});
expect_separate({0,UINT64_MAX-1,2},{2,0,4});
expect_separate({0,0,8},{8,8,UINT64_MAX-7});
expect_separate({UINT64_MAX-10,0,5},{UINT64_MAX-5,5,10});
expect_separate({0,UINT64_MAX-10,5},{5,UINT64_MAX-5,10});
// Two independent capture buffers with partial edges and gaps. Interleave the
// input and include real adjacent runs, with disjoint destination ranges.
std::vector<uint8_t> src[2]={std::vector<uint8_t>(65536),std::vector<uint8_t>(65536)};
for (unsigned b=0;b<2;b++) for (unsigned i=0;i<src[b].size();i++) src[b][i]=(uint8_t)(i*31+b*53);
std::vector<uint8_t> original(65536,0x7d), combined=original;
std::map<int,std::vector<VkBufferCopy>> baseline, optimized;
const std::vector<std::pair<int,VkBufferCopy>> input={
{0,{3,101,7}}, {1,{100,1000,5}}, {0,{10,108,9}},
{1,{105,1005,11}}, {0,{30,150,4}}, {1,{200,1100,3}},
{0,{34,154,2}}, {1,{203,1103,1}}, {0,{60,180,5}}
};
for (const auto& [buffer,r]:input) {
baseline[buffer].push_back(r); append_capture_copy(optimized[buffer],r);
}
CHECK(baseline[0].size()==5 && optimized[0].size()==3);
CHECK(baseline[1].size()==4 && optimized[1].size()==2);
for (const auto& [b,rs]:baseline) for(const auto& r:rs)
std::copy_n(src[b].begin()+r.srcOffset,(size_t)r.size,original.begin()+r.dstOffset);
for (const auto& [b,rs]:optimized) for(const auto& r:rs)
std::copy_n(src[b].begin()+r.srcOffset,(size_t)r.size,combined.begin()+r.dstOffset);
CHECK(original==combined);
// A long contiguous sequence and its fragmented counterpart.
std::vector<VkBufferCopy> pages;
for(uint64_t i=0;i<1024;i++) pages.push_back({(i+i/256)*4096,(i+i/256)*4096,4096});
std::vector<VkBufferCopy> compressed;
for(const auto& r:pages) append_capture_copy(compressed,r);
CHECK(compressed.size()==4);
compressed.clear();
for(uint64_t i=0;i<pages.size();i++) append_capture_copy(compressed,{(i*2)*4096,(i*2)*4096,4096});
CHECK(compressed.size()==pages.size());
std::printf("PASS merged exact bytes, gaps, defensive overlaps, overflow and buffer grouping\n");
}
'''
work = ROOT / "work"
work.mkdir(exist_ok=True)
out = Path(tempfile.mkdtemp(prefix="capture-copy-runs-", dir=work))
cpp, exe = out / "fixture.cpp", out / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
env = dict(os.environ)
compiler = Path(args.cxx)
if compiler.is_file():
env["PATH"] = str(compiler.resolve().parent) + os.pathsep + env.get("PATH", "")
subprocess.run([str(exe)], check=True, env=env)
print("Retained:", out)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,93 @@
"""Exercise production capture sizing, allocation entry and PNG output without a GPU."""
import argparse
from pathlib import Path
import os
import struct
import subprocess
import tempfile
import zlib
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
ROOT = Path(__file__).resolve().parents[1]
def block(source, marker):
start = source.index(marker)
at = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[at] == "{") - (source[at] == "}")
at += 1
return source[start:at]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--source", type=Path, default=ROOT)
p.add_argument("--headers", type=Path, required=True)
p.add_argument("--cxx", default="g++")
p.add_argument("--case", choices=["all", "invalid", "valid", "png"], default="all")
a = p.parse_args()
source = (a.source / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
start = source.index("VkImage off_image =")
globals_and_setup = source[start:source.index("void image_barrier(", start)]
gate = "if (src && (dev.offscreen || shots) && offscreen_init())"
assert gate in source
tail = "if (dev.offscreen || interpolated) return;\n present_image(src, sw, sh, crop, direct_view);"
assert tail in source
png = block(source, "uint32_t crc32(") + "\n" + block(source, "bool write_png(")
template = (ROOT / "tests/capture_dimensions_test.cpp").read_text(encoding="utf-8")
fixture = template.replace("/* SETUP */", globals_and_setup).replace("/* GATE */", gate)
fixture = fixture.replace("/* PRESENT_TAIL */", tail).replace("/* PNG */", png)
cases = [
(None, None, 16384, True), ("3440", "1440", 16384, True), ("5120", "1440", 16384, True),
("1439", "701", 16384, True), ("1", "1", 16384, True), ("16384", "1440", 16384, True),
("256", "256", 256, True), ("257", "256", 256, False), ("16385", "1440", 16384, False),
("0", "720", 16384, False), ("abc", "720", 16384, False), ("", "720", 16384, False),
("3440tail", "1440", 16384, False), (" 3440", "1440", 16384, False), ("+3440", "1440", 16384, False),
("-1", "-1", 4294967295, False), ("4294967296", "1", 4294967295, False),
("999999999999999999999999999999", "1", 4294967295, False),
("1", "4294967295", 4294967295, False), ("32768", "32768", 32768, False),
("715700000", "1", 4294967295, True), ("715800000", "1", 4294967295, False),
("2863303432", "2147489722", 4294967295, False), ("0003440", "1440", 16384, True),
]
with tempfile.TemporaryDirectory(prefix="capture-dimensions-") as temp:
cpp, exe = Path(temp) / "fixture.cpp", Path(temp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-missing-field-initializers",
"-I" + str(a.headers), str(cpp), "-o", str(exe)], check=True)
if a.case != "png":
for width, height, limit, valid in cases:
if a.case == "valid" and not valid or a.case == "invalid" and valid:
continue
env = os.environ.copy()
for key, value in [("RX_VK_SHOT_W", width), ("RX_VK_SHOT_H", height)]:
env.pop(key, None)
if value is not None:
env[key] = value
for headless in ["0", "1"]:
subprocess.run([str(exe), str(limit), str(int(valid)), headless], env=env, check=True, timeout=10)
if a.case in ["all", "png"]:
path = Path(temp) / "normal.png"
subprocess.run([str(exe), "png", str(path)], check=True, timeout=10)
data = path.read_bytes()
assert data[:8] == b"\x89PNG\r\n\x1a\n"
at, payload = 8, b""
while at < len(data):
size = struct.unpack_from(">I", data, at)[0]
kind, body = data[at + 4:at + 8], data[at + 8:at + 8 + size]
assert zlib.crc32(kind + body) == struct.unpack_from(">I", data, at + 8 + size)[0]
if kind == b"IHDR":
assert struct.unpack(">IIBBBBB", body) == (257, 251, 8, 2, 0, 0, 0)
if kind == b"IDAT":
payload += body
at += size + 12
raw = zlib.decompress(payload)
assert raw == (b"\x00" + bytes([17, 73, 211]) * 257) * 251
assert len(payload) == 2 + len(raw) + 5 * ((len(raw) + 65534) // 65535) + 4
print("PASS: production capture entry, dimension rejection and PNG consumer controls")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,73 @@
"""Run production capture reads against separate device bytes and a stale CPU cache."""
import argparse
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
ROOT = Path(__file__).resolve().parents[1]
def block(source, marker):
start = source.index(marker)
at = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[at] == "{") - (source[at] == "}")
at += 1
return source[start:at]
def producer(source, marker, end):
at = source.index(marker)
start = source.rfind("const VkBufferImageCopy c", 0, at)
return source[start:source.index(end, at)]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
p.add_argument("--headers", type=Path, required=True)
p.add_argument("--source", type=Path, default=ROOT)
p.add_argument("--phase", choices=["all", "normal", "owned", "made", "log"], default="all")
p.add_argument("--visibility", choices=["all", "implicit", "explicit"], default="all")
p.add_argument("--cache", choices=["all", "coherent", "noncoherent"], default="all")
a = p.parse_args()
source = (a.source / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
start = source.index(" auto take = [&](uint64_t at_value, int64_t at_present) {")
end = source.index(" // (RX_VK_DUMP_BLACK", start)
take = source[start:end] + "\n(void)at_present; return px; };\n"
helper_start = source.index("std::shared_ptr<std::vector<uint8_t>> shot_pixels(")
helper_end = source.index("void do_present(", helper_start)
helper = source[helper_start:helper_end]
start = source.index(" if (shot && made_src && made_map) {")
end = source.index(" } else if (shot)", start)
made = source[start:end] + "\n}\n"
start = source.index("void framelog_collect(int slot) {")
end = source.index(" { // (a picture blown out", start)
collect = source[start:end] + "\ncaptured = std::move(rec); }\n"
call = source.index("vkWaitForFences(dev.device, 1, &f.done, VK_TRUE, UINT64_MAX)", source.index("void present_image("))
start = source.rfind("\n", 0, call) + 1
end = source.index(" uint32_t index = 0;", start)
fence = source[start:end]
host = block(source, "void capture_host_read_barrier(") if "void capture_host_read_barrier(" in source else ""
normal_copy = producer(source, "vkCmdCopyImageToBuffer(r.cmd, shot_image,", "\n }")
made_copy = producer(source, "vkCmdCopyImageToBuffer(r.cmd, made_image,", "\n }")
log_copy = producer(source, "vkCmdCopyImageToBuffer(f.cmd, log_image,", " l.used =")
template = (ROOT / "tests/capture_readback_visibility_test.cpp").read_text(encoding="utf-8")
fixture = template.replace("/* SHOT_PIXELS */", helper).replace("/* TAKE */", take).replace("/* MADE */", made).replace("/* COLLECT */", collect).replace("/* FENCE */", fence)
fixture = fixture.replace("/* HOST_BARRIER */", host).replace("/* NORMAL_COPY */", normal_copy).replace("/* MADE_COPY */", made_copy).replace("/* LOG_COPY */", log_copy)
with tempfile.TemporaryDirectory(prefix="capture-visibility-") as temp:
cpp, exe = Path(temp) / "fixture.cpp", Path(temp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(a.headers),
str(cpp), "-o", str(exe)], check=True)
for phase in (["normal", "owned", "made", "log"] if a.phase == "all" else [a.phase]):
for cache in (["noncoherent", "coherent"] if a.cache == "all" else [a.cache]):
for visibility in (["implicit", "explicit"] if a.visibility == "all" else [a.visibility]):
subprocess.run([str(exe), phase, cache, visibility], check=True, timeout=30)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,46 @@
"""Execute production screenshot setup with failed allocations and real Vulkan types."""
import argparse
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
ROOT = Path(__file__).resolve().parents[1]
def block(source, signature):
start = source.index(signature)
end = source.index("{", start) + 1
depth = 1
while depth:
depth += (source[end] == "{") - (source[end] == "}")
end += 1
return source[start:end]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
p.add_argument("--headers", type=Path, required=True)
p.add_argument("--source", type=Path, default=ROOT)
a = p.parse_args()
source = (a.source / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
template = (ROOT / "tests/capture_resources_test.cpp").read_text(encoding="utf-8")
actual = block(source, "bool shot_dimensions_ready()") + "\n" + block(source, "bool offscreen_init()")
actual += "\n" + block(source, "bool made_shot_init()")
actual += "\n" + block(source, "void capture_host_read_barrier(")
fixture = template.replace("/* ACTUAL_FUNCTION */", actual)
start = source.index(" if (made_src && !made_shot_init())")
end = source.index(" if (shot) {", start)
fixture = fixture.replace("/* ACTUAL_RECORDING */", source[start:end])
with tempfile.TemporaryDirectory(prefix="capture-resources-") as temp:
cpp, exe = Path(temp) / "fixture.cpp", Path(temp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-Wno-missing-field-initializers",
"-I" + str(a.headers), str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True, timeout=30)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,68 @@
"""Execute the normal-shot producer/read/consume statements with a queued GPU model.
A pending frame2 can survive a non-shot frame3 while the GPU is behind. A new
frame4 copy reuses its slot. Both immediate and delayed completion are legal;
the pending capture must still receive frame2 bytes and metadata. No GPU/game.
"""
import argparse
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
at = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[at] == "{") - (text[at] == "}")
at += 1
return text[start:at]
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--source", type=Path, default=ROOT)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (a.source / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
present = source[source.index("void do_present("):]
copy = next(line.strip() for line in present.splitlines() if "vkCmdCopyImageToBuffer(r.cmd, shot_image," in line)
consume = next(line.strip() for line in present.splitlines() if "if (pending.on &&" in line and "completed()" in line)
rearm = next(line.strip() for line in present.splitlines() if "pending = {true, value, presents" in line)
read_at = present.index("auto take = [&]")
read = present[read_at:present.index("// (RX_VK_DUMP_BLACK", read_at)]
copy_at, submit_at, consume_at = present.index(copy), present.index("submit_recording(r);"), present.index(consume)
assert copy_at < submit_at < read_at < consume_at
state = "struct PendingShot { bool on=false; uint64_t value=0; int64_t presents=0; std::shared_ptr<std::vector<uint8_t>> pixels{}; };"
helper = ""
protect = ""
if "struct PendingShot {" in source:
state = block(source, "struct PendingShot {") + ";"
helper = block(source, "std::shared_ptr<std::vector<uint8_t>> shot_pixels(")
marker = "if (pending.on && (pending.presents & 1)"
actions = [(copy_at, copy), (submit_at, "submit_recording(r);")]
if marker in present:
at = present.index(marker)
assignment = "pending.pixels = shot_pixels(pending.value, pending.presents);"
end = present.index(assignment, at) + len(assignment)
protect = present[at:end].strip()
assert at < read_at, "slot protection must occur in the producer, before the consumer"
actions.append((at, protect))
producer = "\n".join(text for _, text in sorted(actions))
template = (ROOT / "tests/capture_slot_ownership_test.cpp").read_text(encoding="utf-8")
fixture = template.replace("/* STATE */", state).replace("/* READ_HELPER */", helper)
fixture = fixture.replace("/* TAKE */", read).replace("/* PRODUCER */", producer).replace("/* CONSUME */", consume).replace("/* REARM */", rearm)
with tempfile.TemporaryDirectory(prefix="capture-slot-") as temp:
cpp, exe = Path(temp) / "fixture.cpp", Path(temp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True, timeout=20)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,52 @@
"""A capture's GPU wait for its late pages is timed apart from its other work, and the 600-take log line names it.
Before, the wait sat inside "retires and stamps": with a small mirror budget (RX_VK_MIRROR_MB=1024 on an 8 GB card) that
part read 2.4-3 ms a capture, six captures a frame, and nothing said it was the replay thread waiting for the GPU (issue 995).
python -I -B tests/test_capture_wait_log.py
"""
from pathlib import Path
import re
import unittest
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
class CaptureWaitLogTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
mem = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
cls.take = block(mem, "bool take_capture(")
def test_the_late_page_wait_is_timed_on_its_own(self):
late = block(self.take, "if (c->late_value && c->map)")
wait = late.index("vk_timeline_wait(c->late_value);")
self.assertRegex(late[:wait], r"const auto (\w+) = std::chrono::steady_clock::now\(\);")
self.assertIn("st_tk_late +=", late[wait:])
self.assertIn("st_tk_late_n++", late[wait:])
def test_retires_and_stamps_start_after_the_lock(self):
lock = self.take.index("std::lock_guard lk(pools_mu);")
self.assertIn("st_tk_lock +=", self.take[lock:lock + 300])
self.assertRegex(self.take, r"st_tk_stamp \+= std::chrono::duration<double, std::milli>\(tk1 - tk_locked\)")
def test_the_log_line_names_the_wait_and_resets_it(self):
line = re.search(r'logf\("vk: a capture taken \(ms\):.*?\);', self.take, re.S).group(0)
self.assertIn("GPU wait for late pages", line)
self.assertIn("pools lock", line)
self.assertIn("st_tk_late / 600", line)
self.assertRegex(self.take, r"st_tk_late = st_tk_lock = 0, st_tk_late_n = 0;")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,78 @@
"""A compare-exchange loop ends after a ceiling of attempts, not only on success.
Under robust buffer access an atomic on a word out of bounds need not return what it was given, so a loop that only
leaves on success can spin for ever. On an Intel Arc B390 that hung the frame after the intro movie (issue 17), with no
device loss. Every failed attempt on a real word means another invocation's succeeded, so the ceiling is far above what
contention needs. Every loop (CasLoop, the shared-memory, storage-buffer and global byte and halfword stores) counts its
attempts. python -I -B tests/test_cas_loop_ceiling.py
"""
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "gpu" / "vk" / "eden" / "shader_recompiler" / "backend" / "spirv" / "spirv_emit_context.cpp"
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def main():
source = SOURCE.read_text(encoding="utf-8")
ceiling = re.search(r"constexpr u32 CAS_LOOP_MAX_TRIES = (\d+);", source)
if not ceiling:
raise SystemExit("no CAS_LOOP_MAX_TRIES")
if not 64 <= int(ceiling.group(1)) <= 1 << 16:
raise SystemExit("CAS_LOOP_MAX_TRIES is %s: too few for contention or too many to end soon" % ceiling.group(1))
done = block(source, "Id CasLoopDone(")
if "OpLogicalOr" not in done or "OpUGreaterThanEqual" not in done or "CAS_LOOP_MAX_TRIES" not in done:
raise SystemExit("CasLoopDone does not leave on success or the ceiling")
tries = block(source, "Id CasTriesVariable(")
if "OpStore(tries_var, ctx.u32_zero_value)" not in tries:
raise SystemExit("the attempt count does not start at zero")
loops = (
("CasLoop", block(source, "Id CasLoop(EmitContext& ctx"), "CasTriesVariable(ctx)", "CasLoopDone(ctx, tries_var, success)"),
("the shared-memory stores", block(source, "const auto make_function{[&](u32 mask, u32 size)"),
"CasTriesVariable(*this)", "CasLoopDone(*this, tries_var, success)"),
("the storage-buffer stores", block(source, "void EmitContext::DefineWriteStorageCasLoopFunction("),
"CasTriesVariable(*this)", "CasLoopDone(*this, tries_var, store_successful)"),
)
for name, body, start, leave in loops:
if start not in body:
raise SystemExit("%s does not count its attempts" % name)
if leave not in body:
raise SystemExit("%s leaves only on success" % name)
if body.index(start) > body.index("OpLoopMerge"):
raise SystemExit("%s sets its count inside the loop" % name)
# (the global narrow stores: one loop body used twice, so the count is declared in the entry block and reset by each)
marker = "// (hookshot) 8- and 16-bit global stores:"
if marker in source:
glob = block(source, marker)
loop = block(glob, "const auto cas_loop{[&](Id pointer)")
declared = glob.find("const Id tries_var{AddLocalVariable(")
if declared < 0 or declared > glob.index("const auto cas_loop{"):
raise SystemExit("the global stores declare no attempt count before their loop")
if "OpStore(tries_var, u32_zero_value);" not in loop or loop.index("OpStore(tries_var") > loop.index("OpLoopMerge"):
raise SystemExit("the global stores do not reset their count before the loop")
if "CasLoopDone(*this, tries_var, success)" not in loop:
raise SystemExit("the global stores leave only on success")
# (ATOMG.CAS's own function is one compare-exchange that returns what it read, no loop: nothing to end)
single = 0
marker = "// (hookshot) ATOMG.CAS's:"
if marker in source:
single = block(source[source.index(marker):], "const auto cas{[&](Id pointer)").count("OpAtomicCompareExchange(")
exchanges = source.count("OpAtomicCompareExchange(") - single
if exchanges != source.count("CasLoopDone(") - 1:
raise SystemExit("a compare-exchange loop without the ceiling (%d exchanges in loops, %d ceilings)"
% (exchanges, source.count("CasLoopDone(") - 1))
print("compare-exchange loops end at the ceiling ok")
if __name__ == "__main__":
main()

168
tests/test_changelog.py Normal file
View File

@@ -0,0 +1,168 @@
"""scripts/changelog.py, game-free: a Factorio entry from Conventional Commits, and the old entries kept.
python -I -B tests/test_changelog.py
"""
import importlib.util
import os
import shutil
import subprocess
import sys
import tempfile
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SCRIPT = os.path.join(ROOT, "scripts", "changelog.py")
DASHES = "-" * 99
def git(root, *args):
subprocess.run(["git", "-C", root] + list(args), check=True, capture_output=True)
def commit(root, subject, body=None):
with open(os.path.join(root, "x"), "a", encoding="utf-8") as f:
f.write(subject + "\n")
git(root, "add", "x")
args = ["git", "-C", root, "commit", "-q", "-m", subject]
if body:
args += ["-m", body]
subprocess.run(args, check=True, capture_output=True)
def remove(root):
for d, _, files in os.walk(root):
for f in files:
os.chmod(os.path.join(d, f), 0o666)
shutil.rmtree(root)
def main():
if not shutil.which("git"):
print("changelog: skipped (git is not installed)")
return
# (a scratch repository, without the user's and the system's git settings: commit.gpgSign signed every commit with
# the user's key, and tag.gpgSign made `git tag v0.1.0` an annotated tag that wants a message, so the test failed.
# The changelog script's git runs inherit this)
os.environ.update(GIT_CONFIG_GLOBAL=os.devnull, GIT_CONFIG_NOSYSTEM="1")
root = tempfile.mkdtemp(prefix="hookshot-changelog-")
try:
os.makedirs(os.path.join(root, "scripts"))
shutil.copy2(SCRIPT, os.path.join(root, "scripts", "changelog.py"))
with open(os.path.join(root, "changelog.txt"), "w", encoding="utf-8", newline="\n") as f:
f.write(DASHES + "\nVersion: 0.1.0\nDate: 07. 11. 2026\n Features:\n - First release.\n")
git(root, "init", "-q")
git(root, "config", "user.name", "test")
git(root, "config", "user.email", "test@example.com")
git(root, "add", ".")
git(root, "commit", "-q", "-m", "chore: start")
git(root, "tag", "v0.1.0")
commit(root, "feat(renderer): add a menu")
commit(root, "fix: stop a crash")
commit(root, "perf: spend less time in the replay")
commit(root, "docs: explain the build")
commit(root, "refactor(core): split the loader")
commit(root, "feat!: change the save format", "BREAKING CHANGE: saves from 0.1.0 are not read.")
commit(root, "feat: another feature")
commit(root, "fix: stop a crash") # (the same subject twice: Factorio rejects a duplicate entry)
script = os.path.join(root, "scripts", "changelog.py")
subprocess.run([sys.executable, "-I", script, "0.2.0", "v0.1.0", "--date", "2026-11-07"], check=True)
with open(os.path.join(root, "changelog.txt"), encoding="utf-8") as f:
text = f.read()
assert text.startswith(DASHES + "\nVersion: 0.2.0\nDate: 07. 11. 2026\n Features:\n"), text
assert " - Renderer: Add a menu.\n" in text
assert " - Another feature.\n" in text
assert " Changes:\n - Breaking: Change the save format.\n - Core: Split the loader.\n" in text
assert " Bugfixes:\n - Stop a crash.\n" in text
assert text.count(" - Stop a crash.\n") == 1, "a repeated subject is listed once"
assert " Optimizations:\n - Spend less time in the replay.\n" in text
assert "explain the build" not in text, "docs commits do not make a release"
assert (text.index(" Features:") < text.index(" Changes:") < text.index(" Bugfixes:")
< text.index(" Optimizations:"))
assert text.count("Version: 0.1.0") == 1 and text.count("Version: 0.2.0") == 1
assert text.endswith(" - First release.\n"), "the 0.1.0 entry is kept below the new one"
# a retried release does not add its version a second time
subprocess.run([sys.executable, "-I", script, "0.2.0", "v0.1.0", "--date", "2026-11-07"], check=True)
with open(os.path.join(root, "changelog.txt"), encoding="utf-8") as f:
assert f.read() == text
# a prerelease changes nothing: Factorio's parser takes only major.minor.patch
subprocess.run([sys.executable, "-I", script, "0.2.0-rc.1", "v0.1.0"], check=True)
with open(os.path.join(root, "changelog.txt"), encoding="utf-8") as f:
assert f.read() == text
# a release with no Conventional Commits still gets a valid entry
git(root, "tag", "v0.2.0")
subprocess.run([sys.executable, "-I", script, "0.2.1", "v0.2.0", "--date", "2026-11-08"], check=True)
with open(os.path.join(root, "changelog.txt"), encoding="utf-8") as f:
text = f.read()
assert "Version: 0.2.1\nDate: 08. 11. 2026\n Changes:\n - Release 0.2.1.\n" in text
# Errors while generating an entry must not truncate the previous changelog.
changelog_path = os.path.join(root, "changelog.txt")
with open(changelog_path, "rb") as f:
before = f.read()
for args, diagnostic in ((["0.2.2", "missing-release-tag"], b"git log"),
(["0.2.2", "v0.2.0", "--date", "not-a-date"], b"Invalid isoformat")):
r = subprocess.run([sys.executable, "-I", "-B", script] + args, capture_output=True)
assert r.returncode != 0 and diagnostic in r.stderr, r.stderr
with open(changelog_path, "rb") as f:
assert f.read() == before, "a failed entry must preserve the old bytes"
# Replacement failures also preserve the destination and clean up the temporary file.
spec = importlib.util.spec_from_file_location("changelog_under_test", script)
changelog = importlib.util.module_from_spec(spec)
spec.loader.exec_module(changelog)
files_before = set(os.listdir(root))
mode_before = os.stat(changelog_path).st_mode
real_temporary_file = changelog.tempfile.NamedTemporaryFile
def broken_temporary_file(*args, **kwargs):
f = real_temporary_file(*args, **kwargs)
real_write = f.write
def interrupted_write(content):
real_write(content[:16])
raise OSError("write failed")
f.write = interrupted_write
return f
with mock.patch.object(changelog.tempfile, "NamedTemporaryFile", side_effect=broken_temporary_file):
try:
changelog.write("0.2.2", "v0.2.0", "2026-11-08")
except OSError as error:
assert str(error) == "write failed"
else:
raise AssertionError("the write failure must reach the caller")
with open(changelog_path, "rb") as f:
assert f.read() == before
assert set(os.listdir(root)) == files_before, "no temporary file after an interrupted write"
with mock.patch.object(changelog.os, "replace", side_effect=OSError("replacement failed")):
try:
changelog.write("0.2.2", "v0.2.0", "2026-11-08")
except OSError as error:
assert str(error) == "replacement failed"
else:
raise AssertionError("the replacement failure must reach the caller")
with open(changelog_path, "rb") as f:
assert f.read() == before
assert set(os.listdir(root)) == files_before, "no temporary file after a failed replacement"
changelog.write("0.2.2", "v0.2.0", "2026-11-08")
with open(changelog_path, "rb") as f:
assert f.read().endswith(before)
assert os.stat(changelog_path).st_mode == mode_before, "keep the changelog's permissions"
assert set(os.listdir(root)) == files_before, "no temporary file after success"
# a version that is not semantic is refused
r = subprocess.run([sys.executable, "-I", script, "v9"], capture_output=True)
assert r.returncode != 0
finally:
remove(root)
print("changelog: ok")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,36 @@
"""--check says a compiler is missing, not "The game files could not be read".
The check tests the compiler before it opens the package. toolchain.need() raises SystemExit, which make_game.py
re-raised without an @@error line; the builder app then showed its fallback for the game files, for any .nup.
python -I -B tests/test_check_no_compiler.py
"""
import os
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def main():
with tempfile.TemporaryDirectory() as d:
env = dict(os.environ)
env["PATH"] = d # (no clang on PATH)
env["EMSDK"] = d # (an emsdk folder that holds none)
env["HOOKSHOT_MSVC"] = "1" # (the Visual Studio route: no tools/llvm-mingw either)
env.pop("HOOKSHOT_MINGW", None)
r = subprocess.run([sys.executable, "-I", "-B", str(ROOT / "nx2" / "make_game.py"), "--check", "--nup",
str(ROOT / "no-such-game.nup"), "--work", str(Path(d) / "work")],
capture_output=True, text=True, encoding="utf-8", errors="replace", env=env)
said = [line for line in r.stdout.splitlines() if line.startswith("@@")]
if not said:
raise SystemExit("the check left stdout empty: the builder shows its 'game files could not be read' fallback"
" (exit %d, stderr %r)" % (r.returncode, r.stderr.strip()))
if not said[0].startswith("@@error ") or "compiler" not in said[0].lower():
raise SystemExit("the check did not name the missing compiler: %r" % said[0])
print("PASS: a missing compiler is said in the builder's protocol")
if __name__ == "__main__":
main()

Some files were not shown because too many files have changed in this diff Show More