Triforce v0.7.0 "Sage Of Courage"

The 0.6 crashes after warps and area changes fixed (no write-back into memory the capture leaves out), invisible objects fixed (occlusion gate), DLSS frame generation flicker fixed, achievements, the renderer built with a play profile, faster translated game code, helper threads judged on settled scenes, memory, shader preparation, audio effects, launcher, controls and Linux fixes.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-22 11:59:06 -06:00
parent 70c210cee1
commit e6f2f248f2
529 changed files with 55958 additions and 1898 deletions

View File

@@ -0,0 +1,99 @@
// gpu/vk/achievement_rows.h alone (tests/test_achievements.py): the list as the host names it, one earned and its notice,
// the page's choice and pages, and how a notice comes in and goes.
#include <cstdio>
#include <cstring>
#include "achievement_rows.h"
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
namespace ar = achievement_rows;
static ar::List list;
static void say(const char* text) {
for (const char* p = text; *p; p++) list.text((unsigned char)*p);
}
int main() {
// nothing shown until the list is whole; then as named, in order
say("First\nThe line about it.\n2026-11-18");
list.add(1 | 3 << 4 | ar::kEarned);
say("Second\nAnother line.\n");
list.add(5);
say("Third"); // (no line about it and no day)
list.add(2 | 9 << 4);
list.add(0); // (no name at all)
CHECK(list.shown() == 0 && list.earned_count() == 0 && list.take_fresh() == -1);
CHECK(!list.earn(0) && list.take_fresh() == -1); // (earned before the list is whole: no such place yet)
list.done(4);
CHECK(list.shown() == 4 && list.earned_count() == 1);
CHECK(!strcmp(list.names[0], "First") && !strcmp(list.abouts[0], "The line about it.") && !strcmp(list.days[0], "2026-11-18"));
CHECK(list.is_earned(0) && ar::shape_of(list.icons[0]) == ar::kSword && ar::tone_of(list.icons[0]) == ar::kBlue);
CHECK(!strcmp(list.names[1], "Second") && !strcmp(list.abouts[1], "Another line.") && !list.days[1][0] && !list.is_earned(1));
CHECK(ar::shape_of(list.icons[1]) == ar::kToken && ar::tone_of(list.icons[1]) == ar::kGold);
CHECK(!strcmp(list.names[2], "Third") && !list.abouts[2][0] && !list.days[2][0]);
CHECK(ar::shape_of(list.icons[2]) == ar::kGem && ar::tone_of(list.icons[2]) == ar::kGold); // (a tone that is none: gold)
CHECK(!strcmp(list.names[3], "(no name)") && ar::shape_of(15) == ar::kStar && ar::shape_of(ar::kShield) == ar::kShield);
// a list sent again changes nothing
say("Again\nx\n");
list.add(0);
list.done(1);
CHECK(list.shown() == 4 && !strcmp(list.names[0], "First"));
// one earned: its day, and the notice's once
say("2026-11-19");
CHECK(list.earn(1) && list.is_earned(1) && !strcmp(list.days[1], "2026-11-19") && list.earned_count() == 2);
CHECK(list.take_fresh() == 1 && list.take_fresh() == -1);
say("2030-01-01");
CHECK(list.earn(0) && !strcmp(list.days[0], "2026-11-18") && list.take_fresh() == 0); // (earned before: the day it was)
say("junk");
CHECK(!list.earn(4) && !list.earn(-1) && list.take_fresh() == -1 && list.earned_count() == 2);
say("2026-11-20");
CHECK(list.earn(2) && !strcmp(list.days[2], "2026-11-20")); // (the bytes of the refused ones are not this one's day)
CHECK(list.take_fresh() == 2);
// a name, a line and a day longer than their room: cut, a name never inside a letter
static ar::List wide;
for (int i = 0; i < 39; i++) wide.text('n');
wide.text(0xC3), wide.text(0xA9), wide.text(0xC3), wide.text(0xA9); // (two letters of two bytes: the second does not fit)
wide.text('\n');
for (int i = 0; i < 400; i++) wide.text('a');
wide.add(0);
wide.done(1);
CHECK(strlen(wide.names[0]) == 39 && strlen(wide.abouts[0]) <= (size_t)ar::kAbout - 1 && wide.shown() == 1);
// more than there is room for: the first kSlots
static ar::List many;
for (int i = 0; i < ar::kSlots + 5; i++) many.text('a' + i % 26), many.add(0);
many.done(ar::kSlots + 5);
CHECK(many.shown() == ar::kSlots);
many.done(-3);
CHECK(many.shown() == ar::kSlots);
// the page: 2 cards to a row, 6 to a page, 14 of them
ar::View v;
CHECK(ar::View::pages(14, 6) == 3 && ar::View::pages(12, 6) == 2 && ar::View::pages(0, 6) == 1 && ar::View::pages(5, 0) == 5);
CHECK(!v.move(-1, 0, 0, false, false, 14, 2, 6) && v.choice == 0); // (Left at the first: stays)
CHECK(v.move(1, 0, 0, false, false, 14, 2, 6) && v.choice == 1 && v.page == 0);
CHECK(v.move(0, 1, 0, false, false, 14, 2, 6) && v.choice == 3); // (Down: a row)
CHECK(v.move(0, 2, 0, false, false, 14, 2, 6) && v.choice == 7 && v.page == 1); // (onto the next page: shown)
CHECK(v.move(0, 0, 1, false, false, 14, 2, 6) && v.choice == 13 && v.page == 2); // (a page on, kept to the last card)
CHECK(!v.move(0, 0, 1, false, false, 14, 2, 6) && v.choice == 13);
CHECK(v.move(0, 0, 0, true, false, 14, 2, 6) && v.choice == 0 && v.page == 0); // (Home)
CHECK(v.move(0, 0, 0, false, true, 14, 2, 6) && v.choice == 13 && v.page == 2); // (End)
CHECK(v.turn(-1, 14, 6) && v.page == 1 && v.choice == 13); // (the wheel: the page, not the choice)
CHECK(v.turn(-5, 14, 6) && v.page == 0 && !v.turn(-1, 14, 6) && v.turn(9, 14, 6) && v.page == 2);
CHECK(v.choose(4, 14) && v.choice == 4 && !v.choose(4, 14) && !v.choose(14, 14) && !v.choose(-1, 14));
v.keep(3, 6); // (a shorter list under the choice)
CHECK(v.choice == 2 && v.page == 0);
CHECK(!v.move(1, 0, 0, false, false, 0, 2, 6));
v.keep(0, 6);
CHECK(v.choice == 0 && v.page == 0);
// a notice: in over a quarter of a second, there, out over the last 300 ms, gone
CHECK(ar::notice_in(-1, 4000) == 0 && ar::notice_in(0, 4000) == 0 && ar::notice_in(4000, 4000) == 0 && ar::notice_in(9000, 4000) == 0);
CHECK(ar::notice_in(125, 4000) == 0.5f && ar::notice_in(250, 4000) == 1 && ar::notice_in(2000, 4000) == 1 && ar::notice_in(3700, 4000) == 1);
CHECK(ar::notice_in(3850, 4000) == 0.5f);
if (!fails) printf("PASS\n");
return fails != 0;
}

View File

@@ -0,0 +1,174 @@
/* Achievements on the host's side: host/achievements.c's own text (achievements_actual.inc, cut by
tests/test_achievements.py) against a stand-in save data, a stand-in menu and a game folder of the test's.
Run three times: "off" with HOS_ACHIEVEMENTS=0, then "first" and "second" with it on the same folder, the second the
next start. */
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
/* ---- what host/achievements.c takes from hos.h, hos_paths.h, windows.h and menu_sound.c ---- */
#define MAX_PATH 260
typedef unsigned long DWORD;
#define MOVEFILE_REPLACE_EXISTING 1
static char game_dir[400];
static DWORD hos_game_path_a(char *buf, DWORD size) { return (DWORD)snprintf(buf, size, "%s/Triforce.exe", game_dir); }
static int MoveFileExA(const char *from, const char *to, int flags) {
(void)flags;
remove(to);
return rename(from, to) == 0;
}
static char logged[1 << 14];
static void hos_log(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
const size_t at = strlen(logged);
vsnprintf(logged + at, sizeof logged - at - 2, fmt, ap);
va_end(ap);
strcat(logged, "\n");
}
static int said(const char *text) { /* how often the log has it */
int n = 0;
for (const char *p = logged; (p = strstr(p, text)) != NULL; p++) n++;
return n;
}
#include "achievements_constants.inc"
static int sounds;
static int64_t sound_at;
static void hos_menu_sound_play(int id, int64_t now_ns) {
CHECK(id == HOS_MENU_SOUND_SYS_DECIDE);
sounds++, sound_at = now_ns;
}
/* ---- the menu's side of HOS_OPT_ACH_* (gpu/vk/achievement_rows.h is tested by itself) ---- */
static int renderer; /* 0: no renderer yet (rx_option answers what it is given) */
static char incoming[600], row_text[160][400], notice_day[16][32];
static int incoming_n, row_bits[160], rows_added, rows_shown = -1, notices[16], nnotices;
int rx_option(int id, int otherwise) {
if (!renderer) return otherwise;
CHECK(id == HOS_OPT_ACH_ASK);
return 1;
}
void rx_option_set(int id, int value) {
CHECK(renderer);
if (id == HOS_OPT_ACH_TEXT) {
CHECK(value > 0 && value < 256 && incoming_n + 1 < (int)sizeof incoming);
incoming[incoming_n++] = (char)value;
} else if (id == HOS_OPT_ACH_ADD) {
incoming[incoming_n] = 0, incoming_n = 0;
CHECK(rows_shown < 0 && rows_added < 160);
snprintf(row_text[rows_added], sizeof row_text[0], "%s", incoming);
row_bits[rows_added++] = value;
} else if (id == HOS_OPT_ACH_COUNT) {
CHECK(rows_shown < 0 && !incoming_n);
rows_shown = value;
} else if (id == HOS_OPT_ACH_EARNED) {
incoming[incoming_n] = 0, incoming_n = 0;
CHECK(rows_shown >= 0 && value >= 0 && value < rows_shown && nnotices < 16); /* (never before the list is whole) */
snprintf(notice_day[nnotices], sizeof notice_day[0], "%s", incoming);
notices[nnotices++] = value;
} else CHECK(!"an option of another feature set");
}
#include "achievements.h"
#include "achievements_actual.inc"
/* ---- a stand-in for the save data ---- */
static struct { int kind; const char *what; int value; } have[16];
static int nhave, asked;
static void set(int kind, const char *what, int value) { have[nhave].kind = kind, have[nhave].what = what, have[nhave++].value = value; }
static int probe(void *ctx, int kind, const char *what) {
(void)ctx;
asked++;
for (int i = 0; i < nhave; i++)
if (have[i].kind == kind && !strcmp(have[i].what, what)) return have[i].value;
return -1;
}
#define S 1000000000LL
static void at(int64_t seconds_x10) { hos_achievements_check(probe, NULL, seconds_x10 * (S / 10)); }
static int row_of(const char *name) { /* the place the menu was given an achievement at, -1 none */
const size_t n = strlen(name);
for (int i = 0; i < rows_added; i++)
if (!strncmp(row_text[i], name, n) && row_text[i][n] == '\n') return i;
return -1;
}
static int is_day(const char *text) { /* "2026-11-18" */
return strlen(text) == 10 && text[4] == '-' && text[7] == '-' && text[0] == '2';
}
static char file_text[8192];
static const char *file(const char *name) { /* a file of the game folder as it is now ("" when there is none) */
char path[600];
snprintf(path, sizeof path, "%s/%s", game_dir, name);
FILE *f = fopen(path, "rb");
file_text[f ? fread(file_text, 1, sizeof file_text - 1, f) : 0] = 0;
if (f) fclose(f);
return file_text;
}
static int lines(const char *text) {
int n = 0;
for (; *text; text++) n += *text == '\n';
return n;
}
int main(int argc, char **argv) {
if (argc != 3) return 2;
snprintf(game_dir, sizeof game_dir, "%s", argv[1]);
set(ACH_ITEM, "Weapon_Sword_Kokiri", 1), set(ACH_ITEM, "Item_Stone_Forest", 1), set(ACH_ITEM, "Item_Stone_Fire", 1);
set(ACH_ITEM, "Item_Stone_Water", 1), set(ACH_FLAG, "My_Flag", 1), set(ACH_ITEM, "Item_Token_Gold", 9);
if (!strcmp(argv[2], "off")) { /* turned off: nothing asked of the save data or the menu, nothing read, written or said */
CHECK(!hos_achievements_on());
renderer = 1;
hos_achievements_frame(), at(10), hos_achievements_frame(), at(100);
CHECK(!asked && !logged[0] && !rows_added && rows_shown < 0 && !nnotices && !sounds && !ready && !file("triforce_achievements.txt")[0]);
} else if (!strcmp(argv[2], "first")) {
CHECK(hos_achievements_on());
hos_achievements_frame(); /* the first present, before the renderer answers: the lists are read, nothing is named */
CHECK(said("achievements: 26 (1 of achievements.txt), 0 earned so far") == 1 && said("achievements: 2 left out") == 1);
CHECK(!rows_added && rows_shown < 0);
at(20); /* six earned before the menu has the list: written and said at once, shown only once it has */
CHECK(said("achievements: earned \"") == 6 && said("earned \"A Sword of Your Own\" (1 of 26)") == 1 && said("earned \"Mine\" (6 of 26)") == 1);
CHECK(said("earned \"Three Stones\"") == 1 && !said("Spider Hunter") && !said("could not be written"));
CHECK(lines(file("triforce_achievements.txt")) == 7 && strstr(file_text, " A Sword of Your Own\n") && strstr(file_text, " Mine\n"));
CHECK(!nnotices && !sounds);
renderer = 1;
hos_achievements_frame(); /* the list named, whole, in its order: name, the line about it, the day; the icon and earned */
CHECK(rows_added == 26 && rows_shown == 26 && !nnotices);
CHECK(!strncmp(row_text[0], "A Sword of Your Own\nFind a blade in the forest.\n", 48) && is_day(row_text[0] + 48));
CHECK(row_bits[0] == (1 | 1 << 4 | HOS_ACH_EARNED)); /* (sword green, earned) */
CHECK(!strcmp(row_text[1], "First Melody\nLearn the royal family's song.\n") && row_bits[1] == 4); /* (a note, gold; not earned: no day) */
CHECK(row_of("Mine") == 25 && row_bits[25] == (2 | 3 << 4 | HOS_ACH_EARNED) && row_bits[row_of("Twice as Tough")] == (6 | 7 << 4));
hos_achievements_frame(), hos_achievements_frame();
CHECK(rows_added == 26); /* (named once) */
/* shown one at a time, each for its 4.5 s, in the list's order, with the sound and the day */
at(30);
CHECK(nnotices == 1 && sounds == 1 && sound_at == 3 * S && notices[0] == 0 && is_day(notice_day[0]));
at(40), at(74);
CHECK(nnotices == 1);
at(75);
CHECK(nnotices == 2 && sounds == 2 && notices[1] == row_of("Forest's Thanks"));
for (int t = 120; t <= 400; t += 10) at(t);
CHECK(nnotices == 6 && sounds == 6 && notices[5] == 25);
CHECK(said("achievements: earned \"") == 6 && said("achievements: 26 (") == 1 && lines(file("triforce_achievements.txt")) == 7);
} else {
renderer = 1;
hos_achievements_frame(); /* the next start: what was earned is, and is neither said nor shown again */
CHECK(said("achievements: 26 (1 of achievements.txt), 6 earned so far") == 1 && rows_shown == 26);
at(10);
CHECK(!said("earned \"") && !nnotices && !sounds);
CHECK((row_bits[0] & HOS_ACH_EARNED) && !(row_bits[row_of("Spider Hunter")] & HOS_ACH_EARNED));
have[5].value = 10; /* (the tenth token) */
at(20);
CHECK(said("earned \"Spider Hunter\" (7 of 26)") == 1 && nnotices == 1 && notices[0] == row_of("Spider Hunter") && is_day(notice_day[0]));
/* the file whole again: the seven, and the line of an achievement no list has now, as it was */
CHECK(lines(file("triforce_achievements.txt")) == 9 && strstr(file_text, " Spider Hunter\n") && strstr(file_text, "\ndeadbeef 5 Gone\n"));
CHECK(!file("triforce_achievements.txt.new")[0]);
}
if (!fails) printf("PASS\n");
return fails != 0;
}

190
tests/achievements_test.c Normal file
View File

@@ -0,0 +1,190 @@
/* host/achievements.h alone (tests/test_achievements.py): the list's form and what is refused, when an achievement is
earned, and the file that keeps the earned ones. With "names" as its argument it prints the built-in list's
conditions instead, a line each, for the test to hold against gpu/vk/oot_cheat_items.h. */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "achievements.h"
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
static AchList list, again;
/* ---- a stand-in for the save data ---- */
static struct { int kind; const char *what; int value; } have[16];
static int nhave, asked;
static void set(int kind, const char *what, int value) {
for (int i = 0; i < nhave; i++)
if (have[i].kind == kind && !strcmp(have[i].what, what)) { have[i].value = value; return; }
have[nhave].kind = kind, have[nhave].what = what, have[nhave++].value = value;
}
static int probe(void *ctx, int kind, const char *what) {
CHECK(ctx == (void *)&asked);
asked++;
for (int i = 0; i < nhave; i++)
if (have[i].kind == kind && !strcmp(have[i].what, what)) return have[i].value;
return -1;
}
static const char FORM[] =
"stray line\n# a comment\n[One]\r\nabout: First. \r\nitem A_b 3\r\n\tflag F.x-1\r\n\r\n"
"[No conditions]\nabout: x\n"
"[Bad line]\nitem A\nwhat is this\n"
"[One]\nitem Dup\n"
"[]\nitem A\n"
"[A name that is too long to be an achievement's]\nitem A\n"
"[Bad count]\nitem A 0\n"
"[Bad count 2]\nitem A 10000\n"
"[Bad count 3]\nitem A 3x\n"
"[Flag count]\nflag F 2\n"
"[Bad name]\nitem A$b\n"
"[No name]\nitem\n"
"[No bracket\nitem A\n"
"[ Last ]\n# its line about it is left out\nitem Z 2";
static void built_in(int print) {
int refused = 0;
memset(&list, 0, sizeof list);
const int n = ach_parse(&list, ACH_BUILTIN, &refused);
if (print) {
for (int i = 0; i < list.n; i++)
for (int k = 0; k < list.a[i].nconds; k++) printf("%c %s %d\n", list.a[i].conds[k].kind, list.a[i].conds[k].what, list.a[i].conds[k].least);
return;
}
CHECK(n == 25 && list.n == 25 && refused == 0);
for (int i = 0; i < list.n; i++) {
const Ach *a = &list.a[i];
CHECK(a->name[0] && a->about[0] && a->nconds >= 1 && !a->earned && a->key == ach_key(a->name));
CHECK(ach_find(&list, a->key) == i);
for (int k = 0; k < a->nconds; k++) CHECK(a->conds[k].kind == ACH_ITEM && a->conds[k].least >= 1);
}
CHECK(ach_key("") == 2166136261u && ach_key("a") == 0xe40c292cu);
CHECK(list.a[0].icon == (1 | 1 << 4) && list.a[1].icon == 4 && list.a[24].icon == (6 | 7 << 4)); /* (sword green, a note, shield silver) */
}
static void form(void) {
int refused = 0;
memset(&list, 0, sizeof list);
CHECK(ach_parse(&list, FORM, &refused) == 2 && list.n == 2);
CHECK(refused == 12);
const Ach *one = &list.a[0], *last = &list.a[1];
CHECK(!strcmp(one->name, "One") && !strcmp(one->about, "First.") && one->nconds == 2);
CHECK(one->conds[0].kind == ACH_ITEM && !strcmp(one->conds[0].what, "A_b") && one->conds[0].least == 3);
CHECK(one->conds[1].kind == ACH_FLAG && !strcmp(one->conds[1].what, "F.x-1") && one->conds[1].least == 1);
CHECK(!strcmp(last->name, "Last") && !last->about[0] && last->nconds == 1 && last->conds[0].least == 2 && !strcmp(last->conds[0].what, "Z"));
CHECK(ach_parse(&list, "", &refused) == 0 && ach_parse(&list, "\n\n# nothing\n", &refused) == 0 && refused == 12 && list.n == 2);
CHECK(ach_parse(&list, "[Whole]\nitem W\n", NULL) == 1 && list.n == 3);
/* an icon: a shape, and a tone or none (gold); without the line a gold star; anything else is no achievement */
CHECK(ach_icon_parse("star", 4) == 0 && ach_icon_parse("gem blue", 8) == (2 | 3 << 4) && ach_icon_parse("shield \t silver", 15) == (6 | 7 << 4));
CHECK(ach_icon_parse("", 0) < 0 && ach_icon_parse("circle", 6) < 0 && ach_icon_parse("gem pink", 8) < 0 && ach_icon_parse("gem blue x", 10) < 0);
CHECK(ach_icon_parse("gemblue", 7) < 0 && ach_icon_parse("Gem", 3) < 0 && ach_icon_parse("blue", 4) < 0);
CHECK(one->icon == 0 && list.a[2].icon == 0);
refused = 0;
CHECK(ach_parse(&list, "[Pictured]\nicon: medal purple \nitem P\n[Unpictured]\nicon: medal mauve\nitem P\n[Bare]\nicon:\nitem P\n", &refused) == 1);
CHECK(refused == 2 && list.a[list.n - 1].icon == (3 | 4 << 4));
refused = 12;
/* ACH_CONDS conditions are an achievement, one more is none; a name of ACH_NAME bytes is one; a name's 95 bytes */
static char text[1 << 16];
for (int over = 0; over < 2; over++) {
int at = snprintf(text, sizeof text, "[%s]\n", over ? "Over" : "Full");
for (int i = 0; i < ACH_CONDS + over; i++) at += snprintf(text + at, sizeof text - (size_t)at, "item I%d\n", i);
refused = 0;
CHECK(ach_parse(&list, text, &refused) == !over && refused == over);
}
CHECK(ach_parse(&list, "[0123456789012345678901234567890123456789]\nitem A\n", NULL) == 1 && strlen(list.a[list.n - 1].name) == ACH_NAME);
memset(text, 'n', sizeof text);
memcpy(text, "[Long]\nflag ", 12);
text[12 + ACH_WHAT - 1] = 0;
CHECK(ach_parse(&list, text, NULL) == 1 && strlen(list.a[list.n - 1].conds[0].what) == ACH_WHAT - 1);
text[12 + ACH_WHAT - 1] = 'n', text[12 + ACH_WHAT] = 0;
memcpy(text, "[Lon2]", 6);
refused = 0;
CHECK(ach_parse(&list, text, &refused) == 0 && refused == 1);
/* no more than ACH_MAX */
memset(&list, 0, sizeof list);
int at = 0;
for (int i = 0; i < ACH_MAX + 2; i++) at += snprintf(text + at, sizeof text - (size_t)at, "[N%d]\nitem A\n", i);
refused = 0;
CHECK(ach_parse(&list, text, &refused) == ACH_MAX && list.n == ACH_MAX && refused == 2);
}
static void earning(void) {
int fresh[4];
memset(&list, 0, sizeof list);
CHECK(ach_parse(&list, FORM, NULL) == 2);
CHECK(ach_check(&list, probe, &asked, 100, fresh, 4) == 0 && !list.a[0].earned && !list.a[1].earned);
set(ACH_ITEM, "A_b", 3);
CHECK(ach_check(&list, probe, &asked, 100, fresh, 4) == 0); /* (the flag is not in the save data) */
set(ACH_FLAG, "F.x-1", 0);
CHECK(ach_check(&list, probe, &asked, 100, fresh, 4) == 0);
set(ACH_FLAG, "A_b", 1), set(ACH_ITEM, "F.x-1", 9); /* (an item of a flag's name is not that flag) */
CHECK(ach_check(&list, probe, &asked, 100, fresh, 4) == 0);
set(ACH_FLAG, "F.x-1", 1), set(ACH_ITEM, "A_b", 2);
CHECK(ach_check(&list, probe, &asked, 100, fresh, 4) == 0); /* (one too few) */
set(ACH_ITEM, "A_b", 3);
CHECK(ach_check(&list, probe, &asked, 101, fresh, 4) == 1 && fresh[0] == 0 && list.a[0].earned && list.a[0].when == 101 && !list.a[1].earned);
CHECK(ach_earned_count(&list) == 1);
/* earned stays earned, and is not asked about again */
set(ACH_ITEM, "A_b", 0), set(ACH_FLAG, "F.x-1", 0);
asked = 0;
CHECK(ach_check(&list, probe, &asked, 102, fresh, 4) == 0 && asked == 1 && list.a[0].earned && list.a[0].when == 101);
set(ACH_ITEM, "Z", 1);
CHECK(ach_check(&list, probe, &asked, 102, fresh, 4) == 0);
set(ACH_ITEM, "Z", 7);
CHECK(ach_check(&list, probe, &asked, 103, fresh, 4) == 1 && fresh[0] == 1 && list.a[1].when == 103 && ach_earned_count(&list) == 2);
asked = 0;
CHECK(ach_check(&list, probe, &asked, 104, fresh, 4) == 0 && asked == 0);
/* no more than the caller has room for: the rest at the next call */
memset(&list, 0, sizeof list);
CHECK(ach_parse(&list, "[P]\nitem Z\n[Q]\nitem Z\n[R]\nitem Z\n", NULL) == 3);
CHECK(ach_check(&list, probe, &asked, 1, fresh, 2) == 2 && fresh[0] == 0 && fresh[1] == 1 && !list.a[2].earned);
CHECK(ach_check(&list, probe, &asked, 2, fresh, 2) == 1 && fresh[0] == 2 && list.a[2].when == 2);
}
static void kept(void) {
static char text[4096], more[4096];
int fresh[4];
memset(&list, 0, sizeof list);
CHECK(ach_parse(&list, FORM, NULL) == 2);
size_t n = ach_earned_write(&list, text, sizeof text);
CHECK(n == strlen(text) && text[0] == '#' && strchr(text, '\n') == text + n - 1); /* (none earned: the first line alone) */
set(ACH_ITEM, "Z", 2);
CHECK(ach_check(&list, probe, &asked, 1700000000, fresh, 4) == 1);
n = ach_earned_write(&list, text, sizeof text);
char line[80];
snprintf(line, sizeof line, "\n%08x 1700000000 Last\n", (unsigned)ach_key("Last"));
CHECK(n && strstr(text, line) && !strstr(text, " One\n"));
CHECK(ach_earned_write(&list, more, 40) == 0 && ach_earned_write(&list, more, n) == 0 && ach_earned_write(&list, more, n + 1) == n);
/* the next start: the same lists, the file read */
memset(&again, 0, sizeof again);
CHECK(ach_parse(&again, FORM, NULL) == 2);
CHECK(ach_earned_read(&again, text) == 1 && !again.a[0].earned && again.a[1].earned && again.a[1].when == 1700000000 && !again.norphans);
CHECK(ach_earned_read(&again, text) == 0); /* (read twice: marked once) */
/* a line of an achievement no list has is kept as it is, once; what is not such a line is dropped */
snprintf(more, sizeof more, "%sdeadbeef 5 Gone from the lists\r\nhello\n1234 5 short key\ndeadbeef 6 Gone again\n0000000g 1 x\n%08x -3 One\n", text,
(unsigned)ach_key("One"));
memset(&again, 0, sizeof again);
CHECK(ach_parse(&again, FORM, NULL) == 2);
CHECK(ach_earned_read(&again, more) == 2 && again.a[0].earned && again.a[0].when == 0 && again.norphans == 1);
CHECK(!strcmp(again.orphans[0], "deadbeef 5 Gone from the lists"));
n = ach_earned_write(&again, text, sizeof text);
CHECK(n && strstr(text, "\ndeadbeef 5 Gone from the lists\n") && !strstr(text, "hello") && !strstr(text, "short key") && !strstr(text, "Gone again"));
CHECK(strstr(text, " 0 One\n") && strstr(text, " 1700000000 Last\n"));
}
int main(int argc, char **argv) {
if (argc > 1 && !strcmp(argv[1], "names")) return built_in(1), 0;
built_in(0);
form();
earning();
kept();
if (!fails) printf("PASS\n");
return fails != 0;
}

View File

@@ -0,0 +1,297 @@
/* Authored mixes, packets and samples around the actual effect bodies and setters. No audio device or game is opened.
Run with HOS_AUDIO_EFFECTS=0 it checks that every effect leaves the samples as they were. */
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { uint64_t x[32]; uint8_t vreg[32][16]; } Ctx;
static _Alignas(8) uint8_t guest[65536];
#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)
/* Single-threaded test boundaries: the lock is checked for balance only. */
static int lock_depth;
static void au_lock(void) { CHECK(lock_depth++ == 0); }
static void au_unlock(void) { CHECK(--lock_depth == 0); }
#include "actual_audio_effects.inc"
/* the voice's source: one authored sample a frame (render_voice's resampler at 1.0 hands them through a frame late) */
static float tone[4096];
static int tone_pos;
static int next_source_frame(Voice *v) {
v->cur[0] = tone[tone_pos++ % 4096];
return 1;
}
#define PI 3.14159265358979f
enum { CONFIG = 0x10, MIX0 = 0x40, MIX1 = 0x50, BQ = 0x100, DL = 0x110, RV = 0x120, SPLIT = 0x130, PACKET = 0x200,
IN_CH = 0x300, OUT_CH = 0x310, BUFFER = 0x1000 };
static int effects; /* (HOS_AUDIO_EFFECTS: what this run expects) */
static Ctx call(uint64_t a0, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6) {
Ctx c;
memset(&c, 0, sizeof c);
c.x[0] = a0; c.x[1] = a1; c.x[2] = a2; c.x[3] = a3; c.x[4] = a4; c.x[5] = a5; c.x[6] = a6;
return c;
}
static Ctx with_float(uint64_t obj, float f) {
Ctx c = call(obj, 0, 0, 0, 0, 0, 0);
memcpy(&c.vreg[0][0], &f, 4);
return c;
}
static void reset(void) {
memset(guest, 0, sizeof guest);
memset(mixes, 0, sizeof mixes);
memset(splitters, 0, sizeof splitters);
memset(voices, 0, sizeof voices);
memset(biquads, 0, sizeof biquads);
for (int e = 0; e < MAX_FX; e++) free(delays[e].line);
memset(delays, 0, sizeof delays);
memset(reverbs, 0, sizeof reverbs);
ncmds = 0;
for (int m = 0; m < 2; m++) mixes[m].used = 1, mixes[m].buffers = 2, mixes[m].volume = 1.0f;
put_handle(MIX0, K_MIX, 0);
put_handle(MIX1, K_MIX, 1);
}
static void channels(const int8_t *in, const int8_t *out, int n) {
memcpy(GP(IN_CH), in, (size_t)n);
memcpy(GP(OUT_CH), out, (size_t)n);
}
static void float_packet(int on, const float coefs[5]) {
uint8_t packet[24];
memset(packet, 0xa5, sizeof packet);
packet[0] = (uint8_t)on;
memcpy(packet + 4, coefs, 5 * sizeof *coefs);
memcpy(GP(PACKET), packet, sizeof packet);
}
static float rms(const float *x, int n) {
double e = 0;
for (int i = 0; i < n; i++) e += (double)x[i] * x[i];
return (float)sqrt(e / n);
}
/* ---- biquad filter: a Butterworth low pass at 1 kHz (the cookbook's, its denominator added and over a0) ---------- */
static void lowpass(float f0, float out[5]) {
const float w = 2 * PI * f0 / RATE, alpha = sinf(w) / (2 * 0.7071068f), a0 = 1 + alpha;
out[0] = (1 - cosf(w)) / 2 / a0;
out[1] = (1 - cosf(w)) / a0;
out[2] = out[0];
out[3] = 2 * cosf(w) / a0; /* (-a1 / a0) */
out[4] = -(1 - alpha) / a0; /* (-a2 / a0) */
}
static float biquad_tone(float hz) { /* the filtered tone's level against its own, after it has settled */
reset();
Ctx c = call(CONFIG, BQ, MIX0, 0, 0, 0, 0);
imp__ZN2nn5audio15AddBiquadFilterEPNS0_19AudioRendererConfigEPNS0_16BiquadFilterTypeEPNS0_10SubMixTypeE(&c);
CHECK(c.x[0] == 0);
channels((const int8_t[]){0, 1}, (const int8_t[]){0, 1}, 2);
c = call(BQ, IN_CH, OUT_CH, 2, 0, 0, 0);
imp__ZN2nn5audio26SetBiquadFilterInputOutputEPNS0_16BiquadFilterTypeEPKaS4_i(&c);
float coefs[5];
lowpass(1000, coefs);
float_packet(1, coefs);
c = call(BQ, PACKET, 0, 0, 0, 0, 0);
imp__ZN2nn5audio24SetBiquadFilterParameterEPNS0_16BiquadFilterTypeERKNS0_22TBiquadFilterParameterIfEE(&c);
memset(GP(PACKET), 0xa5, 24); /* (the game's packet is its own again at once) */
c = call(BQ, 1, 0, 0, 0, 0, 0);
imp__ZN2nn5audio22SetBiquadFilterEnabledEPNS0_16BiquadFilterTypeEb(&c);
c = call(BQ, 0, 0, 0, 0, 0, 0);
imp__ZN2nn5audio21IsBiquadFilterEnabledEPKNS0_16BiquadFilterTypeE(&c);
CHECK(c.x[0] == 1);
float in = 0, out = 0;
for (int frame = 0; frame < 20; frame++) {
for (int i = 0; i < FRAME; i++) {
const float s = sinf(2 * PI * hz * (float)(frame * FRAME + i) / RATE);
mixes[0].buf[0][i] = s;
mixes[0].buf[1][i] = 0.5f * s;
}
if (frame == 19) in = rms(mixes[0].buf[0], FRAME);
effects_frame(0);
if (frame == 19) {
out = rms(mixes[0].buf[0], FRAME);
CHECK(fabsf(rms(mixes[0].buf[1], FRAME) - 0.5f * out) < 0.001f); /* (each channel its own filter) */
}
}
return out / in;
}
static void biquad(void) {
const float low = biquad_tone(200), high = biquad_tone(10000);
if (!effects) { CHECK(low == 1.0f && high == 1.0f); return; }
CHECK(low > 0.95f && low < 1.05f);
CHECK(high < 0.02f); /* (two poles: 40 dB down a decade above) */
/* disabled: the buffers as they came */
Ctx c = call(BQ, 0, 0, 0, 0, 0, 0);
imp__ZN2nn5audio22SetBiquadFilterEnabledEPNS0_16BiquadFilterTypeEb(&c);
for (int i = 0; i < FRAME; i++) mixes[0].buf[0][i] = (float)(i % 7) - 3.0f;
effects_frame(0);
for (int i = 0; i < FRAME; i++) CHECK(mixes[0].buf[0][i] == (float)(i % 7) - 3.0f);
}
/* ---- delay: 3 ms (144 frames), in gain 0.5, feedback 0.5 ---------------------------------------------------------- */
static void add_delay_on_mix0(float spread, float lowpass_amount) {
reset();
Ctx c = call(CONFIG, DL, BUFFER, 0x10000, MIX0, 10000000, 2); /* (10 ms at most, 2 channels) */
imp__ZN2nn5audio8AddDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPvmPNS0_10SubMixTypeENS_8TimeSpanEi(&c);
CHECK(c.x[0] == 0);
channels((const int8_t[]){0, 1}, (const int8_t[]){0, 1}, 2);
c = call(DL, IN_CH, OUT_CH, 2, 0, 0, 0);
imp__ZN2nn5audio19SetDelayInputOutputEPNS0_9DelayTypeEPKaS4_i(&c);
c = call(DL, 3000000, 0, 0, 0, 0, 0);
imp__ZN2nn5audio12SetDelayTimeEPNS0_9DelayTypeENS_8TimeSpanE(&c);
c = call(DL, 0, 0, 0, 0, 0, 0);
imp__ZN2nn5audio12GetDelayTimeEPKNS0_9DelayTypeE(&c);
CHECK(c.x[0] == 3000000);
c = with_float(DL, 0.5f); imp__ZN2nn5audio14SetDelayInGainEPNS0_9DelayTypeEf(&c);
c = with_float(DL, 0.5f); imp__ZN2nn5audio20SetDelayFeedbackGainEPNS0_9DelayTypeEf(&c);
c = with_float(DL, 1.0f); imp__ZN2nn5audio15SetDelayDryGainEPNS0_9DelayTypeEf(&c);
c = with_float(DL, spread); imp__ZN2nn5audio21SetDelayChannelSpreadEPNS0_9DelayTypeEf(&c);
c = with_float(DL, lowpass_amount); imp__ZN2nn5audio21SetDelayLowPassAmountEPNS0_9DelayTypeEf(&c);
c = call(DL, 1, 0, 0, 0, 0, 0);
imp__ZN2nn5audio15SetDelayEnabledEPNS0_9DelayTypeEb(&c);
c = call(DL, 0, 0, 0, 0, 0, 0);
imp__ZN2nn5audio14IsDelayEnabledEPKNS0_9DelayTypeE(&c);
CHECK(c.x[0] == 1);
}
static void run_impulse(float out[2][4 * FRAME]) { /* an impulse in channel 0 at frame 0, four frames out */
for (int frame = 0; frame < 4; frame++) {
memset(mixes[0].buf, 0, sizeof mixes[0].buf);
if (frame == 0) mixes[0].buf[0][0] = 1.0f;
effects_frame(0);
for (int ch = 0; ch < 2; ch++) memcpy(out[ch] + frame * FRAME, mixes[0].buf[ch], FRAME * sizeof(float));
}
}
static void close_to(float actual, float expected) { CHECK(fabsf(actual - expected) < 0.00001f); }
static void delay(void) {
static float out[2][4 * FRAME];
add_delay_on_mix0(0, 0);
run_impulse(out);
if (!effects) {
for (int i = 0; i < 4 * FRAME; i++) CHECK(out[0][i] == (i == 0) && out[1][i] == 0);
return;
}
/* the dry impulse, then echoes every 144 frames (across the frames' edges), each the feedback gain x 0.98 of the
one before; nothing in between and nothing in the other channel */
const float fb = 0.5f * 0.98f;
for (int i = 0; i < 4 * FRAME; i++) {
const float want = i == 0 ? 1.0f : i % 144 == 0 ? 0.5f * powf(fb, (float)(i / 144 - 1)) : 0.0f;
close_to(out[0][i], want);
CHECK(out[1][i] == 0);
}
/* the whole spread: each echo in the other channel than the one before */
add_delay_on_mix0(1, 0);
run_impulse(out);
close_to(out[0][144], 0.5f); close_to(out[1][144], 0);
close_to(out[1][288], 0.5f * fb); close_to(out[0][288], 0);
close_to(out[0][432], 0.5f * fb * fb); close_to(out[1][432], 0);
/* the low pass: the echo's first frame 1 - 0.95 x amount of it, the rest after it */
add_delay_on_mix0(0, 0.5f);
run_impulse(out);
close_to(out[0][144], 0.5f * (1 - 0.475f));
CHECK(out[0][145] > 0 && out[0][145] < out[0][144]);
/* off: the buffers as they came */
Ctx c = call(DL, 0, 0, 0, 0, 0, 0);
imp__ZN2nn5audio15SetDelayEnabledEPNS0_9DelayTypeEb(&c);
run_impulse(out);
for (int i = 0; i < 4 * FRAME; i++) CHECK(out[0][i] == (i == 0) && out[1][i] == 0);
/* removed: the game's buffer back, the handle gone */
c = call(CONFIG, DL, MIX0, 0, 0, 0, 0);
imp__ZN2nn5audio11RemoveDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPNS0_10SubMixTypeE(&c);
CHECK(c.x[0] == BUFFER && G64(DL) == 0 && !delays[0].used);
}
/* ---- plain reverb: a tail after the dry impulse, and the dry signal alone at out gain 0 ------------------------- */
static void reverb(void) {
reset();
Ctx c = call(CONFIG, RV, BUFFER, 0x10000, MIX0, 2, 0);
imp__ZN2nn5audio9AddReverbEPNS0_19AudioRendererConfigEPNS0_10ReverbTypeEPvmPNS0_10SubMixTypeEi(&c);
CHECK(c.x[0] == 0);
c = call(RV, 2, 0, 0, 0, 0, 0); imp__ZN2nn5audio18SetReverbEarlyModeEPNS0_10ReverbTypeENS1_9EarlyModeE(&c);
c = call(RV, 1, 0, 0, 0, 0, 0); imp__ZN2nn5audio17SetReverbLateModeEPNS0_10ReverbTypeENS1_8LateModeE(&c);
c = call(RV, 1500000000, 0, 0, 0, 0, 0); imp__ZN2nn5audio18SetReverbDecayTimeEPNS0_10ReverbTypeENS_8TimeSpanE(&c);
c = call(RV, 10000000, 0, 0, 0, 0, 0); imp__ZN2nn5audio21SetReverbPredelayTimeEPNS0_10ReverbTypeENS_8TimeSpanE(&c);
c = with_float(RV, 0.6f); imp__ZN2nn5audio18SetReverbEarlyGainEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 0.6f); imp__ZN2nn5audio17SetReverbLateGainEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 0.8f); imp__ZN2nn5audio19SetReverbReverbGainEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 1.0f); imp__ZN2nn5audio16SetReverbOutGainEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 0.5f); imp__ZN2nn5audio16SetReverbDryGainEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 0.5f); imp__ZN2nn5audio32SetReverbHighFrequencyDecayRatioEPNS0_10ReverbTypeEf(&c);
c = with_float(RV, 0.5f); imp__ZN2nn5audio19SetReverbColorationEPNS0_10ReverbTypeEf(&c);
c = call(RV, 1, 0, 0, 0, 0, 0); imp__ZN2nn5audio16SetReverbEnabledEPNS0_10ReverbTypeEb(&c);
c = call(RV, 0, 0, 0, 0, 0, 0); imp__ZN2nn5audio15IsReverbEnabledEPKNS0_10ReverbTypeE(&c);
CHECK(c.x[0] == 1);
static float out[2][4 * FRAME];
run_impulse(out);
if (!effects) {
for (int i = 0; i < 4 * FRAME; i++) CHECK(out[0][i] == (i == 0) && out[1][i] == 0);
return;
}
close_to(out[0][0], 0.5f); /* (the dry gain; the wet part starts after the pre-delay) */
float tail = 0;
for (int i = 1; i < 4 * FRAME; i++) {
CHECK(isfinite(out[0][i]) && isfinite(out[1][i]) && fabsf(out[0][i]) < 1 && fabsf(out[1][i]) < 1);
tail += fabsf(out[0][i]) + fabsf(out[1][i]);
}
CHECK(tail > 0.05f);
c = with_float(RV, 0.0f); imp__ZN2nn5audio16SetReverbOutGainEPNS0_10ReverbTypeEf(&c);
run_impulse(out);
for (int i = 0; i < 4 * FRAME; i++) { close_to(out[0][i], i == 0 ? 0.5f : 0.0f); CHECK(out[1][i] == 0); }
}
/* ---- a splitter destination's filter, through render_voice to the destination's mix ---------------------------- */
static float splitter_level(int filter_on) {
reset();
Splitter *s = &splitters[0];
s->used = 1; s->channels = 1; s->dests = 1; s->dest_mix[0] = 1;
s->vol[0][0][0] = s->ramp[0][0][0] = 1.0f;
put_handle(SPLIT, K_SPLITTER, 0);
/* a one-pole low pass, y = 0.1 x + 0.9 y': a tone at half the rate comes out at 0.1 / 1.9 */
float_packet(filter_on, (const float[]){0.1f, 0, 0, 0.9f, 0});
Ctx c = call(SPLIT, 0, MIX1, 1, PACKET, 0, 0);
imp__ZN2nn5audio32SetSplitterBiquadFilterParameterEPNS0_12SplitterTypeEiPNS0_10SubMixTypeEiRKNS0_22TBiquadFilterParameterIfEE(&c);
CHECK(ncmds == 1 && !s->bq_on[0][1]); /* (queued, applied at the update) */
commit();
CHECK(s->bq_on[0][1] == filter_on && !s->bq_on[0][0]);
Voice *v = &voices[0];
v->used = 1; v->channels = 1; v->rate = RATE; v->pitch = 1; v->volume = v->ramp_volume = 1;
v->state = PLAY_STARTED; v->dest_kind = DEST_SPLITTER; v->dest = 0;
for (int i = 0; i < 4096; i++) tone[i] = i & 1 ? -1.0f : 1.0f;
tone_pos = 0;
float level = 0;
for (int frame = 0; frame < 10; frame++) {
memset(mixes[1].buf, 0, sizeof mixes[1].buf);
render_voice(v);
level = rms(mixes[1].buf[0] + 16, FRAME - 16);
}
return level;
}
static void splitter(void) {
const float plain = splitter_level(0), filtered = splitter_level(1);
CHECK(fabsf(plain - 1.0f) < 0.0001f);
if (!effects) CHECK(filtered == plain);
else CHECK(fabsf(filtered - 0.1f / 1.9f) < 0.001f);
}
int main(void) {
const char *e = getenv("HOS_AUDIO_EFFECTS");
effects = !(e && atoi(e) == 0);
CHECK(effects_on() == effects);
biquad();
delay();
reverb();
splitter();
reset();
free(cmds);
printf("PASS (%s): biquad low pass, delay echo/feedback/spread/low pass, plain reverb tail, splitter filter\n",
effects ? "effects on" : "HOS_AUDIO_EFFECTS=0: samples unchanged");
return 0;
}

View File

@@ -20,6 +20,8 @@ static void hos_log(const char *format, ...) { (void)format; }
/* (host/rx_backend.c's: the lazy write-back's calls around the wave buffer dump, which no switch turns on here) */
static int hos_guest_io_begin(const void *p, uint64_t n) { (void)p; (void)n; return 0; }
static void hos_guest_io_end(int held) { CHECK(!held); }
/* ((Triforce, 11-21, MEM-09) host/mkw_glue.c's: the dump's buffer read once before the kernel's write; not what is tested) */
static void hos_guest_touch(uint64_t addr, uint64_t size) { (void)addr; (void)size; }
/* Single-threaded test boundaries: the mixer's frames run between the game's calls, as its thread can. */
static int lock_depth;

View File

@@ -60,6 +60,7 @@ uint64_t gpu_bind_stamp() { return bind_stamp; }
struct RecordingRes { VkDescriptorPool pool{}; std::vector<VkDescriptorPool> extra; } recording;
RecordingRes* cur = &recording;
VkDescriptorPool make_pool() { return handle<VkDescriptorPool>(0x77); }
void pool_took(const VkWriteDescriptorSet*, size_t) {} // ((Triforce, 11-22, DESC-33) vk_draw.cpp: the sets a recording took, counted)
void vkDestroyDescriptorPool(VkDevice, VkDescriptorPool, const VkAllocationCallbacks*) {}
// ---- the fake driver: each set's descriptors, (binding, element) -> (type, the handles and numbers that make it) ----

96
tests/dmb_light_test.c Normal file
View File

@@ -0,0 +1,96 @@
/* tests/test_dmb_light.py's driver ((Triforce, 11-22, the performance audit's LIFT-47)): each shape (a barrier alone, or
between a store and a load of the same guest address) as lifted with A64_DMB_LIGHT=1 against the same words as the
kit's translator writes them. Both start from one random Ctx and guest memory; the whole Ctx and the bytes the shape
stores must match after, and the calls of recomp_barrier (counted here) must be the kit's count without the light
barriers' on x86-64.
dmb_light_test <n> <seed> n random states for each shape; the flags go through all sixteen combinations
Exit 0: no mismatch. 1: a mismatch. 2: no guest memory where the shapes store. */
#include "a64.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <sys/mman.h>
#endif
static long fences; /* (the runtime's full fence, a64_rt.c's, counted) */
void recomp_barrier(void) {
fences++;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
}
#include "dmb_lifted.h"
enum { kGuestLo = 0x10000, kGuestLen = 1 << 20 }; /* (the guest memory the shapes use, at A64_PTR(kGuestLo)) */
static int map_at(void *p, size_t n) {
#ifdef _WIN32
return VirtualAlloc(p, n, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == p;
#else
return mmap(p, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == p;
#endif
}
static uint64_t rs;
static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; }
static uint64_t edge64(void) {
static const uint64_t e[] = {0, 1, 0x7fffffffffffffffULL, 0x8000000000000000ULL, ~0ULL, 0xffffffffULL, 0x100000000ULL};
return (rnd() & 3) ? rnd() : e[rnd() % (sizeof e / sizeof e[0])];
}
static void fill(Ctx *c, long i) {
memset(c, 0, sizeof *c);
for (int r = 0; r < 31; r++) c->x[r] = edge64();
c->x[2] = kGuestLo + 0x1000 + (rnd() % (kGuestLen - 0x2000)); /* (the shapes' base register: unaligned too) */
c->x[31] = kGuestLo + 0x800;
c->n = (uint8_t)(i & 1), c->z = (uint8_t)(i >> 1 & 1), c->c = (uint8_t)(i >> 2 & 1), c->v = (uint8_t)(i >> 3 & 1);
for (int r = 0; r < 32; r++) c->vreg[r][0] = edge64(), c->vreg[r][1] = edge64();
c->fpcr = (rnd() & 1) ? 0x01000000u : 0, c->fpsr = rnd() & 0x9f;
}
int main(int argc, char **argv) {
if (!map_at(A64_PTR(kGuestLo), kGuestLen)) {
printf("no guest memory at %p\n", (void *)A64_PTR(kGuestLo));
return 2;
}
const long n = argc > 1 ? atol(argv[1]) : 20000;
rs = argc > 2 ? strtoull(argv[2], 0, 10) : 1;
if (!rs) rs = 1;
const int ncases = (int)(sizeof CASES / sizeof CASES[0]);
#if defined(__x86_64__)
const int host_light = 1;
#else
const int host_light = 0; /* (any other host: the light barrier is the full fence too) */
#endif
long bad = 0, checks = 0;
for (int k = 0; k < ncases; k++)
for (long i = 0; i < n; i++) {
static Ctx a, b;
uint8_t before[16], ma[16], mb[16];
fill(&a, i);
b = a;
uint8_t *mem = A64_PTR(a.x[2]);
for (int j = 0; j < 16; j++) mem[j] = (uint8_t)rnd();
memcpy(before, mem, 16);
fences = 0;
CASES[k].now(&a);
const long fa = fences;
memcpy(ma, mem, 16);
memcpy(mem, before, 16);
fences = 0;
CASES[k].old(&b);
const long fb = fences;
memcpy(mb, mem, 16);
checks++;
const int state = memcmp(&a, &b, sizeof a) || memcmp(ma, mb, 16);
const int count = fb != CASES[k].barriers || fa != CASES[k].barriers - (host_light ? CASES[k].light : 0);
if (state || count) {
if (bad++ < 5)
printf("MISMATCH %s: %s%sfences now %ld, kit %ld (of %d, %d light)\n", CASES[k].name,
state ? "registers, flags or memory differ; " : "", count ? "fence count; " : "", fa, fb,
CASES[k].barriers, CASES[k].light);
}
}
printf("%d shapes, %ld checks, %ld mismatches\n", ncases, checks, bad);
return bad ? 1 : 0;
}

View File

@@ -19,6 +19,8 @@
#include <utility>
#include <vector>
#include "barrier_rules.h"
#include "desc_pool_grow.h" // ((Triforce, 11-22, DESC-33) a slot's pool at half, whole once a recording ran out)
namespace desc_pool_grow = mkwvk::desc_pool_grow;
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*;
@@ -55,7 +57,10 @@ void draw_flush_recording(int slot);
VkCommandBuffer rec_cmd{};
uint64_t rec_value=0,targets_serial=0;
VkBuffer zero_buffer{}; VmaAllocation zero_alloc{};
VkDescriptorPool make_pool() { return handle<VkDescriptorPool>(); }
VkDescriptorPool make_pool(uint32_t = 0) { return handle<VkDescriptorPool>(); }
bool pool_grow_on() { return true; } // ((Triforce, 11-22, DESC-33) vk_draw.cpp: RX_VK_DESC_POOL_GROW and its counts)
desc_pool_grow::Took pool_most;
uint64_t pools_added = 0, pools_whole = 0;
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; }
@@ -90,7 +95,9 @@ bool batch=false;bool batch_on() { return batch; }
namespace barrier_rules = mkwvk::barrier_rules; // (the held submits' rule, gpu/vk/barrier_rules.h: RX_VK_BATCH=1 here)
barrier_rules::Batch batch_way() { return batch ? barrier_rules::Batch::Hold : barrier_rules::Batch::Off; }
int submit_depth=0;
int flush_reason=0;constexpr int FR_OTHER=0,FR_CAP=1;
int flush_reason=0;constexpr int FR_OTHER=0,FR_CAP=1,FR_IDLE=2;
bool batch_idle_on() { return false; } // ((Triforce, 11-22, SYNC-09) RX_VK_BATCH_IDLE: off, as unset; not this test's)
bool batch_gpu_idle() { return false; }
bool event_writes_held(VkCommandBuffer) { return false; } // (no event's write held: RX_VK_EVENT_BARRIER's, not this test's)
void split_part_submitted(VkCommandBuffer,bool) {} // (RX_VK_SPLIT_BARRIER's count)
void end_pass(VkCommandBuffer) { require(false,"a pass ended at a submit"); }

View File

@@ -119,7 +119,7 @@ int main(int argc,char** argv) {
if(which=="empty") {
consume_dump(false);require(waits==0 && invalidations==0 && png.empty(),"empty queue reads/waits");return 0;
}
armed=true;
dump_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);

View File

@@ -2,8 +2,10 @@
// initialization/BDA functions are included verbatim from current vk_draw.cpp.
#include <algorithm>
#include <array>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <unordered_map>
@@ -71,6 +73,8 @@ static uint64_t vkGetBufferDeviceAddress(int device, const VkBufferDeviceAddress
return ++address_calls == zero_address ? 0 : info->buffer * 4096;
}
static void logf(const char*, ...) {} // ((11-21, MEM-154) the new pairs' line)
#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
@@ -155,6 +159,44 @@ static void run(const std::string& name) {
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 if (name == "payload_growth_keeps_records") {
// ((11-21, the performance audit's MEM-154) the records are the same size for every program: a program whose
// payloads need more takes a bigger payload buffer, and the records the thread has already)
require(fallback_buffers_of(p, out) && out && out->payload_bytes == 44 && create_calls == 2, "first program's pair");
const VkBuffer records = out->records;
const uint64_t records_address = out->records_address;
Program bigger;
bigger.fallback_payload_words = 8;
require(fallback_buffers_of(bigger, out) && out && out->payload_bytes == 116, "a bigger payload was not made");
require(out->records == records && out->records_address == records_address && create_calls == 3 && live.size() == 3,
"a bigger payload made another records buffer");
const auto calls = create_calls;
require(fallback_buffers_of(p, out) && out->payload_bytes == 116 && create_calls == calls, "the smaller program made more");
require(destroy_calls == 0, "a growth destroyed a buffer");
} else if (name == "shared_records_payload_failure") {
// ((11-21, MEM-154) a growth that shares the records and then cannot make its payload: its create fails, and on
// the next try its address comes back 0. The shared records must stay, and the pair before must still serve)
require(fallback_buffers_of(p, out) && out && create_calls == 2 && address_calls == 2, "first program's pair");
const VkBuffer records = out->records;
const uint64_t payload_bytes = out->payload_bytes;
const auto still_usable = [&](const char* why) {
const auto calls = create_calls;
require(live.contains(records), why);
require(fallback_buffers_of(p, out) && out->records == records && out->payload_bytes == payload_bytes &&
create_calls == calls, why);
};
Program bigger;
bigger.fallback_payload_words = 8;
fail_create = 3;
require(!fallback_buffers_of(bigger, out), "a failed grown payload was accepted");
require(destroy_calls == 0 && live.size() == 2, "a failed grown payload destroyed something");
still_usable("the shared records went with a payload that could not be made");
zero_address = address_calls + 1;
require(!fallback_buffers_of(bigger, out), "a grown payload with no address was accepted");
require(destroy_calls == 1 && live.size() == 2, "a grown payload with no address was not the one buffer destroyed");
still_usable("the shared records went with a payload that had no address");
require(fallback_buffers_of(bigger, out) && out->records == records && out->payload_bytes == 116 && live.size() == 3,
"the growth did not succeed once it could");
} else throw std::runtime_error("unknown authored case");
}
int main(int argc, char** argv) {

339
tests/fma_soft_test.c Normal file
View File

@@ -0,0 +1,339 @@
/* tests/test_fma_soft.py's driver ((Triforce, 11-22, the performance audit's LIFT-06)): the exact fused multiply-add
bodies recomp_fpe_fma64 and recomp_fpe_fma32 with nx2/fma_soft.py's short way out against the same bodies without it
(the kit's text: the way out is the body's first lines and RECOMP_FMA_SOFT_ON() decides it, which the test's hook
header makes a variable). Both run from one Ctx; the value and every byte of the Ctx must match.
fma_soft_test random <n> <seed> n operand triples a width: zeros, subnormals, infinities, NaNs of both kinds with
payloads, the extreme normals, exponents everywhere and matched to the product,
cancellations and half-ulp addends, under random FPCR and FPSR
fma_soft_test sweep edge exponents and fractions for both factors, each with addends placed against the
product (cancelling it within a few ulps, at its half ulp, far below, far above),
under FZ on and off with IXC set and clear
fma_soft_test env (built without the hook) how many of 1000 plain calls the way out answers: what
HOS_FMA_FAST decides
fma_soft_test time ns a call for plain operands, with the way out and without
Exit 0: no mismatch. 1: a mismatch. 3: this CPU cannot run the build. */
#include "a64.h"
#include <stdio.h>
#include <stdlib.h>
#include <cpuid.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <time.h>
#endif
int fma_soft_test_on = 1;
long long fma_soft_test_exits;
static uint64_t rs = 1;
static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; }
static uint32_t rbelow(uint32_t n) { return (uint32_t)(((rnd() >> 32) * n) >> 32); }
static double d_of(uint64_t x) { double f; memcpy(&f, &x, 8); return f; }
static uint64_t d_bits(double f) { uint64_t x; memcpy(&x, &f, 8); return x; }
static float f_of(uint32_t x) { float f; memcpy(&f, &x, 4); return f; }
static uint32_t f_bits(float f) { uint32_t x; memcpy(&x, &f, 4); return x; }
/* ---- states ---- */
static uint64_t rand_fpcr(void) {
static const uint64_t bits[] = {1u << 22, 1u << 23, 3u << 22, 1u << 25, 1u << 26, 1, 2, 4, 1u << 8, 1u << 9, 1u << 10,
1u << 11, 1u << 12, 1u << 15, 1u << 19, UINT64_C(1) << 32};
const uint32_t k = rbelow(100);
if (k < 55) return 0;
if (k < 80) return 1u << 24; /* FZ alone */
uint64_t v = bits[rbelow(sizeof bits / sizeof bits[0])];
if (rnd() & 1) v |= 1u << 24;
if (!(rnd() & 3)) v |= bits[rbelow(sizeof bits / sizeof bits[0])];
return v;
}
static uint64_t rand_fpsr(void) {
uint64_t v = rnd() & 0x9f;
if (!(rnd() & 3)) v |= 1u << 27;
switch (rbelow(4)) {
case 0: return v | 16u;
case 1: return v & ~(uint64_t)16u;
case 2: return 0;
default: return v;
}
}
/* ---- operands ---- */
static uint64_t frac64(void) {
switch (rbelow(8)) {
case 0: return 0;
case 1: return (UINT64_C(1) << 52) - 1;
case 2: return rnd() & ((UINT64_C(1) << 52) - 1) & ~((UINT64_C(1) << (1 + rbelow(51))) - 1); /* (low bits clear) */
case 3: return (UINT64_C(1) << rbelow(52)) | (rnd() & 1);
default: return rnd() & ((UINT64_C(1) << 52) - 1);
}
}
static uint64_t any64(void) {
const uint32_t k = rbelow(100);
const uint64_t sign = (rnd() & 1) << 63;
if (k < 4) return sign; /* zero */
if (k < 8) return sign | (frac64() ? frac64() : 1); /* subnormal (or zero) */
if (k < 10) return sign | UINT64_C(0x7ff0000000000000); /* infinity */
if (k < 12) return sign | UINT64_C(0x7ff8000000000000) | (rnd() & ((UINT64_C(1) << 51) - 1)); /* quiet NaN */
if (k < 14) return sign | UINT64_C(0x7ff0000000000000) | ((rnd() & ((UINT64_C(1) << 51) - 1)) | 1); /* signalling */
if (k < 17) return sign | ((rnd() & 1) ? UINT64_C(0x0010000000000000) | (rnd() & 3) : UINT64_C(0x7fefffffffffffff) - (rnd() & 3));
if (k < 30) return sign | ((uint64_t)(1 + rbelow(2046)) << 52) | frac64(); /* any exponent */
if (k < 40) { /* around the way out's limits */
static const uint32_t e[] = {1, 2, 123, 542, 543, 544, 1502, 1503, 1504, 1923, 2045, 2046};
return sign | ((uint64_t)e[rbelow(sizeof e / sizeof e[0])] << 52) | frac64();
}
return sign | ((uint64_t)(983 + rbelow(81)) << 52) | frac64(); /* ordinary */
}
static void triple64(uint64_t *a, uint64_t *b, uint64_t *z) {
*a = any64(), *b = any64(), *z = any64();
const uint32_t k = rbelow(100);
if (k >= 60) return;
const double p = d_of(*a) * d_of(*b);
const uint64_t pb = d_bits(p);
const uint32_t pe = (uint32_t)(pb >> 52) & 0x7ff;
if (pe == 0 || pe == 0x7ff) return;
if (k < 20) { /* an addend that cancels the product within a few ulps */
*z = (pb ^ (UINT64_C(1) << 63)) + (uint64_t)(int64_t)((int)rbelow(9) - 4);
} else if (k < 35) { /* an addend at the product's half ulp, quarter ulp, ulp ... (its fraction zero or random) */
const int e = (int)pe - 55 + (int)rbelow(6);
if (e >= 1) *z = ((rnd() & 1) << 63) | ((uint64_t)e << 52) | ((rnd() & 1) ? 0 : (rnd() & 3) ? frac64() : 1);
} else if (k < 50) { /* an addend near the product's size */
const int e = (int)pe - 3 + (int)rbelow(7);
if (e >= 1 && e <= 2046) *z = ((rnd() & 1) << 63) | ((uint64_t)e << 52) | frac64();
} else { /* the product at an addend's half ulp */
const int e = (int)pe + 50 + (int)rbelow(6);
if (e <= 2046) *z = ((rnd() & 1) << 63) | ((uint64_t)e << 52) | ((rnd() & 1) ? 0 : frac64());
}
}
static uint32_t frac32(void) {
switch (rbelow(8)) {
case 0: return 0;
case 1: return 0x7fffff;
case 2: return (uint32_t)rnd() & 0x7fffff & ~((1u << (1 + rbelow(22))) - 1);
case 3: return (1u << rbelow(23)) | ((uint32_t)rnd() & 1);
default: return (uint32_t)rnd() & 0x7fffff;
}
}
static uint32_t any32(void) {
const uint32_t k = rbelow(100);
const uint32_t sign = (uint32_t)(rnd() & 1) << 31;
if (k < 4) return sign;
if (k < 8) return sign | (frac32() ? frac32() : 1);
if (k < 10) return sign | 0x7f800000u;
if (k < 12) return sign | 0x7fc00000u | ((uint32_t)rnd() & 0x3fffff);
if (k < 14) return sign | 0x7f800000u | (((uint32_t)rnd() & 0x3fffff) | 1);
if (k < 17) return sign | ((rnd() & 1) ? 0x00800000u | ((uint32_t)rnd() & 3) : 0x7f7fffffu - ((uint32_t)rnd() & 3));
if (k < 35) return sign | ((1 + rbelow(254)) << 23) | frac32();
return sign | ((107 + rbelow(41)) << 23) | frac32();
}
static void triple32(uint32_t *a, uint32_t *b, uint32_t *z) {
*a = any32(), *b = any32(), *z = any32();
const uint32_t k = rbelow(100);
if (k >= 60) return;
const float p = f_of(*a) * f_of(*b);
const uint32_t pb = f_bits(p), pe = (pb >> 23) & 0xff;
if (pe == 0 || pe == 0xff) return;
if (k < 20) {
*z = (pb ^ 0x80000000u) + (uint32_t)((int)rbelow(9) - 4);
} else if (k < 35) {
const int e = (int)pe - 26 + (int)rbelow(6);
if (e >= 1) *z = ((uint32_t)(rnd() & 1) << 31) | ((uint32_t)e << 23) | ((rnd() & 1) ? 0 : (rnd() & 3) ? frac32() : 1);
} else if (k < 50) {
const int e = (int)pe - 3 + (int)rbelow(7);
if (e >= 1 && e <= 254) *z = ((uint32_t)(rnd() & 1) << 31) | ((uint32_t)e << 23) | frac32();
} else {
const int e = (int)pe + 21 + (int)rbelow(6);
if (e <= 254) *z = ((uint32_t)(rnd() & 1) << 31) | ((uint32_t)e << 23) | ((rnd() & 1) ? 0 : frac32());
}
}
/* ---- one comparison ---- */
static struct { long long checks, bad, exits, new_ixc, exact, fz; } st[2]; /* [0] binary64, [1] binary32 */
static void check(int w32, uint64_t a, uint64_t b, uint64_t z, uint64_t fpcr, uint64_t fpsr) {
static Ctx base;
Ctx on, off;
if (!st[0].checks && !st[1].checks) /* (the rest of the context: bytes that must stay as they are) */
for (size_t i = 0; i < sizeof base; i++) ((uint8_t *)&base)[i] = (uint8_t)rnd();
base.fpcr = fpcr, base.fpsr = fpsr;
on = base, off = base;
const long long before = fma_soft_test_exits;
fma_soft_test_on = 1;
const uint64_t von = w32 ? recomp_fpe_fma32(&on, a, b, z) : recomp_fpe_fma64(&on, a, b, z);
const int took = fma_soft_test_exits != before;
fma_soft_test_on = 0;
const uint64_t voff = w32 ? recomp_fpe_fma32(&off, a, b, z) : recomp_fpe_fma64(&off, a, b, z);
st[w32].checks++;
if (took) {
st[w32].exits++;
st[w32].new_ixc += !(fpsr & 16u) && (on.fpsr & 16u);
st[w32].exact += !(fpsr & 16u) && !(on.fpsr & 16u);
st[w32].fz += (fpcr >> 24) & 1;
}
if (von != voff || memcmp(&on, &off, sizeof on)) {
if (st[w32].bad++ < 8)
printf("MISMATCH fma%d a %016llx b %016llx z %016llx fpcr %llx fpsr %llx: with the way out %016llx fpsr %llx, "
"without %016llx fpsr %llx\n", w32 ? 32 : 64, (unsigned long long)a, (unsigned long long)b,
(unsigned long long)z, (unsigned long long)fpcr, (unsigned long long)fpsr, (unsigned long long)von,
(unsigned long long)on.fpsr, (unsigned long long)voff, (unsigned long long)off.fpsr);
}
}
static void random_cases(long long n) {
for (long long i = 0; i < n; i++) {
uint64_t a, b, z;
uint32_t x, y, w;
triple64(&a, &b, &z);
check(0, a, b, z, rand_fpcr(), rand_fpsr());
triple32(&x, &y, &w);
check(1, x, y, w, rand_fpcr(), rand_fpsr());
}
}
/* the sweep's addends, placed against the product p (kind k; i varies the sign and the last bits) */
static uint64_t addend64(double p, int k, unsigned i) {
const uint64_t pb = d_bits(p), neg = pb ^ (UINT64_C(1) << 63), sign = (uint64_t)(i & 1) << 63;
const int pe = (int)((pb >> 52) & 0x7ff);
static const int rel[] = {-108, -106, -105, -56, -55, -54, -53, -52, -51, -2, -1, 0, 1, 2, 51, 52, 53, 54, 55, 106};
if (k < 9) return neg + (uint64_t)(int64_t)(k - 4); /* -p within four ulps */
if (k < 11) return (k == 9 ? pb : neg) ^ ((uint64_t)(i >> 1 & 1) << 52); /* +-p, or its neighbour binade */
k -= 11;
if (k < 20) { /* 2^rel times the product's binade: fraction 0 */
const int e = pe + rel[k];
return e >= 1 && e <= 2046 ? sign | ((uint64_t)e << 52) : sign | UINT64_C(0x3ff0000000000000);
}
k -= 20;
{ /* the same with a fraction: 1, all ones */
const int e = pe + rel[k % 20];
const uint64_t f = k < 20 ? 1 : (UINT64_C(1) << 52) - 1;
return e >= 1 && e <= 2046 ? sign | ((uint64_t)e << 52) | f : sign | UINT64_C(0x4000000000000000) | f;
}
}
static uint32_t addend32(float p, int k, unsigned i) {
const uint32_t pb = f_bits(p), neg = pb ^ 0x80000000u, sign = (uint32_t)(i & 1) << 31;
const int pe = (int)((pb >> 23) & 0xff);
static const int rel[] = {-50, -48, -47, -27, -26, -25, -24, -23, -22, -2, -1, 0, 1, 2, 22, 23, 24, 25, 26, 48};
if (k < 9) return neg + (uint32_t)(k - 4);
if (k < 11) return (k == 9 ? pb : neg) ^ ((uint32_t)(i >> 1 & 1) << 23);
k -= 11;
if (k < 20) {
const int e = pe + rel[k];
return e >= 1 && e <= 254 ? sign | ((uint32_t)e << 23) : sign | 0x3f800000u;
}
k -= 20;
{
const int e = pe + rel[k % 20];
const uint32_t f = k < 20 ? 1 : 0x7fffff;
return e >= 1 && e <= 254 ? sign | ((uint32_t)e << 23) | f : sign | 0x40000000u | f;
}
}
enum { kKinds = 11 + 20 + 40 };
static void sweep(void) {
static const uint32_t e64[] = {1, 2, 123, 542, 543, 544, 1000, 1022, 1023, 1024, 1050, 1502, 1503, 1504, 2046};
static const uint64_t f64[] = {0, 1, 2, 3, (UINT64_C(1) << 26) - 1, UINT64_C(1) << 26, (UINT64_C(1) << 26) + 1,
(UINT64_C(1) << 27) - 1, UINT64_C(1) << 51, (UINT64_C(1) << 52) - 1, (UINT64_C(1) << 52) - 2,
UINT64_C(0x5555555555555), UINT64_C(0xaaaaaaaaaaaaa), UINT64_C(0x8000000000001),
UINT64_C(0xfffffff800000), UINT64_C(0x921fb54442d18)};
static const uint32_t e32[] = {1, 2, 24, 25, 64, 100, 126, 127, 128, 150, 190, 230, 253, 254};
static const uint32_t f32[] = {0, 1, 2, 3, 0xfff, 0x1000, 0x1001, 0x400000, 0x7fffff, 0x7ffffe, 0x555555, 0x2aaaaa,
0x400001, 0x7ff000, 0x490fdb};
static const uint64_t fpcr[] = {0, 1u << 24, 0, 1u << 24}, fpsr[] = {0x1f, 0x1f, 0, 0x8f};
const int ne = sizeof e64 / sizeof e64[0], nf = sizeof f64 / sizeof f64[0];
unsigned i = 0;
for (int ea = 0; ea < ne; ea++) for (int fa = 0; fa < nf; fa++)
for (int eb = 0; eb < ne; eb++) for (int fb = 0; fb < nf; fb++) {
const uint64_t a = ((uint64_t)e64[ea] << 52) | f64[fa] | ((uint64_t)(fa & 1) << 63);
const uint64_t b = ((uint64_t)e64[eb] << 52) | f64[fb] | ((uint64_t)(eb & 1) << 63);
const double p = d_of(a) * d_of(b);
for (int k = 0; k < kKinds; k++, i++)
check(0, a, b, addend64(p, k, i), fpcr[i & 3], fpsr[i & 3]);
}
const int me = sizeof e32 / sizeof e32[0], mf = sizeof f32 / sizeof f32[0];
for (int ea = 0; ea < me; ea++) for (int fa = 0; fa < mf; fa++)
for (int eb = 0; eb < me; eb++) for (int fb = 0; fb < mf; fb++) {
const uint32_t a = (e32[ea] << 23) | f32[fa] | ((uint32_t)(fa & 1) << 31);
const uint32_t b = (e32[eb] << 23) | f32[fb] | ((uint32_t)(eb & 1) << 31);
const float p = f_of(a) * f_of(b);
for (int k = 0; k < kKinds; k++, i++)
check(1, a, b, addend32(p, k, i), fpcr[i & 3], fpsr[i & 3]);
}
}
static double now_ns(void) {
#ifdef _WIN32
LARGE_INTEGER f, t;
QueryPerformanceFrequency(&f), QueryPerformanceCounter(&t);
return (double)t.QuadPart * 1e9 / (double)f.QuadPart;
#else
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1e9 + t.tv_nsec;
#endif
}
static void timing(void) {
enum { kN = 4096, kRounds = 300 };
static uint64_t a[kN], b[kN], z[kN];
static Ctx c;
for (int w32 = 0; w32 < 2; w32++) {
for (int i = 0; i < kN; i++) {
if (w32) a[i] = ((107 + rbelow(41)) << 23) | frac32(), b[i] = ((107 + rbelow(41)) << 23) | frac32(), z[i] = ((107 + rbelow(41)) << 23) | frac32();
else a[i] = ((uint64_t)(983 + rbelow(81)) << 52) | frac64(), b[i] = ((uint64_t)(983 + rbelow(81)) << 52) | frac64(), z[i] = ((uint64_t)(983 + rbelow(81)) << 52) | frac64();
}
double ns[2];
for (int on = 1; on >= 0; on--) {
fma_soft_test_on = on;
double best = 1e30;
uint64_t sink = 0;
for (int rep = 0; rep < 3; rep++) {
const double t0 = now_ns();
for (int r = 0; r < kRounds; r++)
for (int i = 0; i < kN; i++) sink += w32 ? recomp_fpe_fma32(&c, a[i], b[i], z[i]) : recomp_fpe_fma64(&c, a[i], b[i], z[i]);
const double t = (now_ns() - t0) / ((double)kRounds * kN);
if (t < best) best = t;
}
ns[on] = best;
if (!sink) printf(" ");
}
printf(" recomp_fpe_fma%d, plain operands: %.1f ns with the way out, %.1f ns without (%.1fx)\n", w32 ? 32 : 64, ns[1], ns[0], ns[0] / ns[1]);
}
}
int main(int argc, char **argv) {
unsigned eax, ebx, ecx, edx;
#ifdef __SSE4_2__
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\n");
return 3;
}
#endif
#ifdef __FMA__
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx) || !(ecx >> 12 & 1)) {
printf("this CPU has no FMA\n");
return 3;
}
#endif
(void)eax, (void)ebx, (void)ecx, (void)edx;
if (argc == 2 && !strcmp(argv[1], "env")) { /* (built without the hook: HOS_FMA_FAST as the game reads it) */
Ctx c;
memset(&c, 0, sizeof c);
uint64_t sink = 0;
for (int i = 0; i < 1000; i++)
sink += recomp_fpe_fma64(&c, d_bits(1.5 + i), d_bits(3.25), d_bits(0.1)) + recomp_fpe_fma32(&c, f_bits(1.5f + i), f_bits(3.25f), f_bits(0.1f));
printf("fma64=%d exits=%lld sum=%llx\n", RECOMP_FPX_FMA64, fma_soft_test_exits, (unsigned long long)sink);
return 0;
}
if (argc == 2 && !strcmp(argv[1], "time")) { timing(); return 0; }
if (argc == 4 && !strcmp(argv[1], "random")) {
rs = strtoull(argv[3], 0, 10);
if (!rs) rs = 1;
random_cases(atoll(argv[2]));
} else if (argc == 2 && !strcmp(argv[1], "sweep")) {
sweep();
} else {
fprintf(stderr, "usage: random <n> <seed> | sweep | env | time\n");
return 2;
}
for (int w = 0; w < 2; w++)
printf("%s fma%d fma64=%d checks=%lld mismatches=%lld exits=%lld new_ixc=%lld exact=%lld fz=%lld\n", argv[1], w ? 32 : 64,
RECOMP_FPX_FMA64, st[w].checks, st[w].bad, st[w].exits, st[w].new_ixc, st[w].exact, st[w].fz);
return st[0].bad || st[1].bad ? 1 : 0;
}

26
tests/fma_steps_side.c Normal file
View File

@@ -0,0 +1,26 @@
/* One side of tests/test_fma_steps.py's comparison ((Triforce, 11-22, the performance audit's LIFT-05)): the guest words'
lifted C compiled against one runtime header. Built twice, -DSIDE=now with the header nx2/fma_steps.py writes and
-DSIDE=old with the kit's (A64_NO_FMA_STEPS=1); the same lifted text and the same a64_fpx.h in both. */
#include "a64.h"
#include "fma_steps_lifted.h"
#define CAT_(a, b) a##b
#define CAT(a, b) CAT_(a, b)
/* form 0: as lifted; 1: as a hot function has it; 2: the exact body inline (translated with the fast path off) */
void CAT(SIDE, _run)(int k, int form, Ctx *c) { (form == 0 ? CASES[k].lifted : form == 1 ? CASES[k].hot : CASES[k].exact)(c); }
int CAT(SIDE, _count)(void) { return (int)(sizeof CASES / sizeof CASES[0]); }
const char *CAT(SIDE, _name)(int k) { return CASES[k].name; }
int CAT(SIDE, _rn)(int k) { return CASES[k].rn; }
int CAT(SIDE, _rm)(int k) { return CASES[k].rm; }
int CAT(SIDE, _dbl)(int k) { return CASES[k].dbl; }
int CAT(SIDE, _recps32)(uint64_t a, uint64_t b, uint32_t fz, uint64_t *v) { return recomp_fpx_recps32(a, b, fz, v); }
int CAT(SIDE, _rsqrts32)(uint64_t a, uint64_t b, uint32_t fz, uint64_t *v) { return recomp_fpx_rsqrts32(a, b, fz, v); }
/* whether this build's steps are the fused instruction (the guard nx2/fma_steps.py gives them) */
int CAT(SIDE, _fma)(void) {
#if RECOMP_FPX_HOST == 2 && defined(__FMA__) && !defined(A64_FPX_NO_FMA32)
return 1;
#else
return 0;
#endif
}

211
tests/fma_steps_test.c Normal file
View File

@@ -0,0 +1,211 @@
/* tests/test_fma_steps.py's driver ((Triforce, 11-22, the performance audit's LIFT-05)): guest FRECPS and FRSQRTS words
compiled against the runtime header nx2/fma_steps.py writes (now_*) and against the kit's (old_*), tests/fma_steps_side.c
built twice.
fma_steps_test lifted <n> <seed> n random states for each guest word: the word as lifted and as a hot function has
it, on both sides, and the exact body inline; the whole Ctx must be the same in all
fma_steps_test direct <n> <seed> the two fast paths by themselves on n operand pairs each, FZ on and off: what both
sides keep must be the same bits, what either keeps must be the exact body's value
with FPSR untouched, and the old side must keep nothing the new one declines, but
for an FRSQRTS step of 2^127 or more: there the fused 3 - a*b is past binary32
before its halving, the new side declines, and the exact body answers
Operands: zeros, subnormals, infinities, NaNs of both kinds with payloads, the extreme normals, any exponent, products
near 2 and near 3 (the step cancels), and pairs whose step is exactly a binary32 midpoint in binary64.
Exit 0: no mismatch. 1: a mismatch. 3: this CPU cannot run the build. */
#include "a64.h"
#include <stdio.h>
#include <stdlib.h>
#include <cpuid.h>
#define SIDE_DECL(s) \
void s##_run(int k, int form, Ctx *c); int s##_count(void); const char *s##_name(int k); int s##_rn(int k); \
int s##_rm(int k); int s##_dbl(int k); int s##_recps32(uint64_t, uint64_t, uint32_t, uint64_t *); \
int s##_rsqrts32(uint64_t, uint64_t, uint32_t, uint64_t *); int s##_fma(void);
SIDE_DECL(now)
SIDE_DECL(old)
static uint64_t rs = 1;
static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; }
static uint32_t rbelow(uint32_t n) { return (uint32_t)(((rnd() >> 32) * n) >> 32); }
static float f_of(uint32_t x) { float f; memcpy(&f, &x, 4); return f; }
static uint32_t f_bits(float f) { uint32_t x; memcpy(&x, &f, 4); return x; }
static uint64_t rand_fpcr(void) {
static const uint64_t bits[] = {1u << 22, 1u << 23, 3u << 22, 1u << 25, 1u << 26, 1, 2, 4, 1u << 8, 1u << 9, 1u << 10,
1u << 11, 1u << 12, 1u << 15, 1u << 19, UINT64_C(1) << 32};
const uint32_t k = rbelow(100);
if (k < 50) return 0;
if (k < 85) return 1u << 24; /* FZ alone */
uint64_t v = bits[rbelow(sizeof bits / sizeof bits[0])];
if (rnd() & 1) v |= 1u << 24;
return v;
}
static uint64_t rand_fpsr(void) { /* (IXC set, the fast path's gate, three times in four) */
uint64_t v = rnd() & 0x9f;
if (!(rnd() & 3)) v |= 1u << 27;
return (rnd() & 3) ? v | 16u : v & ~(uint64_t)16u;
}
static uint32_t frac32(void) {
switch (rbelow(8)) {
case 0: return 0;
case 1: return 0x7fffff;
case 2: return (uint32_t)rnd() & 0x7fffff & ~((1u << (1 + rbelow(22))) - 1);
case 3: return (1u << rbelow(23)) | ((uint32_t)rnd() & 1);
default: return (uint32_t)rnd() & 0x7fffff;
}
}
static uint32_t any32(void) {
const uint32_t k = rbelow(100);
const uint32_t sign = (uint32_t)(rnd() & 1) << 31;
if (k < 4) return sign;
if (k < 8) return sign | (frac32() ? frac32() : 1);
if (k < 10) return sign | 0x7f800000u;
if (k < 12) return sign | 0x7fc00000u | ((uint32_t)rnd() & 0x3fffff);
if (k < 14) return sign | 0x7f800000u | (((uint32_t)rnd() & 0x3fffff) | 1);
if (k < 17) return sign | ((rnd() & 1) ? 0x00800000u | ((uint32_t)rnd() & 3) : 0x7f7fffffu - ((uint32_t)rnd() & 3));
if (k < 35) return sign | ((1 + rbelow(254)) << 23) | frac32();
return sign | ((117 + rbelow(21)) << 23) | frac32();
}
/* a pair for a step: step 2 for FRECPS (2 - a*b), 3 for FRSQRTS ((3 - a*b) / 2) */
static void pair32(int step, uint32_t *a, uint32_t *b) {
const uint32_t k = rbelow(100);
*a = any32(), *b = any32();
if (k < 40) return;
if (k < 60) { /* the step exactly a binary32 midpoint in binary64 (the pairs the binary64 form declines) */
const uint32_t i = rbelow(512) * 2 + 1, j = rbelow(256) * 2 + 1;
if (step == 2) *a = 0x3f800000u | (i << 11), *b = 0xbf800000u | (j << 12); /* (1 + i/2^12) * -(1 + j/2^11) */
else *a = 0x3f800000u | (i << 11), *b = 0x3f800000u | (j << 11); /* (1 + i/2^12) * (1 + j/2^12) */
if (rnd() & 1) { const uint32_t t = *a; *a = *b, *b = t; }
return;
}
if (k < 80) { /* a*b near the step's constant: the step cancels (a tiny, subnormal or zero result) */
const uint32_t x = ((120 + rbelow(15)) << 23) | frac32();
const uint32_t y = f_bits((float)step / f_of(x)) + (uint32_t)((int)rbelow(9) - 4);
*a = x, *b = y;
if (rnd() & 1) *a ^= 0x80000000u, *b ^= 0x80000000u;
return;
}
if (k < 90) { /* a large or a small product */
*a = ((rnd() & 1 ? 180 + rbelow(74) : 1 + rbelow(60)) << 23) | frac32() | ((uint32_t)(rnd() & 1) << 31);
*b = ((rnd() & 1 ? 180 + rbelow(74) : 1 + rbelow(60)) << 23) | frac32() | ((uint32_t)(rnd() & 1) << 31);
return;
}
*a = ((122 + rbelow(11)) << 23) | frac32() | ((uint32_t)(rnd() & 1) << 31); /* ordinary: the step is an ordinary number */
*b = ((122 + rbelow(11)) << 23) | frac32() | ((uint32_t)(rnd() & 1) << 31);
}
static uint64_t any64(void) {
const uint32_t k = rbelow(100);
const uint64_t sign = (rnd() & 1) << 63, frac = rnd() & ((UINT64_C(1) << 52) - 1);
if (k < 4) return sign;
if (k < 8) return sign | (frac >> rbelow(52)) | 1;
if (k < 10) return sign | UINT64_C(0x7ff0000000000000);
if (k < 14) return sign | UINT64_C(0x7ff0000000000000) | (frac | 1);
if (k < 30) return sign | ((uint64_t)(1 + rbelow(2046)) << 52) | frac;
return sign | ((uint64_t)(1013 + rbelow(21)) << 52) | ((rnd() & 3) ? frac : 0);
}
static int lifted(long n) {
const int ncases = now_count();
long long checks = 0, bad = 0;
for (int k = 0; k < ncases; k++) {
const int rn = now_rn(k), rm = now_rm(k), dbl = now_dbl(k);
const int step = strstr(now_name(k), "FRSQRTS") ? 3 : 2;
for (long i = 0; i < n; i++) {
static Ctx base, r[5];
for (int v = 0; v < 32; v++) base.vreg[v][0] = rnd(), base.vreg[v][1] = rnd();
if (dbl) {
for (int h = 0; h < 2; h++) base.vreg[rn][h] = any64(), base.vreg[rm][h] = any64();
} else {
uint32_t a[4], b[4];
for (int l = 0; l < 4; l++) pair32(step, &a[l], &b[l]);
memcpy(base.vreg[rm], b, 16); /* (rm first: where rn is rm the pairs are (a, a)) */
memcpy(base.vreg[rn], a, 16);
}
for (int x = 0; x < 31; x++) base.x[x] = rnd();
base.n = (uint8_t)(i & 1), base.z = (uint8_t)(i >> 1 & 1), base.c = (uint8_t)(i >> 2 & 1), base.v = (uint8_t)(i >> 3 & 1);
base.fpcr = rand_fpcr(), base.fpsr = rand_fpsr();
for (int j = 0; j < 5; j++) r[j] = base;
now_run(k, 0, &r[0]), now_run(k, 1, &r[1]), old_run(k, 0, &r[2]), old_run(k, 1, &r[3]), now_run(k, 2, &r[4]);
checks++;
int diff = 0;
for (int j = 0; j < 4; j++) diff |= memcmp(&r[j], &r[4], sizeof r[0]) ? 1 << j : 0;
if (diff && bad++ < 8) {
static const char *const who[4] = {"now", "now hot", "old", "old hot"};
for (int j = 0; j < 4; j++)
if (diff >> j & 1)
printf("MISMATCH %s (%s against the exact body): fpcr %llx fpsr %llx; rn %016llx%016llx rm %016llx%016llx\n",
now_name(k), who[j], (unsigned long long)base.fpcr, (unsigned long long)base.fpsr,
(unsigned long long)base.vreg[rn][1], (unsigned long long)base.vreg[rn][0],
(unsigned long long)base.vreg[rm][1], (unsigned long long)base.vreg[rm][0]);
}
}
}
printf("lifted fused=%d shapes=%d checks=%lld mismatches=%lld\n", now_fma(), ncases, checks, bad);
return bad ? 1 : 0;
}
static struct { long long checks, kept_now, kept_old, now_only, old_only, bad; } st;
static void one(int half, uint32_t a, uint32_t b, uint32_t fz) {
uint64_t vn = 0, vo = 0;
const int kn = half ? now_rsqrts32(a, b, fz, &vn) : now_recps32(a, b, fz, &vn);
const int ko = half ? old_rsqrts32(a, b, fz, &vo) : old_recps32(a, b, fz, &vo);
st.checks++, st.kept_now += kn, st.kept_old += ko, st.now_only += kn && !ko, st.old_only += ko && !kn;
int bad = (kn && ko && vn != vo) || (ko && !kn && !(half && (vo & 0x7f800000u) == 0x7f000000u));
if (kn || ko) { /* (what a fast path keeps is Arm's value, and the exact body raises no new flag for it) */
Ctx c;
memset(&c, 0, sizeof c);
c.fpcr = fz, c.fpsr = 0x10 | (rnd() & 0x8f);
const uint64_t s = c.fpsr;
const uint64_t e = half ? recomp_fpe_rsqrts32(&c, a, b) : recomp_fpe_recps32(&c, a, b);
bad |= (kn && vn != e) || (ko && vo != e) || c.fpsr != s;
}
if (bad && st.bad++ < 8)
printf("MISMATCH %s a %08x b %08x fz %d: now %d %08llx, old %d %08llx\n", half ? "rsqrts32" : "recps32", a, b, fz != 0, kn,
(unsigned long long)vn, ko, (unsigned long long)vo);
}
static int direct(long n) {
/* the audit's two midpoint pairs first: kept by the fused form, declined by the binary64 one */
static const uint32_t named[2][2] = {{0x3f800800u, 0xbf801000u}, {0x3f800800u, 0x3f800800u}};
int named_ok = 1;
for (int half = 0; half < 2; half++) {
uint64_t vn = 0, vo = 0;
const int kn = half ? now_rsqrts32(named[1][0], named[1][1], 0, &vn) : now_recps32(named[0][0], named[0][1], 0, &vn);
const int ko = half ? old_rsqrts32(named[1][0], named[1][1], 0, &vo) : old_recps32(named[0][0], named[0][1], 0, &vo);
if (ko || kn != now_fma()) named_ok = 0;
one(half, named[half][0], named[half][1], 0);
}
for (long i = 0; i < n; i++)
for (int half = 0; half < 2; half++) {
uint32_t a, b;
pair32(half ? 3 : 2, &a, &b);
one(half, a, b, (rnd() & 1) ? 0x01000000u : 0);
}
printf("direct fused=%d named=%d checks=%lld kept_now=%lld kept_old=%lld now_only=%lld old_only=%lld mismatches=%lld\n", now_fma(),
named_ok, st.checks, st.kept_now, st.kept_old, st.now_only, st.old_only, st.bad);
return st.bad ? 1 : 0;
}
int main(int argc, char **argv) {
unsigned eax, ebx, ecx, edx;
#ifdef __SSE4_2__
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\n");
return 3;
}
#endif
#ifdef __FMA__
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx) || !(ecx >> 12 & 1)) {
printf("this CPU has no FMA\n");
return 3;
}
#endif
(void)eax, (void)ebx, (void)ecx, (void)edx;
if (argc != 4 || (strcmp(argv[1], "lifted") && strcmp(argv[1], "direct"))) {
fprintf(stderr, "usage: lifted <n> <seed> | direct <n> <seed>\n");
return 2;
}
rs = strtoull(argv[3], 0, 10);
if (!rs) rs = 1;
return argv[1][0] == 'l' ? lifted(atol(argv[2])) : direct(atol(argv[2]));
}

160
tests/gpu_waits_test.cpp Normal file
View File

@@ -0,0 +1,160 @@
// tests/test_gpu_waits.py: gpu/vk/gpu_wait_kinds.h (the host's time in calls that block on the GPU, by kind) and vk_work.h's
// five wrappers, put in the place of WRAPPERS and run over stand-ins for the driver's calls. No game, no GPU.
// (Triforce, 11-22, the performance audit's SYNC-27, after Source A's.)
//
// The calls below are vk_work.h's own five macros (their lines put in the place of MACROS), over its wrappers.
// <exe> on RX_VK_GPU_WAITS unset: everything below
// <exe> off RX_VK_GPU_WAITS=0 (set by the test): the calls are made, their results and arguments pass, nothing is counted
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <string>
#include <thread>
#include <vector>
#include "gpu_wait_kinds.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
namespace gw = gpu_wait_kinds;
// ---- stand-ins for the driver ----
typedef int VkResult;
typedef void* VkQueue;
typedef void* VkDevice;
typedef void* VkFence;
typedef void* VkQueryPool;
typedef uint32_t VkBool32;
typedef uint64_t VkDeviceSize;
typedef uint32_t VkQueryResultFlags;
struct VkSemaphoreWaitInfo { int tag; };
constexpr VkQueryResultFlags VK_QUERY_RESULT_64_BIT = 1, VK_QUERY_RESULT_WAIT_BIT = 2;
static int calls[5];
static int64_t sleep_us = 0; // (what each driver call takes)
static uint64_t seen_timeout;
static VkQueryResultFlags seen_flags;
static void work() {
if (sleep_us) std::this_thread::sleep_for(std::chrono::microseconds(sleep_us));
}
namespace vkx {
VkResult vkQueueWaitIdle(VkQueue q) { calls[0]++, work(); return q ? 11 : -1; }
VkResult vkDeviceWaitIdle(VkDevice d) { calls[1]++, work(); return d ? 12 : -1; }
VkResult vkWaitForFences(VkDevice d, uint32_t n, const VkFence* f, VkBool32 all, uint64_t timeout) {
calls[2]++, seen_timeout = timeout, work();
return d && n == 3 && f && all == 1 ? 13 : -1;
}
VkResult vkWaitSemaphores(VkDevice d, const VkSemaphoreWaitInfo* info, uint64_t timeout) {
calls[3]++, seen_timeout = timeout, work();
return d && info && info->tag == 77 ? 14 : -1;
}
VkResult vkGetQueryPoolResults(VkDevice d, VkQueryPool pool, uint32_t first, uint32_t count, size_t size, void* data, VkDeviceSize stride,
VkQueryResultFlags flags) {
calls[4]++, seen_flags = flags, work();
return d && pool && first == 4 && count == 5 && size == 40 && data && stride == 8 ? 15 : -1;
}
} // namespace vkx
/* WRAPPERS */
// (the five macros as vk_work.h has them, the recorder's boundary a count here: the test puts their lines in the place of MACROS)
static int boundaries = 0;
namespace mkwvk::rec {
enum { B_vkQueueWaitIdle, B_vkDeviceWaitIdle, B_vkWaitForFences, B_vkWaitSemaphores, B_vkGetQueryPoolResults };
inline void boundary(int) { boundaries++; }
}
/* MACROS */
static int64_t ns_of(gw::Kind k) { return gw::totals.ns[k].load(); }
static int64_t n_of(gw::Kind k) { return gw::totals.n[k].load(); }
static int64_t n_all() { return n_of(gw::FENCE) + n_of(gw::TIMELINE) + n_of(gw::IDLE) + n_of(gw::QUERY); }
int main(int argc, char** argv) {
const bool on = !(argc > 1 && std::string(argv[1]) == "off");
int dev = 0, x = 0;
VkFence list[3] = {&x, &x, &x};
VkSemaphoreWaitInfo info{77};
uint64_t data[5];
CHECK(gw::on() == on);
// ---- the wrappers: the driver is called with what they were given, its result comes back, and they count by kind when they block
sleep_us = 0;
CHECK(vkWaitForFences(&dev, 3, list, 1, 0) == 13 && calls[2] == 1 && seen_timeout == 0 && n_all() == 0); // (a poll: nothing)
CHECK(vkWaitSemaphores(&dev, &info, 0) == 14 && calls[3] == 1 && seen_timeout == 0 && n_all() == 0); // (a poll: nothing)
CHECK(vkGetQueryPoolResults(&dev, &x, 4, 5, 40, data, 8, VK_QUERY_RESULT_64_BIT) == 15 && calls[4] == 1 && seen_flags == VK_QUERY_RESULT_64_BIT && n_all() == 0);
sleep_us = 3000;
CHECK(vkWaitForFences(&dev, 3, list, 1, 250000000ull) == 13 && seen_timeout == 250000000ull);
CHECK(n_of(gw::FENCE) == (on ? 1 : 0) && n_of(gw::TIMELINE) == 0 && n_of(gw::IDLE) == 0 && n_of(gw::QUERY) == 0);
if (on) CHECK(ns_of(gw::FENCE) >= 3000000 && ns_of(gw::TIMELINE) == 0 && ns_of(gw::IDLE) == 0 && ns_of(gw::QUERY) == 0);
CHECK(vkWaitSemaphores(&dev, &info, ~0ull) == 14 && seen_timeout == ~0ull && n_of(gw::TIMELINE) == (on ? 1 : 0));
CHECK(vkQueueWaitIdle(&dev) == 11 && vkDeviceWaitIdle(&dev) == 12 && calls[0] == 1 && calls[1] == 1 && n_of(gw::IDLE) == (on ? 2 : 0));
CHECK(vkGetQueryPoolResults(&dev, &x, 4, 5, 40, data, 8, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT) == 15 && n_of(gw::QUERY) == (on ? 1 : 0));
if (on) CHECK(ns_of(gw::TIMELINE) >= 3000000 && ns_of(gw::IDLE) >= 6000000 && ns_of(gw::QUERY) >= 3000000);
CHECK(n_all() == (on ? 5 : 0) && gw::depth == 0 && gw::excluded == 0);
CHECK(boundaries == 8); // (the recorder's boundary before each of the eight calls so far, counted or not)
if (!on) {
CHECK(ns_of(gw::FENCE) + ns_of(gw::TIMELINE) + ns_of(gw::IDLE) + ns_of(gw::QUERY) == 0);
gw::Reader r;
CHECK(r.take(nullptr, nullptr) == 0);
std::printf("off: the five calls made, nothing counted\n");
return fails != 0;
}
// ---- two readers: each sees every nanosecond and every wait, whenever it reads
gw::Reader a, b;
int64_t ns[gw::KINDS], n[gw::KINDS];
const int64_t a1 = a.take(ns, n);
CHECK(a1 == ns[0] + ns[1] + ns[2] + ns[3] && n[gw::FENCE] == 1 && n[gw::TIMELINE] == 1 && n[gw::IDLE] == 2 && n[gw::QUERY] == 1);
CHECK(a.take(ns, n) == 0 && n[0] + n[1] + n[2] + n[3] == 0); // (nothing since)
vkWaitForFences(&dev, 3, list, 1, 1);
const int64_t a2 = a.take(ns, n);
CHECK(a2 >= 3000000 && n[gw::FENCE] == 1 && ns[gw::FENCE] == a2);
const int64_t b1 = b.take(ns, n); // (the other reader reads once, later: all of it)
CHECK(b1 == a1 + a2 && n[gw::FENCE] == 2 && n[0] + n[1] + n[2] + n[3] == 6);
CHECK(b1 == ns_of(gw::FENCE) + ns_of(gw::TIMELINE) + ns_of(gw::IDLE) + ns_of(gw::QUERY));
// ---- a wait inside a wait counts once, as the outer one
{
const int64_t f0 = n_of(gw::FENCE), t0 = n_of(gw::TIMELINE), fns = ns_of(gw::FENCE), tns = ns_of(gw::TIMELINE);
const int r = gw::timed(gw::FENCE, true, [&] { return vkWaitSemaphores(&dev, &info, 5) + 100; });
CHECK(r == 114 && n_of(gw::FENCE) == f0 + 1 && n_of(gw::TIMELINE) == t0 && ns_of(gw::TIMELINE) == tns);
CHECK(ns_of(gw::FENCE) - fns >= 3000000 && gw::depth == 0);
}
// ---- excluded waits count nothing, and the count comes back after the scope (one inside another too)
{
const int64_t all0 = n_all();
{
const gw::Exclude pacing;
CHECK(vkWaitSemaphores(&dev, &info, 100000000ull) == 14 && n_all() == all0);
{
const gw::Exclude again;
CHECK(vkWaitForFences(&dev, 3, list, 1, 9) == 13 && n_all() == all0);
}
CHECK(vkQueueWaitIdle(&dev) == 11 && n_all() == all0);
}
CHECK(gw::excluded == 0 && vkQueueWaitIdle(&dev) == 11 && n_all() == all0 + 1);
}
// ---- every thread's waits are counted; one thread inside a wait or an Exclude does not hide another's
{
sleep_us = 0;
const int64_t all0 = n_all();
std::atomic<bool> go{false}, inside{false}, done{false};
std::thread outer([&] {
const gw::Exclude pacing; // (this thread: excluded for as long as the others run)
inside = true;
while (!done) std::this_thread::yield();
});
while (!inside) std::this_thread::yield();
std::vector<std::thread> threads;
for (int t = 0; t < 4; t++)
threads.emplace_back([&] {
while (!go) std::this_thread::yield();
for (int i = 0; i < 500; i++) vkWaitForFences(&dev, 3, list, 1, 1), vkWaitForFences(&dev, 3, list, 1, 0);
});
go = true;
for (auto& t : threads) t.join();
done = true;
outer.join();
CHECK(n_all() == all0 + 4 * 500);
}
std::printf("on: polls and excluded waits count nothing, a nested wait once, the kinds apart, two readers the whole each, 2,000 waits of 4 threads\n");
return fails != 0;
}

View File

@@ -64,8 +64,8 @@ static bool all_done(int seconds) {
}
static void case_until() {
CHECK(helper_spin::bound_ns(nullptr) == 100000);
CHECK(helper_spin::bound_ns("") == 100000);
CHECK(helper_spin::bound_ns(nullptr) == 20000);
CHECK(helper_spin::bound_ns("") == 20000);
CHECK(helper_spin::bound_ns("0") == 0);
CHECK(helper_spin::bound_ns("-5") == 0);
CHECK(helper_spin::bound_ns("250") == 250000);

View File

@@ -87,7 +87,10 @@ void binds_load(void) {
read_input calls */
int hos_mouse_native(void) { return -1; }
void hos_mouse_native_free(int free) { (void)free; }
void hos_mouse_native_stick(int held) { (void)held; }
/* ((Triforce, 11-22) whether the right stick turns the camera, told after every read: behind another window too, where the
pads alone turned it, and the native mouse put the angles back each frame) */
static int native_held = -1;
void hos_mouse_native_stick(int held) { native_held = held; }
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; }
@@ -113,8 +116,10 @@ static void packet(uint64_t buttons, int32_t lx, int32_t ly, int32_t rx) {
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);
native_held = -1;
npad_states(&context);
CHECK(context.x[0] == 2);
CHECK(native_held == (rx != 0)); /* (the stick the game is given, told to the native mouse, the window in front or not) */
expected_sampling++;
for (int i = 0; i < 2; i++) {
const uint64_t address = 64 + (uint64_t)i * STATE_SIZE;

View File

@@ -64,13 +64,24 @@ static void renderer(uint64_t gpu, uint64_t size) {
}
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); }
// ((Triforce, THREAD-25) the back end's answer to "has this invalidate nothing to wait for?" (mkwvk_invalidate_quick, found by name
// in the product): asked with the caller's core still held, before anything is given up
static int quick_calls, quick_answer;
static int quick(uint64_t gpu, uint64_t size) {
quick_calls++;
CHECK(gpu == want_gpu && size == want_size);
CHECK(!callback_active);
if (cur && enabled) CHECK(cur->vc_held == 1 && cur->releases == 0);
return quick_answer;
}
static int (*volatile invalidate_quick_fn)(uint64_t, uint64_t);
#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 fallback = !std::strcmp(name, "fallback") || !std::strcmp(name, "fallback-immediate") || !std::strcmp(name, "quick-fallback");
const bool already_away = !std::strcmp(name, "already-away");
const bool non_guest = !std::strcmp(name, "non-guest");
enabled = std::strcmp(name, "disabled") != 0;
@@ -78,6 +89,11 @@ int main(int argc, char** argv) {
// 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;
// ((Triforce, THREAD-25) quick: the back end says there is nothing to wait for: no call, no release, no take. quick-declined: it
// says there may be: everything as before. quick-fallback: no Vulkan back end is running: it is not asked)
const bool quick_yes = !std::strcmp(name, "quick"), quick_no = !std::strcmp(name, "quick-declined");
const bool quick_fallback = !std::strcmp(name, "quick-fallback");
if (quick_yes || quick_no || quick_fallback) invalidate_quick_fn = quick, quick_answer = !quick_no;
MkwVkRenderer backend{};
backend.nvn_invalidate = vulkan_invalidate;
vk = fallback ? nullptr : &backend;
@@ -111,11 +127,12 @@ int main(int argc, char** argv) {
}
rx_nvn_invalidate(want_gpu, want_size);
if (worker.joinable()) worker.join();
CHECK(vulkan_calls == (fallback ? 0 : 1));
CHECK(vulkan_calls == (fallback || quick_yes ? 0 : 1));
CHECK(fallback_calls == (fallback ? 1 : 0));
CHECK(quick_calls == (quick_yes || quick_no ? 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.releases - before_release == (owned_on_entry && !quick_yes ? 1 : 0));
CHECK(caller.takes - before_take == (owned_on_entry && !quick_yes ? 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);

View File

@@ -0,0 +1,119 @@
// tests/test_mirror_bookkeeping.py puts vk_memory.cpp's own wb_notes_off, memory_cpu_owned (with its two merges),
// shadow_written_to and its two callers, guest_cpu_write and guest_cpu_wrote in the place of ACTUAL and runs them with
// and without mirrors. No game, no GPU. (Triforce, 11-22, the performance audit's MEM-51, after Source A's.)
//
// <exe> off mirrors off, the device made: nothing is noted, no guest page is looked up, the write still lands
// <exe> early mirrors off, the device not made yet: noted as before, and mirror_on() is not asked
// <exe> dump mirrors on: a fixed random run of calls, then every list printed. The test builds this file twice,
// from the code as it is and from the code with the new condition taken out (the code as it was):
// the two prints must be the same
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <string>
#include <vector>
#define VK_NULL_HANDLE nullptr
#define CHECK(ok) do { if (!(ok)) { std::printf("FAIL:%d %s\n", __LINE__, #ok); fails++; } } while (0)
static int fails = 0;
constexpr uint64_t kPageShift = 12;
struct Dev { void* device = nullptr; } dev;
static bool mirrors = true;
static int mirror_asked = 0;
bool mirror_on() { return mirror_asked++, mirrors; }
bool wb_diff_on() { return true; }
static int looked_up = 0, writes = 0;
struct Host {
static uint64_t gpu_guest(uint64_t gpu) { return looked_up++, gpu >= 0x100000 ? gpu - 0x100000 + 0x4000000 : 0; }
static int gpu_write(uint64_t, const void*, uint64_t) { return writes++, 1; }
};
static Host the_host;
static Host* host = &the_host;
std::mutex cpu_words_mu;
std::vector<uint64_t> cpu_words, cpu_words_recent, cpu_words_new;
std::mutex shadow_written_mu;
std::vector<uint32_t> shadow_written_pages, shadow_written_collected_list, shadow_written_by_replay;
bool shadow_written_all = false;
void shadow_written_to(std::vector<uint32_t>& list, const uint32_t* pages, int64_t n);
void guest_cpu_wrote(uint64_t gpu, uint64_t n);
/* ACTUAL */
static uint64_t rnd_state = 0x1234567887654321ull;
static uint64_t rnd() {
rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 7, rnd_state ^= rnd_state << 17;
return rnd_state;
}
static bool nothing_noted() {
return cpu_words.empty() && cpu_words_recent.empty() && cpu_words_new.empty() && shadow_written_pages.empty() &&
shadow_written_collected_list.empty() && shadow_written_by_replay.empty() && !shadow_written_all;
}
/// The calls the replay and the capture make, n of them, from the generator's state
static void calls(int n) {
uint32_t pages[80];
const uint32_t word = 0;
for (int i = 0; i < n; i++) {
const uint64_t at = 0x100000 + (rnd() % 4096) * 8, len = 1 + rnd() % 20000;
switch (rnd() % 5) {
case 0: memory_cpu_owned(rnd() % 9 == 0 ? 0 : 0x4000000 + (rnd() % 70000) * 8 + rnd() % 8, 1 + rnd() % 40); break;
case 1: {
const int k = (int)(rnd() % 80);
for (int j = 0; j < k; j++) pages[j] = (uint32_t)(rnd() % 100000);
shadow_written(pages, k);
break;
}
case 2: {
const int k = (int)(rnd() % 80);
for (int j = 0; j < k; j++) pages[j] = (uint32_t)(rnd() % 100000);
shadow_written_collected(pages, k);
break;
}
case 3: guest_cpu_wrote(rnd() % 7 == 0 ? 0x1000 : at, rnd() % 11 == 0 ? 0 : len); break;
default: guest_cpu_write(at, &word, 4); break;
}
}
}
template <class T> static void dump(const char* name, const std::vector<T>& v) {
uint64_t h = 1469598103934665603ull;
for (const T& e : v) h = (h ^ (uint64_t)e) * 1099511628211ull;
std::printf("%s %zu %016llx\n", name, v.size(), (unsigned long long)h);
}
int main(int argc, char** argv) {
const std::string test = argc == 2 ? argv[1] : "";
if (test == "off") {
dev.device = &dev, mirrors = false;
calls(20000);
CHECK(nothing_noted());
CHECK(looked_up == 0); // (guest_cpu_wrote asks the host for no page)
CHECK(writes > 1000); // (guest_cpu_write still writes)
CHECK(mirror_asked > 0);
} else if (test == "early") {
dev.device = nullptr, mirrors = false;
calls(2000);
CHECK(!nothing_noted() && looked_up > 0);
CHECK(mirror_asked == 0); // (mirror_on() decides once, by the card: not asked before there is one)
} else if (test == "dump") {
dev.device = &dev, mirrors = true;
for (int round = 0; round < 40; round++) {
calls(3000);
if (round % 8 == 7) { // (a write-back reads them: the lists start again, as shadows_forget_written leaves them)
std::lock_guard lk(shadow_written_mu);
shadow_written_pages.clear(), shadow_written_collected_list.clear(), shadow_written_by_replay.clear(), shadow_written_all = false;
}
dump("words", cpu_words), dump("recent", cpu_words_recent), dump("new", cpu_words_new);
dump("game", shadow_written_pages), dump("collected", shadow_written_collected_list), dump("replay", shadow_written_by_replay);
std::printf("all %d looked_up %d writes %d\n", (int)shadow_written_all, looked_up, writes);
}
CHECK(!nothing_noted() && looked_up > 0);
} else {
std::printf("a known case\n");
return 1;
}
return fails != 0;
}

View File

@@ -359,6 +359,91 @@ int main() {
d.note(1), d.note(1), d.note(2);
CHECK(d.take() == 2 && d.take() == 0);
}
{ // ((Triforce, 11-22, the performance audit's GPUWORK-01) a window with a draw that writes colour
bool keep_now = false;
Rep r;
// the rule off: counted for the line, and the count measured as before, the colour draw under its open query
Counter c;
c.reset(4, A, 10);
c.draw_enter(false);
Counter::Colour m = c.colour_draw(false, false); // (the window's first draw writes colour: no query open yet)
CHECK(m.first && !m.open && !m.lose && c.colour && !c.lost);
CHECK(c.may_begin(false) && c.begin(false, A));
c.open = 10;
c.draw_issued(false);
c.draw_enter(false);
m = c.colour_draw(false, false);
CHECK(!m.first && m.open && !m.lose && !c.lost && c.open == 10); // (a second one, under the open query)
c.draw_issued(false);
c.open = -1;
CHECK(c.report(false, A, 11, &r, &keep_now) == Counter::kMeasured && r.first == 10 && r.n == 1 && c.why == Counter::kNone);
// the rule on: a depth-only window is measured as before (no colour draw is ever told to it)
Counter d;
d.reset(4, A, 20);
d.draw_enter(false);
CHECK(d.begin(false, A));
d.open = 20;
d.draw_issued(false);
d.open = -1;
CHECK(d.report(false, A, 21, &r, &keep_now) == Counter::kMeasured && r.first == 20 && r.n == 1 && !d.colour);
// the rule on: a colour draw gives the count up; no query begins again until the next reset; its report is
// incomplete for that reason, so nothing is written for it
Counter e;
e.reset(4, A, 30);
e.draw_enter(false);
CHECK(e.begin(false, A));
e.open = 30;
e.draw_issued(false);
e.draw_enter(false);
m = e.colour_draw(false, true);
CHECK(m.first && m.open && m.lose);
e.open = -1; // (the caller ends the open query: occ_close)
e.lose_colour();
CHECK(e.lost && e.why == Counter::kColour && !e.may_begin(false) && !e.begin(false, A));
e.draw_issued(false);
e.draw_enter(false);
m = e.colour_draw(false, true); // (a later colour draw of the window: nothing more)
CHECK(!m.first && !m.open && !m.lose);
e.draw_issued(false);
r = Rep{};
CHECK(e.report(false, A, 31, &r, &keep_now) == Counter::kIncomplete && e.why == Counter::kColour && r.n == 0);
e.reset(4, A, 31); // (the next window begins afresh)
CHECK(!e.lost && !e.colour && e.why == Counter::kNone && e.may_begin(false));
// a colour draw before the window's first query, the rule on: the same, with no query to end
Counter f;
f.reset(4, A, 40);
f.draw_enter(false);
m = f.colour_draw(false, true);
CHECK(m.first && !m.open && m.lose);
f.lose_colour();
f.draw_issued(false);
CHECK(!f.may_begin(false) && f.report(false, A, 40, &r, &keep_now) == Counter::kIncomplete && f.why == Counter::kColour);
// not counting (no reset yet), or lost already: a colour draw is nothing to the count
Counter g;
m = g.colour_draw(false, true);
CHECK(!m.first && !m.open && !m.lose && !g.colour);
g.reset(4, 0, 0); // (no recording's: lost)
m = g.colour_draw(false, true);
CHECK(!m.first && !m.lose && g.why == Counter::kOtherCmd);
// counting past a report (keep): a colour draw after the report is told too
Counter h;
h.reset(4, A, 50);
CHECK(h.report(true, A, 50, &r, &keep_now) == Counter::kMeasured);
m = h.colour_draw(true, false);
CHECK(m.first && !m.lose);
m = h.colour_draw(false, false); // (without keep a reported count is not counting)
CHECK(!m.first && !m.open && !m.lose);
// the line
Stats st;
st.colour_windows = 12, st.colour_open = 640, st.colour_lost = 5, st.inc_colour = 4;
char line[512];
st.colour_line(line, sizeof line, 600, true);
CHECK(std::strstr(line, "vk: occlusion queries (RX_VK_OCC_QUERY), 600 presents (3): 12 counting windows met a draw that writes "
"colour, 640 such draws were recorded under an open query; depth-only counting (RX_VK_OCC_DEPTH_ONLY) is "
"on: 5 counts given up at a colour draw, 4 reports kept 4096 for it") == line);
st.colour_line(line, sizeof line, 600, false);
CHECK(std::strstr(line, "(RX_VK_OCC_DEPTH_ONLY) is off: ") != nullptr);
}
if (failures) std::fprintf(stderr, "%d failed\n", failures);
else std::printf("occ_answer: all cases pass\n");
return failures ? 1 : 0;

View File

@@ -0,0 +1,162 @@
// tests/test_page_table_stream.py puts vk_memory.cpp's own page table code (TableRun to memory_table_flush) in the place of
// ACTUAL and runs its flush, streamed (gpu/vk/table_stream.h) and sorted as it was, on the same changes. No game, no GPU.
// (Triforce, 11-22, the performance audit's SYNC-04, after Source A's.)
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <new>
#include <string>
#include <utility>
#include <vector>
#include "table_stream.h"
// (the largest single allocation while `tracking` is set: the flush's scratch)
static bool tracking = false;
static size_t largest = 0;
static void* counted(size_t n) {
if (tracking && n > largest) largest = n;
void* p = std::malloc(n ? n : 1);
if (!p) std::abort();
return p;
}
void* operator new(size_t n) { return counted(n); }
void* operator new[](size_t n) { return counted(n); }
void* operator new(size_t n, const std::nothrow_t&) noexcept { return counted(n); }
void* operator new[](size_t n, const std::nothrow_t&) noexcept { return counted(n); }
void operator delete(void* p) noexcept { std::free(p); }
void operator delete[](void* p) noexcept { std::free(p); }
void operator delete(void* p, size_t) noexcept { std::free(p); }
void operator delete[](void* p, size_t) noexcept { std::free(p); }
namespace fixture {
int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
uint64_t* guest_pages = nullptr; // (nothing is written at once here: every change goes through the flush)
using VkBuffer = int;
using VkCommandBuffer = int;
VkBuffer guest_pages_buffer = 7;
std::atomic<bool> par_active{false};
struct Update {
uint64_t first;
std::vector<uint64_t> values;
bool operator==(const Update& o) const { return first == o.first && values == o.values; }
};
std::vector<Update> updates;
size_t largest_update = 0;
void vkCmdUpdateBuffer(VkCommandBuffer, VkBuffer b, uint64_t offset, uint64_t bytes, const void* data) {
CHECK(b == guest_pages_buffer && offset % 8 == 0 && bytes % 8 == 0 && bytes > 0);
largest_update = std::max<size_t>(largest_update, (size_t)bytes);
const bool was = tracking;
tracking = false; // (the stand-in's own copy is not the flush's scratch)
updates.push_back({offset / 8, std::vector<uint64_t>((const uint64_t*)data, (const uint64_t*)data + bytes / 8)});
tracking = was;
}
VkCommandBuffer upload_command_buffer() { return 1; }
struct GprofSpan {
int items = 0;
uint64_t bytes = 0;
GprofSpan(VkCommandBuffer, const char*) {}
};
void logf(const char*, ...) {}
/* ACTUAL */
uint64_t rnd_state = 0xD1B54A32D192ED03ull;
uint64_t rnd() {
rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 7, rnd_state ^= rnd_state << 17;
return rnd_state;
}
const uint64_t kNone = ~0ull; // (a page no change touched)
/// The runs flushed both ways: the same transfers, entry for entry, no transfer over 64 KiB, and the table they leave is
/// the last writer's of every page
void both_ways(const std::vector<TableRun>& runs, uint64_t span) {
std::vector<uint64_t> model(span, kNone);
for (const TableRun& r : runs)
for (uint64_t i = 0; i < r.count; i++) model[r.first + i] = r.address ? r.address + (i << 12) : 0;
updates.clear(), largest_update = 0;
table_pending = runs;
const bool any = memory_table_flush();
CHECK(any == !runs.empty() && table_pending.empty());
const std::vector<Update> streamed = updates;
CHECK(largest_update <= 65536);
std::vector<uint64_t> table(span, kNone);
uint64_t next_free = 0;
for (const Update& u : streamed) {
CHECK(u.first >= next_free); // (ascending, no page twice)
for (size_t i = 0; i < u.values.size(); i++) table[u.first + i] = u.values[i];
next_free = u.first + u.values.size();
}
CHECK(table == model);
updates.clear();
table_pending = runs;
if (!runs.empty()) table_flush_sorted();
CHECK(table_pending.empty());
CHECK(updates == streamed);
}
int run(int argc, char** argv) {
const std::string test = argc == 2 ? argv[1] : "";
if (test == "random") {
// 50 trials of 128 runs: anywhere in 4,000 pages, so most overlap, in any order
for (int t = 0; t < 50; t++) {
std::vector<TableRun> runs;
for (int i = 0; i < 128; i++) {
const uint64_t count = rnd() % 5 == 0 ? rnd() % 1200 : rnd() % 40; // (some of 0 pages)
const uint64_t first = rnd() % (4000 - count);
runs.push_back({first, count, rnd() % 4 == 0 ? 0 : ((rnd() % 0xFFFFFF) + 1) << 12});
}
both_ways(runs, 4000);
}
// in page order and apart (the usual case: no sweep), some touching, some with gaps, some long
for (int t = 0; t < 50; t++) {
std::vector<TableRun> runs;
uint64_t at = rnd() % 16;
for (int i = 0; i < 128; i++) {
const uint64_t count = rnd() % 9 == 0 ? 8000 + rnd() % 9000 : rnd() % 64;
runs.push_back({at, count, rnd() % 4 == 0 ? 0 : ((rnd() % 0xFFFFFF) + 1) << 12});
at += count + (rnd() % 3 == 0 ? rnd() % 50 : 0);
}
both_ways(runs, at + 1);
// and the same runs with two of them swapped, or one repeated: the sweep
std::swap(runs[rnd() % 128], runs[rnd() % 128]);
runs.push_back(runs[rnd() % 128]);
runs.back().address = 0x7770000000ull;
both_ways(runs, at + 1);
}
// 256 single pages in any order, some twice
std::vector<TableRun> singles;
for (int i = 0; i < 256; i++) singles.push_back({rnd() % 300, 1, ((rnd() % 0xFFFF) + 1) << 12});
both_ways(singles, 300);
both_ways({}, 1);
both_ways({{5, 0, 0x1000}}, 8);
both_ways({{0, 8192, 0x4000000}, {8192, 1, 0x9000000}}, 8200); // (a full transfer, then one entry)
std::printf("ok: the streamed flush writes the sorted flush's transfers, entry for entry\n");
} else if (test == "four" || test == "four-sorted") {
// four overlapping ranges over 150,000 pages (a pool set, zeroed at its merge and set again in parts)
const std::vector<TableRun> runs = {{0, 150000, 0x100000000ull}, {20000, 70000, 0}, {50000, 70000, 0x900000000ull}, {100000, 50000, 0xA00000000ull}};
table_pending = runs;
updates.clear(), largest = 0;
tracking = true;
if (test == "four") memory_table_flush(); // (as the process's environment says: streamed unless RX_VK_TABLE_STREAM=0)
else table_flush_sorted();
tracking = false;
std::printf("largest %zu\n", largest);
const std::vector<Update> got = updates;
updates.clear();
table_pending = runs;
table_flush_sorted();
CHECK(got == updates && !got.empty());
} else {
std::printf("a known case\n");
return 1;
}
return fails != 0;
}
} // namespace fixture
int main(int argc, char** argv) { return fixture::run(argc, argv); }

View File

@@ -221,8 +221,12 @@ 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;
uint32_t bc_scratch_layers() { return kBcLayers; } // ((11-21, TEX-10) the scratch's layers: a run's)
VkImage bc_scratch = VK_NULL_HANDLE, bc_scratch2 = VK_NULL_HANDLE;
bool bc_scratch_fresh = false, bc_scratch2_fresh = false;
VkImage bc_small = VK_NULL_HANDLE; // ((11-21, TEX-09) the small scratch: none here)
bool bc_small_fresh = false;
uint32_t bc_small_layers() { return 1; }
bool bc_scratch_make() {
bc_scratch = kScratch, bc_scratch_fresh = true, bc_have = true;
return true;

View File

@@ -3,6 +3,7 @@
// change the same pages in one order and the queue runs their uploads in the other. No game, no GPU.
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
@@ -13,8 +14,10 @@
#include <thread>
#include <utility>
#include <vector>
#include "table_stream.h" // ((Triforce, 11-22, SYNC-04) the flush's stream: gpu/vk, on the include path)
namespace fixture {
void logf(const char*, ...) {} // (the flush's line for one over 1 ms)
void require(bool ok, const char* why) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", why);

View File

@@ -177,6 +177,7 @@ std::uint64_t pcache_own_stamp() { return 0xabcdef0123456789ull; }
std::atomic<int64_t> pcache_main_built{0};
std::atomic<int> pcache_preparing{0};
bool pcache_play_save(int64_t) { return true; }
thread_local bool pcache_play_only_now = false; // ((Triforce, 11-22, PIPE-23) vk_draw.cpp: precompile_end_save's play-only save)
int pcache_play_cache = 0;
std::atomic<std::size_t> pcache_play_saved{0};
void pcache_main_written(int64_t) {}

View File

@@ -16,6 +16,7 @@
#define VK_USE_64_BIT_PTR_DEFINES 0
#include <vulkan/vulkan.h>
#include "vk_present_rect.h"
#include "framegen_refresh.h" // ((Triforce, 11-21, the upscaler audit's UPS-05) a fixed multiplier and the display's refresh
#include "framegen_rows.h" // (Triforce: the Frame generation row's AMD FSR 4, which framegen_record asks first)
using namespace mkwvk;
using VmaAllocation = void*;
@@ -28,7 +29,7 @@ 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 logf(const char*, ...) { std::abort(); } // ((Triforce, UPS-05) framegen_record's line takes arguments; never reached here)
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(); }
@@ -64,8 +65,10 @@ void vkCmdClearColorImage(VkCommandBuffer, VkImage, VkImageLayout, const VkClear
CHECK(c->float32[0] == 0 && c->float32[1] == 0 && c->float32[2] == 0 && c->float32[3] == 1); ++clears;
}
bool init() { ++initializations; return true; }
int multiplier();
bool ensure_size(VkCommandBuffer, uint32_t w, uint32_t h) {
++sizes; allocated_w = w; allocated_h = h; return true;
// (as the real one: the sets' frames are made for the multiplier, which framegen_record holds its own to)
++sizes; allocated_w = w; allocated_h = h; fg.mult = multiplier(); return true;
}
int64_t now_ns() { return 1000000; }
int multiplier() { return 2; }

View File

@@ -108,6 +108,9 @@ const std::vector<uint32_t>* renodx_replacement(uint32_t) { return nullptr; }
#define PROF_EXACT() // (vk_prof.h's exact-timing scope, since #989: nothing to time here)
// (local, texture detail: vk_shader.cpp's note of which bindings a module reads by exact texel. Nothing to note here)
void exact_note(VkShaderModule, const std::vector<uint32_t>&) {}
void lanes_note(VkShaderModule, const std::vector<uint32_t>&) {} // (PIPE-52: make_module's note of a module's Subgroup barriers)
void size_note(VkShaderModule, const std::vector<uint32_t>&) {} // (PIPE-56: its OpPhi and OpFunctionCall counts)
void size_move(VkShaderModule, VkShaderModule) {}
// (Triforce, RX_VK_SPV_PACK: vk_shader.cpp's packing of a ready program's stages. Off here, as with RX_VK_SPV_PACK=0:
// every stage keeps its module, which is what this test counts. tests/test_spv_store.py has the packing)
void pack_note(VkShaderModule, const uint32_t*, size_t) {}

View File

@@ -20,6 +20,9 @@
#include <unordered_map>
#include <utility>
#include <vector>
#include "flat_map.h" // ((Triforce) RecordingRes's c1_at)
#include "desc_pool_grow.h" // ((Triforce, 11-22, DESC-33) a slot's pool at half, whole once a recording ran out)
namespace desc_pool_grow = mkwvk::desc_pool_grow;
void require(bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", why); std::exit(1); }
@@ -44,6 +47,7 @@ using VmaAllocation = Allocation*;
struct { VkDevice device{}; VkPhysicalDeviceProperties props{}; } dev;
std::map<VkBuffer, Allocation> buffers;
std::set<VkDescriptorPool> pools;
std::map<VkDescriptorPool, uint32_t> pool_sets; // ((Triforce, 11-22, DESC-33) each pool's maxSets as it was made)
std::map<VkDescriptorSet, VkDescriptorPool> sets;
uintptr_t serial = 100;
template<class T> T handle() { return reinterpret_cast<T>(++serial); }
@@ -63,10 +67,10 @@ 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) {
VKAPI_ATTR VkResult VKAPI_CALL vkCreateDescriptorPool(VkDevice, const VkDescriptorPoolCreateInfo* ci, 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;
*pool = handle<VkDescriptorPool>(); pools.insert(*pool); pool_sets[*pool] = ci->maxSets; return VK_SUCCESS;
}
void drop_sets(VkDescriptorPool pool) {
for (auto it = sets.begin(); it != sets.end();) {
@@ -149,6 +153,7 @@ int64_t descs_of(const VkWriteDescriptorSet* w, size_t n) { int64_t k = 0; for (
bool bind_tail() {
VkCommandBuffer cmd{}; VkPipelineBindPoint bp = VK_PIPELINE_BIND_POINT_GRAPHICS;
struct { VkDescriptorSetLayout set_layout{}; VkPipelineLayout layout{}; } p;
auto& dwr = dw(); // ((Triforce, 11-22, DESC-20) as bind_descriptors takes it at its top)
/* DESCRIPTORS */
}
void begin(int slot = 0, uint64_t value = 1) { draw_begin_recording(slot, VkCommandBuffer{}, value); }
@@ -256,6 +261,37 @@ int main(int argc, char** argv) {
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 == "pool-grow" || mode == "pool-grow-off") {
// ((Triforce, 11-22, the performance audit's DESC-33) a slot's pool at half, whole from the reset after a recording
// ran out of it; with RX_VK_DESC_POOL_GROW=0 (the test's driver sets it for pool-grow-off) whole from the start
const bool on = mode == "pool-grow";
const auto first = res[0].pool;
writes.writes[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, writes.writes[0].descriptorCount = 3;
require(pool_sets.at(first) == (on ? 4096u : 8192u) && res[0].shift == (on ? 1u : 0u), "a slot's first pool: half with the rule, whole without");
require(bind_tail() && cur->extra.empty(), "a set from the slot's own pool");
begin(0, 2);
require(res[0].pool == first && pools.count(first) && pools_whole == 0 && pools_added == 0, "a slot that did not run out keeps its pool");
require(pool_most.sets == 1 && pool_most.n[desc_pool_grow::kSampled] == 3, "what the first recording took is not counted");
fail_sets = 1; require(bind_tail() && cur->extra.size() == 1 && pool_sets.at(cur->extra[0]) == 8192u, "the extra pool is not a whole one");
require(bind_tail() && cur->extra.size() == 1, "a second set from the extra pool");
const auto extra = cur->extra[0];
begin(0, 3);
require(cur->extra.empty() && !pools.count(extra) && pools_added == 1, "the extra pool is not destroyed and counted at the slot's reset");
require(pool_most.sets == 2 && pool_most.n[desc_pool_grow::kSampled] == 6 && res[0].took.sets == 0, "the peaks do not follow the recordings");
if (on) require(res[0].pool != first && !pools.count(first) && pool_sets.at(res[0].pool) == 8192u && res[0].shift == 0 && pools_whole == 1,
"a slot that ran out does not have a whole pool from its next reset");
else require(res[0].pool == first && pools.count(first) && pools_whole == 0, "without the rule the slot's pool is replaced");
const auto whole = res[0].pool;
fail_sets = 1; require(bind_tail() && cur->extra.size() == 1, "a whole slot that runs out gets an extra pool as before");
begin(0, 4);
require(res[0].pool == whole && pools.count(whole) && pools_whole == (on ? 1u : 0u) && pools_added == 2, "a whole slot's pool is made again");
// (the whole pool cannot be made: the half one is reset and kept, the slot never without a pool)
begin(1, 5); const auto other = res[1].pool;
fail_sets = 1; require(bind_tail() && cur->extra.size() == 1, "the second slot's extra pool");
fail_pool = true; begin(1, 6); fail_pool = false;
require(res[1].pool == other && pools.count(other) && res[1].extra.empty() && res[1].shift == (on ? 1u : 0u) &&
pools_whole == (on ? 1u : 0u), "a whole pool that could not be made cost the slot its pool");
require(bind_tail(), "the kept pool does not serve");
} 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");

View File

@@ -14,6 +14,11 @@
// better one as often as without it, and as soon after a change; a short trial never changes the way, one that finds the
// other way shorter is followed at once by a full one; a change the scene's means cannot see is found by a backed-off
// trial; the rule's no-trial verdicts hold; a load is no scene change.
// "settled": (Triforce, 11-22) Tuner::settled, after Kaefergeneral's #dev-fixes post (a 5900X and an RTX 3090, where the
// helpers lose in every settled scene): an area's load window (long frames, the replay most of them, nearly every draw
// cuttable) gets no verdict, nor the first window after it; a settled heavy scene where the helpers win still turns them on
// without a trial, a window after it settled; a trial the scene changed under changes nothing and the next comes soon
// after the scene settles. With settled off, each as before.
#include <cmath>
#include <cstdio>
#include <cstring>
@@ -193,11 +198,12 @@ struct Run {
long long first_on = -1, first_trial = -1;
};
/// frames presents of a scene through the tuner, the way in use applied as par_tune_present applies it
/// (backoff: the tuner's back-off; off for the checks of the trials' schedule as it was)
Run run_scene(const Scene& sc, int frames, bool use_rule, uint32_t seed, Tuner* keep = nullptr, bool backoff = false) {
/// (backoff: the tuner's back-off; off for the checks of the trials' schedule as it was. settled: Tuner::settled)
Run run_scene(const Scene& sc, int frames, bool use_rule, uint32_t seed, Tuner* keep = nullptr, bool backoff = false,
bool settled = true) {
Tuner local;
Tuner& t = keep ? *keep : local;
t.use_rule = use_rule, t.threads = 4, t.backoff = backoff;
t.use_rule = use_rule, t.threads = 4, t.backoff = backoff, t.settled = settled;
std::mt19937 r(seed);
bool on = false;
Run out;
@@ -222,7 +228,7 @@ void same_case() {
for (int run = 0; run < 60; run++) {
OldTuner old;
Tuner t;
t.use_rule = false, t.backoff = false;
t.use_rule = false, t.backoff = false, t.settled = false;
bool on = false, order_only = false;
const double base = 6.0 + (r() % 900) / 100.0, par = 0.85 + (r() % 30) / 100.0; // (the helpers 15% faster to 15% slower)
for (int i = 0; i < 12000; i++) {
@@ -277,11 +283,18 @@ void rule_case() {
CHECK(v.gain == v.saved && v.gain < 1.0 && v.kind != Verdict::kOn);
}
// the tuner on each scene, 20,000 presents
// (Triforce, 11-22: settled. On at the second window, the first with one before it to tell the scene settled; with
// settled off at the first, as before)
const Run heavy = run_scene(test4_heavy, 20000, true, 1);
CHECK(heavy.first_on == Tuner::kWindow); // (on at the first window, without a trial)
CHECK(heavy.first_on == 2 * Tuner::kWindow); // (on at the second window, without a trial)
CHECK(heavy.first_trial > heavy.first_on); // (a trial came after, kEvery later)
CHECK(heavy.changes == 0 && heavy.trials >= 6); // (and kept them, 12% shorter frames: no trial took them off)
CHECK(heavy.on_frames > 20000 - 120 - (heavy.trials + 1) * (Tuner::kSettle + Tuner::kTake)); // (but in the trials' serial parts)
CHECK(heavy.on_frames > 20000 - 240 - (heavy.trials + 1) * (Tuner::kSettle + Tuner::kTake)); // (but in the trials' serial parts)
{
const Run was = run_scene(test4_heavy, 20000, true, 1, nullptr, false, false);
CHECK(was.first_on == Tuner::kWindow && was.first_trial > was.first_on && was.changes == 0 && was.trials >= 6);
CHECK(was.on_frames > 20000 - 120 - (was.trials + 1) * (Tuner::kSettle + Tuner::kTake));
}
const Run gpu = run_scene(gpu_bound, 20000, true, 2);
CHECK(gpu.trials == 0 && gpu.flips == 0 && gpu.rule_off > 100); // (no trial at all: it cannot pay)
const Run game = run_scene(test7_trial4, 20000, true, 3);
@@ -299,7 +312,7 @@ void rule_case() {
// cannot pay, nothing at the title), the trials fewer where they decide, and none changed the way where it held
{
const Run h = run_scene(test4_heavy, 20000, true, 1, nullptr, true);
CHECK(h.first_on == Tuner::kWindow && h.first_trial > h.first_on && h.changes == 0);
CHECK(h.first_on == 2 * Tuner::kWindow && h.first_trial > h.first_on && h.changes == 0);
CHECK(h.trials >= 2 && h.trials < heavy.trials);
CHECK(h.on_frames > heavy.on_frames); // (fewer frames the other way)
const Run g = run_scene(gpu_bound, 20000, true, 2, nullptr, true);
@@ -394,7 +407,7 @@ void prev_case() {
PrevTuner prev;
Tuner t;
const bool use_rule = run % 2 == 0;
prev.use_rule = use_rule, t.use_rule = use_rule, t.backoff = false;
prev.use_rule = use_rule, t.use_rule = use_rule, t.backoff = false, t.settled = false;
prev.threads = t.threads = 2 + run % 3;
bool on_p = false, on_t = false, order_only = false;
Scene sc = scenes[r() % 6];
@@ -624,12 +637,119 @@ void backoff_case() {
}
}
// ---- (Triforce, 11-22) settled ----
// (from the post: the window of the teleport to the Market, "a frame of 14.84 ms ... the replay thread 11.37, 77% of it ...
// 1394 of its 1464 draws and dispatches in command buffers to cut"; the settled Market with the replay 62% of the frame and the
// helpers ~10% slower, 71.4 fps without them; Kokiri 85.8 fps, the helpers ~6% slower)
const Scene arrival{14.84, 11.37, 6.0, 1464, 1394, 6.0, 1.0};
const Scene market_settled{14.0, 8.7, 7.0, 3250, 3150, 6.5, 1.10};
const Scene kokiri{11.66, 7.4, 5.8, 2000, 1900, 4.0, 1.06};
struct Arrival {
int on_verdicts = 0, trials = 0, changes = 0;
long long first_on = -1, on_frames = 0;
std::vector<long long> unsettled; // (the presents of the windows the rule did not judge, before the first trial)
};
/// Kokiri for two windows, the load's window, then `to` (each window the rule's: the first trial comes at present 600)
Arrival run_arrival(const Scene& to, bool settled, uint32_t seed) {
Tuner t;
t.use_rule = true, t.backoff = true, t.settled = settled, t.threads = 4;
std::mt19937 r(seed);
bool on = false;
Arrival out;
for (int i = 0; i < 6000; i++) {
const Scene& sc = i < 2 * Tuner::kWindow ? kokiri : i < 3 * Tuner::kWindow ? arrival : to;
const Tuner::Step s = t.step(sample(sc, on, r), on, false);
if (s.ruled && s.verdict.kind == Verdict::kOn) out.on_verdicts++;
if (s.ruled && s.flip && out.first_on < 0) out.first_on = t.frames();
if (s.unsettled && !s.trial && !out.trials) out.unsettled.push_back(t.frames());
if (s.trial) out.trials++, out.changes += s.better;
if (s.flip) on = !on;
out.on_frames += on;
}
return out;
}
struct Straddle {
bool flipped = false, unsettled = false, second_ok = false, second_changed = true, on_after = true;
long long end1 = -1, end2 = -1, settle_at = -1;
};
/// A trial the scene changes under: its first part in test7's busy scene, its other way in a fade (5 ms frames either way,
/// as short as a loading screen's), its last part in a lighter area where the helpers lose by 8% (the trials decide there).
/// The trial's parts are told apart by its own changes of way
Straddle run_straddle(bool settled, uint32_t seed) {
const Scene fade{5.0, 0.6, 0.9, 30, 0, 0.1, 1.0};
const Scene lose{12.0, 9.6, 8.4, 1500, 1400, 3.4, 1.08};
Tuner t;
t.use_rule = true, t.backoff = true, t.settled = settled, t.threads = 4;
std::mt19937 r(seed);
bool on = false;
int part = 0; // (0: before the trial's first change of way, 1: its other way, 2: after)
Straddle out;
for (int i = 0; i < 6000; i++) {
const Scene& sc = part == 0 ? test7_busy : part == 1 ? fade : lose;
const Tuner::Step s = t.step(sample(sc, on, r), on, false);
if (s.settle && out.settle_at < 0) out.settle_at = t.frames();
if (s.trial && out.end1 < 0) {
out.end1 = t.frames(), out.flipped = s.flip, out.unsettled = s.unsettled;
} else if (s.trial && out.end2 < 0) {
out.end2 = t.frames(), out.second_ok = s.ok && !s.unsettled, out.second_changed = s.better;
}
if (s.flip && !s.trial && !s.ruled && part < 2) part++;
if (s.flip) on = !on;
if (out.end2 > 0 && t.frames() == out.end2) out.on_after = on;
}
return out;
}
void settled_case() {
CHECK(rule(mean_of(arrival), 4).kind == Verdict::kOn); // (as the post's log has it, judged alone)
CHECK(rule(mean_of(market_settled), 4).kind == Verdict::kTrial && rule(mean_of(kokiri), 4).kind == Verdict::kTrial);
// the load's window: no verdict, nor the first window after it; the settled Market's are the trials'
{
const Arrival n = run_arrival(market_settled, true, 61), o = run_arrival(market_settled, false, 61);
CHECK(n.on_verdicts == 0 && n.first_on < 0);
// (the first window, none before it; the load's; the Market's first)
CHECK((n.unsettled == std::vector<long long>{Tuner::kWindow, 3 * Tuner::kWindow, 4 * Tuner::kWindow}));
CHECK(n.trials >= 2 && n.changes == 0); // (the trials keep the replay thread alone)
CHECK(o.on_verdicts >= 1 && o.first_on == 3 * Tuner::kWindow && o.unsettled.empty()); // (before: on at the load's window)
std::printf("settled: the load's window into the Market: %d \"on\" verdicts (%d with settled off, on at present %lld), %d "
"windows not judged before the first trial; the helper threads in %lld of 6,000 frames (%lld)\n",
n.on_verdicts, o.on_verdicts, o.first_on, (int)n.unsettled.size(), n.on_frames, o.on_frames);
}
// a settled heavy scene where the helpers win (test4's): on without a trial, a window after it settled, and kept
{
const Arrival n = run_arrival(test4_heavy, true, 62), o = run_arrival(test4_heavy, false, 62);
CHECK(n.first_on == 5 * Tuner::kWindow); // (the heavy scene's second window: the first with one like it before)
CHECK(n.changes == 0 && n.on_frames > 6000 - 600 - (n.trials + 1) * (Tuner::kSettle + Tuner::kTake));
CHECK(o.first_on == 3 * Tuner::kWindow);
std::printf("settled: the load's window into test4's heavy scene: the helper threads on at present %lld without a trial "
"(%lld with settled off), kept by %d trials\n", n.first_on, o.first_on, n.trials);
}
// a trial the scene changed under: nothing changes, and the next comes soon after the scene settles
{
const Straddle n = run_straddle(true, 63), o = run_straddle(false, 63);
CHECK(n.end1 > 0 && n.unsettled && !n.flipped);
CHECK(n.settle_at > n.end1 && n.settle_at <= n.end1 + 2 * Tuner::kWindow);
// (the next trial kWindow after the settled window, and its 80 + 24 + 80 + 24 + 80 frames)
CHECK(n.end2 == n.settle_at + Tuner::kWindow + Tuner::kTake + 2 * (Tuner::kSettle + Tuner::kTake));
CHECK(n.second_ok && !n.second_changed && !n.on_after); // (the settled area's trial keeps the replay thread alone)
CHECK(o.flipped && !o.unsettled); // (before: the fade's short frames passed for the helpers' gain)
CHECK(o.end2 - o.end1 >= Tuner::kEvery);
std::printf("settled: a trial the scene changed under: %s (%s with settled off); the scene settled %lld presents after "
"it, the next trial ended %lld after it (%lld)\n", n.flipped ? "a change" : "no change",
o.flipped ? "the helper threads on" : "no change", n.settle_at - n.end1, n.end2 - n.end1, o.end2 - o.end1);
}
}
int main(int argc, char** argv) {
const std::string c = argc > 1 ? argv[1] : "";
if (c == "same") same_case();
else if (c == "prev") prev_case();
else if (c == "rule") rule_case();
else if (c == "backoff") backoff_case();
else if (c == "settled") settled_case();
else return std::printf("which case?\n"), 2;
return failures ? 1 : 0;
}

View File

@@ -159,6 +159,7 @@ void vkCmdPushDescriptorSetKHR(VkCommandBuffer, VkPipelineBindPoint, VkPipelineL
check_writes(n, writes); descriptor_pushes++;
}
VkDescriptorPool make_pool() { return reinterpret_cast<VkDescriptorPool>(uintptr_t(11)); }
void pool_took(const VkWriteDescriptorSet*, size_t) {} // ((Triforce, 11-22, DESC-33) vk_draw.cpp: the sets a recording took, counted)
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;

213
tests/test_achievements.py Normal file
View File

@@ -0,0 +1,213 @@
"""Achievements (host/achievements.h, host/achievements.c, host/hos_main.c, gpu/vk/achievement_rows.h, gpu/vk/vk_menu.cpp;
authored lists, no game).
Things done in the game that are marked as earned, once, with a notice on the screen and a sound, and a page of the
options that lists them all with their icons: on unless HOS_ACHIEVEMENTS=0. Compiled and run here:
- tests/achievements_test.c: host/achievements.h alone. The built-in list is read whole; the form of a list and each
thing that leaves an achievement out (no condition, a line that is none of the form's, a name that is empty, too long
or another's, a count that is none, an icon that is none, too many conditions, more than there is room for); when one
is earned (every condition, an item's count, a flag set and in the save data), that it stays earned and is not asked
about again; and the file of the earned ones, written and read back, with the lines of achievements no list has now
kept as they were;
- tests/achievements_host_test.c: host/achievements.c's own text against a stand-in save data, menu and game folder.
Without the switch nothing is asked, read, written, said or named; with it a player's own achievements.txt is read
after the built-in list, the list is named to the menu once, whole, when the renderer answers, six earned at once are
written at once and shown one at a time with the sound and never before the menu has the list, and at the next start
(a second run on the same folder) they are neither said nor shown again;
- tests/achievement_rows_test.cpp: gpu/vk/achievement_rows.h alone. Nothing shown until the list is whole, then as
named; one earned, its day and its notice once; names and lines cut to their room; the page's choice and pages under
the keys, the wheel and a click; how a notice comes in and goes.
Read in the sources: the built-in list asks only for items gpu/vk/oot_cheat_items.h already names, and for no flag, so
that no other name of the game's comes into the repository; the save data is asked at Link's update, once a second,
not before the switch is looked at, only while a save is loaded, and through nothing that writes; the host and the menu
have the same icons, the same room and the same option ids; the menu has the row, the page and the notice.
python -I -B tests/test_achievements.py [--cc gcc] [--cxx g++]
"""
import argparse
import os
from pathlib import Path
import re
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cc", "gcc", "--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
OWN_LIST = "[Mine]\nabout: A flag of the player's own choosing.\nicon: gem blue\nflag My_Flag\n[Not one]\n[Nor this]\nitem\n"
def check(cond, what):
if not cond:
raise SystemExit("FAIL: " + what)
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 run(command, says=None, **more):
"""A compiler or a fixture run; a fixture must say `says`"""
flags = (subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS) if os.name == "nt" else 0
result = subprocess.run(command, creationflags=flags, capture_output=True, text=True, encoding="utf-8", errors="replace", **more)
check(result.returncode == 0 and (says is None or says in result.stdout),
"%s:\n%s%s" % (" ".join(str(c) for c in command[:1] + command[-2:]), result.stdout, result.stderr))
return result.stdout
def check_sources():
read = lambda *parts: ROOT.joinpath(*parts).read_text(encoding="utf-8")
host, own, header, api = read("host", "hos_main.c"), read("host", "achievements.c"), read("host", "achievements.h"), read("gpu", "vk", "vk_api.h")
native, rows, menu, replay = read("host", "nvn_native.c"), read("gpu", "vk", "achievement_rows.h"), read("gpu", "vk", "vk_menu.cpp"), read("gpu", "vk", "vk_replay.cpp")
# at Link's update, after the other tools' reads; the switch before anything is asked of the game; only with a save loaded
update = block(host, "static void oot_update_player_cheats(")
check("oot_achievements_tick(c, now);" in update and update.index("oot_link_tick(c, actor, now);") < update.index("oot_achievements_tick(c, now);"),
"host/hos_main.c does not ask about the achievements at Link's update")
check(host.count("oot_achievements_tick(") == 2 and host.count("hos_achievements_check(") == 1, "the achievements are asked about from somewhere else too")
tick = block(host, "static void oot_achievements_tick(")
check("if (!hos_achievements_on() || now < next) return;" in tick and "next = now + 1000000000LL;" in tick
and tick.index("hos_achievements_on()") < tick.index("oot_pouch_count(")
and "if (oot_pouch_count(c, oot_item_names[0]) < 0) return;" in tick
and tick.index("oot_pouch_count(") < tick.index("hos_achievements_check("),
"the game is asked before the switch is, more than once a second, or while no save is loaded")
# reading only: the probe and host/achievements.c write nothing of the game's
probe = block(host, "static int oot_achievement_probe(")
for word in ("recomp_store", "oot_gd_set", "oot_item_give", "oot_flag_set", "A64_PTR"):
check(word not in probe and word not in tick, "asking about an achievement writes into the game: " + word)
for word in ("recomp_", "A64_PTR", "GP(", "a64_lookup"):
check(word not in own, "host/achievements.c reaches into the game: " + word)
check("on = !(e && e[0] == '0' && !e[1]);" in block(own, "int hos_achievements_on("), "the achievements are not on unless HOS_ACHIEVEMENTS=0")
for name in ("void hos_achievements_frame(", "void hos_achievements_check("): # (the switch before the lock, the lists and the menu)
body = block(own, name)
check("hos_achievements_on()" in body and body.index("hos_achievements_on()") < body.index("lock();") < body.index("lists_read();")
and body.count("unlock();") == 1 and "return;" not in body[body.index("lock();"):],
"%s the lists are read before the switch is looked at, or the lock is not let go" % name)
# the list named at the game's present, after the mods' cheats, from one place
present = block(native, "NV(nvnQueuePresentTexture) {")
check(present.index("hos_mod_cheats_frame();") < present.index("hos_achievements_frame();") and native.count("hos_achievements_frame();") == 1,
"the achievements are not named to the menu at the game's present")
# the host and the menu: the same ids, the same room, the same icons in the same order
enum = block(api, "enum { HOS_OPT_ACH_TEXT")
names = re.findall(r"\bHOS_OPT_\w+", enum)
check("HOS_OPT_ACH_TEXT = 165" in enum and names == ["HOS_OPT_ACH_TEXT", "HOS_OPT_ACH_ADD", "HOS_OPT_ACH_COUNT", "HOS_OPT_ACH_EARNED", "HOS_OPT_ACH_ASK"],
"the option ids are not 165 to 169")
others = re.sub(r"enum \{ HOS_OPT_ACH_TEXT[^}]*\}", "", api)
for value in range(165, 170):
check(not re.search(r"= %d\b" % value, others), "another option has the id %d" % value)
check("enum { HOS_ACH_EARNED = 256 };" in api and "constexpr int kEarned = 256;" in rows, "the earned bit is not the same on both sides")
number = lambda text, name: int(re.search(r"\b%s = (\d+)\b" % name, text).group(1))
check(number(header, "ACH_MAX") == number(rows, "kSlots") and number(header, "ACH_NAME") + 1 == number(rows, "kName")
and number(header, "ACH_ABOUT") + 1 == number(rows, "kAbout"), "the host's list and the menu's have not the same room")
words = lambda text, name: re.findall(r'"(\w+)"', re.search(r"#define %s (.*)" % name, text).group(1))
shapes = re.search(r"enum Shape \{ ([^}]*) \}", rows).group(1).split(", ")
tones = re.search(r"enum Tone \{ ([^}]*) \}", rows).group(1).split(", ")
check(["k" + w.capitalize() for w in words(header, "ACH_SHAPES")] + ["kShapes"] == shapes, "the icons' shapes differ: %s" % shapes)
check(["k" + w.capitalize() for w in words(header, "ACH_TONES")] + ["kTones"] == tones, "the icons' tones differ: %s" % tones)
check(len(shapes) - 1 <= 16 and len(tones) - 1 <= 16, "an icon no longer fits its eight bits")
icon = block(menu, "void ach_icon(")
for shape in shapes[1:-1]: # (kStar is the default)
check("case ar::%s" % shape in icon, "the menu does not draw the shape %s" % shape)
check(len(re.findall(r"ui::rgba\(", icon.split("};")[0])) == len(tones) - 1, "the menu has not a colour for every tone")
# the menu: told by the host, the row shown once it was, its page, the notice
check("if (id == HOS_OPT_ACH_ASK) return 1;" in replay, "the renderer does not say that it shows the achievements")
setter = block(menu, "void menu_option_set(")
for line in ("if (id == HOS_OPT_ACH_TEXT) return ach_list.text(value);", "if (id == HOS_OPT_ACH_ADD) return ach_list.add(value);",
"ach_list.done(value);", "if (ach_list.earn(value)) menu_refresh = true;"):
check(line in setter, "the menu does not take the host's achievements: " + line)
table = menu[menu.index("Row rows[] = {"):]
check(table.index('"Bug capture (F8)"') < table.index('{TAB_SYSTEM, "Achievements", nullptr, 0, 0, 0, nullptr,') < table.index('MENU_HEADING(TAB_SYSTEM, "Mod cheats")')
and 'constexpr char kAchRow[] = "Achievements";' in menu, "the Achievements row is not on the System tab after Bug capture (F8)")
check("(strcmp(rows[i].name, kAchRow) || rows[i].tab != TAB_SYSTEM || ach_list.shown() > 0) &&" in block(menu, "bool row_shown("),
"the Achievements row shows before the host named them")
press = menu[menu.index("else if (press[7] && is_ach_row(r)) page = kAchPage"):]
check(press.index("is_ach_row(r)") < press.index("else if (press[7] && !r.value && can_bind()) page = 1"),
"the Achievements row opens the Controls page: it is asked after the rows without a value")
check("else if (page == kAchPage) draw_achievements(k);" in block(menu, "void draw_menu(ui::Ctx& c, bool perf_under) {"), "the Achievements page is not drawn")
page = number(menu, "kAchPage")
check(page not in (0, 1, 3, 4, 5, 6, number(menu, "kModsPage"), number(menu, "kModsRowPage")), "kAchPage is another page's number")
for record in ("void menu_record(", "void menu_record_image("):
body = block(menu, record)
check("ach_notice >= 0 && prof_now() - ach_notice_at < kAchNoticeMs * 1000000" in body and "&& !earned)" in body
and "if (earned) draw_ach_notice(c);" in body, "%s an achievement's notice is not drawn, or not while nothing else shows" % record)
check(number(own, "SHOWN_MS") > int(re.search(r"kAchNoticeMs = (\d+);", menu).group(1)), "the host shows the next notice before this one is gone")
return api, own
def check_names(exe):
"""The built-in list asks only for items the Inventory page already names, a count only of one that has a count, and no flag"""
items = dict(re.findall(r'OOT_ITEM\("[^"]+", "([^"]+)", ([01])\)', (ROOT / "gpu" / "vk" / "oot_cheat_items.h").read_text(encoding="utf-8")))
check(len(items) > 60, "gpu/vk/oot_cheat_items.h: the items were not read")
asked = [line.split() for line in run([str(exe), "names"]).splitlines()]
check(len(asked) >= 25, "the built-in list's conditions were not printed")
for kind, what, least in asked:
check(kind == "I", "the built-in list asks for a flag: %s" % what)
check(what in items, "the built-in list names what gpu/vk/oot_cheat_items.h does not: %s" % what)
check(least == "1" or items[what] == "1", "the built-in list counts an item that has no count: %s" % what)
header = (ROOT / "host" / "achievements.h").read_text(encoding="utf-8")
quoted = set(re.findall(r"\b((?:Item|Weapon|Armor)_[A-Za-z0-9_]+)", header))
check(quoted and quoted <= set(items), "host/achievements.h names what gpu/vk/oot_cheat_items.h does not: %s" % sorted(quoted - set(items))[:3])
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cc", default="cc")
p.add_argument("--cxx", default="c++")
a = p.parse_args()
api, own = check_sources()
with tempfile.TemporaryDirectory(prefix="triforce-achievements-") as tmp:
tmp = Path(tmp)
warnings = ["-std=c11", "-Wall", "-Wextra", "-Werror"]
# the list, the conditions and the file
exe = tmp / "list.exe"
run([a.cc] + warnings + ["-I" + str(ROOT / "host"), str(ROOT / "tests" / "achievements_test.c"), "-o", str(exe)])
run([str(exe)], says="PASS")
check_names(exe)
# the menu's side
exe = tmp / "rows.exe"
run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu" / "vk"), str(ROOT / "tests" / "achievement_rows_test.cpp"),
"-o", str(exe)])
run([str(exe)], says="PASS")
# the host's side: host/achievements.c from after its includes on, with the menu's ids from vk_api.h
marker = '#include "menu_sound.h"\n'
check(own.count(marker) == 1, "host/achievements.c no longer includes menu_sound.h last")
(tmp / "achievements_actual.inc").write_text(own[own.index(marker) + len(marker):], encoding="utf-8")
(tmp / "achievements_constants.inc").write_text(
"".join(block(api, start) + ";\n" for start in ("enum { HOS_OPT_ACH_TEXT = 165", "enum { HOS_ACH_EARNED = 256", "enum { HOS_MENU_SOUND_OPEN = 1")),
encoding="utf-8")
exe = tmp / "host.exe"
run([a.cc] + warnings + ["-Wno-unused-function", "-I" + str(ROOT / "host"), "-I" + str(tmp), str(ROOT / "tests" / "achievements_host_test.c"),
"-o", str(exe)])
game = tmp / "game folder"
game.mkdir()
(game / "achievements.txt").write_bytes(OWN_LIST.encode("utf-8"))
earned = game / "triforce_achievements.txt"
env = {k: v for k, v in os.environ.items() if k != "HOS_ACHIEVEMENTS"}
run([str(exe), str(game), "off"], says="PASS", cwd=str(tmp), env=dict(env, HOS_ACHIEVEMENTS="0")) # (on unless it is 0)
check(not earned.exists(), "a file was written with the achievements off")
on = dict(env, HOS_ACHIEVEMENTS="1")
run([str(exe), str(game), "first"], says="PASS", cwd=str(tmp), env=env) # (on by default: the switch not set)
kept = earned.read_text(encoding="utf-8").splitlines()
check(kept[0].startswith("#") and [line.split(" ", 2)[2] for line in kept[1:]] ==
["A Sword of Your Own", "Forest's Thanks", "Heart of the Mountain", "Jewel of the River", "Three Stones", "Mine"],
"what the game folder keeps of the earned ones: %r" % kept)
check(all(re.fullmatch(r"[0-9a-f]{8} \d{9,} .+", line) for line in kept[1:]), "an earned one is not its key, its time and its name: %r" % kept)
with open(earned, "ab") as f: # (an achievement of a list that was taken away since)
f.write(b"deadbeef 5 Gone\n")
run([str(exe), str(game), "second"], says="PASS", cwd=str(tmp), env=on)
check(sorted(f.name for f in game.iterdir()) == ["achievements.txt", "triforce_achievements.txt"], "the game folder has other files now")
check((game / "achievements.txt").read_bytes() == OWN_LIST.encode("utf-8"), "the player's own list was changed")
print("PASS: achievements: the list and what it refuses, earning, the file of the earned ones, the host's naming and notices, "
"the menu's list and page, and on unless HOS_ACHIEVEMENTS=0")
if __name__ == "__main__":
main()

104
tests/test_alt_menu.py Normal file
View File

@@ -0,0 +1,104 @@
"""A lone Alt does not freeze the start-up screen (host/display.c keymenu_dropped, wndproc; no game, no window).
After Casual Gamer's #dev-fixes post. Pressed and let go alone, Alt has DefWindowProc enter the menu loop (WM_SYSCOMMAND with
SC_KEYMENU) on the window's thread, which during the start-up screen is the one waiting for the shaders: the screen froze
until a key was pressed. The window has no menu bar, so the command is answered 0 there.
Cuts keymenu_dropped out of display.c and runs it with stand-ins for the Windows types: a lone Alt's command (lp 0) and one
for a letter with Alt (lp the character) are dropped, Alt+Space's (the system menu) is not, nor are the other system commands
(SC_CLOSE: Alt+F4; SC_SCREENSAVE; SC_MAXIMIZE), whatever their low bits; HOS_ALT_MENU=1 drops none. Then the source: the
window procedure asks it first in WM_SYSCOMMAND and returns 0, and Alt+Enter and F10 are still WM_SYSKEYDOWN's; the switch is
in docs/development.md.
python -I -B tests/test_alt_menu.py [--cxx g++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
if text.count(marker) != 1:
raise SystemExit("FAIL: not found once in display.c: " + 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]
FIXTURE = r"""
#include <cstdio>
#include <cstdlib>
#include <cstdint>
typedef uintptr_t WPARAM;
typedef intptr_t LPARAM;
#define SC_KEYMENU 0xF100
#define SC_CLOSE 0xF060
#define SC_SCREENSAVE 0xF140
#define SC_MAXIMIZE 0xF030
@@DROPPED@@
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
int main(int argc, char**) {
if (argc > 1) { // (HOS_ALT_MENU=1: as before)
CHECK(!keymenu_dropped(SC_KEYMENU, 0) && !keymenu_dropped(SC_KEYMENU, 'f'));
std::printf("alt menu: HOS_ALT_MENU=1 leaves the menu loop to Windows, as before\n");
return 0;
}
CHECK(keymenu_dropped(SC_KEYMENU, 0)); // (a lone Alt, or F10 let through)
CHECK(keymenu_dropped(SC_KEYMENU | 3, 0)); // (the low four bits are Windows' own)
CHECK(keymenu_dropped(SC_KEYMENU, 'f')); // (Alt and a letter: a menu's mnemonic, and there is no menu)
CHECK(!keymenu_dropped(SC_KEYMENU, ' ')); // (Alt+Space: the system menu)
CHECK(!keymenu_dropped(SC_CLOSE, 0) && !keymenu_dropped(SC_SCREENSAVE, 0) && !keymenu_dropped(SC_MAXIMIZE, 0));
std::printf("alt menu: a lone Alt enters no menu loop\n");
return 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
display = (ROOT / "host/display.c").read_text(encoding="utf-8")
fn = block(display, "static int keymenu_dropped(WPARAM wp, LPARAM lp) {")
proc = block(display, "static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {")
sys_cmd = proc[proc.index("case WM_SYSCOMMAND:"):]
sys_cmd = sys_cmd[:sys_cmd.index("break;")]
if "if (keymenu_dropped(wp, lp)) return 0;" not in sys_cmd or \
sys_cmd.index("keymenu_dropped") > sys_cmd.index("SC_SCREENSAVE"):
raise SystemExit("FAIL: the window procedure does not answer a lone Alt's menu command itself")
sys_key = proc[proc.index("case WM_SYSKEYDOWN:"):]
if "if ((wp == VK_RETURN && (lp & (1 << 29))) || wp == VK_F10) {" not in sys_key[:sys_key.index("break;")]:
raise SystemExit("FAIL: Alt+Enter and F10 are no longer WM_SYSKEYDOWN's")
if "`HOS_ALT_MENU=1`" not in (ROOT / "docs/development.md").read_text(encoding="utf-8"):
raise SystemExit("FAIL: HOS_ALT_MENU is not in docs/development.md's switches")
flags = subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
with tempfile.TemporaryDirectory(prefix="alt-menu-") as tmp:
src, exe = Path(tmp) / "alt.cpp", Path(tmp) / "alt.exe"
src.write_text(FIXTURE.replace("@@DROPPED@@", fn), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", str(src), "-o", str(exe)], check=True)
env = dict(os.environ)
env.pop("HOS_ALT_MENU", None)
ran = 0
for argv, value in (([str(exe)], None), ([str(exe), "before"], "1")):
if value is not None:
env["HOS_ALT_MENU"] = value
r = subprocess.run(argv, env=env, capture_output=True, text=True, timeout=120, creationflags=flags)
print(r.stdout, end="")
ran = ran or r.returncode
if ran:
raise SystemExit("FAIL: a lone Alt's menu command")
print("PASS: a lone Alt enters no menu loop; Alt+Space, Alt+F4, Alt+Enter and F10 as they were")
if __name__ == "__main__":
main()

View File

@@ -1,4 +1,4 @@
// astc_bc_policy::all_by_vram (gpu/vk/astc_bc_policy.h): the cards that keep every large ASTC texture as BC3.
// astc_bc_policy::all_by_vram and all (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"
@@ -24,6 +24,19 @@ int main() {
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");
// (Triforce, 11-22, the performance audit's TEX-12, after Source A's) every integrated GPU, whatever it is allowed
using astc_bc_policy::all;
expect(all(10232 * MB, true), "an integrated GPU allowed 10.2 GB (a 16 GB handheld)");
expect(all(0, true), "an integrated GPU whose allowance is not known");
expect(all(7402 * MB, true), "an integrated GPU allowed 7.4 GB");
expect(all(21000 * MB, true), "an integrated GPU allowed 21 GB (a PC with 32 GB)");
expect(!all(15000 * MB, false), "a discrete 16 GB card: as before");
expect(!all(0, false), "a discrete card whose allowance is not known: as before");
expect(!all(9400 * MB, false) && !all(11200 * MB, false) && !all(31000 * MB, false), "discrete 10, 12 and 32 GB cards: as before");
expect(all(3379 * MB, false) && all(7133 * MB, false) && all(8703 * MB, false) && !all(8704 * MB, false),
"discrete cards under 8.5 GB: as before, and the line itself");
for (uint64_t mb = 0; mb <= 49152; mb += 37) // (a discrete card is the old rule at every size)
expect(all(mb * MB, false) == all_by_vram(mb * MB), "a discrete card differs from the rule by the allowance alone");
if (failures) return EXIT_FAILURE;
std::printf("astc bc policy: all checks passed\n");
return EXIT_SUCCESS;

View File

@@ -4,7 +4,7 @@ import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "g++"]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
@@ -12,10 +12,20 @@ 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"):
# ((Triforce, 11-22, the performance audit's TEX-12) the rule is asked with the card's type; an unknown allowance is 0
for needle in ('#include "astc_bc_policy.h"', "astc_bc_policy::all(allowed, integrated)", 'getenv("RX_VK_ASTC_BC_ALL")',
"(bw * bh >= 36 || all) ? low : 64ull << 20",
"if (!vram_usage(&used, &allowed)) allowed = 0;",
"const bool integrated = dev.props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;\n"
" const bool small = astc_bc_policy::all(allowed, integrated);",
"if (const char* e = getenv(\"RX_VK_ASTC_BC_ALL\")) return e[0] == '1';\n uint64_t used = 0, allowed = 0;"):
if needle not in src:
raise SystemExit("vk_texture.cpp no longer has: " + needle)
if "astc_bc_policy::all_by_vram(" in src:
raise SystemExit("vk_texture.cpp still asks the rule by the allowance alone: an integrated GPU allowed 8.5 GB or more is missed")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `RX_VK_ASTC_BC_ALL=1` or `0` |" not in docs:
raise SystemExit("docs/development.md: RX_VK_ASTC_BC_ALL is not in the switches table")
def main():

197
tests/test_astc_reuse.py Normal file
View File

@@ -0,0 +1,197 @@
"""Decoded ASTC used again, its first step: the blocks hashed, the finalized textures listed, a hit counted
(gpu/vk/astc_reuse.h, vk_texture.cpp; no game, no GPU).
(Triforce, 11-21, the performance audit's TEX-14, after Source A's.) When the game comes back to an area it makes its
textures again, mostly at other addresses, and the same ASTC blocks are decoded and encoded again while the image made
of them the first time may still be in VRAM. This step counts what taking that image would save (RX_VK_ASTC_REUSE=2);
nothing is copied.
This compiles astc_reuse.h alone: the switch's modes; the two lanes are different hashes and both change with one byte;
a storage hashed in pieces of any size gives one hash, and another order or length another; never all zero; shapes that
differ in any one field never match; a texture is listed once, found by hash, both lanes and shape, never itself, and
unlisted by its pointer alone.
And reads the source: with the switch off nothing of it runs; a new ASTC texture's blocks are hashed once at its first
upload and the count never stands in for the decode; a finalized one is listed only when it was uploaded from memory,
never drawn into, and of the finalized texture's own description; it is unlisted when used again or gone; the switch
is in the switches table.
python -I -B tests/test_astc_reuse.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def block(text, head):
at = text.index(head)
return text[at:text.index("\n}\n", at) + 3]
def in_order(text, parts, why):
at = 0
for p in parts:
at = text.find(p, at)
check(at >= 0, why + " (missing, or out of order: " + p + ")")
at += len(p)
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <vector>
#include "astc_reuse.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace astc_reuse;
struct Tex { Shape s; int id; };
static Hash of(const std::vector<uint8_t>& d, uint64_t piece) {
Hasher h;
for (uint64_t at = 0; at < d.size(); at += piece) h.add(d.data() + at, d.size() - at < piece ? d.size() - at : piece);
return h.done();
}
int main() {
CHECK(mode(nullptr) == kOff && mode("") == kOff && mode("0") == kOff && mode("1") == kCopy && mode("2") == kCount && mode("3") == kOff &&
mode("on") == kOff);
std::vector<uint8_t> d(3 * kStep + 12345);
uint32_t r = 7;
for (uint8_t& b : d) r = r * 1664525u + 1013904223u, b = (uint8_t)(r >> 24);
// the lanes: two hashes, each moved by one byte anywhere
CHECK(lane2(d.data(), d.size()) != upload_same::content_hash(d.data(), d.size()));
for (const size_t at : {(size_t)0, (size_t)63, (size_t)4096, d.size() / 2, d.size() - 1}) {
const Hash before = of(d, 64u << 20);
const uint64_t l2 = lane2(d.data(), d.size());
d[at] ^= 0x10;
const Hash after = of(d, 64u << 20);
CHECK(after.a != before.a && after.b != before.b && lane2(d.data(), d.size()) != l2);
d[at] ^= 0x10;
CHECK(of(d, 64u << 20) == before);
}
// pieces: whatever multiple of the step the storage is read in, one hash
const Hash whole = of(d, 64u << 20);
CHECK(of(d, kStep) == whole && of(d, 2 * kStep) == whole && of(d, 1u << 20) == whole);
CHECK(bool(whole));
{ // the length and the order count
std::vector<uint8_t> shorter(d.begin(), d.end() - 1), swapped(d);
for (size_t i = 0; i < kStep; i++) std::swap(swapped[i], swapped[i + kStep]);
CHECK(!(of(shorter, 64u << 20) == whole) && !(of(swapped, 64u << 20) == whole));
Hasher none;
CHECK(bool(none.done())); // (never all zero: zero is "not hashed")
CHECK(!bool(Hash{}));
}
// shapes: every field
const Shape s{131, 1024, 512, 1, 11, 6, 0, 0x79, 2, true, false, false, false, false};
CHECK(s == s);
{ Shape o = s; o.format = 145; CHECK(!(o == s)); }
{ Shape o = s; o.width = 512; CHECK(!(o == s)); }
{ Shape o = s; o.height = 1024; CHECK(!(o == s)); }
{ Shape o = s; o.depth = 2; CHECK(!(o == s)); }
{ Shape o = s; o.levels = 10; CHECK(!(o == s)); }
{ Shape o = s; o.layers = 1; CHECK(!(o == s)); }
{ Shape o = s; o.skip = 1; CHECK(!(o == s)); }
{ Shape o = s; o.nvn_format = 0x7a; CHECK(!(o == s)); }
{ Shape o = s; o.layout = 3; CHECK(!(o == s)); }
{ Shape o = s; o.bc = false; CHECK(!(o == s)); }
{ Shape o = s; o.bc_resized = true; CHECK(!(o == s)); }
{ Shape o = s; o.bc1 = true; CHECK(!(o == s)); }
{ Shape o = s; o.bc1_no = true; CHECK(!(o == s)); }
{ Shape o = s; o.detail_skip = true; CHECK(!(o == s)); }
// the list
Freed<Tex> freed;
Tex a{s, 1}, b{s, 2}, c{s, 3};
c.s.width = 2048;
const Hash h1{11, 22}, h2{11, 33}, h3{44, 22};
const auto shape_of = [](const Tex* t) { return t->s; };
CHECK(freed.size() == 0 && freed.find(h1, s, nullptr, shape_of) == nullptr);
freed.list(h1, &a);
CHECK(freed.size() == 1 && freed.find(h1, s, nullptr, shape_of) == &a);
CHECK(freed.find(h1, s, &a, shape_of) == nullptr); // (never itself)
CHECK(freed.find(h2, s, nullptr, shape_of) == nullptr); // (the second lane differs)
CHECK(freed.find(h3, s, nullptr, shape_of) == nullptr); // (the first lane differs)
CHECK(freed.find(h1, c.s, nullptr, shape_of) == nullptr); // (another shape)
freed.list(h1, &c); // (the same blocks decoded to another shape)
CHECK(freed.find(h1, c.s, nullptr, shape_of) == &c && freed.find(h1, s, nullptr, shape_of) == &a);
freed.list(h2, &b); // (the first lane alike, the second not)
CHECK(freed.size() == 3 && freed.find(h2, s, nullptr, shape_of) == &b && freed.find(h1, s, nullptr, shape_of) == &a);
Tex never{s, 4};
freed.unlist(h1, &never), freed.unlist(h3, &b); // (not listed, or not under that hash: nothing goes)
CHECK(freed.size() == 3);
freed.unlist(h1, &a);
CHECK(freed.size() == 2 && freed.find(h1, s, nullptr, shape_of) == nullptr && freed.find(h1, c.s, nullptr, shape_of) == &c);
freed.unlist(h1, &a); // (once is all)
CHECK(freed.size() == 2);
freed.unlist(h2, &b), freed.unlist(h1, &c);
CHECK(freed.size() == 0);
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
head = (VK / "vk_texture.h").read_text(encoding="utf-8")
check('static const astc_reuse::Mode m = astc_reuse::mode(getenv("RX_VK_ASTC_REUSE"));' in tex, "the switch is not the rule's")
check("astc_reuse::Hash blocks_hash;" in head and "bool freed_listed = false;" in head, "a texture does not keep its blocks' hash")
inner = block(tex, "bool upload_inner(Texture& tex, const Ranges* dirty) {")
ask = ("if (tex.astc && !tex.uploaded && !tex.blocks_hash && astc_reuse_mode() != astc_reuse::kOff && reuse_upload(tex, total))")
in_order(inner, [ask, "if (tex.astc && (up_route = UP_ASTC, upload_astc_gpu(tex, total, dirty))) return true;"],
"upload_inner does not hash a new ASTC texture's blocks once, only with the switch on, before its decode")
up = block(tex, "bool reuse_upload(Texture& tex, uint64_t total) {")
in_order(up, ["h.add(guest_bytes(tex.gpu + at, n, scratch), n);", "tex.blocks_hash = h.done();",
"if (tex.sparse || tex.type != VK_IMAGE_TYPE_2D || !tex.image) return false;", "std::lock_guard lk(reuse_mu);",
"reuse_freed.find(tex.blocks_hash, reuse_shape(tex), &tex,",
"if (!from || !from->image || !from->uploaded || from->gpu_written) return false;",
"reuse_freed.unlist(from->blocks_hash, from);", "from->freed_listed = false;", "st_reuse_hits"],
"reuse_upload does not hash the blocks as the decoder is given them, or look for a listed texture under the lock")
check("return true;" not in up, "reuse_upload stands in for the decode: this step only counts, and nothing was copied")
shape = block(tex, "astc_reuse::Shape reuse_shape(const Texture& t) {")
check("(uint32_t)t.format, t.width, t.height, t.depth, t.levels, t.layers, t.skip, (uint32_t)t.nvn_format, t.key_layout," in shape and
"t.bc, t.bc_resized, t.bc1, t.bc1_no, t.detail_skip};" in shape, "a texture's shape leaves a field of its image out")
rel = block(tex, "void reuse_released(const nvn_tex* t) {")
in_order(rel, ["if (!x->astc || !x->blocks_hash || !x->uploaded || x->gpu_written || x->freed_listed) continue;",
"x->d.format != t->d.format", "x->d.levels != t->d.levels || x->d.target != t->d.target", "std::lock_guard lk(reuse_mu);",
"reuse_freed.list(x->blocks_hash, x);", "x->freed_listed = true;"],
"a finalized texture is listed though it was drawn into, not uploaded, not hashed, or of another description")
# ((Triforce, 11-22, the performance audit's MEM-85) texture_released's body is texture_released_at, from a given generation)
released = block(tex, "void texture_released_at(const nvn_tex* t, WriteGen gen) {")
check("if (astc_reuse_mode() != astc_reuse::kOff) reuse_released(t);" in released, "texture_released does not list, or lists with the switch off")
for name, fn in (("forget_gpu", "void forget_gpu(Texture* tex) {"), ("texture_touch", "Texture* texture_touch(Texture* tex, bool for_sampling) {"),
("texture_touch_quick", "bool texture_touch_quick(Texture* tex, bool for_sampling) {")):
body = block(tex, fn)
check("if (tex->freed_listed) reuse_drop(*tex);" in body and body.index("if (tex->freed_listed) reuse_drop(*tex);") < 400,
name + " does not unlist a texture used again or gone: a later texture could be given a dangling or a live one's image")
drop = block(tex, "void reuse_drop(Texture& t) {")
in_order(drop, ["std::lock_guard lk(reuse_mu);", "if (t.freed_listed) reuse_freed.unlist(t.blocks_hash, &t), t.freed_listed = false;"],
"reuse_drop does not unlist under the lock")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_ASTC_REUSE=2`" in dev, "docs/development.md: RX_VK_ASTC_REUSE is not in the switches table")
with tempfile.TemporaryDirectory(prefix="astc-reuse-") as tmp:
c, exe = Path(tmp) / "reuse.cpp", Path(tmp) / "reuse.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rules as made:\n" + r.stdout)
print("PASS: decoded ASTC's blocks are hashed, the finalized textures listed and a hit counted")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,82 @@
"""Compile the actual mix effects (biquad filter, delay, plain reverb), the splitter destinations' biquad filters and
their nn::audio setters from host/nn_audio.c around authored mixes and samples.
The "Volcano eruption audio bugged" ticket: the game's AddBiquadFilter, AddDelay and AddReverb on its sub mixes and
SetSplitterBiquadFilterParameter were import stubs, so what it routes through them played unfiltered and too loud.
Checked: a low pass passes a low tone and takes a high one down, a delay's echo lands at the delay time and decays by
the feedback gain, the channel spread moves the echo to the other channel, the plain reverb adds a tail on top of the
dry signal, a splitter destination's filter reaches the voice's mix through render_voice, and with
HOS_AUDIO_EFFECTS=0 every sample is left as it was. No game, SDK or output device is used.
"""
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]
SPLIT_BIQUAD = ("_ZN2nn5audio32SetSplitterBiquadFilterParameterEPNS0_12SplitterTypeEiPNS0_10SubMixTypeEiRKNS0_22"
"TBiquadFilterParameterIfEE")
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;")]
effects = source[source.index("/* ---- I3DL2 reverb (on a sub mix"):source.index("/* ---- the mixer")]
setters = source[source.index("/* ---- I3DL2 reverb: AddI3dl2Reverb"):]
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 float farg(", "static int mix_index(",
"static Mix *mix_of(", "static int splitter_index(", "static Splitter *splitter_of("))
actual.extend([queue, helper.block(source, "static void apply("), helper.block(source, "static void submit("),
helper.block(source, "static void commit("), effects,
"static int next_source_frame(Voice *v); /* (the fixture's tone) */",
helper.block(source, "static void render_voice("),
helper.block(source, "static void set_split_biquad("),
helper.block(source, "HLE(" + SPLIT_BIQUAD + ")"), setters])
flags = subprocess.CREATE_NO_WINDOW | subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
with tempfile.TemporaryDirectory(prefix="audio-mix-effects-") as temporary:
folder = Path(temporary)
(folder / "actual_audio_effects.inc").write_text("\n\n".join(actual), encoding="utf-8")
executable = folder / "fixture.exe"
build = [args.cc, "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter",
"-Wno-unused-function", "-I" + str(folder), str(ROOT / "tests/audio_mix_effects_test.c"), "-lm", "-o",
str(executable)]
env = {k: v for k, v in os.environ.items() if not k.startswith("HOS_AUDIO")}
runs = [(build, env), ([str(executable)], env), ([str(executable)], dict(env, HOS_AUDIO_EFFECTS="0"))]
for command, environment in runs:
result = subprocess.run(command, creationflags=flags, capture_output=True, text=True, env=environment,
encoding="utf-8", errors="replace", timeout=60)
print(result.stdout, end="")
print(result.stderr, end="", file=sys.stderr)
if result.returncode:
raise SystemExit(result.returncode)
if __name__ == "__main__":
main()

View File

@@ -106,8 +106,25 @@ def check_source():
submit = block(replay, "void submit_recording(Recording& r, bool signal_value = true, uint64_t signal_at = 0, bool part = false) {")
in_order(submit, ["if (event_writes_held(r.cmd)) {", "end_pass(r.cmd), full_barrier(r.cmd,", "draw_flush_recording(r.slot);",
"if (batch_on()) {", "barrier_rules::batch_hold(way, !signal_value, submit_depth > 0);",
"if (hold || !pend.cbs.empty()) {", "pend.recs++;", "barrier_rules::batch_flush(way, hold,", "return;",
"if (hold || !pend.cbs.empty()) {", "pend.recs++;", "barrier_rules::batch_flush(way, hold,",
# ((Triforce, 11-22, the performance audit's SYNC-09) RX_VK_BATCH_IDLE: after the cap's flush, the rule before the GPU's poll
"if (barrier_rules::batch_idle(way, !pend.cbs.empty(), batch_idle_on()) && batch_gpu_idle()) flush_reason = FR_IDLE, queue_flush_locked();",
"return;",
"vkQueueSubmit2(dev.queue, 1, &si, r.fence);"], "vk_replay.cpp submit_recording")
# ((Triforce, 11-22, the performance audit's SYNC-09) RX_VK_BATCH_IDLE: read once through the rule, off unless set; the GPU's
# poll under the queue's lock; its flushes counted as "idle"; the switches table
idle_on = block(replay, "bool batch_idle_on() {")
if 'barrier_rules::batch_idle_on(getenv("RX_VK_BATCH_IDLE"));' not in idle_on or "static const bool on" not in idle_on:
raise SystemExit("vk_replay.cpp batch_idle_on does not read RX_VK_BATCH_IDLE once through the rule")
gpu_idle = block(replay, "bool batch_gpu_idle() {")
if "vkGetSemaphoreCounterValue(dev.device, batch_tl, &done) == VK_SUCCESS && done >= batch_flushed;" not in gpu_idle:
raise SystemExit("vk_replay.cpp batch_gpu_idle is not the batches' timeline against the last batch flushed")
if replay.count("batch_gpu_idle()") != 2 or "FR_VALUE, FR_IDLE, FR_COUNT };" not in replay or \
"(long long)(st_reason[FR_IDLE] - r0[FR_IDLE])," not in replay or "value %lld, idle %lld, other %lld" not in replay:
raise SystemExit("vk_replay.cpp: the idle flush is asked for somewhere else, or not counted in the batched submits' line")
dev_md = (VK.parent.parent / "docs/development.md").read_text(encoding="utf-8")
if not any(line.startswith("| `RX_VK_BATCH_IDLE=1` |") and "**off unless set on every driver**" in line for line in dev_md.splitlines()):
raise SystemExit("docs/development.md: no row for RX_VK_BATCH_IDLE with its default")
job = block(replay, "void submit_job(std::vector<uint64_t>& handles, uint64_t value, uint64_t queue) {")
in_order(job, ["submit_depth++;", "if (!--submit_depth && batch_way() == barrier_rules::Batch::Parts) queue_flush_for(FR_OTHER);",
"run_submit("], "vk_replay.cpp submit_job")
@@ -116,7 +133,7 @@ def check_source():
raise SystemExit("vk_replay.cpp queue_flush_locked: the held recordings' count is not cleared with them")
begin = block(draw, "void begin_pass(")
in_order(begin, ["end_pass(cmd);", "if (pass_resuming) pass_resume_barrier(cmd);", "else pass_start_barrier(cmd);",
"work_since_barrier = true;", "pass_at = at;", "pass().sampled.clear(), pass().sampled_targets = 0, pass().written = 0;",
"work_since_barrier = true;", "pass_at = at;", "ps.sampled.clear(), ps.sampled_targets = 0, ps.written = 0;",
"vkCmdBeginRendering(cmd, &ri);"], "vk_draw.cpp begin_pass")
take = block(draw, "bool pass_carry_take() {")
if "pass_carry_on = pass().open && !par_active.load(std::memory_order_relaxed);" not in take or \
@@ -208,6 +225,14 @@ int main() {
CHECK(batch_flush(Batch::Off, false, 0, 32));
CHECK(!batch_flush(Batch::Hold, true, 31, 32) && batch_flush(Batch::Hold, true, 32, 32));
CHECK(batch_flush(Batch::Parts, false, 1, 4) && !batch_flush(Batch::Parts, true, 3, 4) && batch_flush(Batch::Parts, true, 4, 4));
// ((Triforce, 11-22, the performance audit's SYNC-09) RX_VK_BATCH_IDLE: off unless set to a number other than 0; the rule
// only in Hold mode, with something held, the switch on
CHECK(!batch_idle_on(nullptr) && !batch_idle_on("") && !batch_idle_on("0") && !batch_idle_on("off") && batch_idle_on("1") && batch_idle_on("2"));
CHECK(batch_idle(Batch::Hold, true, true));
CHECK(!batch_idle(Batch::Hold, true, false) && !batch_idle(Batch::Hold, false, true));
CHECK(!batch_idle(Batch::Parts, true, true) && !batch_idle(Batch::Off, true, true));
for (int m = 0; m < 3; m++) // (unset, nothing asks the GPU, in any mode)
for (int held = 0; held < 2; held++) CHECK(!batch_idle((Batch)m, held != 0, batch_idle_on(nullptr)));
// (submit_recording's way with RX_VK_BATCH=parts, over a game's submits: the command buffers reach the queue in the order they
// were submitted, a part never waits, at most 4 wait, and none is left once the submit's replay returns)
struct Sub { int id; bool part; };

View File

@@ -0,0 +1,114 @@
"""The BC3 scratch has the layers one run uses, not 8 (gpu/vk/bc_scratch.h, vk_texture.cpp bc_layers, bc_scratch_layers,
bc_scratch_make, bc_scratch2_make, bc_scratch_layout, texture_copy_into_bc; no game, no GPU).
(Triforce, 11-21, the performance audit's TEX-10, after Source A's.) Big ASTC textures are decoded into a 2048 x 2048
RGBA8 scratch and encoded again as BC3 into an output buffer. Both were made with 8 layers, 160 MB, while a run decodes
one layer: 140 MB held and never written, for good once made.
This compiles bc_scratch.h alone and checks the rule: a run is 1 layer unless RX_VK_BC_LAYERS says 1 to 8; the scratch
has a run's layers, or 8 with RX_VK_BC_SCRATCH_FULL; one layer is 16 MB of image and 4 MB of output, 20 MB, 36 with the
second scratch, and 8 layers are the 160 and 288 MB it was.
And reads the source: both scratch images, their views, the layout's barriers and the output buffer are sized by
bc_scratch_layers(), nowhere by kBcLayers; a run's layers are bc_layers(), which the scratch is sized from; the copy into
a BC3 texture takes no more layers at once than the scratch has; the made line says the size; the switches are in the
switches table.
python -I -B tests/test_bc_scratch_layers.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <initializer_list>
#include "bc_scratch.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
int main() {
using namespace bc_scratch;
CHECK(kDim == 2048 && kMost == 8);
CHECK(run_layers(nullptr) == 1 && run_layers("") == 1 && run_layers("x") == 1 && run_layers("0") == 1 && run_layers("-3") == 1);
CHECK(run_layers("1") == 1 && run_layers("4") == 4 && run_layers("8") == 8 && run_layers("99") == 8);
for (uint32_t run : {1u, 2u, 8u}) CHECK(layers(run, false) == run && layers(run, true) == 8);
CHECK(layers(0, false) == 1 && layers(64, false) == 8);
CHECK(out_bytes(1) == 4ull << 20 && out_bytes(8) == 32ull << 20);
CHECK(mb(1, false) == 20 && mb(1, true) == 36); // (16 MB of image, 4 MB of output; the second image 16 more)
CHECK(mb(8, false) == 160 && mb(8, true) == 288); // (what it was)
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8")
check('#include "bc_scratch.h"' in tex, "vk_texture.cpp does not use the rule")
run = block(tex, "uint32_t bc_layers() {")
check('bc_scratch::run_layers(getenv("RX_VK_BC_LAYERS"))' in run, "a run's layers are not the rule's of RX_VK_BC_LAYERS")
size = block(tex, "uint32_t bc_scratch_layers() {")
check('bc_scratch::layers(bc_layers(), getenv("RX_VK_BC_SCRATCH_FULL") != nullptr)' in size,
"the scratch's layers are not a run's, with all 8 behind RX_VK_BC_SCRATCH_FULL")
for name in ("bool bc_scratch_make() {", "bool bc_scratch2_make() {"):
body = block(tex, name)
check("kBcLayers" not in body and "ii.arrayLayers = bc_scratch_layers();" in body and
"vi.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, bc_scratch_layers()};" in body,
name.split("(")[0] + ": the image or its view is not sized by bc_scratch_layers()")
make = block(tex, "bool bc_scratch_make() {")
# ((11-21, TEX-09) the one buffer holds a run of either scratch: the first's, or the small one's when it is larger)
check("const uint64_t out = std::max(bc_scratch::out_bytes(bc_scratch_layers()), small_out);" in make,
"the output buffer is not the rule's size")
check('logf("vk: the BC3 scratch is made: %u layer' in make, "the made scratch's size is not logged")
layout = block(tex, "bool bc_scratch_layout(")
check("kBcLayers" not in layout and layout.count("0, 1, 0, bc_scratch_layers()};") == 2,
"bc_scratch_layout's barriers cover layers the scratch may not have")
copy = block(tex, "bool texture_copy_into_bc(")
check("kBcLayers" not in copy and "l0 += bc_scratch_layers()" in copy and "std::min(bc_scratch_layers(), layers - l0)" in copy,
"a copy into a BC3 texture takes more layers at once than the scratch has")
# ((11-21, TEX-09) a level the small scratch holds runs its layers; any other, bc_layers(), what the first is sized by)
check("const uint32_t run = small ? bc_small_layers() : bc_layers();" in tex,
"the upload's run through the first scratch is not bc_layers(), what that scratch is sized by")
idle = block(tex, "void bc_scratch_idle() {")
check("bc_scratch::mb(bc_scratch_layers(), second)" in idle, "the given-back line still states 160 or 288 MB")
stub = (ROOT / "tests" / "parallel_init_order_test.cpp").read_text(encoding="utf-8")
check("uint32_t bc_scratch_layers() { return kBcLayers; }" in stub, "parallel_init_order_test.cpp cannot compile bc_scratch_layout")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_BC_LAYERS=<n>`, `RX_VK_BC_SCRATCH_FULL=1`" in dev, "docs/development.md: the BC scratch's switches are not in the table")
with tempfile.TemporaryDirectory(prefix="bc-scratch-") as tmp:
src, exe = Path(tmp) / "scratch.cpp", Path(tmp) / "scratch.exe"
src.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(src), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: the BC3 scratch has the layers one run uses: 20 MB, not 160")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,148 @@
"""BC mip levels of 1024 or less are re-encoded 8 layers a run, through a small scratch (gpu/vk/bc_scratch.h, vk_texture.cpp
bc_small_layers, bc_scratch_make, bc_scratch_layout, bc_scratch_idle, bc_encode_into, upload_astc_gpu; no game, no GPU).
(Triforce, 11-21, the performance audit's TEX-09, after Source A's.) A run is a decode, an encode, a copy and three full
barriers, and one layer a run made a terrain array of 90 layers and 11 levels about 990 of them.
This compiles bc_scratch.h alone and checks the rule: the small scratch's layers are 8 unless RX_VK_BC_SMALL_LAYERS says 1
to 8, and 1 on AMD's cards when it is not set; a level of 1024 or less each way goes through it, a 1365 level, a level
1025 one way, a texture encoded at another size and a start with no small scratch do not; a layer of a run begins where the
encoder wrote it (16 bytes a block of 4 x 4, 8 as BC1, uneven sizes rounded up to blocks); and it counts a 1365 x 1365 x 90
array's runs: 990 at one layer a run, 210 with 8 for its levels from the second down.
And reads the source: the small scratch is made with the first, only at full texture detail and with more than one layer
asked, and given back with it; its layout is set before its first use; the one output buffer holds a run of either; a
level asks the rule which scratch and takes that scratch's run, view and encode source; the copy has a region a layer at
the rule's offsets; the switch is in the switches table.
python -I -B tests/test_bc_small_layers.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include "bc_scratch.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
// the runs of one array: every level, its layers in runs of the scratch it goes through
static int runs(uint32_t w, uint32_t h, uint32_t layers, uint32_t levels, bool have_small, uint32_t small_n, uint32_t big_n) {
int n = 0;
for (uint32_t l = 0; l < levels; l++) {
const uint32_t lw = std::max(1u, w >> l), lh = std::max(1u, h >> l);
const uint32_t run = bc_scratch::small_level(lw, lh, have_small, false) ? small_n : big_n;
n += (int)((layers + run - 1) / run);
}
return n;
}
int main() {
using namespace bc_scratch;
CHECK(kSmallDim == 1024);
const uint32_t nvidia = 0x10DE, amd = 0x1002, intel = 0x8086;
CHECK(small_layers(nullptr, nvidia) == 8 && small_layers(nullptr, intel) == 8 && small_layers("", nvidia) == 8);
CHECK(small_layers(nullptr, amd) == 1 && small_layers("", amd) == 1); // (until the picture is checked there)
CHECK(small_layers("8", amd) == 8 && small_layers("4", amd) == 4); // (asked for: on any card)
CHECK(small_layers("1", nvidia) == 1 && small_layers("0", nvidia) == 1 && small_layers("99", nvidia) == 8);
CHECK(small_level(1024, 1024, true, false) && small_level(682, 682, true, false) && small_level(1, 1, true, false));
CHECK(!small_level(1365, 1365, true, false) && !small_level(1025, 4, true, false) && !small_level(4, 1025, true, false));
CHECK(!small_level(512, 512, false, false)); // (no small scratch: RX_VK_BC_SMALL_LAYERS=1, AMD, reduced detail)
CHECK(!small_level(512, 512, true, true)); // (encoded at another size: the second scratch's)
CHECK(small_out_bytes(8) == 8ull << 20 && small_out_bytes(1) == 1ull << 20 && small_image_bytes(8) == 32ull << 20);
CHECK(small_out_bytes(8) > out_bytes(1)); // (so the one output buffer has to be the larger of the two)
// where a run's layers are in the encoder's output
CHECK(layer_offset(0, 682, 682, false) == 0);
CHECK(layer_offset(1, 1024, 1024, false) == 256ull * 256 * 16 && layer_offset(7, 1024, 1024, false) == 7 * 256ull * 256 * 16);
CHECK(layer_offset(1, 682, 682, false) == 171ull * 171 * 16); // (682 texels: 171 blocks, rounded up)
CHECK(layer_offset(3, 85, 43, false) == 3 * 22ull * 11 * 16);
CHECK(layer_offset(2, 512, 512, true) == 2 * 128ull * 128 * 8); // (kept as BC1: the colour half alone)
CHECK(layer_offset(7, 1024, 1024, false) + 256ull * 256 * 16 <= small_out_bytes(8)); // (the last layer fits the buffer)
// a terrain array, 1365 x 1365 x 90 with 11 levels
CHECK(runs(1365, 1365, 90, 11, false, 8, 1) == 990);
CHECK(runs(1365, 1365, 90, 11, true, 8, 1) == 90 + 10 * 12); // (its first level one layer a run, the rest 8)
CHECK(runs(1365, 1365, 90, 11, true, 8, 1) == 210);
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8")
layers = block(tex, "uint32_t bc_small_layers() {")
check('bc_scratch::small_layers(getenv("RX_VK_BC_SMALL_LAYERS"), dev.props.vendorID)' in layers,
"the small scratch's layers are not the rule's of the switch and the card's vendor")
make = block(tex, "bool bc_scratch_make() {")
check("if (bc_small_layers() > 1 && texture_detail_level() == tex_detail::kFull) {" in make,
"the small scratch is made with one layer asked, or at reduced texture detail")
check("si.extent = {bc_scratch::kSmallDim, bc_scratch::kSmallDim, 1};" in make and "si.arrayLayers = bc_small_layers();" in make and
"sv.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, bc_small_layers()};" in make,
"the small scratch or its view is not 1024 x 1024 with a run's layers")
check("const uint64_t out = std::max(bc_scratch::out_bytes(bc_scratch_layers()), small_out);" in make and
"bc_small ? bc_scratch::small_out_bytes(bc_small_layers()) : 0" in make,
"the output buffer does not hold a run of either scratch")
check("bc_small_fresh = bc_small != VK_NULL_HANDLE;" in make, "a new small scratch's layout is not marked to be set")
failed = make[make.index("!make_out()) {"):make.index("bc_scratch_fresh = true;")]
check("if (bc_small) destroy_image(bc_small, bc_small_alloc);" in failed, "the small scratch stays when the first could not be made")
layout = block(tex, "bool bc_scratch_layout(")
check("|| (bc_small_fresh && bc_small));" in layout and "b.image = bc_small;" in layout and
"b.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, bc_small_layers()};" in layout and
layout.index("b.image = bc_small;") < layout.index("if (!bc_scratch_fresh) return true;"),
"the small scratch's layout is not set before its first use")
idle = block(tex, "void bc_scratch_idle() {")
check("dead.push_back(Dead{bc_last_value, bc_small, bc_small_alloc, {bc_small_view}, {}});" in idle and
"bc_small = VK_NULL_HANDLE" in idle, "the small scratch is not given back with the first")
start = tex.index(" if (tex.bc) { // (decoded into the scratch a run of consecutive layers at a time, re-encoded, copied in)")
loop = tex[start:tex.index(" st_astc++;", start)]
check("const bool small = bc_scratch::small_level(g.w, g.h, bc_small != VK_NULL_HANDLE, tex.bc_resized);" in loop and
"const uint32_t run = small ? bc_small_layers() : bc_layers();" in loop,
"a level does not ask the rule which scratch it goes through, or does not take that scratch's run")
check("VkDescriptorImageInfo ii{VK_NULL_HANDLE, small ? bc_small_view : bc_scratch_view, VK_IMAGE_LAYOUT_GENERAL};" in loop and
"bc_encode_into(cmd, iw, ih, n, tex, level, layers[k0], 0, 0, small ? bc_small_view : VK_NULL_HANDLE);" in loop,
"a small level is not decoded into the small scratch and encoded from it")
check("bc_encode_resized(cmd, g.w, g.h, iw, ih, n, tex, level, layers[k0]);" in loop, "a resized texture's levels lost their own path")
enc = block(tex, "void bc_encode_into(")
check("VkBufferImageCopy c[kBcLayers];" in enc and
"bc_scratch::layer_offset(k, w, h, dst.bc1), 0, 0, {VK_IMAGE_ASPECT_COLOR_BIT, level, layer + k, 1}" in enc and
"vkCmdCopyBufferToImage(cmd, bc_out, dst.image, VK_IMAGE_LAYOUT_GENERAL, std::min(n, kBcLayers), c);" in enc,
"a run's layers are not copied by a region each, at the encoder's layer stride")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_BC_SMALL_LAYERS=<n>`" in dev, "docs/development.md: RX_VK_BC_SMALL_LAYERS is not in the switches table")
with tempfile.TemporaryDirectory(prefix="bc-small-") as tmp:
c, exe = Path(tmp) / "small.cpp", Path(tmp) / "small.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: BC levels of 1024 or less run 8 layers at once through the small scratch: a 90-layer array's 990 runs are 210")
if __name__ == "__main__":
main()

View File

@@ -82,6 +82,10 @@ void logf(const char*, ...) {}
void guest_pages_set(uint64_t, uint64_t, uint64_t) {}
void retire_pool(std::map<uint64_t, Pool>::iterator it) { retired_pools.push_back(imported.extract(it)); }
std::vector<std::pair<uint64_t, uint64_t>> bg_held;
std::deque<std::map<uint64_t, Pool>::node_type> evicted_pools; // (the imports given back and not freed yet: none here)
@@DEAD_IMPORT@@
std::deque<DeadImport> dead_imports;
@@LEAVING@@
@@SEND@@
@@LET_GO@@
@@DRAIN@@
@@ -180,6 +184,8 @@ def main():
fixture = (FIXTURE.replace("@@SEND@@", send)
.replace("@@LET_GO@@", block(memory, "void bg_let_go(uint64_t lo, uint64_t hi) {"))
.replace("@@DRAIN@@", block(memory, "void bg_drain() {"))
.replace("@@DEAD_IMPORT@@", re.search(r"^struct DeadImport \{[^\n]*\};$", memory, re.M).group(0))
.replace("@@LEAVING@@", block(memory, "bool imports_leaving_overlap(uint64_t lo, uint64_t hi) {"))
.replace("@@IMPORTED@@", block(memory, "int memory_guest_imported(uint64_t lo, uint64_t hi) {")))
with tempfile.TemporaryDirectory(prefix="bg-import-held-") as tmp:
src, exe = Path(tmp) / "held.cpp", Path(tmp) / "held.exe"

View File

@@ -0,0 +1,125 @@
"""The builders start the build tools at normal priority, and below normal only on request (no game, no build).
(Triforce, 11-22, the performance audit's OS-57, after Source A's.) Every Windows build started nx2/make_game.py below
normal priority, from four places, with no switch. Source A starts its tools at normal priority and reports, without a
measurement, that below normal Windows kept the one-process steps of a build on a hybrid Intel CPU's efficiency cores.
HOOKSHOT_LOW_PRIORITY=1 brings the below-normal start back.
Reads the source: in each of the four files the below-normal set is there once and sits directly under a check of
HOOKSHOT_LOW_PRIORITY == "1"; the switches table lists the switch; prepare_priority's comment and the design notes
say what the default is now.
And runs the Python builder's own start (builder/py/hookshot_builder/core.py Runner._run) with a stand-in for the tools:
a child that prints its own priority class. By default the child has the class a plain start gives it; with
HOOKSHOT_LOW_PRIORITY=1 it is below normal; =0 and an empty value are the default. The rule build_priority is asked the
same on every OS; the start itself only on Windows.
python -I -B tests/test_build_priority.py
"""
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
SWITCH = "HOOKSHOT_LOW_PRIORITY"
BELOW_NORMAL, IDLE, NORMAL = 0x4000, 0x40, 0x20
# (a child that says the priority class Windows gave it)
CHILD = "import ctypes; k = ctypes.WinDLL('kernel32'); k.GetCurrentProcess.restype = ctypes.c_void_p; " \
"k.GetPriorityClass.argtypes = [ctypes.c_void_p]; print('class', k.GetPriorityClass(k.GetCurrentProcess()))"
def fail(text):
raise SystemExit("FAIL: " + text)
def check_csharp(rel, set_line):
text = (ROOT / rel).read_text(encoding="utf-8")
code = [line.strip() for line in text.splitlines() if line.strip() and not line.strip().startswith("//")]
sets = [i for i, line in enumerate(code) if "ProcessPriorityClass.BelowNormal" in line]
if len(sets) != 1:
fail("%s sets the below normal class %d times, not once" % (rel, len(sets)))
i = sets[0]
if code[i] != set_line:
fail("%s: the below normal set is no longer the line the check knows: %s" % (rel, code[i]))
if code[i - 1] != 'if (Environment.GetEnvironmentVariable("%s") == "1")' % SWITCH:
fail("%s starts the build tools below normal without asking %s: the line above the set is: %s" % (rel, SWITCH, code[i - 1]))
if "the performance audit's OS-57" not in text:
fail("%s no longer says where the switch comes from" % rel)
def own_class():
import ctypes
k = ctypes.WinDLL("kernel32")
k.GetCurrentProcess.restype = ctypes.c_void_p
k.GetPriorityClass.argtypes = [ctypes.c_void_p]
return k.GetPriorityClass(k.GetCurrentProcess())
def main():
# ---- the three C# starts
check_csharp("builder/HookshotBuilder/Runner.cs", "try { p.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { }")
check_csharp("setup/Setup.cs", "try { p.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { }")
check_csharp("launcher/Logic/Build/BuildRunner.cs",
"try { if (ctx.IsWindows) p.PriorityClass = ProcessPriorityClass.BelowNormal; } catch (Exception) { }")
head = (ROOT / "launcher/Logic/Build/BuildRunner.cs").read_text(encoding="utf-8").split("using System", 1)[0]
if "below-normal priority (Windows)" in head or SWITCH not in head:
fail("BuildRunner.cs's header still says the tools always run below normal")
# ---- the Python builder: the source, the rule, then its own start
core_text = (ROOT / "builder/py/hookshot_builder/core.py").read_text(encoding="utf-8")
uses = re.findall(r"\bBELOW_NORMAL_PRIORITY_CLASS\b", core_text)
if len(uses) != 2 or 'return BELOW_NORMAL_PRIORITY_CLASS if env.get("%s") == "1" else 0' % SWITCH not in core_text:
fail("core.py uses BELOW_NORMAL_PRIORITY_CLASS outside build_priority's %s check" % SWITCH)
if "creationflags=CREATE_NO_WINDOW | build_priority())" not in core_text:
fail("core.py's Runner no longer starts the tools with build_priority()")
sys.path.insert(0, str(ROOT / "builder" / "py"))
from hookshot_builder import core
for env, want in (({}, 0), ({SWITCH: "1"}, BELOW_NORMAL), ({SWITCH: "0"}, 0), ({SWITCH: ""}, 0), ({SWITCH: "yes"}, 0)):
if core.build_priority(env) != want:
fail("build_priority(%r) is 0x%x, want 0x%x" % (env, core.build_priority(env), want))
started = "the rule only (the start is Windows')"
if os.name == "nt":
mine = own_class()
plain = mine if mine in (BELOW_NORMAL, IDLE) else NORMAL # (what a child gets with no priority flag)
with tempfile.TemporaryDirectory() as folder:
tools = SimpleNamespace(python=sys.executable, python_args=[], root=folder)
for value, want in ((None, plain), ("0", plain), ("1", BELOW_NORMAL if mine != IDLE else None)):
if want is None: # (an idle-class parent: Windows does not raise the child; nothing to tell apart)
continue
env = {k: v for k, v in os.environ.items() if k != SWITCH}
if value is not None:
env[SWITCH] = value
lines, ended = [], []
with patch.dict(os.environ, env, clear=True), \
patch.object(core, "build_command", return_value=[sys.executable, "-I", "-c", CHILD]), \
patch.object(core, "build_env", return_value=dict(env)):
core.Runner(tools, [], on_line=lines.append, on_finished=ended.append)._run()
if ended != [None] or ["class %d" % want] != [l for l in lines if l.startswith("class ")]:
fail("the tools started with %s=%s have %s (ended %s), want class %d" % (SWITCH, value, lines, ended, want))
started = "the Python builder's start gives its child class 0x%x by default and 0x%x with the switch" % (plain, BELOW_NORMAL)
# ---- the docs and the comments that described the old start
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
rows = [line for line in docs.splitlines() if line.startswith("| `%s=1` |" % SWITCH)]
if len(rows) != 1 or "OS-57" not in rows[0] or "normal priority" not in rows[0]:
fail("docs/development.md's switches table has no %s=1 row" % SWITCH)
building = docs[docs.index("### Building"):docs.index("## Mods: code patches and cheats")]
if rows[0] not in building:
fail("the %s row is not in the Building table" % SWITCH)
design = (ROOT / "docs/design.md").read_text(encoding="utf-8")
if SWITCH not in design:
fail("docs/design.md's first-build line does not say the builders' start changed")
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
comment = draw[draw.index("/// (Triforce) The priority of the workers of the shader preparation"):draw.index("int prepare_priority() {")]
if SWITCH not in comment or "builders start their build tools so: Runner.cs" in comment:
fail("prepare_priority's comment still says the builders always start their tools below normal")
print("ok: the four builders ask %s before a below-normal start; %s" % (SWITCH, started))
if __name__ == "__main__":
main()

413
tests/test_built_check.py Normal file
View File

@@ -0,0 +1,413 @@
"""Whether the caches still have what the light precompile's record lists (gpu/vk/built_check.h; vk_draw.cpp
built_check_cold, built_check_made, built_play_publish, built_play_build, precompile_run; no game, no GPU).
(Triforce, 11-22, the performance audit's PIPE-21, after Source A's and Source B's.) A light precompile (little RAM) builds
the noted pipelines for the caches, lets them go and records their keys in <notes>.built; later starts left everything the
record lists to its first draw, trusting it on its stamp alone. When the caches lost them, each of those first draws
compiled in play. Now a sample of the recorded notes is asked of Triforce's own caches first (as a draw asks, without
compiling, where the device can) and some listed compute programs as a dispatch asks; at half or more cold, everything is
built again before the title. Cold again right after that: left to the draws, with a hint. RX_VK_BUILT_CHECK=0: as before.
The header is compiled and run as it is: the switch; a pipeline cold by the caches' answer, or without the flag by its
first build against its own second; the sample (24 answers: 12 cold is cold, 11 warm; 15 answers say nothing); the places;
the record of strikes; starts in a row (cold: built again; cold again: left to the draws; warm forgets; another stamp anew;
mode 2 always builds again); the counter's rules.
Then built_check_cold, built_check_made, built_play_publish and built_play_build are cut out of vk_draw.cpp and run over a
stand-in driver whose caches hold pipelines by key:
- warm caches: not cold, 24 notes and 6 compute programs asked, each the note's own variant, nothing compiled, every
pipeline asked for let go, the caches as they were;
- lost caches: cold, still nothing compiled (asked without compiling: the same pipelines exist afterwards); the record
of the rebuild written; cold again: left to the draws with the hint, not cold; warm: the strike forgotten;
- 12 of 24 cold is cold, 11 is not; 15 answers (9 refused) say nothing, 16 do;
- without the flag: each recorded note built twice and timed, the cold ones compiled into the caches (exactly the
recorded pipelines, nothing else), compute programs not asked;
- the switch at 0, or an empty record: nothing asked; at 2: cold with warm caches;
- the counter: nothing before the record is published (nor with the switch at 0), then only the record's keys and its
compute programs, one line at most every 10 s.
And reads the source: precompile_run asks before the erase and forgets the record when cold, records the strikes after the
record is written again, publishes the record for the counter; build_graphics and the dispatch count; the switch is read
once; the switches table has the row.
python -I -B tests/test_built_check.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
RULES = r"""
#include <cstdio>
#include <set>
#include "built_check.h"
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
using namespace built_check;
static Probe asked(bool cold_answer) { Probe p; p.asked = true; p.answered_cold = cold_answer; p.made = !cold_answer; return p; }
static Probe timed(double first, double again) { Probe p; p.made = true; p.first_ms = first; p.again_ms = again; return p; }
static Probe refused() { Probe p; p.asked = true; return p; }
static std::vector<Probe> sample(int cold_n, int warm_n, int refused_n = 0) {
std::vector<Probe> v;
for (int i = 0; i < cold_n; i++) v.push_back(asked(true));
for (int i = 0; i < warm_n; i++) v.push_back(asked(false));
for (int i = 0; i < refused_n; i++) v.push_back(refused());
return v;
}
static Do start(const std::string& rec, uint64_t stamp, Verdict v, bool always = false) {
const State had = state_read(rec);
int next = -1;
const Do d = decide(v, had, stamp, always, &next);
if (v == Verdict::Warm && had.found) remove(rec.c_str());
if (d == Do::Again && !state_write(rec, stamp, next)) return Do::Nothing; // (as built_check_made, once built again)
return d;
}
int main(int argc, char** argv) {
if (argc < 2) return 2;
const std::string dir = argv[1];
CHECK(mode(nullptr) == 1 && mode("") == 1 && mode("1") == 1 && mode("0") == 0 && mode("2") == 2 && mode("9") == 2 && mode("-1") == 0);
// one pipeline
CHECK(cold(asked(true)) && !cold(asked(false)) && !counts(refused()) && !cold(refused()));
CHECK(cold(timed(8.5, 1.0)) && !cold(timed(6.9, 1.0)) && !cold(timed(0.4, 0.3)) && cold(timed(3.2, 0.0)));
{
Probe p = timed(80, 0.5);
p.asked = true; // (asked, the answer alone counts: a slow hit is a hit)
CHECK(!cold(p));
}
// the sample: 24 answers, 12 cold is cold, 11 warm; 15 answers say nothing, 16 do
CHECK(verdict(tally(sample(12, 12))) == Verdict::Cold && verdict(tally(sample(11, 13))) == Verdict::Warm);
CHECK(verdict(tally(sample(15, 0, 9))) == Verdict::Unknown && verdict(tally(sample(16, 0, 8))) == Verdict::Cold);
CHECK(verdict(tally(sample(0, 15))) == Verdict::Unknown && verdict(tally(sample(0, 16))) == Verdict::Warm);
CHECK(verdict(tally(sample(8, 8))) == Verdict::Cold && verdict(tally(sample(7, 9))) == Verdict::Warm);
CHECK(verdict(tally({})) == Verdict::Unknown);
{
const Tally t = tally(sample(5, 7, 3));
CHECK(t.got == 12 && t.cold == 5 && t.answered == 5);
}
// the places: spread, distinct, the same for one seed
{
const std::vector<size_t> a = spots(20000, kLook, 77), b = spots(20000, kLook, 77), c = spots(20000, kLook, 78);
CHECK(a == b && a != c && a.size() == (size_t)kLook && std::set<size_t>(a.begin(), a.end()).size() == a.size());
CHECK(spots(0, kLook, 1).empty() && spots(30, kLook, 5).size() == 30 && spots(1, kLook, 9) == std::vector<size_t>{0});
}
// the record
const std::string rec = dir + "/notes.built.check";
CHECK(!state_read(rec).found);
CHECK(state_write(rec, 0x0123456789ABCDEFull, 2) && state_read(rec).found && state_read(rec).stamp == 0x0123456789ABCDEFull &&
state_read(rec).strikes == 2);
{
FILE* f = fopen(rec.c_str(), "wb");
fputs("predict-check 0000000000000001 0\n", f); // (another check's record is not this one)
fclose(f);
CHECK(!state_read(rec).found);
remove(rec.c_str());
}
// starts in a row
const uint64_t A = 0xA, B = 0xB;
CHECK(start(rec, A, Verdict::Cold) == Do::Again && state_read(rec).strikes == 0);
CHECK(start(rec, A, Verdict::Cold) == Do::Stop && state_read(rec).strikes == 0); // (cold again right after: the draws)
CHECK(start(rec, A, Verdict::Unknown) == Do::Nothing && state_read(rec).found);
CHECK(start(rec, B, Verdict::Cold) == Do::Again && state_read(rec).stamp == B); // (another stamp begins anew)
CHECK(start(rec, B, Verdict::Warm) == Do::Nothing && !state_read(rec).found); // (warm forgets)
CHECK(start(rec, B, Verdict::Cold) == Do::Again);
CHECK(start(rec, B, Verdict::Warm, true) == Do::Again && start(rec, B, Verdict::Unknown, true) == Do::Again); // (mode 2)
// the counter
CHECK(!slow(39999) && slow(40000) && say(1, 0) && !say(10000, 1) && say(10001, 1));
{
const std::vector<uint64_t> s = sorted({5, 3, 5, 9, 1});
CHECK(s == (std::vector<uint64_t>{1, 3, 5, 9}) && has(s, 9) && !has(s, 4) && !has({}, 1));
}
std::printf("ok\n");
return 0;
}
"""
FIXTURE = r"""
#include "built_check.h"
#include <atomic>
#include <chrono>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <thread>
#include <unordered_set>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s (%s)\n", __LINE__, #x, last_log.c_str()); return 1; } } while (0)
// ---- a stand-in driver: the caches hold graphics pipelines by note key and compute pipelines by program key
using VkResult = int;
constexpr VkResult VK_SUCCESS = 0, VK_PIPELINE_COMPILE_REQUIRED = 1000297000;
constexpr uint32_t VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT = 0x100;
using VkPipeline = uint64_t;
#define VK_NULL_HANDLE 0
using VkPrimitiveTopology = int;
struct NvnState { uint64_t note; };
struct PipeNote { uint64_t key, prog, variant; uint32_t topo; NvnState s; };
struct Targets { int n; };
struct Stage { int m; };
struct Program { uint64_t key, variant; bool failed = false, compute = false; std::vector<Stage> stages{Stage{}}; };
struct VkComputePipelineCreateInfo { uint32_t flags; uint64_t key; };
struct { struct { bool pipelineCreationCacheControl = true; } vk13; int device = 0; } dev;
std::set<uint64_t> graphics_cache, compute_cache, refused;
std::map<uint64_t, std::vector<Program>> programs;
std::map<uint64_t, uint64_t> note_variant;
int compiles, asks, compute_asks, alive, wrong_variant, borrows, layouts, test_mode = 1, logs;
std::string dir, last_log;
VkPipeline build_graphics(const Program& p, const NvnState& s, const Targets&, VkPrimitiveTopology, int part, bool* cold) {
if (part != -1) return VK_NULL_HANDLE;
wrong_variant += note_variant[s.note] != p.variant;
if (refused.count(s.note)) return VK_NULL_HANDLE;
if (cold) { // (asked without compiling: the caches' answer)
asks++;
if (!graphics_cache.count(s.note)) return *cold = true, VK_NULL_HANDLE;
return alive++, s.note;
}
if (!graphics_cache.count(s.note)) { // (compiled into the caches: a compile's time)
std::this_thread::sleep_for(std::chrono::milliseconds(9));
graphics_cache.insert(s.note), compiles++;
}
return alive++, s.note;
}
Targets note_targets(const PipeNote&) { return Targets{}; }
void ensure_layout(Program&) { layouts++; }
void program_cache_borrow(uint64_t key, const std::function<void(Program&)>& fn) {
borrows++;
auto it = programs.find(key);
if (it == programs.end()) return;
for (Program p : it->second) fn(p); // (a copy, let go after)
}
void vkDestroyPipeline(int, VkPipeline, const void*) { alive--; }
VkComputePipelineCreateInfo compute_create_info(const Program& p) { return {0, p.key}; }
struct PcacheUse { int cache = 1; };
PcacheUse pcache_use() { return {}; }
VkResult cached_compute(int, const VkComputePipelineCreateInfo* ci, VkPipeline* pl) {
compute_asks++;
if (!(ci->flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)) return compiles++, *pl = ci->key, alive++, VK_SUCCESS;
if (!compute_cache.count(ci->key)) return VK_PIPELINE_COMPILE_REQUIRED;
return *pl = ci->key, alive++, VK_SUCCESS;
}
std::string pipe_notes_path() { return dir + "/notes.bin"; }
bool prepare_stopped() { return false; }
void logf(const char* fmt, ...) {
char line[2048];
va_list a;
va_start(a, fmt);
vsnprintf(line, sizeof line, fmt, a);
va_end(a);
last_log = line, logs++;
}
int built_check_mode() { return test_mode; }
int built_check_next = 0;
std::atomic<const std::vector<uint64_t>*> built_play_keys{nullptr};
std::atomic<int64_t> st_built_slow{0}, st_built_slow_us{0}, st_built_missed{0};
/* CUT */
uint64_t key_of(const PipeNote& n) { return n.key; }
std::vector<std::unique_ptr<PipeNote>> notes;
std::unordered_set<uint64_t> built;
std::vector<uint64_t> listed;
constexpr uint64_t kComputesKey = 0xC0FFEE, kStamp = 0x5157;
static bool run(bool computes_built = true) { return built_check_cold(notes, built, key_of, computes_built, listed, kStamp); }
static void fill_caches() {
graphics_cache.clear(), compute_cache.clear();
for (const auto& n : notes) graphics_cache.insert(n->key);
compute_cache.insert(listed.begin(), listed.end());
}
static bool has(const std::string& s) { return last_log.find(s) != std::string::npos; }
int main(int argc, char** argv) {
if (argc < 2) return 2;
dir = argv[1];
const std::string state = dir + "/notes.bin.built.check";
for (uint64_t k = 1; k <= 40; k++) {
const uint64_t variant = k % 5 == 0 ? 2 : 1; // (some notes are of the program's second variant)
notes.push_back(std::make_unique<PipeNote>(PipeNote{k, 1000 + k, variant, 3, NvnState{k}}));
note_variant[k] = variant;
programs[1000 + k] = {Program{1000 + k, 1}, Program{1000 + k, 2}};
built.insert(k);
}
built.insert(kComputesKey);
for (uint64_t k = 500; k < 520; k++) {
listed.push_back(k);
Program c{k, 1};
c.compute = true;
programs[k] = {c};
}
// warm caches: nothing compiled, every pipeline let go, the caches as they were
fill_caches();
const std::set<uint64_t> had_graphics = graphics_cache;
CHECK(!run() && asks == 24 && compute_asks == 6 && compiles == 0 && alive == 0 && wrong_variant == 0 && has("have what it lists"));
CHECK(graphics_cache == had_graphics && !fopen(state.c_str(), "rb"));
// the caches lost them: cold, and still nothing compiled by the check
graphics_cache.clear(), compute_cache.clear();
asks = compute_asks = 0;
CHECK(run() && asks == 24 && compute_asks == 6 && compiles == 0 && alive == 0 && graphics_cache.empty() && has("built again now"));
built_check_made(kStamp); // (precompile_run's, once everything was built and recorded again)
CHECK(built_check::state_read(state).found && built_check::state_read(state).stamp == kStamp);
// cold again right after: left to the draws, with the hint
CHECK(!run() && has("Shader Cache Size") && built_check::state_read(state).found);
// warm again: the strike forgotten
fill_caches();
CHECK(!run() && !built_check::state_read(state).found);
// the threshold: the first 24 recorded notes are asked (40 notes, under kLook); computes left out here
graphics_cache.clear();
for (uint64_t k = 13; k <= 40; k++) graphics_cache.insert(k); // (1-12 lost: 12 of 24)
CHECK(run(false) && has("12 of 24"));
graphics_cache.insert(12); // (11 of 24)
CHECK(!run(false) && has("11 of 24"));
// too few answers: 9 refused leave 15, 8 leave 16
graphics_cache.clear();
for (uint64_t k = 1; k <= 9; k++) refused.insert(k);
CHECK(!run(false) && has("too few"));
refused.erase(9);
CHECK(run(false));
refused.clear();
// without the flag: built twice and timed; the cold ones compiled, exactly the recorded notes asked, nothing else
dev.vk13.pipelineCreationCacheControl = false;
compute_asks = 0, compiles = 0;
CHECK(run() && compiles == 24 && compute_asks == 0 && alive == 0 && has("built twice"));
for (uint64_t k = 1; k <= 24; k++) CHECK(graphics_cache.count(k));
CHECK(graphics_cache.size() == 24);
fill_caches();
compiles = 0;
CHECK(!run() && compiles == 0 && alive == 0);
dev.vk13.pipelineCreationCacheControl = true;
// the switch at 0, or no record: nothing asked; at 2: cold with warm caches
asks = compute_asks = borrows = 0;
test_mode = 0;
CHECK(!run() && asks == 0 && compute_asks == 0 && borrows == 0);
test_mode = 1;
{
const std::unordered_set<uint64_t> none;
CHECK(!built_check_cold(notes, none, key_of, true, listed, kStamp) && asks == 0 && borrows == 0);
}
test_mode = 2;
CHECK(run() && asks == 24 && has("RX_VK_BUILT_CHECK=2"));
remove(state.c_str());
// the counter: nothing until published, nor with the switch at 0; then the record's keys and its compute programs
test_mode = 1;
built_play_build(3, 50000, false);
CHECK(st_built_slow == 0);
test_mode = 0;
built_play_publish(built, kComputesKey, listed);
CHECK(built_play_keys.load() == nullptr);
test_mode = 1;
built_play_publish(built, kComputesKey, listed);
CHECK(built_play_keys.load() != nullptr && built_check::has(*built_play_keys.load(), 507) && built_check::has(*built_play_keys.load(), 3));
logs = 0;
built_play_build(3, 50000, false);
built_play_build(4, 60000, false);
built_play_build(999, 90000, false); // (not in the record)
built_play_build(507, 0, true);
built_play_build(888, 0, true); // (not listed)
CHECK(st_built_slow == 2 && st_built_slow_us == 110000 && st_built_missed == 1 && logs == 1); // (one line in 10 s)
{ // (a record without the compute key lists no compute program)
std::unordered_set<uint64_t> graphics_only(built);
graphics_only.erase(kComputesKey);
const std::vector<uint64_t>* was = built_play_keys.exchange(nullptr);
built_play_publish(graphics_only, kComputesKey, listed);
CHECK(built_play_keys.load() && !built_check::has(*built_play_keys.load(), 507) && built_check::has(*built_play_keys.load(), 3));
delete was;
}
std::printf("ok\n");
return 0;
}
"""
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def check_source(draw):
need('#include "built_check.h"' in draw, "vk_draw.cpp does not include built_check.h")
need('static const int mode = built_check::mode(getenv("RX_VK_BUILT_CHECK"));' in block(draw, "int built_check_mode() {"),
"the switch is not RX_VK_BUILT_CHECK, read once")
pre = block(draw, "void precompile_run(void (*progress)(size_t done, size_t total)) {")
ordered(pre, ["const std::vector<uint64_t> computes_listed = computes_in_order();", "bool rebuilt = false;", "if (light) {",
"built = built_read(built_stamp);", "computes_built = built.count(computes_key) != 0;",
"if (!built.empty() && built_check_cold(notes, built, built_key, computes_built, computes_listed, built_stamp))",
"built.clear(), computes_built = false, rebuilt = true;", "std::erase_if(notes,",
"const std::unordered_set<uint64_t> used = computes_built ?", "pipeline_cache_save(true);",
"built_write(built_stamp, built);", "if (rebuilt) built_check_made(built_stamp);",
"if (light && !prepare_stopped()) built_play_publish(built, computes_key, computes_listed);"],
"precompile_run does not ask before the erase, forget the record when cold, record the strikes after the record, "
"or publish it for the counter")
need(pre.count("built_check_cold(") == 1, "precompile_run asks more than once")
check = block(draw, "bool built_check_cold(")
ordered(check, ["if (!mode || built.empty()) return false;", "const bool ask = dev.vk13.pipelineCreationCacheControl;",
"built_check::spots(recorded.size(), (size_t)built_check::kLook, seed)", "program_cache_borrow(n.prog,",
"build_graphics(p, n.s, note_targets(n), (VkPrimitiveTopology)n.topo, -1, ask ? &cold : nullptr);",
"if (computes_built && ask)", "ci.flags |= VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT;",
"cached_compute(pcache_use().cache, &ci, &pl);", 'pipe_notes_path() + ".built.check"',
"built_check::decide(v, had, stamp, mode == 2, &built_check_next);", "return true;", "return false;"],
"built_check_cold does not ask our caches as a draw and a dispatch ask, or does not decide by the record of strikes")
need("driver_cache_only" not in check, "the check asks the driver's own cache, not ours (AMD's Windows driver keeps none)")
build = block(draw, "VkPipeline build_graphics(const Program& p, const NvnState& s, const Targets& at, VkPrimitiveTopology topo, int part, bool* cold) {")
ordered(build, ["const int64_t build_us =", "if (!driver_cache_only && pl && built_check::slow(build_us) && built_play_keys.load(std::memory_order_relaxed))",
"built_play_build(pipeline_key(p, s, at, topo), build_us, false);", "return pl;"],
"build_graphics does not count a slow play build of a recorded pipeline")
disp = block(draw, "void dispatch(VkCommandBuffer cmd, NvnState& s,")
ordered(disp, ["cached_compute(pcache_use().cache, &asked, &cached)",
"if (!cached && dev.vk13.pipelineCreationCacheControl && built_play_keys.load(std::memory_order_relaxed))",
"built_play_build(p.key, 0, true);", "auto build = [job, info] {"],
"the dispatch does not count a recorded compute program its ask missed")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
need("`RX_VK_BUILT_CHECK=0`, `=2`" in docs and "`gpu/vk/built_check.h`" in docs,
"docs/development.md: the switches table has no RX_VK_BUILT_CHECK row")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
need("bool built_check_cold(" in draw, "vk_draw.cpp has no built_check_cold")
cut = "\n".join(block(draw, m) for m in ("bool built_check_cold(", "void built_check_made(uint64_t stamp) {",
"void built_play_publish(", "void built_play_build(uint64_t key, int64_t us, bool compute) {"))
env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_")}
with tempfile.TemporaryDirectory(prefix="built-check-") as tmp:
tmp = Path(tmp)
for name, text in (("rules", RULES), ("cut", FIXTURE.replace("/* CUT */", cut))):
src, exe = tmp / (name + ".cpp"), tmp / (name + ".exe")
src.write_text(text, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-I" + str(ROOT / "gpu/vk"),
str(src), "-o", str(exe)], check=True)
work = tmp / ("work-" + name)
work.mkdir()
r = subprocess.run([str(exe), str(work).replace("\\", "/")], capture_output=True, text=True, timeout=120, env=env)
need(r.returncode == 0 and r.stdout.strip() == "ok", "the built check (%s): %s" % (name, r.stdout.strip() or r.returncode))
check_source(draw)
print("PASS: the light precompile's record is asked of the caches before it is trusted; lost, everything is built again "
"before the title, then left to the draws if lost again; RX_VK_BUILT_CHECK=0 as before")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,241 @@
"""Retired capture buffers the spares cannot keep are destroyed on a thread of their own (gpu/vk/vk_memory.cpp cap_buf_free,
destroy_buffer_later, RX_VK_CAP_FREE_ASYNC).
take_capture retires old captures on the replay's thread with pools_mu held, and cap_buf_free destroyed each one past the
spares' limits right there, under cap_free_mu too: freeing a 64-256 MB host-cached buffer makes the driver unmap and release
every page. The code from cap_free_async_on to cap_buf_free and cap_exit_note is cut out unchanged and compiled with stand-ins
(a destroy_buffer that notes the thread and whether cap_free_mu is held, cap_free_mu as a mutex that knows its owner).
Checked: a spare is kept and not destroyed; one past the limits is destroyed once, on another thread, not under cap_free_mu;
RX_VK_CAP_FREE_ASYNC=0 (and off, no) destroys where it was retired, under the lock, as before; after cap_exit_note the thread
destroys nothing more; no thread to be had: destroyed inline after the lock; 4 threads retiring 400 buffers: each destroyed
exactly once; the 600-take line counts what was handed over, and the switch has its docs row.
python -I -B tests/test_cap_free_async.py [--cxx clang++]
"""
import argparse
import os
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
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 fail(message):
raise SystemExit("FAIL: " + message)
FIXTURE = r'''#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <mutex>
#include <string>
#include <system_error>
#include <thread>
#include <vector>
#include "cap_spare.h" // (cap_buf_free asks its rules: MEM-33)
static void logf(const char*, ...) {} // (the switches' log lines: not checked here)
#include "linux_pressure.h" // (MEM-141's swap hold in cap_buf_free: off, as it always is on Windows)
static bool memory_swap_hold() { return false; }
static void memory_swap_valve(int, uint64_t) {}
typedef uint32_t VkBufferUsageFlags; // (cap_buf_make, MEM-39, in the cut: never called here)
VkBufferUsageFlags cap_usage() { return 0; }
typedef struct VkBuffer_T* VkBuffer;
typedef struct VmaAllocation_T* VmaAllocation;
struct OwnedMutex { // (cap_free_mu: knows which thread holds it)
std::mutex m;
std::atomic<std::thread::id> owner{};
void lock() { m.lock(); owner = std::this_thread::get_id(); }
void unlock() { owner = std::thread::id(); m.unlock(); }
bool mine() const { return owner.load() == std::this_thread::get_id(); }
};
OwnedMutex cap_free_mu;
std::atomic<bool> cap_shutting_down{false};
uint64_t cap_free_bytes = 0;
uint64_t cap_spare_max() { return ~0ull; }
bool create_buffer_cached(uint64_t, VkBufferUsageFlags, VkBuffer&, VmaAllocation&, void**, bool) { return false; }
bool create_buffer_uncached(uint64_t, VkBufferUsageFlags, VkBuffer&, VmaAllocation&, void**, bool) { return false; }
struct Destroyed { uintptr_t id; std::thread::id by; bool under_free_mu; };
std::mutex dmu;
std::vector<Destroyed> destroyed;
void destroy_buffer(VkBuffer b, VmaAllocation) {
std::lock_guard g(dmu);
destroyed.push_back({(uintptr_t)b, std::this_thread::get_id(), cap_free_mu.mine()});
}
struct test_thread { // (std::thread, or none to be had when TEST_NO_THREAD is set)
template <class F> explicit test_thread(F f) {
if (getenv("TEST_NO_THREAD")) throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again));
t = std::thread(f);
}
void detach() { t.detach(); }
std::thread t;
};
@FREEBUF@
std::vector<FreeBuf> cap_free;
@ENVOFF@
@BLOCK@
@EXIT@
static VkBuffer buf(uintptr_t i) { return (VkBuffer)(i); }
static size_t count_of(uintptr_t id) {
std::lock_guard g(dmu);
size_t n = 0;
for (auto& d : destroyed) n += d.id == id;
return n;
}
static bool wait_for(uintptr_t id, size_t n = 1) {
for (int i = 0; i < 2000; i++) {
if (count_of(id) >= n) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return false;
}
static void fill_spares() { // (12 spares: the next one is past the limits)
for (int i = 0; i < 12; i++) cap_buf_free(buf(1000 + i), nullptr, (void*)1, 1ull << 20);
}
static int bad(const char* why) { printf("BAD %s\n", why); return 1; }
int main(int argc, char** argv) {
const std::string k = argc > 1 ? argv[1] : "";
const auto me = std::this_thread::get_id();
if (k == "keep") {
cap_buf_free(buf(1), nullptr, (void*)1, 32ull << 20);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (cap_free.size() != 1 || count_of(1)) return bad("a spare was not kept, or was destroyed");
} else if (k == "past" || k == "nothread" || k == "off") {
fill_spares();
cap_buf_free(buf(7), nullptr, (void*)1, 32ull << 20);
const size_t right_after = count_of(7);
if (!wait_for(7)) return bad("never destroyed");
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (count_of(7) != 1) return bad("destroyed more than once");
Destroyed d{};
{ std::lock_guard g(dmu); for (auto& x : destroyed) if (x.id == 7) d = x; }
if (cap_free.size() != 12) return bad("the spares changed");
if (k == "past") {
if (right_after) return bad("destroyed inside cap_buf_free");
if (d.by == me) return bad("destroyed on the retiring thread");
if (d.under_free_mu) return bad("destroyed under cap_free_mu");
} else if (k == "nothread") {
if (d.by != me) return bad("no thread: not destroyed inline");
if (d.under_free_mu) return bad("no thread: destroyed under cap_free_mu");
} else {
if (d.by != me || !d.under_free_mu) return bad("switched off: not destroyed under the lock as before");
}
} else if (k == "exit") {
fill_spares();
cap_buf_free(buf(7), nullptr, (void*)1, 32ull << 20); // (the thread started)
if (!wait_for(7)) return bad("never destroyed");
cap_exit_note();
destroy_buffer_later(buf(8), nullptr, 1ull << 20); // (queued after the flag: the thread leaves it)
std::this_thread::sleep_for(std::chrono::milliseconds(200));
if (count_of(8)) return bad("destroyed after cap_exit_note");
cap_buf_free(buf(9), nullptr, (void*)1, 32ull << 20, true); // (a worker after the flag: nothing, as before)
if (count_of(9) || cap_free.size() != 12) return bad("a worker's buffer touched after cap_exit_note");
} else if (k == "many") {
fill_spares();
std::vector<std::thread> ts;
for (int t = 0; t < 4; t++)
ts.emplace_back([t] { for (int i = 0; i < 100; i++) cap_buf_free(buf(100000 + t * 1000 + i), nullptr, (void*)1, 64ull << 20); });
for (auto& t : ts) t.join();
for (int t = 0; t < 4; t++)
for (int i = 0; i < 100; i++)
if (!wait_for(100000 + t * 1000 + i) || count_of(100000 + t * 1000 + i) != 1) return bad("not destroyed exactly once");
std::lock_guard g(dmu);
for (auto& d : destroyed) if (d.under_free_mu) return bad("destroyed under cap_free_mu");
if (cap_destroyed_later.load() != 400 || cap_destroyed_later_bytes.load() != 400ll * (64 << 20))
return bad("the handed-over count is not 400 buffers of 64 MB");
} else {
return bad("no such case");
}
printf("OK %s\n", k.c_str());
fflush(stdout);
_Exit(0); // (the detached thread is left, as at the process's exit)
}
'''
def fixture(mem):
start = mem.index("bool cap_free_async_on() {")
end_marker = "void cap_buf_free(VkBuffer buffer, VmaAllocation alloc, void* map, uint64_t size, bool from_worker = false) {"
code = mem[start:mem.index(end_marker)] + block(mem, end_marker)
code = code.replace("std::thread(", "test_thread(")
if "test_thread(" not in code:
raise ValueError("destroy_buffer_later starts no thread")
return (FIXTURE.replace("@FREEBUF@", block(mem, "struct FreeBuf {", ";"))
.replace("@ENVOFF@", block(mem, "bool env_value_off(const char* v) {"))
.replace("@BLOCK@", code).replace("@EXIT@", block(mem, "void cap_exit_note() {")))
def check_source(mem, dev):
take = block(mem, "bool take_capture(")
if "cap_buf_retire(oldest);" not in take:
fail("take_capture no longer retires through cap_buf_retire")
line = mem[mem.index("vk: a capture taken (ms): all"):]
line = line[:line.index("logf(", 1) if "logf(" in line[1:] else len(line)]
if "destroyed " not in line or "cap_destroyed_later.exchange(0" not in line or "cap_destroyed_later_bytes.exchange(0" not in line:
fail("the 600-take line does not count the buffers handed to the destroying thread")
if "`RX_VK_CAP_FREE_ASYNC=0`" not in dev:
fail("docs/development.md has no RX_VK_CAP_FREE_ASYNC row")
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
mem = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
try:
text = fixture(mem)
check_source(mem, dev)
except ValueError as e:
fail("a text this test reads moved, or is not there (%s)" % e)
flags = subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
problems = []
with tempfile.TemporaryDirectory(prefix="cap-free-async-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / ("fixture" + (".exe" if os.name == "nt" else ""))
cpp.write_text(text, encoding="utf-8")
built = subprocess.run([a.cxx, "-std=c++20", "-O1", "-pthread", "-Wall", "-Wno-unused-function", "-I" + str(ROOT / "gpu" / "vk"), str(cpp), "-o", str(exe)],
capture_output=True, text=True, creationflags=flags)
if built.returncode:
print(built.stdout + built.stderr)
fail("the fixture did not build")
base = {k: v for k, v in os.environ.items() if k not in ("RX_VK_CAP_FREE_ASYNC", "TEST_NO_THREAD", "RX_VK_CAPTURE_KEEP_OLD")}
cases = [("keep", {}), ("past", {}), ("past", {"RX_VK_CAP_FREE_ASYNC": "1"}), ("off", {"RX_VK_CAP_FREE_ASYNC": "0"}),
("off", {"RX_VK_CAP_FREE_ASYNC": "off"}), ("off", {"RX_VK_CAP_FREE_ASYNC": "No"}), ("exit", {}),
("nothread", {"TEST_NO_THREAD": "1"}), ("many", {})]
for case, extra in cases:
done = subprocess.run([str(exe), case], env={**base, **extra}, capture_output=True, text=True, timeout=60,
creationflags=flags)
if done.returncode or done.stdout.strip() != "OK " + case:
problems.append("%s %s: %s" % (case, extra, (done.stdout + done.stderr).strip()))
if problems:
fail("\n ".join([""] + problems))
print("PASS: retired capture buffers past the spares destroyed once on a thread of their own, not under cap_free_mu; "
"=0 as before; nothing after the exit note; inline without a thread (%d cases)" % len(cases))
if __name__ == "__main__":
main()

View File

@@ -190,6 +190,7 @@ def production_fixture(mem):
#include <thread>
#include <vector>
#include "capture_behind.h"
#include "cpu_count.h" // ((Triforce, 11-22, OS-49) copy_helper_main counts by cpu_count()
namespace capture_behind = mkwvk::capture_behind;
#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;
@@ -391,7 +392,7 @@ def check_source(mem, replay, texture, dev):
check("if (behind && behind_on())" in make, "capture_make: the copies go behind without the caller's leave or the switch")
for a, b in (("behind_now.store(b.get()", "behind_start(b);"), ("c->behind = b;", "behind_start(b);"),
("behind_start(b);", "copy_part(w);"), ("for (uint32_t i : c->late) na += !ahead[i], ahead[i] = 1;", "b->plan(ahead.data(), n);"),
("g < std::prev(it)->second) ahead[(size_t)i] = 1", "b->plan(ahead.data(), n);")):
("if (!ahead[(size_t)i] && in_texr.in(g)) ahead[(size_t)i] = 1", "b->plan(ahead.data(), n);")): # ((Triforce, 11-22, MEM-42) the loop's walk)
before(make, a, b, "capture_make: %s before %s" % (a, b))
check("if (!ahead[(size_t)i] || b->ok[(size_t)i]) c->copied.push_back(pages[i])" in make,
"capture_make: the pages copied ahead that failed are counted as copied, or those behind are not")

View File

@@ -68,6 +68,7 @@ def main():
#include <thread>
#include <vector>
#include "capture_behind.h"
#include "cpu_count.h" // ((Triforce, 11-22, OS-49) copy_helper_main counts by cpu_count()
namespace capture_behind = mkwvk::capture_behind;
#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;

View File

@@ -0,0 +1,440 @@
"""A capture's buffer: made with the spares' lock let go, a load's big spare left alone, the spare a load took made again
(gpu/vk/cap_spare.h, vk_memory.cpp cap_buf_get, cap_prewarm_class, cap_spare_make and cap_buf_free; no game, no GPU).
(Triforce, 11-22, the performance audit's MEM-33, after Source A's.) With no spare that fits, cap_buf_get made the buffer
on the game's submitting thread with cap_free_mu held, and the replay's take, holding pools_mu, waits on that mutex to
give a buffer back. Any spare that fit was taken, so a small capture could hold a load's 192 MB spare for the 24
submissions it is kept; and a spare made ahead was made once.
This compiles cap_spare.h and checks its rules against the code as it was on random inputs: pick() with any_size is the
old loop (the smallest that fits, the first of equals), keeps() with nothing reserved is the old limit, pick() with the
skip is the smallest fitting spare that is not a big one over twice the class, and stepped() stays between the need and
the class.
Then it cuts cap_buf_free, cap_buf_size, cap_spare_make, cap_prewarm_class and cap_buf_get out of vk_memory.cpp and runs
them over stand-ins (the worker's thread is a list the test runs by hand):
- the first capture starts the workers and reserves their bytes; each worker's spare ends its reservation;
- a 1 MB capture leaves the 32, 64 and 128 MB spares alone and makes its own, with cap_free_mu NOT held;
- a capture of a 32 MB class takes the 32 MB spare, and no worker starts while a bigger spare is there; one of a 128
MB class takes the 128 MB spare and a worker of 128 MB starts;
- the allocation fails: the big spare is taken after all; with no spare, the exact size without a memory object of
its own; a buffer made but not mapped is destroyed, not lost;
- what is being made counts: a buffer given back is not kept past the limit a worker's spare is about to fill, the
worker's spare is kept, no worker starts without room, and a worker that gets no buffer frees its reservation;
- spares capped at 32 MB: any spare is taken, as before; steps only with RX_VK_CAP_STEP_MB;
- RX_VK_CAP_SPARE_FIT=0: any fitting spare, no worker at a take, the buffer made with the lock held, nothing reserved.
And reads the source: the switches, the 600-take count, the switches table.
python -I -B tests/test_capture_spare_reuse.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def block(text, marker):
check(marker in text, "vk_memory.cpp has no " + marker)
start = text.index(marker)
end = text.index("{", start + len(marker) - 1) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def line(text, marker):
check(marker in text, "vk_memory.cpp has no " + marker)
start = text.index(marker)
return text[start:text.index("\n", start)]
HEAD = r"""
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <set>
#include <thread>
#include <vector>
#include "cap_spare.h"
// ((Triforce, 11-22) MEM-141's swap hold, merged into cap_buf_free: off, as it always is on Windows
#include "linux_pressure.h"
static bool memory_swap_hold() { return false; }
static void memory_swap_valve(int, uint64_t) {}
typedef uint64_t VkBuffer;
typedef void* VmaAllocation;
typedef unsigned VkBufferUsageFlags;
#define VK_NULL_HANDLE 0
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
static const uint64_t MB = 1ull << 20;
struct FreeBuf { VkBuffer buffer; VmaAllocation alloc; void* map; uint64_t size; };
std::vector<FreeBuf> cap_free;
std::atomic<bool> cap_shutting_down{false};
std::mutex cap_free_mu;
uint64_t cap_free_bytes = 0;
uint64_t cap_reserved_bytes = 0;
size_t cap_reserved_n = 0;
std::atomic<uint64_t> cap_made_n{0}, cap_made_us{0};
std::atomic<int> cap_prewarm_busy{0};
std::atomic<int> cap_prewarm_class_busy[4]{};
// the stand-ins
static uint64_t spare_max_v = ~0ull, step_v = 0;
static bool fit_v = true, prewarm_v = true;
uint64_t cap_spare_max() { return spare_max_v; }
bool cap_spare_fit_on() { return fit_v; }
uint64_t cap_step_mb() { return step_v; }
bool cap_prewarm_on() { return prewarm_v; }
bool cap_free_async_on() { return false; }
VkBufferUsageFlags cap_usage() { return 0; }
void cap_exit_note() {}
void logf(const char*, ...) {}
static std::vector<VkBuffer> destroyed;
void destroy_buffer(VkBuffer b, VmaAllocation) { destroyed.push_back(b); }
void destroy_buffer_later(VkBuffer b, VmaAllocation, uint64_t) { destroyed.push_back(b); }
struct Made { uint64_t size; bool own, lock_held; VkBuffer id; };
static std::vector<Made> made;
static std::set<uint64_t> fail_sizes, unmapped_sizes;
static bool fail_all = false;
static VkBuffer next_id = 100;
bool cap_buf_make(uint64_t size, VkBuffer& buffer, VmaAllocation& allocation, void** mapped, bool dedicated) { // (the capture buffers' maker)
const bool held = !cap_free_mu.try_lock(); // (is the spares' lock held by the caller, the only thread here)
if (!held) cap_free_mu.unlock();
if (fail_all || fail_sizes.count(size)) {
made.push_back({size, dedicated, held, 0});
buffer = VK_NULL_HANDLE, allocation = nullptr;
return false;
}
buffer = next_id++, allocation = (void*)&made;
made.push_back({size, dedicated, held, buffer});
if (mapped) *mapped = unmapped_sizes.count(size) ? nullptr : (void*)&made;
return true;
}
struct Spawn { uint64_t bytes; int index; };
static std::vector<Spawn> spawned; // (the workers' threads: run by hand)
"""
TAIL = r"""
static void reset(uint64_t spare_max, bool fit, uint64_t step = 0) {
cap_free.clear(), cap_free_bytes = 0, cap_reserved_bytes = 0, cap_reserved_n = 0;
spawned.clear(), made.clear(), destroyed.clear(), fail_sizes.clear(), unmapped_sizes.clear();
spare_max_v = spare_max, fit_v = fit, step_v = step, fail_all = false;
cap_prewarm_busy = 0;
for (auto& b : cap_prewarm_class_busy) b = 0;
}
static VkBuffer spare(uint64_t mb) {
cap_free.push_back({next_id, nullptr, (void*)&made, mb * MB});
cap_free_bytes += mb * MB;
return next_id++;
}
static void run_workers() {
const std::vector<Spawn> s = spawned;
spawned.clear();
for (const Spawn& w : s) cap_spare_make(w.bytes, w.index);
}
static bool has_spare(uint64_t mb) {
for (const FreeBuf& f : cap_free)
if (f.size == mb * MB) return true;
return false;
}
struct Got { bool ok; VkBuffer buffer; VmaAllocation alloc; void* map; uint64_t size; };
static Got get(uint64_t need) {
Got g{false, 0, nullptr, nullptr, 0};
g.ok = cap_buf_get(need, g.buffer, g.alloc, g.map, g.size);
return g;
}
static uint64_t rnd_state = 0x9E3779B97F4A7C15ull;
static uint64_t rnd() {
rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 7, rnd_state ^= rnd_state << 17;
return rnd_state;
}
int main() {
// ---- the rules against the code as it was, on random inputs ----
static const uint64_t kSizes[] = {1, 2, 4, 8, 16, 20, 32, 48, 64, 100, 128, 192, 256}; // (no spare is over 256 MB)
for (int t = 0; t < 20000; t++) {
std::vector<uint64_t> sizes(rnd() % 13);
for (uint64_t& s : sizes) s = kSizes[rnd() % 13] * MB;
const uint64_t need = (rnd() % (260 * 256) + 1) * 4096;
uint64_t cls = 2 * MB; // (cap_buf_size without its switches)
while (cls < need && cls < 16 * MB) cls <<= 1;
if (cls < need) {
cls = (need + MB - 1) & ~(MB - 1);
for (uint64_t c : {32, 64, 128, 192, 256})
if (c * MB >= need) { cls = c * MB; break; }
}
const auto size_of = [&](size_t i) { return sizes[i]; };
size_t old_best = sizes.size(); // (the loop as it was)
for (size_t i = 0; i < sizes.size(); i++)
if (sizes[i] >= need && (old_best == sizes.size() || sizes[i] < sizes[old_best])) old_best = i;
CHECK(cap_spare::pick(sizes.size(), size_of, need, cls, true) == old_best);
size_t want = sizes.size(); // (with the skip: the smallest that fits and is not a big one over twice the class)
for (size_t i = 0; i < sizes.size(); i++) {
if (sizes[i] < need || (sizes[i] >= 32 * MB && sizes[i] > 2 * cls)) continue;
if (want == sizes.size() || sizes[i] < sizes[want]) want = i;
}
const size_t got = cap_spare::pick(sizes.size(), size_of, need, cls, false);
CHECK(got == want);
if (got != sizes.size()) CHECK(sizes[got] >= need && old_best != sizes.size() && sizes[got] >= sizes[old_best]);
if (cls >= 128 * MB) CHECK(got == old_best); // (nothing kept is over twice a class of 128 MB or more)
// the limit as it was, with nothing being made
const size_t n = rnd() % 14;
const uint64_t held = (rnd() % 900) * MB, size = kSizes[rnd() % 13] * MB;
static const uint64_t kMax[] = {~0ull, 32ull << 20, 12ull << 20, 512ull << 20, 2048ull << 20};
const uint64_t spare_max = kMax[rnd() % 5];
for (int keep_old = 0; keep_old < 2; keep_old++) {
const bool was = n < 12 && size <= (keep_old ? 64ull << 20 : 256ull << 20) &&
held + size <= std::min<uint64_t>(spare_max, keep_old ? ~0ull : 768ull << 20);
CHECK(cap_spare::keeps(n, 0, held, 0, size, spare_max, keep_old != 0) == was);
// and what is being made counts as if it were held already
const size_t workers = rnd() % 4;
const uint64_t reserved = (rnd() % 300) * MB;
const bool with = n + workers < 12 && size <= (keep_old ? 64ull << 20 : 256ull << 20) &&
held + reserved + size <= std::min<uint64_t>(spare_max, keep_old ? ~0ull : 768ull << 20);
CHECK(cap_spare::keeps(n, workers, held, reserved, size, spare_max, keep_old != 0) == with);
CHECK(cap_spare::room(held, reserved, size, spare_max) == (held + reserved + size <= std::min<uint64_t>(spare_max, 768ull << 20)));
}
const uint64_t step = rnd() % 9;
for (int capped = 0; capped < 2; capped++) {
const uint64_t s = cap_spare::stepped(need, cls, step, capped != 0);
CHECK(s >= need && s <= cls);
if (!step || !capped || need <= 16 * MB || need > 32 * MB) CHECK(s == cls);
else CHECK(s == cls || (s % (step * MB) == 0 && s - need < step * MB));
}
}
CHECK(cap_spare::skips(192 * MB, 32 * MB, false) && !cap_spare::skips(64 * MB, 32 * MB, false) && cap_spare::skips(32 * MB, 2 * MB, false));
CHECK(!cap_spare::skips(192 * MB, 32 * MB, true) && !cap_spare::skips(16 * MB, 2 * MB, false) && !cap_spare::skips(256 * MB, 128 * MB, false));
CHECK(cap_spare::stepped(17 * MB, 32 * MB, 4, true) == 20 * MB && cap_spare::stepped(17 * MB, 32 * MB, 0, true) == 32 * MB);
CHECK(cap_spare::stepped(17 * MB, 32 * MB, 4, false) == 32 * MB && cap_spare::stepped(31 * MB, 32 * MB, 4, true) == 32 * MB);
CHECK(cap_spare::stepped(16 * MB, 16 * MB, 4, true) == 16 * MB && cap_spare::stepped(40 * MB, 64 * MB, 4, true) == 64 * MB);
// ---- the cut code ----
{ // the first capture: the workers start, their bytes reserved; the capture's own buffer is made with the lock let go
reset(~0ull, true);
const Got g = get(1 * MB);
CHECK(g.ok && g.size == 2 * MB && made.size() == 1 && made[0].size == 2 * MB && !made[0].own && !made[0].lock_held);
CHECK(spawned.size() == 3 && spawned[0].bytes == 32 * MB && spawned[1].bytes == 64 * MB && spawned[2].bytes == 128 * MB);
CHECK(cap_reserved_bytes == 224 * MB && cap_reserved_n == 3 && cap_made_n.load() == 1);
run_workers();
CHECK(cap_reserved_bytes == 0 && cap_reserved_n == 0 && cap_free.size() == 3 && cap_free_bytes == 224 * MB);
CHECK(cap_prewarm_busy.load() == 0 && has_spare(32) && has_spare(64) && has_spare(128) && destroyed.empty());
CHECK(made.size() == 4 && made[1].own && !made[1].lock_held); // (the workers' are not the submitting thread's count)
CHECK(cap_made_n.load() == 1);
// a 1 MB capture leaves the three big spares alone
const Got small = get(1 * MB);
CHECK(small.ok && small.size == 2 * MB && cap_free.size() == 3 && made.size() == 5 && !made[4].lock_held && spawned.empty());
// a capture of the 32 MB class takes the 32 MB spare; a bigger spare is still there: no worker
const Got mid = get(20 * MB);
CHECK(mid.ok && mid.size == 32 * MB && cap_free.size() == 2 && !has_spare(32) && made.size() == 5 && spawned.empty());
// a capture of the 128 MB class takes the 128 MB spare: a worker of 128 MB starts, its bytes reserved
const Got big = get(100 * MB);
CHECK(big.ok && big.size == 128 * MB && cap_free.size() == 1 && made.size() == 5);
CHECK(spawned.size() == 1 && spawned[0].bytes == 128 * MB && spawned[0].index == 2 && cap_reserved_bytes == 128 * MB && cap_reserved_n == 1);
run_workers();
CHECK(has_spare(128) && cap_reserved_bytes == 0 && cap_reserved_n == 0);
// a capture of the 64 MB class: the 64 MB spare, not the 128 MB one
const Got c64 = get(40 * MB);
CHECK(c64.ok && c64.size == 64 * MB && has_spare(128) && spawned.empty());
}
{ // the allocation fails: the big spare after all, and a worker for the one taken
reset(~0ull, true);
const VkBuffer s192 = spare(192);
fail_sizes.insert(32 * MB);
const Got g = get(20 * MB);
CHECK(g.ok && g.buffer == s192 && g.size == 192 * MB && cap_free.empty() && cap_free_bytes == 0);
CHECK(made.size() == 1 && made[0].size == 32 * MB && made[0].own && !made[0].lock_held);
CHECK(spawned.size() == 1 && spawned[0].bytes == 192 * MB && spawned[0].index == 3);
}
{ // no spare either: the exact size, without a memory object of its own, the lock let go
reset(~0ull, true);
fail_sizes.insert(32 * MB);
const uint64_t need = 20 * MB + 4096;
const Got g = get(need);
CHECK(g.ok && g.size == need && made.size() == 2 && made[1].size == need && !made[1].own && !made[1].lock_held);
reset(~0ull, true);
fail_all = true;
const Got none = get(need);
CHECK(!none.ok && made.size() == 2);
}
{ // made but not mapped: destroyed, then the exact size
reset(~0ull, true);
unmapped_sizes.insert(2 * MB);
const Got g = get(1 * MB + 4096);
CHECK(g.ok && g.size == 1 * MB + 4096 && made.size() == 2 && destroyed.size() == 1 && destroyed[0] == made[0].id && g.buffer == made[1].id);
}
{ // what is being made counts with what is held
reset(256 * MB, true);
spare(64);
{
std::lock_guard lk(cap_free_mu);
cap_prewarm_class(128 * MB);
}
CHECK(spawned.size() == 1 && cap_reserved_bytes == 128 * MB && cap_reserved_n == 1);
cap_buf_free(900, nullptr, (void*)&made, 100 * MB); // (64 held + 128 being made + 100 > 256: not kept)
CHECK(destroyed.size() == 1 && destroyed[0] == 900 && cap_free.size() == 1);
cap_buf_free(901, nullptr, (void*)&made, 64 * MB); // (64 + 128 + 64 = 256: kept)
CHECK(destroyed.size() == 1 && cap_free.size() == 2 && cap_free_bytes == 128 * MB);
{
std::lock_guard lk(cap_free_mu);
cap_prewarm_class(32 * MB); // (no room: 128 held + 128 being made + 32 > 256. And a spare of 64 MB is there)
cap_prewarm_class(192 * MB);
}
CHECK(spawned.size() == 1);
run_workers(); // (the worker's 128 MB lands and is kept: its room was reserved)
CHECK(destroyed.size() == 1 && cap_free.size() == 3 && cap_free_bytes == 256 * MB && cap_reserved_bytes == 0 && cap_reserved_n == 0);
// twelve with the one being made
reset(~0ull, true);
for (int i = 0; i < 11; i++) spare(2);
{
std::lock_guard lk(cap_free_mu);
cap_prewarm_class(32 * MB);
}
CHECK(spawned.size() == 1 && cap_reserved_n == 1);
cap_buf_free(902, nullptr, (void*)&made, 2 * MB);
CHECK(destroyed.size() == 1 && cap_free.size() == 11);
run_workers();
CHECK(destroyed.size() == 1 && cap_free.size() == 12 && has_spare(32));
// a worker that gets no buffer frees what it reserved
reset(~0ull, true);
{
std::lock_guard lk(cap_free_mu);
cap_prewarm_class(64 * MB);
}
CHECK(cap_reserved_bytes == 64 * MB);
fail_all = true;
run_workers();
CHECK(cap_reserved_bytes == 0 && cap_reserved_n == 0 && cap_free.empty() && cap_prewarm_busy.load() == 0);
}
{ // spares capped at 32 MB: any spare is taken, as before; a 32 MB class capture's spare is made again
reset(32 * MB, true);
const VkBuffer s32 = spare(32);
const Got g = get(1 * MB);
CHECK(g.ok && g.buffer == s32 && made.empty() && spawned.empty());
reset(32 * MB, true);
spare(32);
const Got h = get(20 * MB);
CHECK(h.ok && h.size == 32 * MB && made.empty() && spawned.size() == 1 && spawned[0].bytes == 32 * MB);
// steps only with the switch, and only where spares are capped
reset(32 * MB, true, 4);
const Got st = get(17 * MB);
CHECK(st.ok && st.size == 20 * MB && made.size() == 1 && made[0].own);
reset(32 * MB, true, 0);
CHECK(get(17 * MB).size == 32 * MB);
reset(~0ull, true, 4);
CHECK(get(17 * MB).size == 32 * MB);
reset(32 * MB, false, 4);
CHECK(get(17 * MB).size == 32 * MB);
}
{ // RX_VK_CAP_SPARE_FIT=0: as before
reset(~0ull, false);
const VkBuffer s192 = spare(192);
const Got g = get(1 * MB);
CHECK(g.ok && g.buffer == s192 && made.empty() && spawned.empty() && cap_reserved_bytes == 0);
const Got n = get(1 * MB);
CHECK(n.ok && n.size == 2 * MB && made.size() == 1 && made[0].lock_held && !made[0].own);
const Got big = get(100 * MB);
CHECK(big.ok && big.size == 128 * MB && made.size() == 2 && made[1].lock_held && made[1].own && spawned.empty());
reset(256 * MB, false);
spare(64);
{
std::lock_guard lk(cap_free_mu);
cap_prewarm_class(128 * MB);
}
CHECK(spawned.size() == 1 && cap_reserved_bytes == 0 && cap_reserved_n == 0);
cap_buf_free(903, nullptr, (void*)&made, 100 * MB); // (kept: nothing is counted for the worker)
CHECK(destroyed.empty() && cap_free.size() == 2);
run_workers(); // (and the worker's spare is destroyed as it lands: what the reservation is for)
CHECK(destroyed.size() == 1 && cap_free.size() == 2 && cap_reserved_bytes == 0);
}
if (fails) std::printf("capture spares: FAILED (%d checks)\n", fails);
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
mem = (VK / "vk_memory.cpp").read_text(encoding="utf-8")
check((VK / "cap_spare.h").exists(), "gpu/vk/cap_spare.h is missing")
check('#include "cap_spare.h"' in mem, "vk_memory.cpp does not include cap_spare.h")
# the source
on = block(mem, "bool cap_spare_fit_on() {")
check('getenv("RX_VK_CAP_SPARE_FIT")' in on and "!(e && *e && atoi(e) == 0)" in on and "static const bool on" in on,
"RX_VK_CAP_SPARE_FIT is not on unless set to 0, or is read more than once")
step = block(mem, "uint64_t cap_step_mb() {")
check('getenv("RX_VK_CAP_STEP_MB")' in step and "e && atoi(e) > 0 ? (uint64_t)atoi(e) : 0" in step and "static const uint64_t v" in step,
"RX_VK_CAP_STEP_MB is not off unless set, or is read more than once")
for decl in ("uint64_t cap_reserved_bytes = 0;", "size_t cap_reserved_n = 0;", "std::atomic<uint64_t> cap_made_n{0}, cap_made_us{0};"):
check(decl in mem, "vk_memory.cpp has no " + decl)
get = block(mem, "bool cap_buf_get(uint64_t need, VkBuffer& buffer, VmaAllocation& alloc, void*& map, uint64_t& size) {")
order = [get.find(s) for s in (
"std::unique_lock lk(cap_free_mu);",
"if (take_spare(!fit || capped)) return map != nullptr;",
"size = fit ? cap_spare::stepped(need, cls, cap_step_mb(), capped) : cls;",
"if (!fit) {",
"lk.unlock();\n const bool made = cap_buf_make(size, buffer, alloc, &map, own);",
"if (made && map) return true;",
"lk.lock();\n if (take_spare(true)) return map != nullptr;\n lk.unlock();",
"return cap_buf_make(size, buffer, alloc, &map, false) && map;")]
check(min(order) >= 0 and order == sorted(order),
"cap_buf_get: the spare, then the buffer made with the lock let go, then the two fallbacks, are not there in that order")
check("if (fit && cls >= cap_spare::kBig) cap_prewarm_class(size);" in get, "a load's spare taken is not made again")
check("const bool capped = cap_spare_max() <= cap_spare::kBig;" in get, "the skip and the steps do not go by the spares' cap")
free = block(mem, "void cap_buf_free(VkBuffer buffer, VmaAllocation alloc, void* map, uint64_t size, bool from_worker = false) {")
# ((Triforce, 11-22) MEM-141 caps the spares while its swap hold is on (never on Windows): spare_cap(false, x) is x
check("cap_spare::keeps(cap_free.size(), cap_reserved_n, cap_free_bytes, cap_reserved_bytes, size," in free and
"linux_pressure::spare_cap(swap_hold_on, cap_spare_max()), keep_old)) {" in free,
"cap_buf_free does not count what is being made")
cls = block(mem, "void cap_prewarm_class(uint64_t size) {")
thread_line = "std::thread(cap_spare_make, bytes, index).detach();"
check(cls.count(thread_line) == 1, "cap_prewarm_class does not start its worker as the test expects")
check(cls.index("if (fit) cap_reserved_bytes += bytes, cap_reserved_n++;") < cls.index(thread_line),
"the worker's bytes are not reserved before its thread starts")
check("made on a submitting thread (600 takes)" in mem and "cap_made_n.load(std::memory_order_relaxed)" in mem,
"the 600-take line does not count the buffers made on a submitting thread")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_CAP_SPARE_FIT=0`" in dev and "`RX_VK_CAP_STEP_MB=<n>`" in dev, "docs/development.md: the two switches are not in the switches table")
cut = "\n".join([
free,
block(mem, "uint64_t cap_buf_size(uint64_t need) {"),
line(mem, "int cap_prewarm_index(uint64_t size) {"),
block(mem, "void cap_spare_make(uint64_t size, int index) {"),
cls.replace(thread_line, "spawned.push_back({bytes, index});"),
get,
])
env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_")}
with tempfile.TemporaryDirectory(prefix="cap-spare-") as tmp:
src, exe = Path(tmp) / "spare.cpp", Path(tmp) / "spare.exe"
src.write_text(HEAD + cut + TAIL, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-pthread", "-I" + str(VK), str(src), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True, timeout=120, env=env)
check(r.returncode == 0, "the capture spares as made:\n" + r.stdout + r.stderr)
print("PASS: a capture's buffer is made with the spares' lock let go, a load's spare is left alone and made again, and "
"what is being made counts with the spares")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,203 @@
"""The stored draws' tables keep their memory from frame to frame (gpu/vk/capture_tables.h, vk_temporal.cpp
capture_motion, temporal_capture_draw and take_captures; no game, no GPU).
(Triforce, 11-21, the upscaler audit's UPS-02.) While DLSS, FSR 4 or FSR 3.1 upscales, every plain draw into the scene
stores its positions and is drawn again into the motion vectors. The count of a model's draws and the frame before's draws
by key were std::unordered_map, emptied and filled every frame: a node from the heap for every stored draw, about 1,400 of
each a frame in the owner's log, on the thread that records the frame.
This compiles capture_tables.h alone and checks it: both ways (kept, and std::unordered_map with RX_VK_CAPTURE_TABLES=0)
answer the same over frames of draws with repeated keys, draws that come and go and a clear; the frame before's entries
are found until the next turn and no longer after it; and, with the heap's calls counted, a kept frame the size of the one
before takes nothing from the heap while the old way takes a node an entry.
And reads the source: the chain's two tables are capture_tables' with the switch read once, capture_motion turns the table
before it fills it and looks the frame before's up through it, the draws are counted through next(), take_captures gives
the next frame's list its room, and the switch is in the switches table.
python -I -B tests/test_capture_tables.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <new>
#include "capture_tables.h"
static long news = 0;
void* operator new(std::size_t n) { news++; if (void* p = std::malloc(n ? n : 1)) return p; throw std::bad_alloc(); }
void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace capture_tables;
struct Draw { uint64_t address; uint32_t first, count; };
static uint32_t rng = 12345;
static uint32_t next_random() { rng = rng * 1664525u + 1013904223u; return rng >> 8; }
int main() {
CHECK(kept(nullptr) && kept("") && kept("1") && kept("2") && !kept("0"));
CHECK(room(1444, true) == 1444 && room(1444, false) == 0 && room(0, true) == 0);
{ // the n-th of a model, both ways the same; a clear starts every model at 0 again
Nth a(true), b(false);
for (int frame = 0; frame < 6; frame++) {
for (int i = 0; i < 3000; i++) {
const uint64_t model = (next_random() % 700) * 0x9E3779B97F4A7C15ull;
const int x = a.next(model), y = b.next(model);
CHECK(x == y);
}
CHECK(a.size() == b.size());
a.clear(), b.clear();
CHECK(a.size() == 0 && a.next(7) == 0 && b.next(7) == 0 && a.next(7) == 1);
a.clear(), b.clear();
}
}
{ // the frame before's entries, both ways the same: repeated keys (the last stands), draws that come and go
Before<Draw> a(true), b(false);
for (int frame = 0; frame < 8; frame++) {
a.turn(), b.turn();
const uint32_t n = 500 + next_random() % 1500;
for (uint32_t i = 0; i < n; i++) {
const uint64_t key = ((uint64_t)(next_random() % 2500) << 8) | (next_random() % 3);
const Draw d{0x1000ull * frame + i, i, n};
a.put(key, d), b.put(key, d);
}
CHECK(a.size() == b.size());
for (uint32_t k = 0; k < 2500; k++)
for (uint32_t nth = 0; nth < 3; nth++) {
const uint64_t key = ((uint64_t)k << 8) | nth;
const Draw* x = a.before(key);
const Draw* y = b.before(key);
CHECK(!x == !y);
if (x && y) CHECK(x->address == y->address && x->first == y->first && x->count == y->count);
if (frame == 0) CHECK(!x); // (nothing before the first frame)
}
}
// (a frame's entries are the frame before's only after the turn, and gone after the next one)
Before<Draw> t(true);
t.turn();
t.put(5, {50, 1, 2});
t.put(5, {51, 1, 2}); // (the last with a key stands)
CHECK(t.size() == 1 && !t.before(5));
t.turn();
CHECK(t.before(5) && t.before(5)->address == 51 && !t.before(6));
t.turn();
CHECK(!t.before(5));
t.put(9, {90, 0, 0});
t.turn();
t.clear();
CHECK(!t.before(9) && t.size() == 0);
a.clear(), b.clear();
a.turn(), b.turn();
CHECK(!a.before(0) && !b.before(0));
}
{ // the heap: a kept frame the size of the one before takes nothing from it; the old way a node an entry
for (int keep = 1; keep >= 0; keep--) {
Nth nth(keep != 0);
Before<Draw> before(keep != 0);
long steady = 0;
for (int frame = 0; frame < 12; frame++) {
const long from = news;
before.turn();
for (uint32_t i = 0; i < 1444; i++) {
const uint64_t model = (uint64_t)(i / 2) * 0x100000001B3ull;
const int n = nth.next(model);
before.put(model << 8 | (uint64_t)n, {i, i, 1});
if (frame) CHECK(before.before(model << 8 | (uint64_t)n) != nullptr);
}
nth.clear();
if (frame >= 4) steady += news - from;
}
if (keep) CHECK(steady == 0);
else CHECK(steady >= 8 * (1444 + 722));
}
}
{ // an array far larger than what a frame puts in it is given back, so emptying it does not walk all of it
FlatMap<uint64_t, int> m;
for (uint64_t i = 0; i < 40000; i++) m[i * 977] = 1;
const size_t large = m.capacity();
empty_for_next(m);
CHECK(m.size() == 0 && m.capacity() == large); // (it held that many: kept for a frame as large)
for (uint64_t i = 0; i < 100; i++) m[i] = 1;
empty_for_next(m); // (100 entries in an array for 40,000: given back)
CHECK(m.size() == 0 && m.capacity() < large);
for (uint64_t i = 0; i < 1400; i++) m[i] = 1;
const size_t fits = m.capacity();
empty_for_next(m);
CHECK(m.size() == 0 && m.capacity() == fits);
}
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
src = (VK / "vk_temporal.cpp").read_text(encoding="utf-8")
check('#include "capture_tables.h"' in src, "vk_temporal.cpp does not use the tables")
check('const bool capture_tables_kept = capture_tables::kept(getenv("RX_VK_CAPTURE_TABLES"));' in src,
"the switch is not RX_VK_CAPTURE_TABLES, read once with the chain's state")
check("capture_tables::Nth captured_models{capture_tables_kept};" in src and
"capture_tables::Before<Captured> captured_before{capture_tables_kept};" in src,
"the chain's two tables are not capture_tables' with the switch")
check("std::unordered_map<uint64_t, Captured> captured_before" not in src and
"std::unordered_map<uint64_t, int> captured_models" not in src, "a table is still a std::unordered_map of the chain's own")
motion = block(src, "int capture_motion(VkCommandBuffer cmd, const Camera& cam, const float* before, const float* reproject, uint32_t rw,")
at = [motion.index(part) for part in (
"s.captured_before.turn();",
"const bool old_ok = history && s.captured_frame && s.captured_frame == to.last;",
"if (c.k.depth == cam.depth) s.captured_before.put(c.key, c);",
"if (caps.empty() || !make_capture_pipeline()) return 0;",
"const State::Captured* const was = old_ok ? s.captured_before.before(c.key) : nullptr;",
"const State::Captured& o = *was;")]
check(at == sorted(at), "capture_motion does not turn the table, fill it with this frame's draws and then look up the frame before's")
check("old.find(" not in motion and "std::unordered_map<uint64_t, State::Captured> old" not in motion,
"capture_motion still moves the table into a map of its own")
draw = block(src, "void temporal_capture_draw(const CaptureDraw& k, CaptureSlot* out) {")
check("const int nth = s.captured_models.next(model >> 8);" in draw, "a stored draw is not counted through the kept table")
take = block(src, "std::vector<State::Captured> take_captures() {")
check(take.index("std::vector<State::Captured> c = std::move(s.captured);") <
take.index("s.captured.reserve(capture_tables::room(c.size(), s.capture_tables_kept));"),
"take_captures does not give the next frame's list its room")
drop = block(src, "void drop_all() {")
check("s.captured_models.clear(), s.captured_before.clear()" in drop, "the chain let go does not empty the tables")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_CAPTURE_TABLES=0`" in dev, "docs/development.md: RX_VK_CAPTURE_TABLES=0 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="capture-tables-") as tmp:
c, exe = Path(tmp) / "tables.cpp", Path(tmp) / "tables.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the tables as made:\n" + r.stdout)
print("PASS: the stored draws' tables keep their memory from frame to frame, and answer as std::unordered_map did")
if __name__ == "__main__":
main()

135
tests/test_chunk_default.py Normal file
View File

@@ -0,0 +1,135 @@
"""The size of a submission's parts: RX_VK_CHUNK, the lab's knob, the driver's default (gpu/vk/barrier_rules.h part_draws,
vk_replay.cpp part_draws and maybe_split; no game, no GPU).
(Triforce, 11-22, the performance audit's SYNC-66, after Source B's.) A long submission goes to the GPU in parts of 120 draws
and dispatches on every card, read once into a static of maybe_split, so nothing could compare sizes inside a run. The size is
now RX_VK_CHUNK when set, else the RX_VK_LAB_AB knob chunk:<n> in its phases, else the driver's default: 120 for every driver
until the lab picks one.
Compiles a driver against barrier_rules.h and lab_ab.h and checks:
the default is 120 for NVIDIA, AMD, Intel and any other vendor, with RADV or not;
with no knob the size is what the old read gave, for 100,000 random texts of the variable (unset, 0, 120, negative, words,
any number) on random vendors: the default path splits where it split;
RX_VK_CHUNK beats the knob and the default; 0 stays 0 (never split); the knob beats the default only in its own phases
(lab_ab.h's value_of with the phase's knob set, another knob's phase, and no phase).
And reads vk_replay.cpp: maybe_split has no static size and asks part_draws() at each call, still returns for a size of 0 or
less and still counts ++part_work against it after the tail's test; part_draws reads the variable once and the knob at each
call; the switches table has the row and no longer says the in-pass split is on with NVIDIA's driver.
python -I -B tests/test_chunk_default.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
DRIVER = r'''
#include "barrier_rules.h"
#include "lab_ab.h"
#include <cstdio>
namespace br = mkwvk::barrier_rules;
namespace lab = mkwvk::lab_ab;
static int checks, fails;
#define CHECK(what, cond) do { checks++; if (!(cond)) fails++, printf("FAIL %s (line %d)\n", what, __LINE__); } while (0)
int main() {
static const uint32_t kVendors[] = {0x10de, 0x1002, 0x8086, 0x5143, 0x13b5, 0, 0xffffffffu};
for (const uint32_t v : kVendors)
for (int radv = 0; radv < 2; radv++) CHECK("120 for every driver until the lab picks", br::part_draws_default(v, radv != 0) == 120);
CHECK("the constant", br::kPartDraws == 120);
// with no knob: the old read, whatever the variable says
uint64_t rng = 0xD1B54A32D192ED03ull;
const auto rnd = [&](uint64_t n) { rng ^= rng << 13, rng ^= rng >> 7, rng ^= rng << 17; return rng % n; };
static const char* const kTexts[] = {nullptr, "", "0", "120", "480", "4096", "-5", "junk", "12x", " 64", "1", "999999999999"};
for (int i = 0; i < 100000; i++) {
char num[32];
const char* env = kTexts[rnd(sizeof kTexts / sizeof *kTexts)];
if (rnd(3) == 0) snprintf(num, sizeof num, "%lld", (long long)rnd(5000) - 500), env = num;
const uint32_t vendor = kVendors[rnd(sizeof kVendors / sizeof *kVendors)];
const int64_t now = br::part_draws(env != nullptr, env ? atoll(env) : 0, br::part_draws_default(vendor, rnd(2) != 0));
CHECK("with no knob the size is the old read's", now == br::part_draws_before(env));
if (fails) break;
}
CHECK("unset: 120", br::part_draws(false, 0, br::part_draws_default(0x10de, false)) == 120);
CHECK("RX_VK_CHUNK beats the knob", br::part_draws(true, 240, 480) == 240);
CHECK("RX_VK_CHUNK=0 stays 0 under a knob", br::part_draws(true, 0, 480) == 0);
CHECK("the knob beats the default", br::part_draws(false, 0, 480) == 480);
// the knob through lab_ab.h, as vk_replay.cpp's part_draws asks it: only in its own phases
CHECK("no RX_VK_LAB_AB: the default", (lab::n_knobs = 0, lab::value_of("chunk", 120)) == 120);
lab::n_knobs = 2;
snprintf(lab::knobs[0].name, sizeof lab::knobs[0].name, "chunk"), lab::knobs[0].value = 480;
snprintf(lab::knobs[1].name, sizeof lab::knobs[1].name, "rblate"), lab::knobs[1].value = 1;
lab::active.store(0);
CHECK("a phase with no knob: the default", br::part_draws(false, 0, lab::value_of("chunk", 120)) == 120);
lab::active.store(1);
CHECK("the knob's phase: its number", br::part_draws(false, 0, lab::value_of("chunk", 120)) == 480);
CHECK("the knob's phase, the variable set: the variable", br::part_draws(true, 120, lab::value_of("chunk", 120)) == 120);
lab::active.store(2);
CHECK("another knob's phase: the default", br::part_draws(false, 0, lab::value_of("chunk", 120)) == 120);
if (fails) printf("chunk default: FAILED (%d of %d checks)\n", fails, checks);
else printf("chunk default: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check(ok, what):
if not ok:
raise SystemExit("FAIL: " + what)
def function(text, head):
at = text.index(head)
return text[at:text.index("\n}\n", at)]
def check_source():
replay = (ROOT / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
split = function(replay, "void maybe_split(VkCommandBuffer& cmd) {")
check("static const int64_t every" not in replay, "maybe_split still reads the size once into a static")
check(split.startswith("void maybe_split(VkCommandBuffer& cmd) {\n const int64_t every = part_draws();\n"),
"maybe_split does not ask part_draws() at each call")
check('getenv("RX_VK_CHUNK")' not in split, "maybe_split reads RX_VK_CHUNK itself")
check("if (tail_on()) return;" in split and
"if (!rec_cur || every <= 0 || ++part_work < every || rec_parts + 1 >= kMaxParts || dump_on()) return;" in split and
split.index("if (tail_on()) return;") < split.index("++part_work < every"),
"maybe_split's test changed: a size of 0 or less must never split, and the count comes after the tail's test")
part = function(replay, "int64_t part_draws() {")
check('static const char* const env = getenv("RX_VK_CHUNK");' in part and "static const int64_t env_value = env ? atoll(env) : 0;" in part,
"part_draws does not read RX_VK_CHUNK once")
check("barrier_rules::part_draws_default(dev.props.vendorID, radv);" in part, "the default is not asked of the rule with the driver")
check(part.rstrip().endswith('return barrier_rules::part_draws(env != nullptr, env_value, lab_ab::value_of("chunk", by_driver));'),
"part_draws does not ask the knob at each call, behind the variable")
check(replay.count('getenv("RX_VK_CHUNK")') == 1, "RX_VK_CHUNK is read somewhere else")
dev = (ROOT / "docs/development.md").read_text(encoding="utf-8")
row = [line for line in dev.splitlines() if line.startswith("| `RX_VK_CHUNK=<n>` |")]
check(len(row) == 1 and "**120 on every driver**" in row[0] and "`chunk:<n>`" in row[0], "docs/development.md: no row for RX_VK_CHUNK with its default")
check("Unset: `pass` with NVIDIA's driver" not in dev, "docs/development.md still says the in-pass split is on with NVIDIA's driver")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="chunk-default-") as tmp:
src = Path(tmp) / "driver.cpp"
src.write_text(DRIVER, encoding="utf-8")
exe = Path(tmp) / "driver.exe"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I", str(ROOT / "gpu/vk"), str(src), "-o", str(exe)],
check=True)
out = subprocess.run([str(exe)], capture_output=True, text=True)
print(out.stdout, end="")
check(out.returncode == 0, "the rule's checks failed")
print("PASS: a part's size: RX_VK_CHUNK, then the lab knob, then the driver's default, 120 for each")
if __name__ == "__main__":
main()

323
tests/test_chunk_parts.py Normal file
View File

@@ -0,0 +1,323 @@
"""RX_COLD_OPT: the lifted chunks at -O2, each compiled as parts cut where a function begins (nx2/cold_parts.py), on
made-up chunks (game-free).
(Triforce, 11-22, the performance audit's LIFT-17, after Source A's.) The build compiles the hot pieces at -O2 and every
other lifted chunk at -O1. RX_COLD_OPT=O2 (or a file that lists chunks) asks for -O2 on those too, and each such chunk is
then compiled as parts of RX_COLD_PART_MB, written under <build>/split.
The rules:
- RX_COLD_OPT unset, empty or O1 is nothing: the build's sources come back as the same list, no file takes other flags,
and nothing is written. O2 is every lifted chunk (code_NNN.c and the lift's own parts code_NNN_M.c), a file is the
chunks it lists; a hot piece, the hot file and the runtime's files never. Any other value, or a list with a name that
is not a chunk's, stops the build with a message.
- RX_COLD_PART_MB: 6 when unset, 0 for no cut; anything that is not a size stops the build.
- The flags of a -O2 chunk are the chunks' flags with -O2 where the level was and the part size as a define, so the size
is in every object's key; nothing else changes.
- The parts hold the chunk's functions once and in order; every part begins with the chunk's declarations and then
with a function's first line (or the note above it); no part is past the budget unless it is one function; a chunk
that fits the budget is one part, its own text.
- The key file: a second call with the same chunk and size writes no file; another size, another chunk text, a part
gone or a key gone writes them again, and parts the new cut does not make are removed. The key holds the size.
- The lift's files are not touched: the chunk's bytes and its folder's listing are the same after.
The compile (clang; skipped without one): a made-up chunk of functions that call each other is built whole and as its
parts, both at -O2, and run on the same contexts: the same registers after every function.
And reads the source: nx2/build.py asks cold_parts.expand for its sources and gives the parts cold_fl, the hot pieces
their own flags as before; nx2/make_game.py leaves nx2/cold_parts.py out of the lift key; the switches are in the table.
python -I -B tests/test_chunk_parts.py [--cc clang]
"""
import argparse
import os
import re
import shutil
import subprocess
import sys
import tempfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
sys.path.insert(0, os.path.join(ROOT, "nx2"))
import kit # noqa: E402,F401 (a64-kit on the path, for chunks)
import chunks # noqa: E402
import cold_parts # noqa: E402
import lifted_c # noqa: E402
RUN_ALL_ARGS = ["--cc", "clang"]
def check(what, ok):
if not ok:
raise SystemExit("FAIL: chunk parts: " + what)
def stops(fn):
try:
fn()
except SystemExit as e:
return bool(str(e))
return False
def function(addr, size, callee=None, note=None):
head = "void f_%08x(Ctx *__restrict A64_NOESC c) {\n" % addr
line = "c->x[1] = c->x[1] * 3 + c->x[2] + %d;\n" % (addr & 0xff)
call = "f_%08x(c);\n" % callee if callee else ""
return ("/* %s */\n" % note if note else "") + head + line * max(1, (size - len(head) - len(call) - 2) // len(line)) + call + "}\n"
def made_up_chunk(n, giant=0):
"""(the preamble, the functions): n small functions, each calling the one after it now and then; one giant among them"""
addrs = [0x80000000 + 16 * i for i in range(n)]
preamble = '#include "a64.h"\n' + "".join("void f_%08x(Ctx *A64_NOESC);\n" % a for a in addrs)
fns = []
for i, a in enumerate(addrs):
size = giant if giant and i == n // 3 else 1500 + 37 * (i % 11)
fns.append(function(a, size, addrs[i + 1] if i % 4 == 0 and i + 1 < n else None, "a note" if i % 7 == 3 else None))
return preamble, fns
def check_rules(tmp):
lvl = cold_parts.cold_level
check("unset, empty and O1 ask nothing", lvl({}) is None and lvl({"RX_COLD_OPT": ""}) is None and lvl({"RX_COLD_OPT": " O1 "}) is None)
check("O2 is every chunk", lvl({"RX_COLD_OPT": "O2"}) == "all")
check("another level stops the build", all(stops(lambda v=v: lvl({"RX_COLD_OPT": v})) for v in ("O3", "o2", "-O2", "1", "all")))
listed = os.path.join(tmp, "cold.txt")
with open(listed, "w") as f:
f.write("# the next tier\ncode_003.c\n\ncode_012_1.c # the giant's part\n")
check("a file lists chunks", lvl({"RX_COLD_OPT": listed}) == frozenset(("code_003.c", "code_012_1.c")))
for bad in ("code_hot_2.c", "code_hot.c", "a64_table.c", "code_3.c", "code_003.p0.c"):
with open(os.path.join(tmp, "bad.txt"), "w") as f:
f.write("code_001.c\n%s\n" % bad)
check("a list naming %s stops the build" % bad, stops(lambda: lvl({"RX_COLD_OPT": os.path.join(tmp, "bad.txt")})))
o2 = cold_parts.chunk_opt_o2
check("every chunk under O2", o2("code_000.c", "all") and o2("code_047.c", "all") and o2("code_012_3.c", "all"))
check("the hot pieces and the runtime's files never", not any(o2(n, "all") for n in (
"code_hot.c", "code_hot_0.c", "code_hot_7.c", "a64_table.c", "a64_hooks.c", "hos_image.c", "a64_stubs.c", "code_000.p0.c",
"code_000.c.whole")))
some = frozenset(("code_003.c",))
check("only the listed chunks under a list", o2("code_003.c", some) and not o2("code_004.c", some) and not o2("code_hot_3.c", some))
check("no chunk when nothing is asked", not o2("code_003.c", None))
pb = cold_parts.part_bytes
check("6 MB when unset", pb({}) == 6 << 20 == pb({"RX_COLD_PART_MB": ""}) and cold_parts.PART_MB_DEFAULT == 6)
check("the size as given", pb({"RX_COLD_PART_MB": "8"}) == 8 << 20 and pb({"RX_COLD_PART_MB": "2.5"}) == int(2.5 * (1 << 20)))
check("0 is no cut", pb({"RX_COLD_PART_MB": "0"}) == 0)
check("what is not a size stops the build", all(stops(lambda v=v: pb({"RX_COLD_PART_MB": v})) for v in ("-1", "six", "1e9", "nan")))
fl = ["-march=x86-64-v3", "-ffp-contract=off", "-Igen", "-O1", "-g0", "-mllvm", "-rotation-max-header-size=0", "-DA64_POLLING"]
cold = cold_parts.cold_flags(fl, "O1", {})
check("the flags: -O2 where the level was, the part size, nothing else",
cold == [f if f != "-O1" else "-O2" for f in fl] + ["-DRX_COLD_PART_MB=6"] and fl.count("-O1") == 1)
check("the part size is in the flags", cold_parts.cold_flags(fl, "O1", {"RX_COLD_PART_MB": "8"}) != cold)
def check_cut():
preamble, fns = made_up_chunk(400, giant=90000)
text = preamble + "".join(fns)
budget = 60000
parts = cold_parts.chunk_parts(text, budget, chunks.fn_cost)
check("the chunk is cut", len(parts) > 5)
check("every part has the chunk's declarations", all(p.startswith(preamble) for p in parts))
bodies = [p[len(preamble):] for p in parts]
check("the parts hold the chunk's functions once, in order", "".join(bodies) == "".join(fns))
check("every part begins where a function begins", all(lifted_c.HEAD_NOTED.match(b) for b in bodies))
check("a note stays with its function", not any(b.rstrip().endswith("*/") for b in bodies))
for b in bodies:
marks = [m.start() for m in lifted_c.HEAD_NOTED.finditer(b)] + [len(b)]
cost = sum(chunks.fn_cost(y - x) for x, y in zip(marks, marks[1:]))
check("no part is past the budget unless it is one function", cost <= budget or len(marks) == 2)
giant = [b for b in bodies if len(b) > 80000]
check("the giant function is a part by itself", len(giant) == 1 and len(lifted_c.HEAD_NOTED.findall(giant[0])) == 1)
check("a chunk that fits is its own text", cold_parts.chunk_parts(text, 1 << 30, chunks.fn_cost) == [text])
check("no cut at 0", cold_parts.chunk_parts(text, 0, chunks.fn_cost) == [text])
one = preamble + fns[0]
check("a chunk of one function is its own text", cold_parts.chunk_parts(one, 10, chunks.fn_cost) == [one])
check("the key holds the size and the text", len({cold_parts.parts_key(text, budget), cold_parts.parts_key(text, budget + 1),
cold_parts.parts_key(text + " ", budget)}) == 3)
return preamble, fns, text
def read(path):
with open(path, encoding="utf-8", newline="") as f:
return f.read()
def check_files(tmp, preamble, fns, text):
gen, split = os.path.join(tmp, "gen"), os.path.join(tmp, "build", "split")
os.makedirs(gen)
small_pre, small_fns = made_up_chunk(12)
names = {"code_000.c": text, "code_001.c": small_pre + "".join(small_fns), "code_002_1.c": text.replace("f_8", "f_9"),
"code_hot_0.c": text.replace("f_8", "f_a"), "a64_table.c": "int a64_table;\n"}
for name, body in names.items():
with open(os.path.join(gen, name), "w", encoding="utf-8", newline="") as f:
f.write(body)
srcs = [os.path.join(gen, n) for n in sorted(names)]
listing = sorted(os.listdir(gen))
# nothing asked: the same list, nothing cold, nothing written
out, cold = cold_parts.expand(srcs, split, {})
check("with RX_COLD_OPT unset the sources are the list given, with no cold file", out is srcs and cold == frozenset())
check("with RX_COLD_OPT unset nothing is written", not os.path.exists(os.path.join(tmp, "build")))
env = {"RX_COLD_OPT": "O2", "RX_COLD_PART_MB": "0.05"} # (about 52 KB a part)
stats = {}
out, cold = cold_parts.expand(srcs, split, env, stats=stats) # (fn_cost: chunks.fn_cost, found by expand itself)
budget = cold_parts.part_bytes(env)
check("the hot piece and the runtime's file stay in the list, with their flags", all(
s in out and s not in cold for s in srcs if os.path.basename(s) in ("code_hot_0.c", "a64_table.c")))
check("the chunks are replaced by their parts", not any(s in out for s in srcs if cold_parts.CHUNK.fullmatch(os.path.basename(s))))
check("every part takes the cold flags, and nothing else does", cold == frozenset(s for s in out if os.path.dirname(s) == split))
check("the order is the chunks' order, a chunk's parts together",
[os.path.basename(s).split(".p")[0] for s in out if s in cold] == sorted(os.path.basename(s).split(".p")[0] for s in out if s in cold))
check("three chunks written", stats == {"written": 3})
for chunk in ("code_000.c", "code_001.c", "code_002_1.c"):
mine = [s for s in out if os.path.basename(s).startswith(chunk[:-2] + ".p")]
check("%s: its parts are named after it, in order" % chunk,
[os.path.basename(s) for s in mine] == [cold_parts.part_name(chunk, i) for i in range(len(mine))])
check("%s: its parts are the cut of its text" % chunk,
[read(s) for s in mine] == cold_parts.chunk_parts(names[chunk], budget, chunks.fn_cost))
key = read(os.path.join(split, chunk + ".key")).split()
check("%s: the key file names the key and the parts" % chunk,
key == [cold_parts.parts_key(names[chunk], budget)] + [os.path.basename(s) for s in mine])
check("the small chunk is one part, its own text", [read(s) for s in out if "code_001.p" in s] == [names["code_001.c"]])
check("the lift's files are as they were", sorted(os.listdir(gen)) == listing and all(
read(os.path.join(gen, n)) == body for n, body in names.items()))
# a second build: nothing written (every part given an old date first: a rewrite would change it)
for s in cold:
os.utime(s, (1000000000, 1000000000))
stats = {}
out2, cold2 = cold_parts.expand(srcs, split, env, stats=stats)
check("a second build makes the same parts", out2 == out and cold2 == cold and stats == {"kept": 3})
check("a second build writes no part", all(int(os.path.getmtime(s)) == 1000000000 for s in cold))
# another size: cut again, the parts the new cut does not make removed
before = len([s for s in out if "code_000.p" in s])
stats = {}
out3, cold3 = cold_parts.expand(srcs, split, dict(env, RX_COLD_PART_MB="0.2"), stats=stats)
after = [s for s in out3 if "code_000.p" in s]
check("another part size cuts the chunks again", stats == {"written": 3} and 1 < len(after) < before)
check("the parts of the old cut are removed", sorted(f for f in os.listdir(split) if f.startswith("code_000.p")) ==
sorted(os.path.basename(s) for s in after))
check("the new parts hold the chunk once", "".join(read(s)[len(preamble):] for s in after) == "".join(fns))
# a changed chunk, a part gone, a key gone: that chunk alone is cut again
env = dict(env, RX_COLD_PART_MB="0.2")
with open(os.path.join(gen, "code_001.c"), "a", encoding="utf-8", newline="") as f:
f.write(function(0x80008000, 900))
stats = {}
cold_parts.expand(srcs, split, env, stats=stats)
check("a changed chunk alone is cut again", stats == {"written": 1, "kept": 2})
os.remove(after[1])
stats = {}
cold_parts.expand(srcs, split, env, stats=stats)
check("a chunk with a part gone is cut again", stats == {"written": 1, "kept": 2} and os.path.isfile(after[1]))
os.remove(os.path.join(split, "code_002_1.c.key"))
stats = {}
cold_parts.expand(srcs, split, env, stats=stats)
check("a chunk with its key gone is cut again", stats == {"written": 1, "kept": 2})
# a list of chunks, and no cut
listed = os.path.join(tmp, "listed.txt")
with open(listed, "w") as f:
f.write("code_002_1.c\n")
out4, cold4 = cold_parts.expand(srcs, split, dict(env, RX_COLD_OPT=listed))
check("a list: only its chunks are replaced", {os.path.basename(s).split(".p")[0] for s in cold4} == {"code_002_1"} and
os.path.join(gen, "code_000.c") in out4 and os.path.join(gen, "code_000.c") not in cold4)
out5, cold5 = cold_parts.expand(srcs, split, {"RX_COLD_OPT": "O2", "RX_COLD_PART_MB": "0"})
check("no cut at 0: the chunks themselves take the cold flags", out5 == srcs and cold5 == frozenset(
s for s in srcs if cold_parts.CHUNK.fullmatch(os.path.basename(s))))
return [s for s in after]
STAND_IN = """/* tests/test_chunk_parts.py: a stand-in for the guest model, enough for made-up functions */
#include <stdint.h>
typedef struct { uint64_t x[32]; } Ctx;
#define A64_NOESC
"""
MAIN = """#include <stdio.h>
#include "a64.h"
%(decls)s
static void (*const fns[])(Ctx *) = {%(names)s};
int main(void) {
Ctx c;
uint64_t sum = 0;
for (unsigned k = 0; k < sizeof fns / sizeof fns[0]; k++) {
for (int r = 0; r < 32; r++) c.x[r] = 0x9e3779b97f4a7c15ULL * (k + 1) + r;
fns[k](&c);
sum = sum * 31 + c.x[1];
printf("%%u %%016llx\\n", k, (unsigned long long)c.x[1]);
}
printf("sum %%016llx\\n", (unsigned long long)sum);
return 0;
}
"""
def check_compile(tmp, cc, preamble, fns, text):
"""The chunk whole and as parts, at -O2: the same results"""
work = os.path.join(tmp, "cc")
os.makedirs(work)
with open(os.path.join(work, "a64.h"), "w", newline="\n") as f:
f.write(STAND_IN)
names = lifted_c.HEAD.findall(text)
with open(os.path.join(work, "main.c"), "w", newline="\n") as f:
f.write(MAIN % {"decls": "".join("void %s(Ctx *);\n" % n for n in names), "names": ", ".join(names)})
whole = os.path.join(work, "code_000.c")
with open(whole, "w", encoding="utf-8", newline="") as f:
f.write(text)
parts = cold_parts.write_parts(whole, os.path.join(work, "split"), 40000, chunks.fn_cost)
check("the chunk to compile is cut", len(parts) > 3)
import test_fpx_sticky as fs # (the build helpers the lifted-code tests use: the target and linker flags)
outs = []
for label, srcs in (("whole", [whole]), ("parts", parts)):
exe = os.path.join(work, label + ".exe")
cmd = [cc] + fs.target_flags(cc, work) + ["-O2", "-g0", "-Wno-everything", "-I" + work, os.path.join(work, "main.c")] + srcs + ["-o", exe]
r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
check("the chunk compiles %s:\n%s" % (label, r.stdout[-2000:]), r.returncode == 0)
outs.append(subprocess.run([exe], stdout=subprocess.PIPE, text=True, timeout=120).stdout)
check("the parts compute what the whole chunk computes", outs[0] == outs[1] and outs[0].count("\n") == len(names) + 1)
return len(names), len(parts)
def check_sources():
with open(os.path.join(ROOT, "nx2", "build.py"), encoding="utf-8") as f:
build = f.read()
for line in (' srcs, cold = cold_parts.expand(srcs, os.path.join(out, "split"), os.environ, stats=cold_stats)\n',
" cold_fl = cold_parts.cold_flags(fl, a.opt, os.environ) if cold else fl",
' hot_fl if os.path.basename(s).startswith("code_hot") else cold_fl if s in cold else fl,\n'):
check("nx2/build.py no longer has the line %r" % line.strip(), build.count(line) == 1)
at = [build.find(p) for p in (" srcs, cold = cold_parts.expand(", " srcs.sort(key=lambda s: -compile_cost(s, costs))",
" cold_fl = cold_parts.cold_flags(", " jobs = [(cc, s, os.path.join(obj_dir,")]
check("nx2/build.py expands its sources before it orders them and makes the jobs", min(at) >= 0 and at == sorted(at))
with open(os.path.join(ROOT, "nx2", "make_game.py"), encoding="utf-8") as f:
make_game = f.read()
not_keyed = re.search(r"^LIFT_KEY_NOT = \((.*?)\) #", make_game, re.S | re.M)
check("nx2/make_game.py leaves nx2/cold_parts.py out of the lift key", bool(not_keyed) and '"cold_parts.py"' in not_keyed.group(1))
for lift_file in ("lift.py", "split_hot.py", "chunks.py"): # (the lift reads none of it)
with open(os.path.join(ROOT, "nx2", lift_file), encoding="utf-8") as f:
check("nx2/%s names cold_parts" % lift_file, "cold_parts" not in f.read())
with open(os.path.join(ROOT, "docs", "development.md"), encoding="utf-8") as f:
check("docs/development.md: RX_COLD_OPT and RX_COLD_PART_MB are not in the switches table",
"| `RX_COLD_OPT=O2` or `=<file>`, `RX_COLD_PART_MB=<n>` |" in f.read())
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--cc")
a = p.parse_args()
check_sources()
with tempfile.TemporaryDirectory(prefix="triforce-chunk-parts-") as tmp:
check_rules(tmp)
preamble, fns, text = check_cut()
check_files(tmp, preamble, fns, text)
cc = shutil.which(a.cc) if a.cc else shutil.which("clang")
if not cc:
print("PASS: the rules, the cut and the key files (the compile skipped: no clang)")
return
n, parts = check_compile(tmp, cc, preamble, fns, text)
print("PASS: the rules, the cut and the key files; %d made-up functions compute the same whole and in %d parts at -O2" % (n, parts))
if __name__ == "__main__":
main()

376
tests/test_commit_limit.py Normal file
View File

@@ -0,0 +1,376 @@
"""The waits at Windows' commit limit: the game's commits, the host's own allocations and the renderer's `new`
(host/host_alloc.h, host/mkw_glue.c commit_wait and hos_xmalloc, gpu/vk/new_wait.h, vk_main.cpp new_refused; no game).
(Triforce, 11-22, the performance audit's OS-04, after Source A's and Source B's.) A refusal at the limit is tried again
every 50 ms for HOS_COMMIT_WAIT_MS (5000 unless set; 0: no wait).
- host_alloc.h's numbers and loop; then commit_wait, cut out of mkw_glue.c over a VirtualAlloc that refuses as the case
says, a process a case (the switch is read once): refused twice more and then made (150 ms slept, one line); refused
for good (100 tries, 5 s, one line); another error (given up at once); the switch at 0 (no try, no sleep, no line), at
200 (4 tries) and far too large (60 s at most);
- hos_xmalloc, hos_xcalloc and hos_xrealloc, cut out of mkw_glue.c over a C library that refuses as the case says: no
refusal costs nothing; refused three times, then made after 150 ms; refused for good: NULL after 5 s and the
out-of-memory notice, once however many fail; a size of 0 is not waited for; a refused realloc keeps its block and
its bytes; the switch at 0: NULL at once;
- new_wait.h's rule; then new_refused, cut out of vk_main.cpp and set as the new handler of the fixture, with the commit
left as the case says and real allocations that cannot be made: near the limit a `new` waits its 5 s and throws, the
same thread's next refusal throws at once (5 s a thread), another thread has its own; with 4 GiB or more left, or the
commit not known, it throws at once; a nothrow new in a NoWait scope answers null at once, and without the scope
waits like any other; the switch at 0: at once;
- the sources: the host's tables that end the game on NULL allocate through the three; every nothrow new of the
renderer is in a NoWait scope; mkwvk_init sets the handler; the switch is in the switches table.
python -I -B tests/test_commit_limit.py [--cc clang] [--cxx clang++]
"""
import argparse
import os
import re
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cc", "clang", "--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
HOST = ROOT / "host"
VK = ROOT / "gpu" / "vk"
def fail(message):
raise SystemExit("FAIL: " + message)
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]
WINDOWS = r'''
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "host_alloc.h"
static int fails;
#define CHECK(x) do { if (!(x)) { printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
/* Windows, standing in */
typedef size_t SIZE_T;
typedef long LONG;
typedef unsigned long DWORD;
typedef void *LPVOID;
typedef void *HANDLE;
#define WINAPI
#define MEM_COMMIT 0x1000
#define PAGE_READWRITE 4
#define ERROR_COMMITMENT_LIMIT 1455
static int slept_ms, logs, boxes;
static char last_log[256];
static void Sleep(DWORD ms) { slept_ms += (int)ms; }
static LONG InterlockedIncrement(volatile LONG *v) { return ++*v; }
static LONG InterlockedExchange(volatile LONG *v, LONG to) { const LONG was = *v; *v = to; return was; }
static HANDLE CreateThread(void *a, size_t b, DWORD (*fn)(LPVOID), void *c, DWORD d, void *e) { (void)a, (void)b, (void)fn, (void)c, (void)d, (void)e; boxes++; return NULL; }
static void CloseHandle(HANDLE h) { (void)h; }
static void hos_log(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(last_log, sizeof last_log, fmt, ap);
va_end(ap);
logs++;
}
'''
COMMIT = WINDOWS + r'''
static int refuse, tries;
static DWORD error = ERROR_COMMITMENT_LIMIT;
static void *VirtualAlloc(void *p, SIZE_T n, int a, int b) {
(void)n, (void)a, (void)b;
tries++;
if (refuse > 0) { refuse--; return NULL; }
return p;
}
static DWORD GetLastError(void) { return error; }
@WAIT_MS@
@COMMIT_WAIT@
int main(int argc, char **argv) {
const char *c = argc > 1 ? argv[1] : "";
static char page[4096];
if (!strcmp(c, "made")) { /* the caller's own try was refused; two more are, the third is made */
refuse = 2;
CHECK(commit_wait(page, 4096) == 1 && slept_ms == 150 && tries == 3 && logs == 1 && strstr(last_log, "made after 150 ms"));
refuse = 0;
CHECK(commit_wait(page, 4096) == 1 && slept_ms == 200 && tries == 4); /* (made at the first try again) */
} else if (!strcmp(c, "never")) { /* refused for good */
refuse = 1 << 30;
CHECK(commit_wait(page, 4096) == 0 && slept_ms == atoi(argv[2]) && tries == atoi(argv[2]) / 50 && logs == 1);
CHECK(strstr(last_log, "still refused after") != NULL);
} else if (!strcmp(c, "other")) { /* not the limit's refusal: given up at once */
refuse = 1 << 30, error = 8;
CHECK(commit_wait(page, 4096) == 0 && slept_ms == 50 && tries == 1 && logs == 0);
} else if (!strcmp(c, "off")) { /* HOS_COMMIT_WAIT_MS=0 */
refuse = 0;
CHECK(commit_wait(page, 4096) == 0 && slept_ms == 0 && tries == 0 && logs == 0);
} else { /* the numbers */
CHECK(host_alloc_wait_ms(NULL) == 5000 && host_alloc_wait_ms("") == 5000);
CHECK(host_alloc_wait_ms("0") == 0 && host_alloc_wait_ms("-5") == 0 && host_alloc_wait_ms("x") == 0);
CHECK(host_alloc_wait_ms("200") == 200 && host_alloc_wait_ms("60000") == 60000 && host_alloc_wait_ms("999999") == 60000);
CHECK(host_alloc_tries(5000) == 100 && host_alloc_tries(200) == 4 && host_alloc_tries(49) == 0 && host_alloc_tries(0) == 0 && host_alloc_tries(-1) == 0);
CHECK(hos_commit_wait_ms() == 5000);
}
if (!fails) printf("PASS %s\n", c);
return fails != 0;
}
'''
ALLOC = WINDOWS + r'''
/* the C library, refusing as the case says */
static int refuse, asked;
static void *lib_malloc(size_t n) { asked++; if (refuse > 0 && n) { refuse--; return NULL; } return malloc(n); }
static void *lib_calloc(size_t c, size_t n) { asked++; if (refuse > 0 && c && n) { refuse--; return NULL; } return calloc(c, n); }
static void *lib_realloc(void *p, size_t n) { asked++; if (refuse > 0 && n) { refuse--; return NULL; } return realloc(p, n); }
#define malloc lib_malloc
#define calloc lib_calloc
#define realloc lib_realloc
@WAIT_MS@
@ALLOC@
#undef malloc
#undef calloc
#undef realloc
static void *count_try(void *ctx) { int *n = (int *)ctx; return ++*n >= 3 ? ctx : NULL; }
static int stepped;
static void count_sleep(int ms) { stepped += ms; }
int main(int argc, char **argv) {
const char *c = argc > 1 ? argv[1] : "";
if (!strcmp(c, "off")) { /* HOS_COMMIT_WAIT_MS=0: NULL at once, and the notice */
refuse = 1;
CHECK(hos_xmalloc(64) == NULL && slept_ms == 0 && asked == 1 && boxes == 1);
} else {
/* the loop alone: the third try gives it */
int n = 0, waited = -1;
CHECK(host_alloc_retry(count_try, &n, 100, count_sleep, &waited) == &n && n == 3 && waited == 150 && stepped == 150);
n = -1000, stepped = 0;
CHECK(host_alloc_retry(count_try, &n, 4, count_sleep, &waited) == NULL && waited == 200 && stepped == 200);
CHECK(host_alloc_retry(count_try, &n, 0, count_sleep, &waited) == NULL && waited == 0);
/* nothing refused: nothing slept, nothing said */
void *a = hos_xmalloc(100), *b = hos_xcalloc(10, 10), *r = hos_xrealloc(NULL, 50);
CHECK(a && b && r && slept_ms == 0 && logs == 0 && asked == 3);
memset(r, 0x5a, 50);
for (int i = 0; i < 100; i++) CHECK(((unsigned char *)b)[i] == 0);
/* refused three times (the first try and two more), then made */
refuse = 3;
void *m = hos_xmalloc(4096);
CHECK(m && slept_ms == 150 && logs == 1 && strstr(last_log, "made after 150 ms") && boxes == 0);
refuse = 2, slept_ms = 0;
unsigned char *z = (unsigned char *)hos_xcalloc(8, 32);
CHECK(z && slept_ms == 100 && z[0] == 0 && z[255] == 0);
/* a refused realloc keeps its block and gives its bytes on once it is made */
refuse = 4, slept_ms = 0;
unsigned char *g = (unsigned char *)hos_xrealloc(r, 5000);
CHECK(g && slept_ms == 200 && g[0] == 0x5a && g[49] == 0x5a);
/* a size of 0 is the C library's own answer, never waited for */
slept_ms = 0, refuse = 0;
free(hos_xmalloc(0)), free(hos_xcalloc(0, 16)), free(hos_xcalloc(16, 0));
CHECK(slept_ms == 0);
/* refused for good: NULL after the 5 s, the notice once however many fail */
refuse = 1 << 30, logs = 0;
CHECK(hos_xmalloc(1 << 20) == NULL && slept_ms == 5000 && boxes == 1 && logs == 2 && strstr(last_log, "out of memory"));
slept_ms = 0;
CHECK(hos_xcalloc(4, 4) == NULL && hos_xrealloc(g, 9000) == NULL && slept_ms == 10000 && boxes == 1);
CHECK(g[0] == 0x5a); /* (the block a refused realloc was given is still its own) */
free(a), free(b), free(m), free(z), free(g);
}
if (!fails) printf("PASS %s\n", c);
return fails != 0;
}
'''
NEW = r'''
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <new>
#include <string>
#include <thread>
#include "new_wait.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
namespace mkwvk {
static std::atomic<int> slept_ms{0}, logs{0};
static void Sleep(int ms) { slept_ms += ms; }
static void logf(const char*, ...) { logs++; }
static bool known = true;
static uint64_t left = 1ull << 30; // (1 GiB of commit left: at the limit)
static bool new_commit_left(uint64_t* avail) { *avail = left; return known; }
/* REFUSED */
} // namespace mkwvk
using namespace mkwvk;
static volatile size_t huge = ~size_t(0) / 4; // (no memory can hold it: every such `new` is refused)
// 1: bad_alloc thrown; the sleeps it made are in slept_ms
static int refused_new() {
try {
::operator delete(::operator new(huge)); // (the function itself: a new-expression's allocation may be left out by the compiler)
return 0;
} catch (const std::bad_alloc&) {
return 1;
}
}
int main(int argc, char** argv) {
const std::string c = argc > 1 ? argv[1] : "";
using namespace new_wait;
std::set_new_handler(new_refused);
if (c == "off") { // HOS_COMMIT_WAIT_MS=0
CHECK(refused_new() == 1 && slept_ms.load() == 0);
} else {
// the rule
CHECK(kNear == (4ull << 30) && kStepMs == 50);
CHECK(wait_ms(nullptr) == 5000 && wait_ms("") == 5000 && wait_ms("0") == 0 && wait_ms("-1") == 0 && wait_ms("300") == 300 && wait_ms("999999") == 60000);
CHECK(waits(true, 1ull << 30, 0, 5000, 0) && waits(true, kNear - 1, 4950, 5000, 0));
CHECK(!waits(true, kNear, 0, 5000, 0) && !waits(true, 64ull << 30, 0, 5000, 0)); // (4 GiB or more left: not the limit's)
CHECK(!waits(false, 0, 0, 5000, 0)); // (the commit not known)
CHECK(!waits(true, 0, 5000, 5000, 0) && !waits(true, 0, 0, 0, 0)); // (its time used up; no wait asked)
CHECK(!waits(true, 0, 0, 5000, 1)); // (a NoWait scope)
// the handler as the runtime calls it: one step slept and back for another try, while it waits
new_refused(), new_refused(), new_refused();
CHECK(slept_ms.load() == 150 && t_waited_ms == 150 && logs.load() == 3);
t_waited_ms = 0, slept_ms = 0;
// a `new` that cannot be made, at the limit: 5 s of tries, then it throws; this thread's next one throws at once
CHECK(refused_new() == 1 && slept_ms.load() == 5000 && t_waited_ms == 5000);
CHECK(refused_new() == 1 && slept_ms.load() == 5000);
// another thread has its own 5 s
std::thread([] { CHECK(t_waited_ms == 0 && refused_new() == 1 && t_waited_ms == 5000); }).join();
CHECK(slept_ms.load() == 10000 && logs.load() == 8 + 0); // (the first 8 waits are said)
// far from the limit, and the commit not known: at once
t_waited_ms = 0, slept_ms = 0, left = 8ull << 30;
CHECK(refused_new() == 1 && slept_ms.load() == 0);
left = 1ull << 30, known = false;
CHECK(refused_new() == 1 && slept_ms.load() == 0);
known = true;
// a nothrow new: null at once in a NoWait scope; like any other outside one
{
NoWait at_once;
CHECK(t_no_wait == 1);
{ NoWait inner; CHECK(t_no_wait == 2); }
CHECK(::operator new(huge, std::nothrow) == nullptr && slept_ms.load() == 0);
}
CHECK(t_no_wait == 0);
CHECK(::operator new(huge, std::nothrow) == nullptr && slept_ms.load() == 5000);
}
if (!fails) std::printf("PASS %s\n", c.c_str());
return fails ? 1 : 0;
}
'''
# (file, what must be there once: the host's tables that end the game on NULL)
HOST_SITES = [
("nvn_native.c", "if (c > POOL_MAX) return hos_xmalloc(n);"),
("nvn_native.c", "return b ? b : hos_xmalloc((size_t)1 << c);"),
("nvn_native.c", "cmd_rec *k = (cmd_rec *)hos_xcalloc((size_t)1 << CMD_CHUNK_BITS, sizeof *k);"),
("nvn_native.c", "cmd_free = (uint32_t *)hos_xrealloc(cmd_free, cap_cmd_free * sizeof *cmd_free);"),
("nvn_native.c", "uint64_t *t = (uint64_t *)hos_xcalloc(cap, sizeof *t);"),
("nvn_native.c", "live_table *pub = (live_table *)hos_xmalloc(sizeof *pub);"),
("nvn_native.c", "dead = (dead_copy *)hos_xrealloc(dead, cap_dead * sizeof *dead);"),
("nvn_native.c", "nvn_tex **t = (nvn_tex **)hos_xcalloc(n, sizeof *t);"),
("nn_video.c", "host_objs = (HostObj *)hos_xrealloc(host_objs, cap_host_objs * sizeof *host_objs);"),
("nn_video.c", "Pool *o = (Pool *)hos_xcalloc(1, sizeof *o);"),
]
def read_sources():
glue = (HOST / "mkw_glue.c").read_text(encoding="utf-8")
for name, needle in HOST_SITES:
text = (HOST / name).read_text(encoding="utf-8")
if text.count(needle) != 1 or '#include "host_alloc.h"' not in text:
fail("host/%s: %r is not there once: that table no longer waits at the commit limit" % (name, needle))
for path in sorted(VK.glob("*.cpp")) + sorted(VK.glob("*.h")):
if path.name == "new_wait.h":
continue
lines = path.read_text(encoding="utf-8").split("\n")
for i, line in enumerate(lines):
if "std::nothrow" in line and not line.lstrip().startswith("//"):
if not any("new_wait::NoWait at_once;" in before for before in lines[max(0, i - 2):i]):
fail("gpu/vk/%s:%d: a nothrow new outside a NoWait scope: at the commit limit it waits 5 s for a null it has "
"its own answer to" % (path.name, i + 1))
main = (VK / "vk_main.cpp").read_text(encoding="utf-8")
init = block(main, "const MkwVkRenderer* mkwvk_init(const MkwVkHost* h) {")
if "std::set_new_handler(new_refused);" not in init:
fail("vk_main.cpp mkwvk_init does not set the renderer's new handler")
refused = block(main, "void new_refused() {")
for need in ('new_wait::wait_ms(getenv("HOS_COMMIT_WAIT_MS"))', "throw std::bad_alloc();", "Sleep(new_wait::kStepMs);",
"new_wait::waits(known, avail, new_wait::t_waited_ms, max_ms, new_wait::t_no_wait)"):
if need not in refused:
fail("vk_main.cpp new_refused: %r is not there" % need)
if "ms.ullAvailPageFile" not in block(main, "bool new_commit_left(uint64_t* avail) {"):
fail("vk_main.cpp new_commit_left does not read the system's commit left")
commit = block(glue, "int hos_commit(uint64_t addr, uint64_t size) {")
if "commit_err == ERROR_COMMITMENT_LIMIT && commit_wait(GP(lo), (SIZE_T)(hi - lo))" not in commit:
fail("mkw_glue.c hos_commit no longer waits out a refusal at the commit limit")
if "| `HOS_COMMIT_WAIT_MS=<ms>` |" not in (ROOT / "docs/development.md").read_text(encoding="utf-8"):
fail("docs/development.md: HOS_COMMIT_WAIT_MS is not in the switches table")
last = block(glue, "void *hos_xrealloc(void *p, size_t n) {")
alloc = glue[glue.index("typedef struct { int kind; void *p; size_t count, n; } HostAllocAsk;"):glue.index(last) + len(last)]
return {"@WAIT_MS@": block(glue, "int hos_commit_wait_ms(void) {"), "@COMMIT_WAIT@": block(glue, "static int commit_wait(void *p, SIZE_T size) {"),
"@ALLOC@": re.sub(r"^DWORD WINAPI hos_out_of_memory_box\(LPVOID\);$", "static DWORD hos_out_of_memory_box(LPVOID p) { (void)p; return 0; }",
alloc, flags=re.M)}, refused
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cc", default="clang")
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
try:
pieces, refused = read_sources()
except (ValueError, OSError) as e:
fail("a text this test reads is not there (%s)" % e)
env = {k: v for k, v in os.environ.items() if k != "HOS_COMMIT_WAIT_MS"}
with tempfile.TemporaryDirectory(prefix="commit-limit-") as tmp:
def build(name, text, cxx=False):
for mark, piece in pieces.items():
text = text.replace(mark, piece)
src, exe = Path(tmp) / (name + (".cpp" if cxx else ".c")), Path(tmp) / (name + ".exe")
src.write_text(text, encoding="utf-8")
command = [args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-pthread", "-I" + str(VK)] if cxx else \
[args.cc, "-std=gnu17", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", "-Wno-unused-variable", "-Wno-unknown-warning-option",
"-Wno-unused-but-set-variable", "-Wno-unused-but-set-global", "-I" + str(HOST)]
built = subprocess.run(command + [str(src), "-o", str(exe)], capture_output=True, text=True)
if built.returncode:
print(built.stdout + built.stderr)
fail("the %s fixture did not build" % name)
return exe
def run(exe, *case, switch=None):
case_env = dict(env) if switch is None else dict(env, HOS_COMMIT_WAIT_MS=switch)
done = subprocess.run([str(exe), *case], env=case_env, capture_output=True, text=True, timeout=120)
if done.returncode or "PASS" not in done.stdout:
print(done.stdout[-3000:] + done.stderr[-1000:])
fail("%s %s, HOS_COMMIT_WAIT_MS %s" % (exe.stem, " ".join(case), "not set" if switch is None else switch))
commit = build("commit", COMMIT)
run(commit, "numbers")
run(commit, "made")
run(commit, "never", "5000")
run(commit, "other")
run(commit, "off", switch="0")
run(commit, "never", "200", switch="200")
run(commit, "never", "60000", switch="999999")
print("PASS: the game's commits: tried again every 50 ms for HOS_COMMIT_WAIT_MS, not at 0, not for another error")
alloc = build("alloc", ALLOC)
run(alloc, "wait")
run(alloc, "off", switch="0")
print("PASS: the host's allocations: the same wait, then NULL and the notice once; a size of 0 and the switch at 0 never wait")
new = build("new", NEW.replace("/* REFUSED */", refused), cxx=True)
run(new, "wait")
run(new, "off", switch="0")
print("PASS: the renderer's new: 5 s a thread at the limit, at once with 4 GiB left or in a NoWait scope, and with the switch at 0")
print("PASS: a refusal at Windows' commit limit is waited out in all three places, for HOS_COMMIT_WAIT_MS")
if __name__ == "__main__":
main()

498
tests/test_commit_room.py Normal file
View File

@@ -0,0 +1,498 @@
"""The textures near Windows' commit limit, and the page files' room to grow counted as free commit (gpu/vk/commit_room.h,
vk_texture.cpp commit_now, page_file_growth and texture_frame's cut; no game, no GPU).
(Triforce, 11-22, the performance audit's OS-05, after Source A's.) Off unless RX_VK_TEX_COMMIT_RESERVE_MB is set (or, for the
growth alone, RX_VK_TEX_COMMIT_GROWTH=1): with neither, nothing changes.
- the rules: the switches' values; commit_low's line, which must agree with tex_pressure::commit_low over a grid of free
commit, limits and its switch; when the growth is read; the cap (the shortfall under the reserve off what the textures
hold, 1 GB at least, none with no reserve or the reserve free);
- the page files' records walked over made-up buffers laid out as 64-bit Windows gives them: a page file that may grow,
one of fixed size, one shrunk, the growth capped by its drive's free space, two on one drive capped together, two
drives apart, a name with no drive, a lower-case drive, a buffer too short; a record whose next one lies outside the
buffer, overlaps it, or whose name lies outside, ends the walk with what came before counted; a drive's free space is
asked only for drives with growth;
- commit_now, cut out of vk_texture.cpp over a scripted GlobalMemoryStatusEx and a stand-in growth, a process a case (the
switches are read once): with no switch the growth is never asked and the answer is Windows' as before; with a reserve
it is asked only while less is free than the reserve or commit_low's line, and added to the free commit and the limit;
RX_VK_TEX_COMMIT_GROWTH=0 never asks, =1 asks under commit_low's line without a reserve, and not with that line off;
- page_file_growth compiled against Windows and run on this PC (NtQuerySystemInformation and the drives' free space, no
network): it answers, and a second ask within the second gives the same answer without asking again;
- texture_frame's cut, cut out and run against stand-ins: no reserve, no look at the commit; the reserve free, nothing
cut; short of it, the budget and the rules' budget held to what the textures hold less the shortfall, 1 GB at least,
a lower budget kept; Linux (commit not known) nothing; the "commit limit near" line on a change and every 600 presents;
- mutations of the cut, of commit_now and of the header must each fail; the sources: the cut after the 97% rule and
before rule_to, the growth in commit_now after Windows' numbers, the switches table.
python -I -B tests/test_commit_room.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
CUT = "if (const uint64_t reserve = commit_reserve()) {"
def fail(message):
raise SystemExit("FAIL: " + message)
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 in_order(where, text, needles):
at = [text.find(n) for n in needles]
if -1 in at or at != sorted(at):
fail(where + ": not in this order: " + " / ".join(needles))
RULES = r'''
#include <cstdio>
#include <cstring>
#include "commit_room.h"
#include "tex_pressure.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace commit_room;
static const uint64_t MB = 1ull << 20, GB = 1ull << 30;
// a buffer of records as 64-bit Windows lays them out, the names after them
struct Buf {
alignas(8) uint8_t b[2048] = {};
size_t len = 0, names = 1024;
const uint16_t* name(const char* s) {
uint16_t* at = (uint16_t*)(b + names);
size_t n = std::strlen(s);
for (size_t i = 0; i < n; i++) at[i] = (uint16_t)s[i];
names += n * 2 + 8;
return at;
}
// a record at `at`: its next, its size and the size it may grow to (pages), its name
void put(size_t at, uint32_t next, uint32_t total, uint32_t most, const char* file, const uint16_t* where = nullptr) {
PageFileRecord r{};
r.next = next, r.total_pages = total, r.max_pages = most, r.min_pages = total;
if (file) {
r.name = where ? where : name(file);
r.name_bytes = r.name_room = (uint16_t)(std::strlen(file) * 2);
}
std::memcpy(b + at, &r, sizeof r);
len = names;
}
};
static uint64_t free_c, free_d;
static int asked_c, asked_d, asked_other;
static uint64_t free_of(char d) {
if (d == 'C') return asked_c++, free_c;
if (d == 'D') return asked_d++, free_d;
return asked_other++, 0;
}
static uint64_t walk(const Buf& B) { return growth(B.b, B.len, 4096, free_of); }
static void drives(uint64_t c, uint64_t d) { free_c = c, free_d = d, asked_c = asked_d = asked_other = 0; }
int main() {
// ---- the switches ----
CHECK(reserve_of(nullptr) == 0 && reserve_of("") == 0 && reserve_of("0") == 0 && reserve_of("x") == 0 && reserve_of("-5") == 0);
CHECK(reserve_of("2560") == 2560 * MB && reserve_of("1") == MB && reserve_of("999999") == 65536 * MB);
CHECK(!growth_on(nullptr, 0) && growth_on(nullptr, 2560 * MB) && !growth_on("", 0) && growth_on("", MB)); // (unset: with a reserve)
CHECK(!growth_on("0", 2560 * MB) && !growth_on("0", 0) && growth_on("1", 0) && growth_on("1", MB) && !growth_on("x", MB));
// ---- commit_low's line, as tex_pressure::commit_low has it ----
CHECK(low_line(0, -1) == 0 && low_line(32 * GB, 0) == 0);
CHECK(low_line(32 * GB, -1) == 4096 * MB && low_line(64 * GB, -1) == 64 * GB / 10 && low_line(32 * GB, 6000) == 6000 * MB);
for (uint64_t limit : {0ull, 8 * GB, 16 * GB, 40 * GB, 41 * GB, 64 * GB, 128 * GB})
for (int64_t want : {-1ll, 0ll, 1ll, 3000ll, 4096ll, 9000ll})
for (uint64_t avail = 0; avail <= 20 * GB; avail += 256 * MB) {
const uint64_t low = low_line(limit, want);
if (tex_pressure::commit_low(avail, limit, want) != (low && avail < low)) {
std::printf("FAIL: low_line(%llu MB, %lld) disagrees with tex_pressure::commit_low at %llu MB free\n",
(unsigned long long)(limit / MB), (long long)want, (unsigned long long)(avail / MB));
fails++;
limit = 1ull << 62, avail = 1ull << 62;
}
}
// ---- when the growth is read: counted, and under the higher of the reserve and the line ----
CHECK(!read_growth(false, 0, 2560 * MB, 4096 * MB));
CHECK(read_growth(true, 2000 * MB, 2560 * MB, 0) && !read_growth(true, 2560 * MB, 2560 * MB, 0));
CHECK(read_growth(true, 3000 * MB, 2560 * MB, 4096 * MB) && !read_growth(true, 4096 * MB, 2560 * MB, 4096 * MB));
CHECK(!read_growth(true, 0 + 1, 0, 0)); // (no reserve, the line off: never)
// ---- the cap ----
CHECK(cap(4 * GB, 1024 * MB, 0) == UINT64_MAX); // (no reserve)
CHECK(cap(4 * GB, 2560 * MB, 2560 * MB) == UINT64_MAX && cap(4 * GB, 9 * GB, 2560 * MB) == UINT64_MAX);
CHECK(cap(4 * GB, 1024 * MB, 2560 * MB) == 4 * GB - 1536 * MB); // (the shortfall off what they hold)
CHECK(cap(1500 * MB, 1024 * MB, 2560 * MB) == GB); // (1 GB at least)
CHECK(cap(4 * GB, 0, 2560 * MB) == 4 * GB - 2560 * MB && cap(2 * GB, 0, 2560 * MB) == GB && cap(0, 0, 2560 * MB) == GB);
// ---- the drive of a name ----
{
const uint16_t a[] = {'\\', '?', '?', '\\', 'C', ':', '\\', 'p'}, b[] = {'\\', '?', '?', '\\', 'd', ':', '\\'}, c[] = {'\\', 'D', 'e', 'v'};
CHECK(drive_of(a, 8) == 'C' && drive_of(b, 7) == 'D' && drive_of(c, 4) == 0 && drive_of(a, 5) == 0);
}
// ---- the records ----
{ // one that may grow by 2000 pages, on a drive with room
Buf B;
B.put(0, 0, 1000, 3000, "\\??\\C:\\pagefile.sys");
drives(100 * GB, 0);
CHECK(walk(B) == 2000ull * 4096 && asked_c == 1 && asked_d == 0 && asked_other == 0);
drives(MB, 0);
CHECK(walk(B) == MB); // (capped by the drive's free space)
}
{ // fixed, and shrunk below what it may be: nothing
Buf B;
B.put(0, 40, 3000, 3000, "\\??\\C:\\pagefile.sys");
B.put(40, 0, 3000, 2000, "\\??\\D:\\pagefile.sys");
drives(100 * GB, 100 * GB);
CHECK(walk(B) == 0 && asked_c == 0 && asked_d == 0); // (no drive asked without growth)
}
{ // two on C capped together, one on D apart
Buf B;
B.put(0, 40, 1000, 3000, "\\??\\C:\\pagefile.sys");
B.put(40, 40, 0, 1000, "\\??\\C:\\swap\\second.sys");
B.put(80, 0, 0, 500, "\\??\\d:\\pagefile.sys");
drives(10 * MB, 100 * GB);
CHECK(walk(B) == 10 * MB + 500ull * 4096 && asked_c == 1 && asked_d == 1);
drives(100 * GB, MB);
CHECK(walk(B) == 3000ull * 4096 + MB);
}
{ // a name with no drive counts nothing, and the walk goes on
Buf B;
B.put(0, 40, 0, 1000, "\\Device\\HarddiskVolume3\\pagefile.sys");
B.put(40, 0, 0, 1000, "\\??\\C:\\pagefile.sys");
drives(100 * GB, 0);
CHECK(walk(B) == 1000ull * 4096 && asked_other == 0);
}
{ // the next record outside the buffer: the walk ends, the first counted
Buf B;
B.put(0, 4000, 0, 1000, "\\??\\C:\\pagefile.sys");
drives(100 * GB, 0);
CHECK(walk(B) == 1000ull * 4096);
B.put(0, 8, 0, 1000, "\\??\\C:\\pagefile.sys"); // (one that would overlap it)
B.put(40, 0, 0, 7000, "\\??\\C:\\pagefile.sys");
CHECK(walk(B) == 1000ull * 4096);
}
{ // a name outside the buffer: that record and those after it count nothing
Buf B;
static const uint16_t elsewhere[] = {'\\', '?', '?', '\\', 'C', ':', '\\', 'p', 'f'};
B.put(0, 40, 0, 1000, "\\??\\C:\\pagefile.sys");
B.put(40, 40, 0, 2000, "\\??\\C:\\pf", elsewhere);
B.put(80, 0, 0, 4000, "\\??\\C:\\pagefile.sys");
drives(100 * GB, 0);
CHECK(walk(B) == 1000ull * 4096);
uint16_t* near_end = (uint16_t*)(B.b + B.len - 4); // (a name that starts inside and runs past the end)
near_end[0] = 'C', near_end[1] = ':';
B.put(40, 40, 0, 2000, "\\??\\C:\\pf", near_end);
B.len = B.names;
CHECK(walk(B) == 1000ull * 4096);
}
{ // too short for a record, or empty
Buf B;
B.put(0, 0, 0, 1000, "\\??\\C:\\pagefile.sys");
drives(100 * GB, 0);
CHECK(growth(B.b, 39, 4096, free_of) == 0 && growth(B.b, 0, 4096, free_of) == 0 && asked_c == 0);
}
if (!fails) std::printf("PASS rules\n");
return fails ? 1 : 0;
}
'''
NOW = r'''
#include <windows.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include "commit_room.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
static const uint64_t MB = 1ull << 20, GB = 1ull << 30;
// (Windows' numbers as the case sets them, and the page files' growth as a stand-in that counts its asks)
static uint64_t win_avail, win_limit;
static BOOL fake_status(LPMEMORYSTATUSEX m) {
m->ullAvailPageFile = win_avail, m->ullTotalPageFile = win_limit;
return TRUE;
}
#define GlobalMemoryStatusEx fake_status
static int growth_asks;
static uint64_t page_file_growth_fake() { return growth_asks++, 3 * GB; }
/* CUT */
static bool look(uint64_t avail_mb, uint64_t limit_mb, uint64_t* avail, uint64_t* limit) {
win_avail = avail_mb * MB, win_limit = limit_mb * MB, growth_asks = 0;
return commit_now(avail, limit);
}
int main(int argc, char** argv) {
const std::string c = argc > 1 ? argv[1] : "";
uint64_t a = 0, l = 0;
if (c == "unset") { // (no switch: Windows' numbers, the growth never asked, however little is free)
for (uint64_t free_mb : {0ull, 100ull, 2000ull, 4000ull, 9000ull}) {
CHECK(look(free_mb, 40000, &a, &l) && a == free_mb * MB && l == 40000 * MB && growth_asks == 0 && commit_grown == 0);
}
} else if (c == "reserve") { // (RX_VK_TEX_COMMIT_RESERVE_MB=2560: under the higher of it and commit_low's line, 4 GB here)
CHECK(look(9000, 40000, &a, &l) && a == 9000 * MB && l == 40000 * MB && growth_asks == 0);
CHECK(look(4096, 40000, &a, &l) && growth_asks == 0);
CHECK(look(4000, 40000, &a, &l) && a == 4000 * MB + 3 * GB && l == 40000 * MB + 3 * GB && growth_asks == 1 && commit_grown == 3 * GB);
CHECK(look(1000, 40000, &a, &l) && a == 1000 * MB + 3 * GB && growth_asks == 1);
CHECK(look(9000, 40000, &a, &l) && a == 9000 * MB && commit_grown == 0); // (the last growth not carried over)
} else if (c == "reserve-big") { // (a reserve above commit_low's line: the reserve's)
CHECK(look(5000, 40000, &a, &l) && growth_asks == 1 && a == 5000 * MB + 3 * GB);
CHECK(look(6000, 40000, &a, &l) && growth_asks == 1);
CHECK(look(8000, 40000, &a, &l) && growth_asks == 0 && a == 8000 * MB);
} else if (c == "never") { // (RX_VK_TEX_COMMIT_GROWTH=0, a reserve set: never)
CHECK(look(100, 40000, &a, &l) && a == 100 * MB && l == 40000 * MB && growth_asks == 0);
} else if (c == "growth-alone") { // (RX_VK_TEX_COMMIT_GROWTH=1, no reserve: under commit_low's line)
CHECK(look(5000, 40000, &a, &l) && growth_asks == 0);
CHECK(look(4000, 40000, &a, &l) && growth_asks == 1 && a == 4000 * MB + 3 * GB);
} else if (c == "line-off") { // (RX_VK_TEX_COMMIT_GROWTH=1, RX_VK_TEX_COMMIT_FREE_MB=0, no reserve: nothing to read it for)
CHECK(look(100, 40000, &a, &l) && growth_asks == 0 && a == 100 * MB);
} else {
std::printf("FAIL: unknown case\n");
fails++;
}
if (!fails) std::printf("PASS %s\n", c.c_str());
return fails ? 1 : 0;
}
'''
NOW_CASES = [
("unset", {}), ("reserve", {"RX_VK_TEX_COMMIT_RESERVE_MB": "2560"}), ("reserve-big", {"RX_VK_TEX_COMMIT_RESERVE_MB": "8000"}),
("never", {"RX_VK_TEX_COMMIT_RESERVE_MB": "2560", "RX_VK_TEX_COMMIT_GROWTH": "0"}),
("growth-alone", {"RX_VK_TEX_COMMIT_GROWTH": "1"}),
("line-off", {"RX_VK_TEX_COMMIT_GROWTH": "1", "RX_VK_TEX_COMMIT_FREE_MB": "0"}),
]
REAL = r'''
#include <windows.h>
#include <chrono>
#include <cstdio>
#include <vector>
#include "commit_room.h"
/* GROWTH */
int main() {
const auto t0 = std::chrono::steady_clock::now();
const uint64_t g = page_file_growth();
const auto t1 = std::chrono::steady_clock::now();
const uint64_t again = page_file_growth();
const auto t2 = std::chrono::steady_clock::now();
const double first_ms = std::chrono::duration<double, std::milli>(t1 - t0).count(), again_ms = std::chrono::duration<double, std::milli>(t2 - t1).count();
std::printf("page files' room to grow on this PC: %llu MB (asked in %.3f ms, again in %.4f ms)\n", (unsigned long long)(g >> 20), first_ms, again_ms);
if (again != g) { std::printf("FAIL: a second ask within the second gave another answer\n"); return 1; }
if (g > (1ull << 50)) { std::printf("FAIL: an answer past any drive\n"); return 1; }
std::printf("PASS real\n");
return 0;
}
'''
FRAME = r'''
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include "commit_room.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
static const uint64_t MB = 1ull << 20, GB = 1ull << 30;
static uint64_t reserve_now, now_avail, now_limit = 40 * GB, commit_grown, frame_no;
static bool now_known = true;
static int asks, lines;
static char last_line[600];
static uint64_t commit_reserve() { return reserve_now; }
static bool commit_now(uint64_t* avail, uint64_t* limit) {
asks++;
if (!now_known) return false;
*avail = now_avail, *limit = now_limit;
return true;
}
static void logf(const char* f, ...) {
va_list ap;
va_start(ap, f);
vsnprintf(last_line, sizeof last_line, f, ap);
va_end(ap);
lines++;
}
// one look's cut: budget and rule_budget as texture_frame has them after the 97% rule
static void look(uint64_t& budget, uint64_t& rule_budget, uint64_t total_bytes) {
/* CUT */
}
static uint64_t b, r;
static void at(uint64_t frame, uint64_t free_mb, uint64_t total_mb, uint64_t budget_mb = 0, uint64_t rule_mb = 6000) {
frame_no = frame, now_avail = free_mb * MB, asks = 0;
b = budget_mb ? budget_mb * MB : UINT64_MAX, r = rule_mb * MB;
look(b, r, total_mb * MB);
}
int main() {
// no reserve: no look at the commit, nothing cut, no line
at(10, 100, 4096);
CHECK(asks == 0 && b == UINT64_MAX && r == 6000 * MB && lines == 0);
reserve_now = 2560 * MB;
// the reserve free: nothing cut
at(20, 3000, 4096);
CHECK(asks == 1 && b == UINT64_MAX && r == 6000 * MB && lines == 0);
// short by 1536 MB: what the textures hold less that, on both budgets, one line
at(30, 1024, 4096);
CHECK(b == 2560 * MB && r == 2560 * MB && lines == 1 && std::strstr(last_line, "vk: commit limit near: 1024 MB of 40960 free"));
// still short: no line until a 600th present, then one
at(40, 1024, 4096);
CHECK(b == 2560 * MB && lines == 1);
at(600, 1024, 4096);
CHECK(lines == 2 && std::strstr(last_line, "near:"));
// a budget already lower is kept; the floor
at(610, 1024, 4096, 1000, 900);
CHECK(b == 1000 * MB && r == 900 * MB);
at(620, 1024, 1500);
CHECK(b == GB && r == GB);
at(630, 0, 900);
CHECK(b == GB && r == GB); // (1 GB at least: more than they hold, nothing goes)
// free again: nothing cut, one line
at(640, 3000, 4096);
CHECK(b == UINT64_MAX && r == 6000 * MB && lines == 3 && std::strstr(last_line, "no longer near"));
// the commit not known (Linux): nothing
now_known = false;
at(650, 0, 4096);
CHECK(b == UINT64_MAX && r == 6000 * MB && lines == 3);
if (!fails) std::printf("PASS frame\n");
return fails ? 1 : 0;
}
'''
# (each: where, what is taken out, the text, what replaces it)
MUTATIONS = [
("frame", "the budget capped", " budget = std::min(budget, cap);\n", ""),
("frame", "the rules' budget capped", " rule_budget = std::min(rule_budget, cap);\n", ""),
("frame", "the commit not known: no cut", "commit_now(&avail, &limit) ? commit_room::cap(total_bytes, avail, reserve) : UINT64_MAX",
"(commit_now(&avail, &limit), commit_room::cap(total_bytes, avail, reserve))"),
("now", "the growth added", "*avail += commit_grown, *limit += commit_grown;", "(void)0;"),
("now", "the growth only where it can matter", "if (commit_room::read_growth(", "if (true || commit_room::read_growth("),
("header", "the floor", "return std::max(kFloor, total > shortfall ? total - shortfall : 0);", "return total > shortfall ? total - shortfall : 0;"),
("header", "the drive's cap", "sum += std::min<uint64_t>(by_drive[d], free_of((char)('A' + d)));", "sum += by_drive[d];"),
("header", "a name outside the buffer ends the walk", "if (n < base || n - base > len || r.name_bytes > len - (n - base)) break;",
"if (n < base || n - base > len) break;"),
("header", "no growth unset without a reserve", "if (!env || !*env) return reserve != 0;", "if (!env || !*env) return true;"),
]
def read_sources():
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
if '#include "commit_room.h"' not in tex:
fail("vk_texture.cpp does not include commit_room.h")
now = block(tex, "bool commit_now(uint64_t* avail, uint64_t* limit) {")
in_order("vk_texture.cpp commit_now (Windows' numbers, then the growth where it can matter, added to both)", now,
["*avail = ms.ullAvailPageFile, *limit = ms.ullTotalPageFile;",
"if (commit_room::read_growth(commit_growth_on(), *avail, commit_reserve(), commit_room::low_line(*limit, commit_free_mb()))) {",
"commit_grown = page_file_growth();", "*avail += commit_grown, *limit += commit_grown;", "return true;"])
switches = {name: block(tex, sig) for name, sig in (("reserve", "uint64_t commit_reserve() {"), ("growth", "bool commit_growth_on() {"),
("free", "int64_t commit_free_mb() {"))}
for name, text, need in (("reserve", switches["reserve"], 'static const uint64_t v = commit_room::reserve_of(getenv("RX_VK_TEX_COMMIT_RESERVE_MB"));'),
("growth", switches["growth"], 'static const bool v = commit_room::growth_on(getenv("RX_VK_TEX_COMMIT_GROWTH"), commit_reserve());'),
("free", switches["free"], 'getenv("RX_VK_TEX_COMMIT_FREE_MB") ? atoll(getenv("RX_VK_TEX_COMMIT_FREE_MB")) : -1;')):
if need not in text:
fail("vk_texture.cpp: the %s switch is not read once as the rule says" % name)
growth = block(tex, "uint64_t page_file_growth() {")
for need in ("commit_room::kPageFileClass", "commit_room::growth(", "GetDiskFreeSpaceExW", "std::chrono::seconds(1)"):
if need not in growth:
fail("vk_texture.cpp page_file_growth: %r is not there" % need)
frame = block(tex, "void texture_frame(uint64_t completed) {")
if "static const int64_t want_mb = commit_free_mb();" not in frame:
fail("vk_texture.cpp texture_frame: commit_low's switch is not the one commit_now reads")
if frame.count(CUT) != 1:
fail("vk_texture.cpp texture_frame: the reserve's cut is not there once")
in_order("vk_texture.cpp texture_frame (the cut after the 97% rule, before rule_to and the pass)", frame,
["if (known && !by_blocks && used > allowed / 100 * 97 && !room_in_blocks) {",
"rule_budget = std::min(rule_budget, total_bytes > over ? total_bytes - over : 0);", CUT,
"const uint64_t rule_to =", "if (budget < evict_to) evict_to = hold_to = 0;",
"texture_sweep(completed, total_bytes <= budget, budget,"])
in_order("docs/development.md (the rows beside RX_VK_TEX_COMMIT_FREE_MB's)", dev,
["| `RX_VK_TEX_COMMIT_FREE_MB=<MB>` |", "| `RX_VK_TEX_COMMIT_RESERVE_MB=<MB>` |", "| `RX_VK_TEX_COMMIT_GROWTH=0` or `=1` |"])
now_cut = "\n".join([switches["reserve"], switches["growth"], switches["free"], "uint64_t commit_grown = 0;", now])
if now_cut.count("commit_grown = page_file_growth();") != 1:
fail("vk_texture.cpp commit_now: the growth is not asked once")
return block(frame, CUT), now_cut.replace("commit_grown = page_file_growth();", "commit_grown = page_file_growth_fake();"), growth
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
try:
cut, now, growth = read_sources()
header = (VK / "commit_room.h").read_text(encoding="utf-8")
except (ValueError, OSError) as e:
fail("a text this test reads is not there (%s)" % e)
env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_TEX_COMMIT_")}
with tempfile.TemporaryDirectory(prefix="commit-room-") as tmp:
tmpdir = Path(tmp)
def build(name, text, head=None):
inc = tmpdir / ("inc-" + name)
inc.mkdir()
(inc / "commit_room.h").write_text(head if head is not None else header, encoding="utf-8")
src, exe = tmpdir / (name + ".cpp"), tmpdir / (name + ".exe")
src.write_text(text, encoding="utf-8")
built = subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function",
"-Wno-unused-parameter", "-I" + str(inc), "-I" + str(VK), str(src), "-o", str(exe)], capture_output=True, text=True)
if built.returncode:
print(built.stdout + built.stderr)
fail("the fixture did not build (%s)" % name)
return exe
def run(exe, case=None, **switches):
try:
return subprocess.run([str(exe)] + ([case] if case else []), env=dict(env, **switches), capture_output=True, text=True, timeout=120)
except subprocess.TimeoutExpired:
return subprocess.CompletedProcess([], 1, "no end in 120 s", "")
def passes(exe, cases):
for case, switches in cases:
done = run(exe, case, **switches)
if done.returncode or "PASS" not in done.stdout:
return False, (done.stdout + done.stderr)[-2000:]
return True, ""
fixtures = {"rules": (RULES, None), "now": (NOW, now), "frame": (FRAME, cut)}
cases = {"rules": [(None, {})], "now": NOW_CASES, "frame": [(None, {})]}
def text_of(which, part):
body, cut_text = fixtures[which]
return body.replace("/* CUT */", part if part is not None else (cut_text or ""))
for which in ("rules", "now", "frame"):
ok, out = passes(build(which, text_of(which, None)), cases[which])
if not ok:
print(out)
fail(which)
print("PASS " + which + ("" if which != "now" else " (%d cases, each its own process)" % len(NOW_CASES)))
done = run(build("real", REAL.replace("/* GROWTH */", growth)))
print(done.stdout.strip())
if done.returncode or "PASS real" not in done.stdout:
fail("page_file_growth on this PC")
for i, (where, what, old, new) in enumerate(MUTATIONS):
if where == "header":
if header.count(old) != 1:
fail("mutation %r: not in commit_room.h once (update the test with the code)" % what)
ok = all(passes(build("m%d%s" % (i, w), text_of(w, None), header.replace(old, new)), cases[w])[0] for w in ("rules", "now", "frame"))
else:
part = fixtures[where][1]
if part.count(old) != 1:
fail("mutation %r: not in the cut code once (update the test with the code)" % what)
ok, _ = passes(build("m%d" % i, text_of(where, part.replace(old, new))), cases[where])
if ok:
fail("mutation %r: the fixtures still passed without it" % what)
print("PASS mutation: without %s a case fails" % what)
print("PASS: off unless set; the growth read only where it can matter and the walk bounded by the buffer; short of the "
"reserve, the textures held to what they hold less the shortfall, 1 GB at least")
if __name__ == "__main__":
main()

147
tests/test_compact_start.py Normal file
View File

@@ -0,0 +1,147 @@
"""The free space a VRAM compaction run starts from, by the card (gpu/vk/vram_compact.h default_min_free; the performance
audit's TEX-38, after Source A's; no game, no GPU).
The rule, compiled and run: a sixteenth of what the driver allows, 64 MB at least and 512 at most, 512 while nothing is
known; for cards from 2 to 32 GB as drivers report them (a little under the card's size) it is 512 from 8 GB up, exactly as
before, and less only under that; 192 MB still while the count is pressed. Each part taken out (a mutation) fails it. Then
the source: compact_step takes RX_VK_COMPACT_MB first and the rule otherwise, with the allowance of texture_frame's last
look; and the docs row.
python -I -B tests/test_compact_start.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
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 in_order(text, parts, where):
at = 0
for part in parts:
found = text.find(part, at)
if found < 0:
raise SystemExit("%s: %r is missing, or not after the part before it (update the test with the code)" % (where, part))
at = found + len(part)
RULE_TEST = r'''
#include <cstdio>
#include <cstdlib>
#include "vram_compact.h"
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); std::exit(1); } } while (0)
using namespace vram_compact;
int main() {
const uint64_t MB = 1ull << 20, GB = 1ull << 30;
// (the entry's four: not known, 512 MB, a 6 GB laptop's 5,233 MB, 16 GB)
CHECK(default_min_free(0) == 512 * MB);
CHECK(default_min_free(512 * MB) == 64 * MB);
CHECK(default_min_free(5233 * MB) >> 20 == 327);
CHECK(default_min_free(16 * GB) == 512 * MB);
// (cards from 2 to 32 GB, the allowance a driver gives a little under the card: 512 from 8 GB up, as before)
const struct { uint64_t card_gb, allowed_mb, want_mb; } cards[] = {
{2, 1843, 115}, {3, 2867, 179}, {4, 3686, 230}, {6, 5233, 327}, {6, 5734, 358}, {8, 7300, 456},
{8, 7782, 486}, {10, 9420, 512}, {11, 10900, 512}, {12, 11468, 512}, {16, 15564, 512}, {20, 19456, 512},
{24, 23552, 512}, {32, 31784, 512}, {32, 32607, 512},
};
for (const auto& c : cards) {
const uint64_t v = default_min_free(c.allowed_mb * MB);
if ((v >> 20) != c.want_mb) {
std::fprintf(stderr, "FAIL: a %llu GB card allowing %llu MB: %llu MB, want %llu\n", (unsigned long long)c.card_gb,
(unsigned long long)c.allowed_mb, (unsigned long long)(v >> 20), (unsigned long long)c.want_mb);
return 1;
}
CHECK(v >= 64 * MB && v <= 512 * MB);
if (c.allowed_mb >= 8192) CHECK(v == 512 * MB); // (this PC's 32 GB card and every card from 8 GB: today's 512)
CHECK(run_from(v, true) == (v < 192 * MB ? v : 192 * MB) && run_from(v, false) == v);
}
// (the bounds)
CHECK(default_min_free(1) == 64 * MB && default_min_free(1024 * MB) == 64 * MB && default_min_free(1040 * MB) == 65 * MB);
CHECK(default_min_free(8 * GB) == 512 * MB && default_min_free(8 * GB - 16 * MB) == 511 * MB && default_min_free(~0ull) == 512 * MB);
std::puts("PASS rule: a sixteenth of the allowance, 64 to 512 MB, 512 unknown; 512 on every card from 8 GB");
return 0;
}
'''
MUTATIONS = [
("512 while not known", " if (!allowed) return 512ull << 20;\n", ""),
("a sixteenth", "return std::clamp<uint64_t>(allowed / 16, 64ull << 20, 512ull << 20);",
"return std::clamp<uint64_t>(allowed / 8, 64ull << 20, 512ull << 20);"),
("64 MB at least", "return std::clamp<uint64_t>(allowed / 16, 64ull << 20, 512ull << 20);",
"return std::clamp<uint64_t>(allowed / 16, 1, 512ull << 20);"),
("512 MB at most", "return std::clamp<uint64_t>(allowed / 16, 64ull << 20, 512ull << 20);",
"return std::clamp<uint64_t>(allowed / 16, 64ull << 20, 4096ull << 20);"),
]
def check_source(tex, dev):
step = block(tex, "void compact_step(uint64_t completed) {")
in_order(step, ['static const uint64_t asked = (uint64_t)(getenv("RX_VK_COMPACT_MB") ? std::max(1, atoi(getenv("RX_VK_COMPACT_MB"))) : 0) << 20;',
"const uint64_t min_free = asked ? asked : vram_compact::default_min_free(compact.allowed);",
"free_bytes < vram_compact::run_from(min_free, compact.pressed))"], "vk_texture.cpp compact_step")
in_order(tex, ["const bool known = vram_usage(&used, &allowed, &block_free);",
"compact.pressed = known && vram_compact::pressed(used, allowed);", "compact.allowed = known ? allowed : 0;"],
"vk_texture.cpp texture_frame: the allowance of the last look")
if "uint64_t allowed = 0;" not in block(tex, "struct Compact {"):
raise SystemExit("vk_texture.cpp: struct Compact has no allowance")
if "default_min_free" not in dev or "RX_VK_COMPACT_MB=512" not in dev:
raise SystemExit("docs/development.md: the compaction's row does not say the start by the card, nor the switch back")
def build(cxx, d, name, src, include):
cpp, exe = d / (name + ".cpp"), d / (name + ".exe")
cpp.write_text(src, encoding="utf-8")
r = subprocess.run([cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(include), str(cpp), "-o", str(exe)],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
if r.returncode:
return None, r.stdout
return exe, ""
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--cxx", default=os.environ.get("CXX", "g++"))
a = ap.parse_args()
check_source((VK / "vk_texture.cpp").read_text(encoding="utf-8"), (ROOT / "docs" / "development.md").read_text(encoding="utf-8"))
print("PASS sources: compact_step takes RX_VK_COMPACT_MB first, the rule with the last look's allowance otherwise; the row")
header = (VK / "vram_compact.h").read_text(encoding="utf-8")
with tempfile.TemporaryDirectory(prefix="compact-start-") as tmp:
d = Path(tmp)
exe, out = build(a.cxx, d, "rule", RULE_TEST, VK)
if not exe:
raise SystemExit("rule: the build failed:\n" + out)
r = subprocess.run([str(exe)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=60)
if r.returncode:
raise SystemExit("the rule's test failed:\n" + r.stdout)
print(r.stdout.strip())
for i, (what, old, new) in enumerate(MUTATIONS):
if header.count(old) != 1:
raise SystemExit("mutation %r: its text is not once in vram_compact.h (update the test with the header)" % what)
inc = d / ("mutant%d" % i)
inc.mkdir()
(inc / "vram_compact.h").write_text(header.replace(old, new), encoding="utf-8")
exe, out = build(a.cxx, d, "mutant%d" % i, RULE_TEST, inc)
if exe:
r = subprocess.run([str(exe)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=60)
if r.returncode == 0:
raise SystemExit("mutation %r: the rule's test still passed without it" % what)
print("PASS mutations: %d parts of the rule, each taken out, fail its test" % len(MUTATIONS))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,166 @@
"""The compile stops when a compile raises (nx2/build.py main's compile loop, MemoryGate.stop; no compiler, no game).
(Triforce, 11-21, the performance audit's LIFT-31, after Source A's.) A compiler's error comes back as a value and is
tried again alone. An exception does not (a missing or blocked compiler, an object that cannot be written, a full disk,
Ctrl-C): it left the pool's block, and the block's exit ran every compile still queued first, the whole compile with
nothing said, before the build stopped.
Runs main's own statements, from "t0, failed =" to "if still", cut out through the AST as tests/test_compile_retry_jobs.py
does, and the real MemoryGate, with stand-in compiles:
1. one worker, 12 jobs, the first raises: the exception reaches the caller, and at most one more compile starts;
2. the real memory gate holding three workers back (no memory free): the first compile raises, the gate is stopped, at
most one of the three starts (the one that took the raising compile's lease first), and none is left holding a lease;
3. the gate alone: a waiter and a late comer both raise CompileStopped once it is stopped, and a gate not stopped gives
leases as before.
python -I -B tests/test_compile_stops_on_exception.py
"""
import argparse
import ast
import concurrent.futures
from contextlib import redirect_stdout
import io
import os
from pathlib import Path
import threading
import time
from types import SimpleNamespace
import unittest
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "nx2/build.py"
def actual(source):
"""main's compile statements as run_compile_jobs(), and the real MemoryGate and CompileStopped, in one scope"""
tree = ast.parse(source.read_text(encoding="utf-8"))
classes = [node for node in tree.body if isinstance(node, ast.ClassDef) and node.name in ("MemoryGate", "CompileStopped")]
main, = [node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == "main"]
start, = [i for i, node in enumerate(main.body) if isinstance(node, ast.Assign)
and any(isinstance(t, ast.Tuple) and [getattr(n, "id", None) for n in t.elts] == ["t0", "failed"]
for t in node.targets)]
end, = [i for i, node in enumerate(main.body) if i > start and isinstance(node, ast.If)
and isinstance(node.test, ast.Name) and node.test.id == "still"]
wrapper = ast.parse("def run_compile_jobs():\n return 0\n").body[0]
wrapper.body = main.body[start:end + 1] + wrapper.body
module = ast.fix_missing_locations(ast.Module(body=classes + [wrapper], type_ignores=[]))
scope = dict(os=os, time=time, concurrent=concurrent, threading=threading, GATE=None, COMPILER_MB=1000,
counted=False, expected={}, pending=[], eta_jobs=1, eta_left=lambda pending, jobs: 0)
exec(compile(module, str(source), "exec"), scope)
return scope
class CompileStopsOnException(unittest.TestCase):
def setUp(self):
self.scope = actual(SOURCE)
self.started, self.lock = [], threading.Lock()
def run_jobs(self):
with redirect_stdout(io.StringIO()):
return self.scope["run_compile_jobs"]()
def test_one_worker_stops_after_the_compile_that_raised(self):
jobs = [("cc", __file__, "obj%d.o" % i, [], b"") for i in range(12)]
def compile_one(job):
with self.lock:
self.started.append(job[2])
if job[2] == "obj0.o":
raise FileNotFoundError("authored: the compiler is gone")
time.sleep(0.05)
return job[1], 0.05, ""
self.scope.update(compile_one=compile_one, jobs=jobs, a=SimpleNamespace(jobs=1))
with self.assertRaisesRegex(FileNotFoundError, "the compiler is gone"):
self.run_jobs()
# (the worker may have taken the next job before the queue was cancelled: that one, no more. Before: all 12)
self.assertLessEqual(len(self.started), 2, self.started)
self.assertEqual(self.started[0], "obj0.o")
def test_workers_waiting_at_the_memory_gate_start_nothing(self):
gate = self.scope["MemoryGate"]()
gate.floor = 1
# (nothing free: with one compile running, every other waits at the gate)
self.scope.update(mem_need=lambda src: 400, free_mb=lambda: 0, rss_mb=lambda p: 0, GATE=gate)
waiting = threading.Event()
jobs = [("cc", __file__, "obj%d.o" % i, [], b"") for i in range(4)]
def compile_one(job): # (as build.py's: the lease first, then the compiler)
first = job[2] == "obj0.o"
if first:
lease = gate.enter(job[1])
for _ in range(400): # (until the other three wait at the gate)
if len(self.started) == 3:
break
time.sleep(0.005)
gate.leave(lease)
raise OSError("authored: the object cannot be written")
with self.lock:
self.started.append(job[2]) # (asked for its lease)
lease = gate.enter(job[1])
with self.lock:
self.started.append("compiling " + job[2])
time.sleep(0.3) # (a compile takes its time: the gate is stopped long before this one's lease is free)
gate.leave(lease)
return job[1], 0.3, ""
# (obj0 holds the one lease the floor gives before the others ask)
real_submit = concurrent.futures.ThreadPoolExecutor.submit
def submit(ex, fn, job):
future = real_submit(ex, fn, job)
if job[2] == "obj0.o":
for _ in range(400):
if gate.running:
break
time.sleep(0.005)
return future
concurrent.futures.ThreadPoolExecutor.submit = submit
try:
self.scope.update(compile_one=compile_one, jobs=jobs, a=SimpleNamespace(jobs=4))
with self.assertRaisesRegex(OSError, "cannot be written"):
self.run_jobs()
finally:
concurrent.futures.ThreadPoolExecutor.submit = real_submit
self.assertTrue(gate.stopped, "the gate was not told the build is stopping")
# (the raising compile's own lease is free before the exception reaches the loop: the waiter that takes it in
# between is the one compile that may still start. Before: all three)
self.assertLessEqual(len([s for s in self.started if s.startswith("compiling")]), 1, self.started)
self.assertEqual(len(gate.running), 0, "a stopped compile was left holding a lease")
waiting.set()
def test_a_stopped_gate_gives_no_lease(self):
Stopped = self.scope["CompileStopped"]
gate = self.scope["MemoryGate"]()
gate.floor = 1
self.scope.update(mem_need=lambda src: 400, free_mb=lambda: 0, rss_mb=lambda p: 0)
first = gate.enter("a.c") # (under the floor: at once, as before)
self.assertEqual(len(gate.running), 1)
raised = []
def waiter():
try:
gate.enter("b.c")
except Stopped:
raised.append(1)
t = threading.Thread(target=waiter)
t.start()
time.sleep(0.1)
self.assertTrue(t.is_alive(), "the waiter did not wait at the gate")
t0 = time.time()
gate.stop()
t.join(5)
self.assertFalse(t.is_alive())
self.assertLess(time.time() - t0, 0.9, "the waiter was not woken: it waited out its second")
self.assertEqual(raised, [1])
with self.assertRaises(Stopped): # (and one that comes after)
gate.enter("c.c")
self.assertEqual(len(gate.running), 1)
gate.leave(first)
self.assertEqual(len(gate.running), 0)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source", type=Path, default=SOURCE)
args, remaining = parser.parse_known_args()
SOURCE = args.source.resolve()
unittest.main(argv=[__file__, *remaining])

214
tests/test_compute_pin.py Normal file
View File

@@ -0,0 +1,214 @@
"""Compute programs held to 32 lanes only where they can tell (gpu/vk/compute_pin.h; no game, no GPU).
(Triforce, 11-22, the performance audit's PIPE-52, after Source A's and Source B's.) Every compute pipeline asked for
subgroups of 32 lanes. Mesa's Intel driver compiles such a program for 32 lanes only, minutes and gigabytes on the large
ones; most programs cannot tell how many lanes run beside them. On Mesa's Intel driver only the ones that can are pinned
now; every other driver keeps "all", its pipelines exactly as they were.
Compiles a small driver against the header and checks:
the mode unset: lanes on Mesa's Intel driver, all on any other; all / lanes / shared as written; an unknown value
is the driver's default;
all every program pinned, whatever it is;
lanes each of the six flags pins by itself (vote, lane mask, lane index, shuffles, FSWZADD, matrices); a
barrier of Subgroup scope pins, and so does a module nobody looked at (the default); shared memory pins at
1, 32 invocations, not at 33 or 64; a program with none of these is free; a program without a stage is
pinned;
shared as lanes, and shared memory pins at 64 and 4096 invocations too;
the scan a made-up module with OpControlBarrier at Subgroup scope: found; at Workgroup scope: not; none: not;
a scope that is no constant, a cut-off module, a wrong magic word: counted as one (a doubt pins);
compute_entry: a GLCompute entry point yes, a Fragment one no, no entry point no.
And reads the source: the four places that make a compute program's pipeline call pin_compute and no longer
pin_subgroup; build_graphics and the mesh fallback still call pin_subgroup; pin_compute with "all" is pin_subgroup with
the workgroup's width, before anything is asked or counted; the note is made in make_module between vk_shader.cpp's
"Outside the translation stamp" marks; and the switches table has the row.
python -I -B tests/test_compute_pin.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
DRIVER = r'''
#include "compute_pin.h"
#include <cstdio>
#include <vector>
using namespace compute_pin;
static int checks, fails;
static void want(const char* what, bool got, bool wanted) {
checks++;
if (got != wanted) fails++, printf("FAIL %s: %d, want %d\n", what, (int)got, (int)wanted);
}
// a program that sees nothing: its module looked at, no barrier, no shared memory
static Facts plain() {
Facts f;
f.has_stage = true, f.subgroup_barrier = false, f.invocations = 64;
return f;
}
static std::vector<uint32_t> module(uint32_t model, int barrier_scope, bool constant_scope = true) {
std::vector<uint32_t> m{0x07230203, 0x00010600, 0, 100, 0};
const auto op = [&m](uint32_t code, std::initializer_list<uint32_t> words) {
m.push_back((uint32_t)((words.size() + 1) << 16) | code);
for (uint32_t w : words) m.push_back(w);
};
op(17, {1}); // OpCapability Shader
if (model != ~0u) op(15, {model, 4, 0x6e69616d, 0}); // OpEntryPoint <model> %4 "main"
op(21, {2, 32, 0}); // %2 = OpTypeInt 32 0
op(43, {2, 10, 2}); // %10 = OpConstant %2 2 (Workgroup)
op(43, {2, 11, 3}); // %11 = OpConstant %2 3 (Subgroup)
op(43, {2, 12, 0x100}); // %12 = semantics
if (!constant_scope) op(50, {2, 13, 0}); // %13 = OpSpecConstant: no plain constant
op(19, {3}); // %3 = OpTypeVoid
op(33, {5, 3}); // %5 = OpTypeFunction %3
op(54, {3, 4, 0, 5}); // %4 = OpFunction
op(248, {6}); // OpLabel
if (barrier_scope) op(224, {(uint32_t)(constant_scope ? (barrier_scope == 3 ? 11 : 10) : 13), 10, 12}); // OpControlBarrier
op(253, {}); // OpReturn
op(56, {}); // OpFunctionEnd
return m;
}
int main() {
// the mode
want("unset on Mesa's Intel driver: lanes", mode_for(nullptr, true) == Mode::Lanes, true);
want("unset on any other driver: all", mode_for(nullptr, false) == Mode::All, true);
for (int intel = 0; intel < 2; intel++) {
want("all as written", mode_for("all", intel) == Mode::All, true);
want("lanes as written", mode_for("lanes", intel) == Mode::Lanes, true);
want("shared as written", mode_for("shared", intel) == Mode::Shared, true);
want("an unknown value is the driver's default", mode_for("1", intel) == mode_for(nullptr, intel), true);
want("an empty value is the driver's default", mode_for("", intel) == mode_for(nullptr, intel), true);
}
// all: everything pinned
want("all: a plain program", needed(Mode::All, plain()), true);
want("all: no stage", needed(Mode::All, Facts{}), true);
// lanes and shared
for (const Mode m : {Mode::Lanes, Mode::Shared}) {
want("a plain program is free", needed(m, plain()), false);
want("no stage: pinned", needed(m, Facts{}), true);
Facts unknown; // (a stage, but its module nobody looked at)
unknown.has_stage = true, unknown.invocations = 64;
want("a module nobody looked at: pinned", needed(m, unknown), true);
bool Facts::* const flags[] = {&Facts::vote, &Facts::mask, &Facts::lane_index, &Facts::shuffles, &Facts::fswzadd,
&Facts::matrices, &Facts::subgroup_barrier};
for (bool Facts::* flag : flags) {
Facts f = plain();
f.*flag = true;
want("each flag pins by itself", needed(m, f) && sees_lanes(f), true);
}
want("a plain program sees no lanes", sees_lanes(plain()), false);
for (const uint64_t n : {1ull, 32ull, 33ull, 64ull, 4096ull}) {
Facts f = plain();
f.shared_memory = 256, f.invocations = n;
want("shared memory by the workgroup's invocations", needed(m, f), m == Mode::Shared || n <= 32);
f.shared_memory = 0;
want("no shared memory: the workgroup's size alone pins nothing", needed(m, f), false);
}
}
// the scan of a module
const auto barrier = [](const std::vector<uint32_t>& m) { return subgroup_barrier(m.data(), m.size()); };
want("a barrier of Subgroup scope", barrier(module(5, 3)), true);
want("a barrier of Workgroup scope", barrier(module(5, 2)), false);
want("no barrier", barrier(module(5, 0)), false);
want("a scope that is no constant: a doubt", barrier(module(5, 3, false)), true);
std::vector<uint32_t> cut = module(5, 0);
cut.pop_back();
cut.back() = (9u << 16) | 253; // (an instruction longer than what is left)
want("a cut-off module: a doubt", barrier(cut), true);
std::vector<uint32_t> wrong = module(5, 0);
wrong[0] = 0;
want("a wrong magic word: a doubt", barrier(wrong), true);
want("no module: a doubt", subgroup_barrier(nullptr, 0), true);
const auto compute = [](const std::vector<uint32_t>& m) { return compute_entry(m.data(), m.size()); };
want("a GLCompute entry point", compute(module(5, 0)), true);
want("a Fragment entry point", compute(module(4, 0)), false);
want("no entry point", compute(module(~0u, 0)), false);
want("a wrong magic word is no compute module", compute(wrong), false);
if (fails) printf("compute pin: FAILED (%d of %d checks)\n", fails, checks);
else printf("compute pin: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check_source():
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
at = draw.find("void pin_compute(VkPipelineShaderStageCreateInfo& st, const Program& p) {")
if at < 0:
raise SystemExit("vk_draw.cpp has no pin_compute")
pin = draw[at:draw.index("\n}\n", at)]
first = "if (mode == compute_pin::Mode::All) return pin_subgroup(st, p.workgroup[0]);"
if first not in pin or min(pin.find(n) for n in ("compute_pin::Facts f;", "st_pin_free++", "st_pin_pinned++")) < pin.index(first):
raise SystemExit("pin_compute: with RX_VK_COMPUTE_PIN=all it is not pin_subgroup as it was, before anything is asked or counted")
for needle in ("f.vote = i.uses_subgroup_vote", "f.mask = i.uses_subgroup_mask", "f.lane_index = i.uses_subgroup_invocation_id",
"f.shuffles = i.uses_subgroup_shuffles", "f.fswzadd = i.uses_fswzadd", "f.matrices = i.uses_cooperative_matrix",
"f.subgroup_barrier = !compute_lanes_free(st.module);", "f.shared_memory = p.shared_memory;",
"f.invocations = (uint64_t)p.workgroup[0] * p.workgroup[1] * p.workgroup[2];",
"if (!compute_pin::needed(mode, f)) {"):
if needle not in pin:
raise SystemExit("pin_compute does not fill the rule's facts: " + needle)
if pin.index("if (!compute_pin::needed(mode, f)) {") > pin.rindex("pin_subgroup(st, p.workgroup[0]);"):
raise SystemExit("pin_compute pins before it asks the rule")
# the four makers of a compute program's pipeline, and the two that are not asked
makers = (("bool prepare_compute_pipeline(Program& p) {", "pin_compute(ci.stage, p);"),
("VkComputePipelineCreateInfo compute_create_info(const Program& q) {", "pin_compute(ci.stage, q);"))
for start, call in makers:
body = draw[draw.index(start):]
if call not in body[:body.index("\n}\n")]:
raise SystemExit(start + " does not call pin_compute")
if draw.count("pin_compute(ci.stage, p);") != 3 or draw.count("pin_compute(ci.stage, q);") != 1:
raise SystemExit("wanted pin_compute at the four makers of a compute program's pipeline")
if "pin_subgroup(ci.stage," in draw:
raise SystemExit("a compute program's pipeline is still pinned by pin_subgroup directly")
if draw.count(" pin_subgroup(si);\n") != 2:
raise SystemExit("build_graphics and the mesh fallback's stages no longer call pin_subgroup")
if "compute_pin_mode() != compute_pin::Mode::All)\n logf(\"vk: precompile: compute pipelines held to 32 lanes" not in draw:
raise SystemExit("the precompile does not log the pinned and free counts, only where the rule is asked")
shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8")
out_mark, in_mark = "// ---- (Triforce) Outside the translation stamp", "// ---- (Triforce) Inside the translation stamp again"
lo = shader.index(out_mark)
hi = shader.index(in_mark, lo)
for needle in ("void lanes_note(VkShaderModule m, const std::vector<uint32_t>& code) {",
"if (!m || compute_pin_mode() == compute_pin::Mode::All) return;",
"lanes_note(m, code);", "compute_pin::Mode compute_pin_mode() {", "bool compute_lanes_free(VkShaderModule m) {",
'compute_pin::mode_for(env, dev.driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA)'):
found = shader.find(needle)
if not lo < found < hi:
raise SystemExit("vk_shader.cpp: not between the first pair of translation stamp marks: " + needle)
if shader.count("compute_pin") != shader[lo:hi].count("compute_pin") or shader.count("lanes_") != shader[lo:hi].count("lanes_"):
raise SystemExit("vk_shader.cpp names the rule outside the marks: the translation stamp would move")
make = shader[shader.index("VkShaderModule make_module("):hi]
if make.count("lanes_note(m, code);") != 2 or make.index("exact_note(m, code);") > make.index("lanes_note(m, code);"):
raise SystemExit("make_module does not note each module it makes")
header = (ROOT / "gpu/vk/vk_shader.h").read_text(encoding="utf-8")
if '#include "compute_pin.h"' not in header or "compute_pin::Mode compute_pin_mode();" not in header:
raise SystemExit("vk_shader.h does not declare compute_pin_mode")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `RX_VK_COMPUTE_PIN=all`, `lanes` or `shared` |" not in docs or "`gpu/vk/compute_pin.h`" not in docs:
raise SystemExit("docs/development.md: the switches table has no row for RX_VK_COMPUTE_PIN")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="compute-pin-") as tmp:
cpp, exe = Path(tmp) / "compute_pin_test.cpp", Path(tmp) / "compute-pin.exe"
cpp.write_text(DRIVER, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the compute pin's checks failed")
if __name__ == "__main__":
main()

91
tests/test_dead_count.py Normal file
View File

@@ -0,0 +1,91 @@
"""What destroying the dead images costs a present is counted (gpu/vk/dead_count.h, vk_texture.cpp texture_frame; no game,
no GPU).
(Triforce, 11-21, the performance audit's THREAD-21, its first step.) texture_frame destroys every dead image the GPU is
done with in the middle of the present being recorded. The audit's port bounds that to a share a present, and its own
proof asks for the cost first: under 3 ms in every present, the bounded step is not built. This is the count.
This compiles dead_count.h alone: a present's loop adds to the totals, the worst present is kept with what it destroyed
and left, a present of 2 ms or more gets a line and one of 3 ms or more is counted apart, the totals' line is due every
600 presents and only when a present was counted, and reset starts afresh.
And reads the source: texture_frame times its loop, counts a destroyed image and a pooled one apart, and the loop itself
is as it was (the compaction's break, the GPU's progress, the pool, the four destroys).
python -I -B tests/test_dead_count.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include "dead_count.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
int main() {
dead_count::Counts c;
CHECK(!c.due(600)); // (nothing counted yet: no line)
CHECK(!c.note(3, 5, 0.4, 7));
CHECK(c.presents == 1 && c.destroyed == 3 && c.pooled == 5 && c.worst_ms == 0.4 && c.worst_destroyed == 3 && c.worst_left == 7);
CHECK(!c.note(0, 0, 0.0, 0) && c.presents == 2 && c.worst_ms == 0.4);
CHECK(!c.note(40, 0, 1.99, 2) && c.over_say == 0 && c.worst_ms == 1.99 && c.worst_destroyed == 40);
CHECK(c.note(300, 12, 2.0, 9) && c.over_say == 1 && c.over_worth == 0); // (2 ms: a line of its own)
CHECK(c.note(700, 0, 18.6, 0) && c.over_say == 2 && c.over_worth == 1); // (3 ms or more: the audit's line)
CHECK(c.worst_ms == 18.6 && c.worst_destroyed == 700 && c.worst_left == 0);
CHECK(!c.note(1, 1, 0.1, 0) && c.worst_ms == 18.6); // (a smaller one after it does not replace it)
CHECK(c.presents == 6 && c.destroyed == 1044 && c.pooled == 18);
CHECK(c.ms > 23.08 && c.ms < 23.10);
CHECK(c.due(600) && c.due(1200) && !c.due(601) && !c.due(599));
c.reset();
CHECK(c.presents == 0 && c.destroyed == 0 && c.worst_ms == 0 && c.over_say == 0 && !c.due(600));
CHECK(dead_count::kSayMs == 2.0 && dead_count::kWorthMs == 3.0 && dead_count::kReport == 600);
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
start = tex.index("void texture_frame(uint64_t completed) {")
frame = tex[start:tex.index("\n}\n", start)]
check('#include "dead_count.h"' in tex, "vk_texture.cpp does not use dead_count.h")
at = 0
for part in ("static dead_count::Counts dead_ct;", "const auto dead_t0 = std::chrono::steady_clock::now();",
"for (auto it = dead.begin(); it != dead.end();) {", "if (compact.in_pass) break;",
"if (it->value > completed) {", "pool_put(*it);", "dead_pooled++;",
"for (VkImageView v : it->views) vkDestroyImageView(dev.device, v, nullptr);",
"destroy_image(it->image, it->allocation);", "if (it->buffer) destroy_buffer(it->buffer, it->buffer_alloc);",
"for (VkDeviceMemory m : it->memory) vkFreeMemory(dev.device, m, nullptr);", "dead_destroyed++;",
"dead_ct.note(dead_destroyed, dead_pooled, dead_ms, dead.size())", "if (dead_ct.due(frame_no)) {",
"dead_ct.reset();", "pool_trim();"):
at = frame.find(part, at)
check(at >= 0, "texture_frame: the dead images' loop is not timed and counted as it was (missing, or out of order: " + part + ")")
at += len(part)
with tempfile.TemporaryDirectory(prefix="dead-count-") as tmp:
c, exe = Path(tmp) / "dead.cpp", Path(tmp) / "dead.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the counts as made:\n" + r.stdout)
print("PASS: what destroying the dead images costs a present is counted")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,55 @@
"""A dead render target's image goes to the pool in the pass that lets it go (gpu/vk/vk_texture.cpp texture_sweep; no game,
no GPU).
(Triforce, 11-21, the performance audit's TEX-30, after Source A's.) A render target let go waited in the dead list until
the next present's texture_frame put its image in the pool: a target made in this present's recording made a new image
meanwhile. texture_sweep now pools the ones the GPU is done with right after it lets them go.
Reads the source: after the pass has erased the textures it let go, the dead list's entries with a pool key whose value
the GPU has completed go to pool_put, never while a compaction pass is open, and only with the switch on; texture_frame's
own loop still pools and destroys the rest, and the pool is still trimmed each present; the switches are in the table.
python -I -B tests/test_dead_pool_now.py
"""
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def in_order(text, parts, why):
at = 0
for p in parts:
at = text.find(p, at)
check(at >= 0, why + " (missing, or out of order: " + p + ")")
at += len(p)
def main():
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
sweep = tex[tex.rindex("void texture_sweep("):]
sweep = sweep[:sweep.index("\n}\n")]
in_order(sweep, ["destroy_later(*tex);", "it = textures.erase(it);",
'static const bool dead_pool_now = !(getenv("RX_VK_DEAD_POOL_NOW") && !atoi(getenv("RX_VK_DEAD_POOL_NOW")));',
"if (dead_pool_now && !compact.in_pass)", "for (auto it = dead.begin(); it != dead.end();) {",
"if (it->pool_key && it->value <= completed) {", "pool_put(*it);", "it = dead.erase(it);", "++it;"],
"texture_sweep does not pool the dead targets' images the GPU is done with after it lets them go, or does so in a "
"compaction pass, for an image with no pool key, or for one the GPU still uses")
frame = tex[tex.index("void texture_frame(uint64_t completed) {"):]
frame = frame[:frame.index("\n}\n")]
in_order(frame, ["for (auto it = dead.begin(); it != dead.end();) {", "if (compact.in_pass) break;", "if (it->value > completed) {",
"if (it->pool_key) {", "pool_put(*it);", "destroy_image(it->image, it->allocation);", "pool_trim();"],
"texture_frame no longer pools and destroys what is left of the dead list, or no longer trims the pool each present")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_DEAD_SUM=0`, `RX_VK_DEAD_POOL_NOW=0`, `RX_VK_DEAD_MIN_UNUSED=3`" in dev,
"docs/development.md: TEX-30's switches are not in the switches table")
print("PASS: a dead render target's image goes to the pool in the pass that lets it go")
if __name__ == "__main__":
main()

217
tests/test_desc_counts.py Normal file
View File

@@ -0,0 +1,217 @@
"""A program's descriptor counts kept with the program (gpu/vk/desc_counts.h; vk_shader.h Program::desc_n, vk_draw.cpp
build_descriptors; no game, no GPU).
(Triforce, 11-22, the performance audit's DESC-22, after Source A's.) build_descriptors summed the program's buffers,
images and texel views at every build, six lists a stage, to size three reserve() calls. The sums are fixed once the
program is translated, so they are kept packed in one 32-bit word on the program and read at every later build.
Compiles a driver against the header: the round trip of every count up to 1023; more than 1023 of a kind packs to the
unset word; the unset and the never words hold no counts and leave the outputs alone; a build stores only with the
rule on, the program ready and the word still unset; the guard fires only for a build that did not walk and had a
vector move.
Runs a model of the build over the real rule with a std::vector as build_descriptors uses it: by kept counts the
vector never moves; with a count kept short it does, the guard sees it, the word becomes never and the next build walks.
Reads the source:
- vk_shader.h: desc_n right after ready and before stages, mutable, ~0u, and the static_assert that keeps it in the
padding (sizeof(Program) is in the program cache's stamp; this change must not move it);
- build_descriptors: the word read only with the switch on, the walk only when it holds no counts, the store after
the walk and by the rule (ready, acquire), the three data() pointers taken after the reserve and compared at the
end, the rebuild; RX_VK_DESC_COUNTS read once and the lab's knob;
- vk_shader.cpp, a stamp file, does not name the field; the switches table has the row.
python -I -B tests/test_desc_counts.py [--cxx clang++]
"""
import argparse
import re
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
DRIVER = r'''
#include "desc_counts.h"
#include <cstdio>
#include <vector>
using namespace mkwvk::desc_counts;
static int checks, fails;
static void want(const char* what, bool ok) {
checks++;
if (!ok) fails++, printf("FAIL %s\n", what);
}
// build_descriptors' use of the rule, on one vector of each kind: reserve by the word or by the walk, push what the
// program really has, store, guard
struct Model {
uint32_t word = kUnset;
size_t bufs, imgs, views; // (what the program has)
std::vector<int> b, i, v;
int walks = 0, rebuilds = 0;
bool build(bool on, bool ready) {
b.clear(), i.clear(), v.clear();
size_t nb = 0, ni = 0, nv = 0;
const uint32_t counted = on ? word : kUnset;
const bool walked = !unpack(counted, &nb, &ni, &nv);
if (walked) {
walks++, nb = bufs, ni = imgs, nv = views;
if (stores(on, ready, word)) word = pack(nb, ni, nv);
}
b.reserve(nb), i.reserve(ni), v.reserve(nv);
const void *b0 = b.data(), *i0 = i.data(), *v0 = v.data();
for (size_t k = 0; k < bufs; k++) b.push_back(1);
for (size_t k = 0; k < imgs; k++) i.push_back(1);
for (size_t k = 0; k < views; k++) v.push_back(1);
if (moved(walked, b0, b.data(), i0, i.data(), v0, v.data())) {
word = kNever, rebuilds++;
return build(on, ready);
}
return walked;
}
};
int main() {
want("on when unset", on(nullptr) && on("1") && !on("0"));
size_t a = 7, b = 8, c = 9;
for (size_t n = 0; n <= kMost; n++) {
const size_t x = n, y = (n * 7) % (kMost + 1), z = kMost - n;
a = b = c = 5000;
if (!unpack(pack(x, y, z), &a, &b, &c) || a != x || b != y || c != z) {
want("round trip", false);
break;
}
}
want("round trip of every count up to 1023", true);
want("all three at the most", holds(pack(1023, 1023, 1023)) && pack(1023, 1023, 1023) == 0x3FFFFFFFu);
want("none at all", holds(pack(0, 0, 0)) && pack(0, 0, 0) == 0);
want("over 1023 buffers: unset", pack(1024, 1, 1) == kUnset);
want("over 1023 images: unset", pack(1, 1024, 1) == kUnset);
want("over 1023 texel views: unset", pack(1, 1, 1024) == kUnset && pack(1, 1, (size_t)1 << 40) == kUnset);
a = 7, b = 8, c = 9;
want("the unset word holds nothing", !holds(kUnset) && !unpack(kUnset, &a, &b, &c) && a == 7 && b == 8 && c == 9);
want("nor the never word", !holds(kNever) && !unpack(kNever, &a, &b, &c) && a == 7 && b == 8 && c == 9 && kNever != kUnset);
want("stored: on, ready, unset", stores(true, true, kUnset));
want("not while it is translated", !stores(true, false, kUnset));
want("not with the switch off", !stores(false, true, kUnset));
want("not over the never word", !stores(true, true, kNever));
want("not over counts", !stores(true, true, pack(1, 2, 3)));
int p0 = 0, p1 = 0;
want("the guard: nothing moved", !moved(false, &p0, &p0, &p0, &p0, &p0, &p0));
want("the guard: buffers moved", moved(false, &p0, &p1, &p0, &p0, &p0, &p0));
want("the guard: images moved", moved(false, &p0, &p0, &p0, &p1, &p0, &p0));
want("the guard: texel views moved", moved(false, &p0, &p0, &p0, &p0, &p0, &p1));
want("the guard: a walked build is not asked", !moved(true, &p0, &p1, &p0, &p1, &p0, &p1));
Model m;
m.bufs = 12, m.imgs = 30, m.views = 2;
want("a program still translated walks and stores nothing", m.build(true, false) && m.word == kUnset && m.walks == 1);
want("ready: its first build walks and stores", m.build(true, true) && m.word == pack(12, 30, 2) && m.walks == 2);
for (int k = 0; k < 50; k++) m.build(true, true);
want("then no build walks and none moves", m.walks == 2 && m.rebuilds == 0 && m.b.size() == 12 && m.i.size() == 30 && m.v.size() == 2);
want("the switch off: every build walks", m.build(false, true) && m.build(false, true) && m.walks == 4 && m.word == pack(12, 30, 2));
Model big;
big.bufs = 3, big.imgs = 2000, big.views = 0;
for (int k = 0; k < 5; k++) big.build(true, true);
want("more than 1023 of a kind: every build walks", big.walks == 5 && big.word == kUnset && big.rebuilds == 0);
Model bad; // (a word kept short: what the guard is for. Fresh vectors, so the short reserve is all the room there is)
bad.bufs = 400, bad.imgs = 300, bad.views = 200;
bad.word = pack(4, 300, 200);
want("a count kept short: the build moves, is made again walked", bad.build(true, true) && bad.rebuilds == 1 && bad.word == kNever);
for (int k = 0; k < 5; k++) bad.build(true, true);
want("and walks from then on, the word never set again", bad.walks == 6 && bad.rebuilds == 1 && bad.word == kNever && bad.b.size() == 400);
if (fails) printf("desc counts: FAILED (%d of %d checks)\n", fails, checks);
else printf("desc counts: all %d checks passed\n", checks);
return fails != 0;
}
'''
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def check_source():
header = (ROOT / "gpu/vk/vk_shader.h").read_text(encoding="utf-8")
prog = block(header, "struct Program {")
ordered(prog, ["std::atomic<bool> ready{false};", "mutable std::atomic<uint32_t> desc_n{~0u};", "std::vector<Stage> stages;"],
"vk_shader.h: Program::desc_n is not right after ready and before stages")
between = prog[prog.index("std::atomic<bool> ready{false};"):prog.index("mutable std::atomic<uint32_t> desc_n{~0u};")].splitlines()[1:]
need(all(not line.strip() or line.strip().startswith("//") for line in between),
"vk_shader.h: a field came between Program::ready and Program::desc_n")
need("MUST NOT GROW THE STRUCT" in prog, "vk_shader.h: the field's comment no longer says it must not grow the struct")
after = header[header.index(prog) + len(prog):][:700]
need(re.search(r"static_assert\(sizeof\(std::atomic<uint32_t>\) == 4 && offsetof\(Program, desc_n\) \+ 4 <= offsetof\(Program, stages\) &&\s*"
r"offsetof\(Program, stages\) == 16,", after),
"vk_shader.h: the static_assert that keeps desc_n in the padding before stages is gone")
need("#include <cstddef>" in header, "vk_shader.h uses offsetof without <cstddef>")
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
need('#include "desc_counts.h"' in draw, "vk_draw.cpp does not include desc_counts.h")
on = block(draw, "bool desc_counts_on() {")
need('static const bool env = desc_counts::on(getenv("RX_VK_DESC_COUNTS"));' in on and 'return env && !lab_ab::on("descwalk");' in on,
"the switch is not RX_VK_DESC_COUNTS read once, with the lab's descwalk knob")
build = block(draw, "void build_descriptors(")
ordered(build, ["size_t nbuf = 0, nimg = 0, nview = 0;", "const bool counts_on = desc_counts_on();",
"const uint32_t counted = counts_on ? p.desc_n.load(std::memory_order_relaxed) : desc_counts::kUnset;",
"const bool walked = !desc_counts::unpack(counted, &nbuf, &nimg, &nview);", "if (walked) {",
"for (const Stage& st : p.stages) {", "st.info.constant_buffer_descriptors) nbuf += d.count;",
"st.info.image_descriptors) nimg += d.count;",
"if (desc_counts::stores(counts_on, p.ready.load(std::memory_order_acquire), p.desc_n.load(std::memory_order_relaxed)))",
"p.desc_n.store(desc_counts::pack(nbuf, nimg, nview), std::memory_order_relaxed);",
"dwr.bufs.reserve(nbuf), dwr.imgs.reserve(nimg), dwr.views.reserve(nview);",
"const void* const bufs_at = dwr.bufs.data();", "const void* const imgs_at = dwr.imgs.data();",
"const void* const views_at = dwr.views.data();", "desc_keep::prog_of(kept_descs(), &p, nbuf + nimg + nview)",
"if (desc_counts::moved(walked, bufs_at, dwr.bufs.data(), imgs_at, dwr.imgs.data(), views_at, dwr.views.data())) {",
"p.desc_n.store(desc_counts::kNever, std::memory_order_relaxed);", "build_descriptors(s, p, keep, scene);"],
"build_descriptors does not read the kept counts, walk only without them, store by the rule, and guard its vectors")
need(build.count("for (const auto& d : st.info.") == 6, "build_descriptors sums other than its six lists a stage")
need(build.rstrip().endswith("build_descriptors(s, p, keep, scene);\n }\n}"), "the guard is not the last thing build_descriptors does")
named = lambda text: len(re.findall(r"(?<![A-Za-z0-9_])desc_n(?![A-Za-z0-9_])", text))
need(build.count("p.desc_n.store(") == 2 and named(draw) == named(build), "something outside build_descriptors reads or writes a program's kept counts")
# (vk_shader.cpp is a stamp file: this change does not touch it, and nothing there needs the field)
need("desc_n" not in (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8"), "vk_shader.cpp, a stamp file, names desc_n")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
need("`RX_VK_DESC_COUNTS=0`" in docs and "`gpu/vk/desc_counts.h`" in docs and "`descwalk`" in docs,
"docs/development.md: the switches table has no RX_VK_DESC_COUNTS row")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
with tempfile.TemporaryDirectory(prefix="desc-counts-") as tmp:
cpp, exe = Path(tmp) / "desc_counts_test.cpp", Path(tmp) / "desc-counts.exe"
cpp.write_text(DRIVER, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the descriptor counts' checks failed")
check_source()
print("PASS: a program's descriptor counts are kept with it, in the padding of Program; RX_VK_DESC_COUNTS=0 walks as before")
if __name__ == "__main__":
main()

View File

@@ -64,7 +64,7 @@ def check_build(source):
"a storage buffer other than a single read-only one is kept")
at = ssbo.index("buf_stamps(desc_keep::kSsbo)")
need(at < ssbo.index("gpu_buffer_bind("), "a storage buffer's stamps are read after it is bound")
need(ssbo.index("dw().bufs.push_back(b ?") < ssbo.index("buf_keep(*kp, jb + k, *ke, desc_keep::kSsbo, addr, size, kn, kept_took, (bool)b"),
need(ssbo.index("dwr.bufs.push_back(b ?") < ssbo.index("buf_keep(*kp, jb + k, *ke, desc_keep::kSsbo, addr, size, kn, kept_took, (bool)b"),
"a storage buffer is kept before its result, or without saying whether it was bound")
begin = block(source, "void draw_begin_recording(")
need("desc_rec++" in begin, "a new recording keeps the last one's buffers")

View File

@@ -0,0 +1,168 @@
"""A recording slot's descriptor pool at half size, whole once a recording ran out (gpu/vk/desc_pool_grow.h; no game, no GPU).
(Triforce, 11-22, the performance audit's DESC-33, after Source A's.) Each recording slot kept a whole descriptor pool
(8192 sets, up to 32768 descriptors a type), 48 slots at most, and few recordings come near it. A slot's pool is now made
at half; a recording that runs out gets a whole extra pool on the spot, as before, and at the slot's next reset its own
pool is made again whole. RX_VK_DESC_POOL_GROW=0: whole from the start.
Compiles a small driver against the header and checks the rule: the switch (on unless 0), the first shift, the sizes at
each shift and under RX_VK_DESC_POOL_DIV (never 0), when a pool is made again, which descriptor type counts where, the
recordings' counts and their peaks.
Reads the source: make_pool takes the shift and sizes by the header; the extra pool of bind_descriptors is a whole one
and the set is counted after it; slot_reset_locked makes the whole pool before it lets the half one go and resets
otherwise; draw_begin_recording gives a new slot the first shift, by the switch read once, and logs the counts in a
"recording streams" line; the switches table has the three rows' names.
Runs the two cases of tests/test_recording_admission.py that drive the real make_pool, bind_descriptors' tail,
slot_reset_locked and draw_begin_recording on a fake driver (pool-grow, and pool-grow-off with the switch at 0).
python -I -B tests/test_desc_pool_grow.py --headers <Vulkan include folder> [--cxx clang++]
"""
import argparse
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
DRIVER = r'''
#include "desc_pool_grow.h"
#include <cstdio>
using namespace mkwvk::desc_pool_grow;
static int checks, fails;
static void want(const char* what, bool ok) {
checks++;
if (!ok) fails++, printf("FAIL %s\n", what);
}
struct Write { int descriptorType; unsigned descriptorCount; };
int main() {
want("on when unset", on(nullptr));
want("on at 1", on("1"));
want("off at 0", !on("0"));
want("a new slot: half with the rule", first_shift(true) == 1);
want("a new slot: whole without it", first_shift(false) == 0);
// the sizes: whole as before, half of each at shift 1, an n-th under the divider, never none
want("whole sets", count(kSets, 0, 1) == 8192);
want("half sets", count(kSets, 1, 1) == 4096);
static const unsigned whole[kKinds] = {32768, 16384, 32768, 4096, 2048, 2048};
for (int k = 0; k < kKinds; k++) {
want("a whole pool's descriptors are as they were", count(kWhole[k], 0, 1) == whole[k]);
want("half of each type", count(kWhole[k], 1, 1) == whole[k] / 2);
want("divided as before when whole", count(kWhole[k], 0, 4) == whole[k] / 4);
want("the divider and the half together", count(kWhole[k], 1, 32) == whole[k] / 64);
want("never none", count(kWhole[k], 8, 32) >= 1 && count(kWhole[k], 40, 32) >= 1 && count(kWhole[k], 1, 0) == whole[k] / 2);
}
// made again whole: only a pool that is not whole, of a slot whose last recording had extra pools
want("a half pool that ran out", remake(1, 1) && remake(1, 3));
want("a half pool that did not", !remake(1, 0));
want("a whole pool that ran out", !remake(0, 1));
want("a whole pool that did not", !remake(0, 0));
// which type counts where (VkDescriptorType's numbers)
want("kinds", kind(6) == kUniform && kind(7) == kStorage && kind(1) == kSampled && kind(3) == kStorageImage &&
kind(4) == kUniformTexel && kind(5) == kStorageTexel && kind(0) == kKinds && kind(2) == kKinds && kind(1000) == kKinds);
// a recording's counts and the peaks
Took a, b, most;
const Write w1[] = {{6, 4}, {1, 9}, {1, 2}, {0, 5}};
const Write w2[] = {{7, 3}, {5, 1}};
took(a, w1, 4), took(a, w2, 2), took(b, w1, 4);
want("sets counted", a.sets == 2 && b.sets == 1);
want("descriptors by type", a.n[kUniform] == 4 && a.n[kSampled] == 11 && a.n[kStorage] == 3 && a.n[kStorageTexel] == 1 &&
a.n[kStorageImage] == 0 && a.n[kUniformTexel] == 0);
peak(most, b), peak(most, a), peak(most, b);
want("the peaks are the most of each", most.sets == 2 && most.n[kSampled] == 11 && most.n[kUniform] == 4 && most.n[kStorage] == 3);
Took big;
big.sets = 289, big.n[kSampled] = 17251; // (Source A's busiest recording: fits a whole pool, not half of the sampled images)
want("a busy recording outgrows the half", fits(big, 0, 1) && !fits(big, 1, 1));
Took small;
small.sets = 289, small.n[kSampled] = 9000;
want("an ordinary one fits it", fits(small, 1, 1) && !fits(small, 1, 4));
if (fails) printf("desc pool grow: FAILED (%d of %d checks)\n", fails, checks);
else printf("desc pool grow: all %d checks passed\n", checks);
return fails != 0;
}
'''
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
at = [text.find(p) for p in parts]
need(min(at) >= 0 and at == sorted(at), why)
def check_source():
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
need('#include "desc_pool_grow.h"' in draw, "vk_draw.cpp does not include desc_pool_grow.h")
need('static const bool on = desc_pool_grow::on(getenv("RX_VK_DESC_POOL_GROW"));' in block(draw, "bool pool_grow_on() {"),
"the switch is not RX_VK_DESC_POOL_GROW, read once")
res = block(draw, "struct RecordingRes {")
need("uint32_t shift = 0;" in res and "desc_pool_grow::Took took;" in res, "RecordingRes does not keep its pool's shift and its counts")
pool = block(draw, "VkDescriptorPool make_pool(uint32_t shift = 0) {")
need("ci.maxSets = grow::count(grow::kSets, shift, div);" in pool and "grow::count(grow::kWhole[kind], shift, div)" in pool,
"make_pool does not size the pool by the header with its shift")
need('getenv("RX_VK_DESC_POOL_DIV")' in pool, "RX_VK_DESC_POOL_DIV no longer divides")
for gone in ("32768", "8192 / div"):
need(gone not in pool.split("static const uint32_t div")[1], "make_pool still has a size of its own: " + gone)
bind = block(draw, "bool bind_descriptors(")
ordered(bind, ["if (vkAllocateDescriptorSets(dev.device, &ai, &set) != VK_SUCCESS) {", "const VkDescriptorPool pool = make_pool();",
"cur->extra.push_back(pool);", "pool_took(dwr.writes.data(), dwr.writes.size());", "set_write_template(set, p)"],
"bind_descriptors: the extra pool is not a whole one, or the set is not counted once it is allocated")
need("desc_pool_grow::took(cur->took, writes, n);" in block(draw, "void pool_took("), "pool_took does not count into the recording's own")
reset = block(draw, "void slot_reset_locked(RecordingRes& r) {")
ordered(reset, ["if (desc_pool_grow::remake(r.shift, r.extra.size())) {", "if (const VkDescriptorPool made = make_pool(0)) {",
"vkDestroyDescriptorPool(dev.device, r.pool, nullptr);", "r.pool = made, r.shift = 0, whole = true;", "pools_whole++;",
"if (!whole) vkResetDescriptorPool(dev.device, r.pool, 0);", "pools_added += r.extra.size();",
"for (VkDescriptorPool p : r.extra) vkDestroyDescriptorPool(dev.device, p, nullptr);", "r.extra.clear();"],
"slot_reset_locked does not make the whole pool before it lets the half one go, reset otherwise, and count")
begin = block(draw, "void draw_begin_recording(int slot, VkCommandBuffer cmd, uint64_t value) {")
ordered(begin, ["if (!r.pool) {", "r.shift = desc_pool_grow::first_shift(pool_grow_on());", "r.pool = make_pool(r.shift);",
"} else if (r.reset_ahead) {", "desc_pool_grow::peak(pool_most, r.took);", "r.took = {};",
'logf("vk: the recording streams: ', "logf(\"vk: the recording streams' descriptor pools: ", "pools_whole",
"pools_added", "pool_most.sets", "pool_most.n[desc_pool_grow::kSampled]"],
"draw_begin_recording does not make a new slot's pool at the first shift, or its line lost the counts")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
for name in ("`RX_VK_DESC_POOL_GROW=0`", "`RX_VK_DESC_POOL_DIV=<1..32>`", "`RX_VK_STREAM_MB=<MB>`", "`gpu/vk/desc_pool_grow.h`"):
need(name in docs, "docs/development.md: the switches table has no " + name)
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
parser.add_argument("--headers", type=Path, required=True)
args = parser.parse_args()
with tempfile.TemporaryDirectory(prefix="desc-pool-grow-") as tmp:
cpp, exe = Path(tmp) / "desc_pool_grow_test.cpp", Path(tmp) / "desc-pool-grow.exe"
cpp.write_text(DRIVER, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the half pool rule's checks failed")
check_source()
for case in ("pool-grow", "pool-grow-off"):
subprocess.run([sys.executable, "-I", "-B", str(ROOT / "tests/test_recording_admission.py"), "--cxx", args.cxx,
"--headers", str(args.headers), "--case", case], check=True)
print("PASS: the slots' descriptor pools start at half and are whole after a recording ran out; RX_VK_DESC_POOL_GROW=0 as before")
if __name__ == "__main__":
main()

View File

@@ -88,7 +88,7 @@ def fixture_of(source):
# ((Triforce) a whole push tries the push template first, the write structs when it gives none; the changed bindings by theirs)
full = bind[:bind.index("static thread_local std::vector<VkWriteDescriptorSet> changed")]
if not re.search(r"if \(!push_write_template\(cmd, bp, p\)\)[^\n]*\n\s*vkCmdPushDescriptorSetKHR\(cmd, bp, p\.layout, 0, "
r"\(uint32_t\)dw\(\)\.writes\.size\(\), dw\(\)\.writes\.data\(\)\);", full):
r"\(uint32_t\)dwr\.writes\.size\(\), dwr\.writes\.data\(\)\);", full):
raise SystemExit("a whole push does not try the push template first, with the write structs when it gives none")
if "push_write_template(" in bind[len(full):]:
raise SystemExit("a push of the changed bindings only goes through the push template")

View File

@@ -0,0 +1,188 @@
"""The leftover per-dispatch diagnostics quieted (gpu/vk/vk_draw.cpp dispatch, taau_key; vk_texture.cpp texture_frame;
no game, no GPU).
(Triforce, 11-22, the performance audit's DESC-30, after Source A's and Source B's.) Every compute dispatch still paid for
diagnostics nobody had asked for: RX_VK_LOG_PUSH's key written out as text, taau_key's key written out and its list
walked as text (several times a dispatch), two clock reads and a sequentially consistent add around the program pick, and
a clock read for the first-pipeline timer when there was nothing to time. Every 600 presents texture_frame filled a list
only RX_VK_LOG_BAKED reads and sorted every texture to print eight.
Cuts taau_key out of vk_draw.cpp and runs it both ways, the parsed prefixes and the text compare it was (kept for the
lab's "diagold" phases), over 16 lists and some 300,000 keys each: the same answer for every key, and the answers a
list must give (a prefix, a whole key, several entries, an entry that begins with '-', upper case, 17 digits, an empty
list, empty entries: none of those last ones ever match).
Reads the source:
- taau_key: no snprintf before the parsed prefixes answer; the list's expression as it was (its reads of the
environment are counted by tests/test_taau_env_lookup.py);
- dispatch: no clock around program_for outside prof_full and the lab's knob, the pick's time taken from prof_t.exact
with a relaxed add; RX_VK_LOG_PUSH's key formatted only under its switch (or the knob); PipeClock's clock read only
when it times something (or the knob);
- program_pick, the picks' slow path, is still under PROF_EXACT: what the pick's time now stands on;
- texture_frame: RX_VK_LOG_BAKED read once, its list filled only for it, no whole sort outside the knob; the VRAM and
textures lines keep their text.
python -I -B tests/test_dispatch_quiet.py [--cxx clang++]
"""
import argparse
import re
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
FIXTURE = r'''
#define _CRT_SECURE_NO_WARNINGS
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
static const char *configured_keys, *no_f16;
static const char* fixture_getenv(const char* name) {
if (!strcmp(name, "RX_VK_TAAU_KEYS")) return configured_keys;
if (!strcmp(name, "RX_VK_NO_F16")) return no_f16;
return nullptr;
}
#define getenv fixture_getenv
namespace Shader { namespace Backend { namespace SPIRV { static bool mma_software = true; } } }
static bool diag_old_now;
static bool dispatch_diag_old() { return diag_old_now; }
/* CUT */
int main(int argc, char** argv) {
// (argv[1]: the list, "(unset)" for none set; argv[2]: "hardware" for a card that runs the network itself)
if (argc > 1 && strcmp(argv[1], "(unset)")) configured_keys = argv[1];
if (argc > 2 && !strcmp(argv[2], "hardware")) Shader::Backend::SPIRV::mma_software = false;
std::vector<uint64_t> keys = {0xfd029a317a9a4b94ull, 0x9dc98871eb7fc101ull, 0, ~0ull, 0xfd029a317a9a4b95ull, 0xfd029a317a9a4b84ull,
0x0d029a317a9a4b94ull, 0xfd02000000000000ull, 0xfd01ffffffffffffull, 0x00000000000000fdull,
0xabcdef0123456789ull, 0x0123456789abcdefull, 0xa000000000000000ull, 0x0a00000000000000ull};
uint64_t x = 0x9E3779B97F4A7C15ull;
for (int i = 0; i < 300000; i++) {
x ^= x << 13, x ^= x >> 7, x ^= x << 17;
keys.push_back(x);
if (i % 3 == 0) keys.push_back((x & 0x0000ffffffffffffull) | 0xfd02000000000000ull); // (many under the usual prefix)
if (i % 7 == 0) keys.push_back(x >> (i % 61)); // (leading zeros)
}
size_t matched = 0, differ = 0;
for (uint64_t k : keys) {
diag_old_now = false;
const bool now = taau_key(k);
diag_old_now = true;
const bool was = taau_key(k);
matched += now, differ += now != was;
if (now != was && differ <= 4) printf("DIFFERS %016llx: parsed %d, text %d\n", (unsigned long long)k, (int)now, (int)was);
}
diag_old_now = false;
printf("%zu %zu %zu %d %d %d\n", keys.size(), matched, differ, (int)taau_key(0xfd029a317a9a4b94ull), (int)taau_key(0x9dc98871eb7fc101ull),
(int)taau_key(0));
return differ != 0;
}
'''
# the list, the card, and the answers for fd029a317a9a4b94, 9dc98871eb7fc101 and 0
LISTS = [("(unset)", "software", (1, 0, 0)), ("(unset)", "hardware", (0, 0, 0)), ("-", "software", (0, 0, 0)), ("", "software", (0, 0, 0)),
("fd029a317a9a4b94", "hardware", (1, 0, 0)), ("fd02", "software", (1, 0, 0)), ("f", "software", (1, 0, 0)),
("fd029a317a9a4b94,9dc98871eb7fc101", "software", (1, 1, 0)), ("9dc9,-fd02", "software", (0, 1, 0)),
("FD029A317A9A4B94", "software", (0, 0, 0)), ("fd029a317a9a4b940", "software", (0, 0, 0)), (",,fd,,0000,", "software", (1, 0, 1)),
("0", "software", (0, 0, 1)), ("g,fd0x,9d c9, fd", "software", (0, 0, 0)), ("0000000000000000", "software", (0, 0, 1)),
("-,fd029a317a9a4b9", "software", (1, 0, 0))]
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def check_source(draw):
key = block(draw, "bool taau_key(uint64_t key) {")
ordered(key, ['static const char* keys = getenv("RX_VK_TAAU_KEYS") ? getenv("RX_VK_TAAU_KEYS")',
"static const std::vector<Prefix> prefixes = [] {", "if (!dispatch_diag_old()) {",
"if ((key & p.mask) == p.value) return true;", "return false;", "snprintf(hex, sizeof hex,"],
"taau_key does not answer from its list parsed once before any text is made")
need(key.count("snprintf(") == 1 and key.count("getenv(") == 3, "taau_key formats or reads the environment in another place")
need('return lab_ab::on("diagold");' in block(draw, "bool dispatch_diag_old() {"), "the lab's knob is not diagold")
disp = block(draw, "void dispatch(VkCommandBuffer cmd, NvnState& s, uint32_t x, uint32_t y, uint32_t z, uint64_t indirect) {")
ordered(disp, ["const bool pick_clocked = prof_full.load(std::memory_order_relaxed) != 0 || dispatch_diag_old();",
"const auto t_pick = pick_clocked ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};",
"const int64_t x_pick = prof_t.exact;", "const Program* cp = program_for(s, ProgramKind::Compute, types);",
"if (pick_clocked)", "st_disp_pick_ns +=", "else if (const int64_t dx = prof_t.exact - x_pick)",
"st_disp_pick_ns.fetch_add(dx, std::memory_order_relaxed);"],
"dispatch still reads a clock around every program pick, or lost the pick's time")
pick = disp[disp.index("const uint8_t types[8]{};"):disp.index("dclock.t_prog =")]
need(pick.count("steady_clock::now()") == 2 and "pick_clocked ?" in pick and pick.index("if (pick_clocked)") < pick.rindex("steady_clock::now()"),
"dispatch reads the clock around the pick outside prof_full")
at = disp.index('static const char* want = getenv("RX_VK_LOG_PUSH");')
push = disp[at:disp.index("Program& p = const_cast<Program&>(*cp);", at)]
ordered(push, ["if ((want && !done) || dispatch_diag_old()) {", "snprintf(hex, sizeof hex,",
"if (want && !done && !strncmp(hex, want, strlen(want))) {", "done = true;"],
"dispatch formats RX_VK_LOG_PUSH's key without its switch")
ordered(disp, ["struct PipeClock {", "const bool pipe_timed = !p.compute_pipeline || !p.layout;",
"PipeClock pipe_clock{pipe_timed || dispatch_diag_old() ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{},",
"pipe_timed};", "ensure_layout(p);", "pipe_clock.stop();"],
"dispatch reads PipeClock's clock when it times nothing")
need("} pipe_clock{std::chrono::steady_clock::now()," not in disp, "PipeClock is made with a clock read at every dispatch")
shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8")
slow = block(shader, "const Program* program_pick(const NvnState& s, ProgramKind kind, const uint8_t color_types[8]) {")
need(slow.split("\n")[1].strip().startswith("PROF_EXACT();"), "program_pick is no longer timed by PROF_EXACT: the pick's time stood on it")
internal = (ROOT / "gpu/vk/vk_internal.h").read_text(encoding="utf-8")
need("prof_t.exact = x0 + dt;" in internal and "inline thread_local ProfThread prof_t;" in internal,
"PROF_EXACT no longer adds its time to the thread's prof_t.exact")
tex = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
frame = block(tex, "void texture_frame(uint64_t completed) {")
need(frame.count('getenv("RX_VK_LOG_BAKED")') == 1 and 'static const bool log_baked = getenv("RX_VK_LOG_BAKED") != nullptr;' in frame,
"texture_frame does not read RX_VK_LOG_BAKED once")
need("if (log_baked || diag_old) baked.push_back(t);" in frame and "if (log_baked) {" in frame and frame.count("baked.push_back(") == 1,
"texture_frame fills the list of RX_VK_LOG_BAKED without the switch")
need(frame.count("std::sort(") == 1 and "if (diag_old) std::sort(list.begin(), list.end(), larger);" in frame and
"else std::partial_sort(list.begin(), list.begin() + (ptrdiff_t)std::min(n, list.size()), list.end(), larger);" in frame and
"largest(big, 8);" in frame and "largest(baked, 10);" in frame and 'const bool diag_old = lab_ab::on("diagold");' in frame,
"texture_frame sorts a whole list outside the lab's knob")
for line in ('logf("vk: VRAM: %llu MB used of %llu MB allowed (%llu MB of it free in the blocks); textures %llu MB (budget %llu "',
'logf("vk: textures: decoded ASTC %llu MB, rendered %llu MB (%zu drawn in one frame, kept: %llu KB), other %llu MB; "',
"for (size_t i = 0; i < big.size() && i < 8; i++)", "for (size_t i = 0; i < baked.size() && i < 10; i++) {"):
need(line in frame, "texture_frame's lines changed: " + line)
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
check_source(draw)
with tempfile.TemporaryDirectory(prefix="dispatch-quiet-") as tmp:
cpp, exe = Path(tmp) / "taau_key.cpp", Path(tmp) / "taau-key.exe"
cpp.write_text(FIXTURE.replace("/* CUT */", block(draw, "bool taau_key(uint64_t key) {")), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
for keys, card, want in LISTS:
r = subprocess.run([str(exe), keys, card], capture_output=True, text=True, timeout=120)
row = r.stdout.strip().splitlines()[-1].split() if r.stdout.strip() else []
need(r.returncode == 0 and len(row) == 6, "RX_VK_TAAU_KEYS=%r (%s): the parsed list and the text compare differ:\n%s" % (keys, card, r.stdout))
need(tuple(int(v) for v in row[3:]) == want, "RX_VK_TAAU_KEYS=%r (%s): answers %s, want %s" % (keys, card, row[3:], want))
need((int(row[1]) > 0) == (1 in want) or keys in (",,fd,,0000,",), "RX_VK_TAAU_KEYS=%r: %s keys matched" % (keys, row[1]))
print("RX_VK_TAAU_KEYS=%-36r %s: %s keys, %s match, both ways alike" % (keys, card, row[0], row[1]))
print("PASS: a dispatch's leftover diagnostics are quiet, and taau_key answers as its text compare did")
if __name__ == "__main__":
main()

View File

@@ -46,6 +46,45 @@ int main() {
CHECK("wider than the output", !dc::scene_size_fits(1537, 864, 1536, 864));
CHECK("no 32-bit overflow in the third", dc::scene_size_fits(0x60000000u, 1, 0xFFFFFFFFu, 3));
// ---- ((Triforce, 11-22, the performance audit's PRESENT-38) the quality mode that takes a render size ----
{
// (NGX's ranges for 2560x1440, in vk_dlss.cpp's order: MaxQuality with its dynamic range, DLAA, Balanced and MaxPerf
// inside MaxQuality's, Ultra Performance one size)
const dc::ModeRange r1440[5] = {{1280, 720, 2560, 1440, true}, {2560, 1440, 2560, 1440, true}, {1280, 720, 2560, 1440, true},
{1280, 720, 2560, 1440, true}, {853, 480, 853, 480, true}};
bool wide = true;
CHECK("Quality's share is in the first range", dc::mode_for(r1440, 5, 1707, 960, 2560, 1440, true, &wide) == 0 && !wide);
CHECK("a range that holds the size needs no slack", dc::mode_for(r1440, 5, 1707, 960, 2560, 1440, false, &wide) == 0 && !wide);
CHECK("the row's 33% (854x480) is Ultra Performance's by a rounding", dc::mode_for(r1440, 5, 854, 480, 2560, 1440, true, &wide) == 4 && wide);
CHECK("without the slack it is no mode's, as before", dc::mode_for(r1440, 5, 854, 480, 2560, 1440, false, &wide) == -1 && !wide);
CHECK("NGX's own 853x480 is taken strictly", dc::mode_for(r1440, 5, 853, 480, 2560, 1440, false, &wide) == 4 && !wide);
CHECK("42% is between the ranges: no mode's", dc::mode_for(r1440, 5, 1075, 605, 2560, 1440, true, &wide) == -1);
CHECK("under a third of the output is not DLSS's at all", dc::mode_for(r1440, 5, 851, 478, 2560, 1440, true, &wide) == -1);
CHECK("a size just under the dynamic range's floor is taken by the slack", dc::mode_for(r1440, 5, 1278, 719, 2560, 1440, true, &wide) == 0 && wide);
CHECK("the slack is a 64th of the range's largest size", dc::mode_slack(2560) == 40 && dc::mode_slack(853) == 13 && dc::mode_slack(100) == 2 && dc::mode_slack(0) == 2);
CHECK("past the slack on one axis is refused", dc::mode_for(r1440, 5, 867, 480, 2560, 1440, true, &wide) == -1);
CHECK("14 pixels over Ultra Performance's height is refused", dc::mode_for(r1440, 5, 854, 494, 2560, 1440, true, &wide) == -1);
// (5120x2160: the row's 33% is 1707x720, NGX's 1706x720; 42% is in no range)
const dc::ModeRange r2160[5] = {{2560, 1080, 5120, 2160, true}, {5120, 2160, 5120, 2160, true}, {2560, 1080, 5120, 2160, true},
{2560, 1080, 5120, 2160, true}, {1706, 720, 1706, 720, true}};
CHECK("5120x2160 at 33%", dc::mode_for(r2160, 5, 1707, 720, 5120, 2160, true, &wide) == 4 && wide);
CHECK("5120x2160 at 42%", dc::mode_for(r2160, 5, 2150, 907, 5120, 2160, true, &wide) == -1);
// (a mode NGX did not answer is never taken, and the first that fits wins)
dc::ModeRange gaps[5] = {{}, {}, {1280, 720, 2560, 1440, true}, {1280, 720, 2560, 1440, true}, {}};
CHECK("an unanswered mode is skipped", dc::mode_for(gaps, 5, 1707, 960, 2560, 1440, true, &wide) == 2 && !wide);
CHECK("no mode answered", dc::mode_for(gaps, 2, 1707, 960, 2560, 1440, true, &wide) == -1);
// (every twelfth of every output the menu offers is some mode's with the slack, where NGX's sizes are the share rounded down)
const uint32_t outs[][2] = {{1920, 1080}, {2048, 1152}, {2560, 1440}, {3840, 2160}, {3440, 1440}, {5120, 2160}, {1536, 864}, {4416, 1864}};
for (const auto& o : outs) {
const dc::ModeRange r[5] = {{o[0] / 2, o[1] / 2, o[0], o[1], true}, {o[0], o[1], o[0], o[1], true}, {o[0] / 2, o[1] / 2, o[0], o[1], true},
{o[0] / 2, o[1] / 2, o[0], o[1], true}, {o[0] / 3, o[1] / 3, o[0] / 3, o[1] / 3, true}};
for (const uint32_t twelfths : {4u, 6u, 7u, 8u}) {
const uint32_t w = (o[0] * twelfths + 11) / 12, h = (o[1] * twelfths + 11) / 12;
CHECK("a share of the row at an output of the menu", dc::mode_for(r, 5, w, h, o[0], o[1], true, &wide) >= 0);
}
}
}
// ---- UTF-8 paths as NGX's wide strings, in the C locale (the decoding must not depend on it) ----
std::setlocale(LC_ALL, "C");
// (which paths NGX's Linux library takes as they are: ASCII only. The others go to it as /proc/self/fd/<n>)
@@ -80,6 +119,50 @@ int main() {
CHECK("good after bad", decode("\xFF\xC3\xA9", bad) == L"<EFBFBD>é" && bad);
CHECK("no flag asked", dc::utf8_to_wide("\xFF") == L"<EFBFBD>");
CHECK("empty", decode("", bad).empty() && !bad);
{ // ((Triforce, 11-21, PRESENT-37) the DLSS model row stepped through: no feature for a preset passed over
dc::PresetSettle s;
const unsigned K = 0, M = 1, L = 2;
CHECK("no feature: the row's preset at once", s.pick(K, ~0u, false, 100, 30) == K);
CHECK("the feature's own preset", s.pick(K, K, true, 101, 30) == K);
CHECK("a new preset waits", s.pick(M, K, true, 200, 30) == K && s.pick(M, K, true, 210, 30) == K);
CHECK("stepped on: the count starts again", s.pick(L, K, true, 211, 30) == K && s.pick(L, K, true, 240, 30) == K);
CHECK("held for 30 frames: taken", s.pick(L, K, true, 241, 30) == L);
CHECK("and it is the feature's now", s.pick(L, L, true, 242, 30) == L);
CHECK("away and back within the wait: nothing made", s.pick(M, L, true, 300, 30) == L && s.pick(L, L, true, 310, 30) == L);
CHECK("the feature let go: at once", s.pick(M, L, false, 311, 30) == M);
dc::PresetSettle now;
CHECK("0 frames: at once, as before", now.pick(K, ~0u, false, 1, 0) == K && now.pick(M, K, true, 2, 0) == M &&
now.pick(L, M, true, 3, 0) == L);
CHECK("the switch", dc::settle_frames(nullptr) == 30 && dc::settle_frames("") == 30 && dc::settle_frames("30") == 30 &&
dc::settle_frames("0") == 0 && dc::settle_frames("120") == 120 && dc::settle_frames("-5") == 0 &&
dc::settle_frames("off") == 0);
}
{ // ((Triforce, 11-22, the performance audit's PRESENT-32) the one-size (DLAA) modes: before, whenever the render size is
// the output's; with the rule, only where DLAA is the Upscaler's choice
CHECK("as before: one size whenever the sizes are equal", dc::dlaa_feature(false, false, 2560, 1440, 2560, 1440) &&
dc::dlaa_feature(false, true, 2560, 1440, 2560, 1440) &&
!dc::dlaa_feature(false, true, 1707, 960, 2560, 1440) &&
!dc::dlaa_feature(false, false, 2560, 1439, 2560, 1440));
CHECK("the rule: DLAA at the output's size is one size", dc::dlaa_feature(true, true, 2560, 1440, 2560, 1440));
CHECK("the rule: DLSS at the output's size is not", !dc::dlaa_feature(true, false, 2560, 1440, 2560, 1440));
CHECK("the rule: a smaller render size never is", !dc::dlaa_feature(true, true, 1707, 960, 2560, 1440) &&
!dc::dlaa_feature(true, false, 1707, 960, 2560, 1440));
CHECK("RX_VK_DLSS_PREWARM: on only at 1", !dc::prewarm_rule(nullptr) && !dc::prewarm_rule("") && !dc::prewarm_rule("0") &&
dc::prewarm_rule("1") && !dc::prewarm_rule("2") && !dc::prewarm_rule("on"));
// (DLSS at full size takes the scaled mode whose range holds it, and that range holds Auto's smaller sizes: NGX's
// ranges as it answers them, Quality's from half the output to the output's own)
const unsigned outs[][2] = {{1920, 1080}, {2560, 1440}, {3840, 2160}, {4416, 1864}, {5120, 2160}};
for (const auto& o : outs) {
const dc::ModeRange r[5] = {{o[0] / 2, o[1] / 2, o[0], o[1], true}, {o[0], o[1], o[0], o[1], true}, {o[0] / 2, o[1] / 2, o[0], o[1], true},
{o[0] / 2, o[1] / 2, o[0], o[1], true}, {o[0] / 3, o[1] / 3, o[0] / 3, o[1] / 3, true}};
bool wide = false;
const int at = dc::mode_for(r, 5, o[0], o[1], o[0], o[1], true, &wide);
CHECK("DLSS at the output's size: the first scaled mode's", at == 0 && !wide);
for (unsigned pct = 50; pct <= 100; pct += 5)
CHECK("Auto's sizes are in that mode's range", o[0] * pct / 100 >= r[at].min_w && o[0] * pct / 100 <= r[at].max_w &&
o[1] * pct / 100 >= r[at].min_h && o[1] * pct / 100 <= r[at].max_h);
}
}
std::puts("dlss checks: ok");
return 0;
}

View File

@@ -33,6 +33,34 @@ def check_sources():
# (NGX's Linux library converts its wide paths back in the "C" locale, ASCII only: a folder named otherwise goes to it
# as /proc/self/fd/<n>)
assert "dlss_checks::ascii(s)" in dlss and '"/proc/self/fd/"' in dlss, "wide(): non-ASCII folders by descriptor"
# ((Triforce, 11-21, the performance audit's PRESENT-37) prepare reads the DLSS model row's preset only through the settle
# rule, before it asks whether the feature there is can be used: a preset stepped past makes no feature
prepare = dlss[dlss.index("int prepare(VkCommandBuffer cmd"):]
prepare = prepare[:prepare.index("\n}\n")]
assert prepare.count("wanted_preset()") == 1, "prepare: the row's preset read past the settle rule"
settle = ("const unsigned preset = s.settle.pick(wanted_preset(), s.feature_preset, s.feature != nullptr, texture_frame_no(), "
"settle_frames);")
assert settle in prepare and prepare.index(settle) < prepare.index("const bool same_feature ="), \
"prepare: the preset is not the settle rule's, or is picked after the feature's reuse test"
assert 'dlss_checks::settle_frames(getenv("RX_VK_DLSS_MODEL_SETTLE"))' in prepare, "prepare: no RX_VK_DLSS_MODEL_SETTLE"
assert "dlss_checks::PresetSettle settle;" in dlss, "vk_dlss.cpp: the settle rule's state is not kept with the feature"
# ((Triforce, 11-22, the performance audit's PRESENT-38) the mode is found by mode_for, with its slack unless the switch
# says strict, and the feature there is is let go only after a mode took the size: a refused size keeps it
order = [prepare.find(n) for n in (
'static const bool slack_env = !(getenv("RX_VK_DLSS_MODE_SLACK") && atoi(getenv("RX_VK_DLSS_MODE_SLACK")) == 0);',
'dlss_checks::mode_for(ranges, nmodes, rw, rh, ow, oh, slack_env && !lab_ab::on("dlssstrict"), &widened);',
'if (!found) return *fail = "no DLSS mode takes this render size", 0;',
"temporal_later([old] { NVSDK_NGX_VULKAN_ReleaseFeature(old); });",
"NGX_VULKAN_CREATE_DLSS_EXT(cmd, 1, 1, &s.feature, s.params, &cp);")]
assert min(order) >= 0 and order == sorted(order), \
"prepare: the mode is not found by mode_for before the old feature is let go (%s)" % order
assert prepare.count("NVSDK_NGX_VULKAN_ReleaseFeature(old)") == 1, "prepare: the old feature is let go in a second place"
assert "max_w = std::max<unsigned>(r.max_w, rw), max_h = std::max<unsigned>(r.max_h, rh);" in prepare, \
"prepare: a size taken by the slack is not kept in the feature's range (it would be made again every frame)"
assert "`RX_VK_DLSS_MODE_SLACK=0`" in (ROOT / "docs" / "development.md").read_text(encoding="utf-8"), \
"docs/development.md: RX_VK_DLSS_MODE_SLACK is not in the switches table"
assert "`RX_VK_DLSS_MODEL_SETTLE=<frames>`" in (ROOT / "docs" / "development.md").read_text(encoding="utf-8"), \
"docs/development.md: RX_VK_DLSS_MODEL_SETTLE is not in the switches table"
def main():

149
tests/test_dlss_feature.py Normal file
View File

@@ -0,0 +1,149 @@
"""NVIDIA DLSS's feature is made for a fixed share's own render size, not for the largest of its mode's range
(gpu/vk/dlss_feature.h, vk_dlss.cpp prepare; no game, no GPU, no NGX).
(Triforce, 11-21, the upscaler audit's UPS-01.) Every feature was made for the largest render size its quality mode
allows, which NGX answers as the output's own: with the Render resolution row at Quality the owner's log has a feature
"2944x1243 (2208x932 to 4416x1864) -> 4416x1864" made for 4416x1864, 2.25 times the pixels it is ever given, and DLSS's
own video memory at 1069 MB. NVIDIA's guide has the render size passed to the creation, and says the cost of presets L and
M follows the size the feature is initialized for.
This compiles dlss_feature.h alone and checks the rule: a fixed share's feature is its own render size and takes that size
alone; the row's Auto, a size seen changing and the switch give the mode's largest, which takes its whole range as
before; DLAA's is the output's; a render size over the mode's largest never makes a feature larger than it; a frame of
another size under an exact feature of the same output is what shows the range, and nothing else is.
And reads the source: prepare asks the rule for the feature it has and for the size it makes, with the row's Auto, what
this run saw and RX_VK_DLSS_FEATURE_MAX; the feature's size, not the mode's largest, is what NGX is given and what the
chain's inputs are made at; the log says which; and the switch is in the switches table.
python -I -B tests/test_dlss_feature.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include "dlss_feature.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace dlss_feature;
int main() {
CHECK(!whole_range(nullptr) && !whole_range("") && !whole_range("0") && whole_range("1"));
// (the owner's log: Quality, 2944x1243 into 4416x1864, the mode's range 2208x932 to 4416x1864)
{ // a fixed share: its own render size, and that size alone
const Size s = size(false, false, 2944, 1243, 4416, 1864);
CHECK(s.w == 2944 && s.h == 1243 && s.exact);
CHECK(takes(s.exact, s.w, s.h, 2208, 932, 2944, 1243));
CHECK(!takes(s.exact, s.w, s.h, 2208, 932, 2943, 1243) && !takes(s.exact, s.w, s.h, 2208, 932, 2944, 1244));
CHECK(!takes(s.exact, s.w, s.h, 2208, 932, 2208, 932) && !takes(s.exact, s.w, s.h, 2208, 932, 4416, 1864));
CHECK((uint64_t)s.w * s.h * 2 < (uint64_t)4416 * 1864); // (under half the pixels of the largest)
}
{ // the row's Auto, a size seen changing, the switch: the mode's largest, every size of its range, as before
const Size s = size(false, true, 2944, 1243, 4416, 1864);
CHECK(s.w == 4416 && s.h == 1864 && !s.exact);
CHECK(takes(s.exact, s.w, s.h, 2208, 932, 2944, 1243) && takes(s.exact, s.w, s.h, 2208, 932, 2208, 932) &&
takes(s.exact, s.w, s.h, 2208, 932, 4416, 1864));
CHECK(!takes(s.exact, s.w, s.h, 2208, 932, 2207, 932) && !takes(s.exact, s.w, s.h, 2208, 932, 2208, 931) &&
!takes(s.exact, s.w, s.h, 2208, 932, 4417, 1864));
}
{ // DLAA: the output's, its one size, whatever the rest says
for (int range = 0; range < 2; range++) {
const Size s = size(true, range != 0, 4416, 1864, 4416, 1864);
CHECK(s.w == 4416 && s.h == 1864 && !s.exact);
CHECK(takes(s.exact, s.w, s.h, 4416, 1864, 4416, 1864) && !takes(s.exact, s.w, s.h, 4416, 1864, 2944, 1243));
}
}
{ // a render size past the mode's largest (never asked: the mode is found by its range) makes none larger than it
const Size s = size(false, false, 5000, 1243, 4416, 1864);
CHECK(s.w == 4416 && s.h == 1864 && !s.exact);
}
{ // what shows that the render size changes: an exact feature of the same output, and a frame of another size
CHECK(found_range(true, true, 2944, 1243, 2800, 1182));
CHECK(found_range(true, true, 2944, 1243, 2944, 1242));
CHECK(!found_range(true, true, 2944, 1243, 2944, 1243)); // (the same size: nothing)
CHECK(!found_range(false, true, 2944, 1243, 2800, 1182)); // (another output, preset or colour: a feature of its own)
CHECK(!found_range(true, false, 4416, 1864, 2800, 1182)); // (a feature for the range already)
}
{ // every share the Render resolution row has, at 3840x2160: the feature's pixels against the largest's
const uint32_t ow = 3840, oh = 2160;
const uint32_t shares[4][2] = {{2560, 1440}, {2240, 1260}, {1920, 1080}, {1280, 720}};
for (const auto& r : shares) {
const Size s = size(false, false, r[0], r[1], ow, oh);
CHECK(s.exact && s.w == r[0] && s.h == r[1] && takes(s.exact, s.w, s.h, 0, 0, r[0], r[1]));
}
}
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
src = (VK / "vk_dlss.cpp").read_text(encoding="utf-8")
check('#include "dlss_feature.h"' in src, "vk_dlss.cpp does not use the rule")
prep = block(src, "int prepare(VkCommandBuffer cmd, uint32_t rw, uint32_t rh, uint32_t ow, uint32_t oh, bool hdr, uint32_t* in_w,")
at = [prep.index(part) for part in (
"const bool same_feature = s.feature && s.feature_ow == ow && s.feature_oh == oh && s.feature_preset == preset && s.feature_hdr == hdr;",
"if (same_feature && dlss_feature::takes(s.feature_exact, s.feature_rw, s.feature_rh, s.min_rw, s.min_rh, rw, rh))",
"return *in_w = s.feature_rw, *in_h = s.feature_rh, 1;",
"const bool found_range = dlss_feature::found_range(same_feature, s.feature_exact, s.feature_rw, s.feature_rh, rw, rh);",
"if (found_range) s.range_seen = true;",
'static const bool whole_range = dlss_feature::whole_range(getenv("RX_VK_DLSS_FEATURE_MAX"));',
"dlss_feature::size(aa, whole_range || s.range_seen || opts.resolution == 0, rw, rh, max_w, max_h);",
"cp.Feature.InWidth = made.w, cp.Feature.InHeight = made.h, cp.Feature.InTargetWidth = ow, cp.Feature.InTargetHeight = oh;",
"NGX_VULKAN_CREATE_DLSS_EXT(cmd, 1, 1, &s.feature, s.params, &cp);",
"s.feature_rw = made.w, s.feature_rh = made.h, s.feature_ow = ow, s.feature_oh = oh, s.min_rw = min_w, s.min_rh = min_h;",
"s.feature_exact = made.exact;",
"*in_w = s.feature_rw, *in_h = s.feature_rh;",
"return 2;")]
check(at == sorted(at), "prepare does not ask the rule for the feature it has, then for the size it makes, and keep what it made")
check("cp.Feature.InWidth = max_w" not in prep and "s.feature_rw = max_w" not in prep,
"a feature is still made for its mode's largest render size whatever the row says")
check("this render size alone (RX_VK_DLSS_FEATURE_MAX=1: its mode's largest)" in prep and "the largest of its range" in prep,
"the log does not say what size the feature was made for")
check("for its mode's whole range from now on" in prep, "the log does not say why a feature is made again when the render size changes")
rel = block(src, "void release() {")
check("s.feature_exact = false;" in rel, "a feature let go keeps its size's kind")
# (unchanged, as the chain reads them: the evaluation is given the frame's own render size, whatever the feature's)
ev = block(src, "bool evaluate(VkCommandBuffer cmd, const TemporalFrame& f) {")
check("ep.InRenderSubrectDimensions = {f.render_w, f.render_h};" in ev, "the evaluation is not given the frame's render size")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_DLSS_FEATURE_MAX=1`" in dev, "docs/development.md: RX_VK_DLSS_FEATURE_MAX=1 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="dlss-feature-") as tmp:
c, exe = Path(tmp) / "feature.cpp", Path(tmp) / "feature.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: NVIDIA DLSS's feature is made for a fixed share's own render size, the mode's largest only where the size changes")
if __name__ == "__main__":
main()

160
tests/test_dlss_prewarm.py Normal file
View File

@@ -0,0 +1,160 @@
"""NVIDIA DLSS at the output's size takes a scaled mode with RX_VK_DLSS_PREWARM=1, so the Render resolution row's Auto
makes one feature, not two (gpu/vk/dlss_checks.h dlaa_feature, vk_dlss.cpp prepare; no game, no GPU, no NVIDIA SDK).
(Triforce, 11-22, the performance audit's PRESENT-32, after Source A's: its first step.) prepare made a one-size feature
(NGX's DLAA mode) whenever the render size was the output's. With the row at Auto the game starts at the output's size
and lowers it some frames later: a second feature made in play. With the rule only NVIDIA DLAA gets the one-size mode;
DLSS at full size takes the scaled mode whose range holds it, which the smaller sizes reuse.
The entry's other steps (the feature, the chain's images and a first evaluation made ahead in a light frame) are not
built: see the commit. This checks what is.
It compiles dlss_checks.h and dlss_feature.h and runs prepare's decisions (the reuse test, the one-size test, the mode,
the size the feature is made for: the same functions in the same order, NGX's ranges as it answers them) over a load-in:
17 frames at the output's size, then the game's dynamic resolution going down and up. With the row at Auto: two
features without the rule, one with it, and that one takes every later size. With NVIDIA DLAA: one feature, of one
size, either way. With a fixed share: the same feature either way (the rule changes nothing there). Without the switch
every decision is the one from before.
Then the source: prepare asks dlaa_feature with the switch (read once) and whether DLAA is the Upscaler's choice, the
switch is on only at 1, and its row is in the development guide.
python -I -B tests/test_dlss_prewarm.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
PROGRAM = r"""
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include "dlss_checks.h"
#include "dlss_feature.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
namespace dc = mkwvk::dlss_checks;
// vk_dlss.cpp prepare's decisions, without NGX: the feature there is, and what a frame of rw x rh into ow x oh does to it
struct Feature {
bool has = false, exact = false, one_size = false;
uint32_t w = 0, h = 0, ow = 0, oh = 0, min_w = 0, min_h = 0;
int mode = -1; // (the index into the scaled modes; -1: the one-size list's first, DLAA's)
};
struct Run {
Feature f;
bool range_seen = false;
int made = 0, refused = 0;
bool rule, dlaa, auto_row;
Run(bool rule, bool dlaa, bool auto_row) : rule(rule), dlaa(dlaa), auto_row(auto_row) {}
void frame(uint32_t rw, uint32_t rh, uint32_t ow, uint32_t oh) {
const bool same = f.has && f.ow == ow && f.oh == oh;
if (same && dlss_feature::takes(f.exact, f.w, f.h, f.min_w, f.min_h, rw, rh)) return; // (the feature there is draws it)
if (dlss_feature::found_range(same, f.exact, f.w, f.h, rw, rh)) range_seen = true;
const bool aa = dc::dlaa_feature(rule, dlaa, rw, rh, ow, oh);
uint32_t max_w = 0, max_h = 0, min_w = 0, min_h = 0;
int mode = -1;
if (aa) {
max_w = min_w = ow, max_h = min_h = oh;
} else {
// (NGX's ranges for the scaled list: Quality, DLAA, Balanced, Performance from half the output to the output's own,
// DLAA's and Ultra Performance's one size)
const dc::ModeRange r[5] = {{ow / 2, oh / 2, ow, oh, true}, {ow, oh, ow, oh, true}, {ow / 2, oh / 2, ow, oh, true},
{ow / 2, oh / 2, ow, oh, true}, {ow / 3, oh / 3, ow / 3, oh / 3, true}};
bool widened = false;
mode = dc::mode_for(r, 5, rw, rh, ow, oh, true, &widened);
if (mode < 0) { refused++; return; }
min_w = std::min(r[mode].min_w, rw), min_h = std::min(r[mode].min_h, rh);
max_w = std::max(r[mode].max_w, rw), max_h = std::max(r[mode].max_h, rh);
}
const dlss_feature::Size s = dlss_feature::size(aa, range_seen || auto_row, rw, rh, max_w, max_h);
f = {true, s.exact, aa, s.w, s.h, ow, oh, min_w, min_h, mode};
made++;
}
};
/// A load-in at an output of ow x oh: 17 frames at `start` percent, then the game's dynamic resolution moving
static void load_in(Run& run, uint32_t ow, uint32_t oh, unsigned start) {
for (int i = 0; i < 17; i++) run.frame(ow * start / 100, oh * start / 100, ow, oh);
for (unsigned pct : {95u, 90u, 80u, 75u, 60u, 50u, 65u, 85u, 100u, 70u}) run.frame(ow * pct / 100, oh * pct / 100, ow, oh);
}
int main() {
const uint32_t outs[][2] = {{1920, 1080}, {2560, 1440}, {3840, 2160}, {4416, 1864}, {5120, 2160}};
for (const auto& o : outs) {
// NVIDIA DLSS, the Render resolution row at Auto, the game starting at the output's size
Run before(false, false, true), with(true, false, true);
load_in(before, o[0], o[1], 100), load_in(with, o[0], o[1], 100);
CHECK(before.made == 2 && before.refused == 0); // (the one-size feature, then the scaled mode's)
CHECK(with.made == 1 && with.refused == 0); // (the scaled mode's from the first frame)
CHECK(with.f.mode == 0 && !with.f.one_size && !with.f.exact && with.f.w == o[0] && with.f.h == o[1]);
CHECK(before.f.mode == with.f.mode && before.f.w == with.f.w && before.f.h == with.f.h && before.f.exact == with.f.exact);
// NVIDIA DLAA: the output's one size, either way, and never a second feature at that size
Run aa_before(false, true, false), aa_with(true, true, false);
for (int i = 0; i < 40; i++) aa_before.frame(o[0], o[1], o[0], o[1]), aa_with.frame(o[0], o[1], o[0], o[1]);
CHECK(aa_before.made == 1 && aa_with.made == 1 && aa_before.f.one_size && aa_with.f.one_size);
CHECK(aa_with.f.w == o[0] && aa_with.f.h == o[1] && aa_with.f.mode == -1);
// a fixed share (the row's Quality, 8 twelfths rounded up as the menu does): the rule changes nothing
const uint32_t rw = (o[0] * 8 + 11) / 12, rh = o[1] * 8 / 12;
Run fixed_before(false, false, false), fixed_with(true, false, false);
for (int i = 0; i < 40; i++) fixed_before.frame(rw, rh, o[0], o[1]), fixed_with.frame(rw, rh, o[0], o[1]);
CHECK(fixed_before.made == 1 && fixed_with.made == 1 && fixed_with.f.exact && fixed_with.f.w == rw && fixed_with.f.h == rh);
CHECK(fixed_before.f.mode == fixed_with.f.mode && fixed_before.f.w == fixed_with.f.w);
// DLSS with a fixed share of all of the output (one saved with the game's own): one size before, the scaled mode's with
Run full_before(false, false, false), full_with(true, false, false);
for (int i = 0; i < 40; i++) full_before.frame(o[0], o[1], o[0], o[1]), full_with.frame(o[0], o[1], o[0], o[1]);
CHECK(full_before.made == 1 && full_with.made == 1 && full_before.f.one_size && !full_with.f.one_size && full_with.f.mode == 0);
}
std::printf("the row's Auto from the output's size down: 2 features before, 1 with the rule, at %d outputs\n",
(int)(sizeof outs / sizeof outs[0]));
return fails ? 1 : 0;
}
"""
def check_source():
dlss = (VK / "vk_dlss.cpp").read_text(encoding="utf-8")
prepare = dlss[dlss.index("int prepare(VkCommandBuffer cmd"):]
prepare = prepare[:prepare.index("\n}\n")]
check('static const bool dlaa_rule = dlss_checks::prewarm_rule(getenv("RX_VK_DLSS_PREWARM"));' in prepare,
"prepare does not read RX_VK_DLSS_PREWARM once")
one = "const bool aa = dlss_checks::dlaa_feature(dlaa_rule, temporal_mode() == upscaler_rows::DLAA, rw, rh, ow, oh);"
check(one in prepare and "const bool aa = rw == ow && rh == oh;" not in prepare and prepare.count("const bool aa =") == 1,
"prepare's one-size test is not dlaa_feature's, with the switch and the Upscaler's choice")
check(prepare.index(one) < prepare.index("const NVSDK_NGX_PerfQuality_Value* modes = aa ? same : scaled;")
< prepare.index("dlss_feature::size(aa, "), "prepare picks its modes or its size before the one-size test")
checks = (VK / "dlss_checks.h").read_text(encoding="utf-8")
check("return rule ? full && dlaa : full;" in checks and "return text && *text && std::atoi(text) == 1;" in checks,
"dlss_checks.h: the rule is not off unless RX_VK_DLSS_PREWARM=1, or is not the entry's")
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("| `RX_VK_DLSS_PREWARM=1` |" in docs, "docs/development.md has no row for RX_VK_DLSS_PREWARM")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="dlss-prewarm-") as tmp:
src, exe = Path(tmp) / "prewarm.cpp", Path(tmp) / "prewarm.exe"
src.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I", str(VK), str(src), "-o", str(exe)], check=True)
out = subprocess.run([str(exe)], stdout=subprocess.PIPE, text=True, timeout=60)
print(out.stdout.strip())
if out.returncode:
raise SystemExit("FAIL: the rule's run")
print("PASS: with RX_VK_DLSS_PREWARM=1 DLSS at the output's size takes a scaled mode (one feature for the row's Auto); "
"off, every decision is the one from before")
if __name__ == "__main__":
main()

View File

@@ -48,7 +48,8 @@ def main():
ns = dict(os=os, hashlib=hashlib, kit=SimpleNamespace(ROOT=str(root)), NGX_SDK=str(sdk_path) if sdk_path else "",
vk=str(vk), GPU=str(root / "gpu"), vk_jobs=[], cxx=a.cxx, obj_dir=str(root / "obj"),
vflags=["-std=c++17", "-O0"], imgui_flags=[], imgui_users=(), shader_stamp="", stamp_def="",
vk_lib_deps=b"", logo_text=b"", imgui_deps=b"", vk_sources=lambda: [str(src), str(other)])
vk_lib_deps=b"", logo_text=b"", imgui_deps=b"", vk_sources=lambda: [str(src), str(other)],
vk_pgo=[], VK_PGO="") # ((Triforce, 11-21, LIFT-10) the renderer profile's flags and switch, set before the loop: none here
exec(code, ns)
return {Path(j[1]).name: (ns["object_key"](j[1], j[3], j[4]), j) for j in ns["vk_jobs"]}

View File

@@ -0,0 +1,186 @@
"""NVIDIA DLSS frame generation's camera matrices are in the clip space of the depth it is given (gpu/vk/dlss_math.h
depth_clip_projection and depth_clip_reprojection, vk_dlssg.cpp frame, fsr3_math.h Camera::flip_depth; no game, no GPU).
(Triforce, 11-20) The owner's "DLSS flicker": the whole picture and the shadows flickered while the camera moved, with
NVIDIA DLSS frame generation on and not without it. Streamline takes its matrices in the clip space of its depth buffer:
the depth is clip z over w, 1 at the near plane with depthInverted. It was given the game's own projection and
reprojection, whose clip z is NVN's (-1 at the near plane, 1 at the far one), beside FSR's copy of the depth (1 at the
near plane, 0 far). Read with those matrices, that depth put what is far a fifth of a metre from the camera and what is
near at the far plane: wrong for everything as soon as the camera moved, and right while it stood. Only DLSS frame
generation takes matrices, so no other path showed it.
This compiles dlss_math.h and fsr3_math.h alone and checks, with the game's projection (near 0.1, far 1000.53, 45
degrees) and two cameras a step and a turn apart:
- a point's clip z over w through depth_clip_projection is the depth FSR's copy holds for it (1 - d where the game's
buffer has 0 at the near plane; d itself where it is reversed already), 1 at the near plane and 0 at the far one;
- with the game's own matrix that depth is not the point's clip z (the error the flicker came from);
- a pixel and its depth through depth_clip_reprojection land on the same pixel of the frame before as the game's own
reprojection puts the point, with the frame before's depth; the identity (a reset) stays the identity;
- Streamline's linear depth for an inverted buffer, 1 / depth, grows with the distance.
And reads the source: frame gives Streamline the converted matrices and their inverses, by the frame's flip_depth, with
depthInverted, and RX_VK_DLSSG_CLIP=0 is in the switches table.
python -I -B tests/test_dlssg_depth_clip.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include "dlss_math.h"
#include "fsr3_math.h"
using namespace mkwvk;
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
static bool near(double a, double b, double tol) { return std::fabs(a - b) <= tol; }
static void mul(const float a[16], const float b[16], float out[16]) {
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++) {
double v = 0;
for (int k = 0; k < 4; k++) v += (double)a[r * 4 + k] * b[k * 4 + c];
out[r * 4 + c] = (float)v;
}
}
static void apply(const float m[16], const double p[4], double out[4]) {
for (int r = 0; r < 4; r++) out[r] = m[r * 4] * p[0] + m[r * 4 + 1] * p[1] + m[r * 4 + 2] * p[2] + m[r * 4 + 3] * p[3];
}
/// NVN's projection, row major (clip = P * view position, the camera looking down -z): clip z -1 at near, 1 at far;
/// reversed: 1 at near, -1 at far (a buffer with 1 at the near plane)
static void projection(float p[16], float n, float f, bool reversed) {
std::memset(p, 0, 16 * sizeof(float));
const float t = 1.0f / std::tan(0.5f * 0.785398f);
p[0] = t / 2.369f, p[5] = t;
p[10] = -(f + n) / (f - n), p[11] = -2.0f * f * n / (f - n);
if (reversed) p[10] = -p[10], p[11] = -p[11];
p[14] = -1.0f;
}
/// A view matrix: a turn of a radians about y, then a move of the camera by (x, 0, z)
static void view(float v[16], float a, float x, float z) {
std::memset(v, 0, 16 * sizeof(float));
v[0] = std::cos(a), v[2] = -std::sin(a), v[5] = 1, v[8] = std::sin(a), v[10] = std::cos(a), v[15] = 1;
v[3] = -(v[0] * x + v[2] * z), v[11] = -(v[8] * x + v[10] * z);
}
static void run(bool reversed) {
const float n = 0.1f, f = 1000.53f;
float p[16], vn[16], vb[16], cur[16], prev[16], game_to_prev[16], dp[16], dr[16];
projection(p, n, f, reversed);
view(vn, 0.02f, 0.3f, 0.1f), view(vb, 0.0f, 0.0f, 0.0f); // (now: a small turn and a step; before: at the origin)
mul(p, vn, cur), mul(p, vb, prev);
CHECK(dlss_math::reprojection(cur, prev, game_to_prev));
fsr3_math::Camera cam;
CHECK(fsr3_math::camera_from_projection(p, cam));
CHECK(cam.flip_depth == !reversed && near(cam.near_plane, n, 1e-4));
dlss_math::depth_clip_projection(p, cam.flip_depth, dp);
dlss_math::depth_clip_reprojection(game_to_prev, cam.flip_depth, dr);
double last_linear = 0;
const double dist[] = {0.1, 0.5, 2.0, 15.0, 120.0, 900.0, 1000.53};
for (double v : dist) {
// (a point v in front of this frame's camera, off the axis: in its view space)
const double pv[4] = {0.31 * v, -0.17 * v, -v, 1.0};
double c[4], d[4];
apply(p, pv, c), apply(dp, pv, d);
const double ndc = c[2] / c[3], stored = (ndc + 1.0) * 0.5; // (the game's depth buffer: fsr3_math.h)
const double copy = cam.flip_depth ? 1.0 - stored : stored; // (FSR's copy: what frame generation is given)
CHECK(near(d[2] / d[3], copy, 2e-5));
CHECK(near(d[0] / d[3], c[0] / c[3], 1e-6) && near(d[1] / d[3], c[1] / c[3], 1e-6) && near(d[3], c[3], 1e-6));
CHECK(copy > -1e-4 && copy < 1.0 + 1e-4);
if (v > 0.2 && v < 900.0) CHECK(std::fabs(ndc - copy) > 0.3); // (the game's clip z is not that depth)
if (copy > 1e-6) { // (Streamline's linear depth of an inverted buffer)
CHECK(1.0 / copy > last_linear);
last_linear = 1.0 / copy;
}
// (the pixel and its depth to the frame before: as the game's own reprojection puts the point)
const double game_clip[4] = {c[0] / c[3], c[1] / c[3], ndc, 1.0}, depth_clip[4] = {c[0] / c[3], c[1] / c[3], copy, 1.0};
double g[4], s[4];
apply(game_to_prev, game_clip, g), apply(dr, depth_clip, s);
CHECK(near(s[0] / s[3], g[0] / g[3], 2e-4) && near(s[1] / s[3], g[1] / g[3], 2e-4));
const double before_stored = (g[2] / g[3] + 1.0) * 0.5, before_copy = cam.flip_depth ? 1.0 - before_stored : before_stored;
CHECK(near(s[2] / s[3], before_copy, 2e-4));
if (v > 0.5 && v < 120.0) { // (and what the game's own matrix makes of that depth: somewhere else, by far more)
double w[4];
apply(game_to_prev, depth_clip, w);
CHECK(std::fabs(w[0] / w[3] - g[0] / g[3]) > 20 * 2e-4);
}
}
// (the near and far planes: 1 and 0)
const double pn[4] = {0, 0, -n, 1}, pf[4] = {0, 0, -f, 1};
double a[4], b[4];
apply(dp, pn, a), apply(dp, pf, b);
CHECK(near(a[2] / a[3], 1.0, 1e-4) && near(b[2] / b[3], 0.0, 1e-4));
// (a reset: the identity stays the identity)
float id[16], out[16];
for (int i = 0; i < 16; i++) id[i] = i % 5 == 0 ? 1.0f : 0.0f;
dlss_math::depth_clip_reprojection(id, cam.flip_depth, out);
for (int i = 0; i < 16; i++) CHECK(near(out[i], id[i], 1e-6));
}
int main() {
run(false); // (the game's: 0 at the near plane, flipped for FSR's copy)
run(true); // (a buffer reversed already)
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
sl = (VK / "vk_dlssg.cpp").read_text(encoding="utf-8")
frame = block(sl, "void frame(VkCommandBuffer cmd, uint32_t out_width, uint32_t out_height) {")
check("dlss_math::depth_clip_projection(in.projection, in.camera.flip_depth, projection);" in frame and
"dlss_math::depth_clip_reprojection(in.clip_to_prev, in.camera.flip_depth, clip_to_prev);" in frame,
"frame: the matrices are not brought into the clip space of the depth Streamline is given")
check("if (!inverse(projection, clip_to_view) || !inverse(clip_to_prev, prev_to_clip)) {" in frame,
"frame: the inverses are not the converted matrices'")
check("matrix(c.cameraViewToClip, projection);" in frame and "matrix(c.clipToPrevClip, clip_to_prev);" in frame and
"matrix(c.clipToCameraView, clip_to_view);" in frame and "matrix(c.prevClipToClip, prev_to_clip);" in frame,
"frame: Streamline is not given the converted matrices")
check("matrix(c.cameraViewToClip, in.projection);" not in frame and "matrix(c.clipToPrevClip, in.clip_to_prev);" not in frame,
"frame: Streamline is still given the game's own matrices")
check("c.depthInverted = sl::Boolean::eTrue;" in frame, "frame: the depth is FSR's copy, 1 at the near plane")
check('getenv("RX_VK_DLSSG_CLIP")' in frame, "frame: no switch for the game's own matrices, for comparing")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_DLSSG_CLIP=0`" in dev, "docs/development.md: RX_VK_DLSSG_CLIP=0 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="dlssg-depth-clip-") as tmp:
src, exe = Path(tmp) / "clip.cpp", Path(tmp) / "clip.exe"
src.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(src), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the matrices in the depth's clip space:\n" + r.stdout)
print("PASS: DLSS frame generation's matrices are in the clip space of the depth it is given")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,208 @@
"""No udmabuf dma-bufs are offered to NVIDIA's proprietary driver on Linux (gpu/vk/dmabuf_gate.h; no game, no GPU).
(Triforce, 11-22, the performance audit's OS-08, after Source A's.) On Linux each pool of guest memory is first offered
to the driver as a dma-buf made by udmabuf. NVIDIA's proprietary driver refuses every one and the pool ends as a
host-pointer import anyway, after a refused import each (one took 198 ms during a load). With that driver
/dev/udmabuf is no longer opened.
Compiles the rule and import_dma_buf's first-call block, cut out of gpu/vk/vk_memory.cpp, over stand-ins for the
device, dlsym, open, fopen, the environment and the log (so it runs on Windows too), and checks:
RADV, NVK /dev/udmabuf opened once, "yes", pieces of 64 MB
NVIDIA's proprietary driver not opened, "no (NVIDIA's driver refuses them: host pointers)", no pieces line
... with RX_VK_DMABUF_NVIDIA=1 opened, "yes" (as before); =0: not opened
RADV, /dev/udmabuf does not open "no (/dev/udmabuf cannot be opened)"
an anonymous window, RX_VK_NO_DMABUF, not opened, each with its own reason (and before NVIDIA's, when both hold)
no dma-buf import on the device
And reads the source: the rule is asked before the open, the answer comes from the header, the fd guard of a failed
import is still there; the switches table has the row.
python -I -B tests/test_dmabuf_driver_gate.py --headers <Vulkan-Headers>/include [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}", "--headers", "{vulkan_headers}"]
def block(text, marker, start=0):
at = text.find(marker, start)
if at < 0:
raise SystemExit("FAIL: vk_memory.cpp has no '%s'" % marker)
end = text.index("{", at) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[at:end]
FIXTURE = r'''#define VK_NO_PROTOTYPES
#include <vulkan/vulkan_core.h>
#include <algorithm>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <string>
#include <vector>
#include "dmabuf_gate.h"
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
static int checks, fails;
#define CHECK(x) do { checks++; if (!(x)) { fails++; printf("FAIL line %d: %s (logged: %s)\n", __LINE__, #x, logged.c_str()); } } while (0)
// stand-ins: the device, the host's memfd function, /dev/udmabuf, the limit file, the environment, the log
static struct { bool dma_buf_import = true, sparse_buffers = true; VkDriverId driver_id{}; } dev;
static bool vkGetMemoryFdPropertiesKHR = true; // (the fd properties call is there)
static int t_memfd = 5, t_opens, t_dlsyms;
static bool t_open_ok = true;
static std::string logged;
static const char *t_no_dmabuf, *t_piece_mb, *t_nvidia_too;
static int memfd_fn() { return t_memfd; }
#define RTLD_DEFAULT nullptr
static void* dlsym(void*, const char* name) {
if (strcmp(name, "hos_posix_guest_memfd")) printf("FAIL: dlsym(%s)\n", name), exit(1);
t_dlsyms++;
return (void*)memfd_fn;
}
static int t_open(const char* path, int) {
if (strcmp(path, "/dev/udmabuf")) printf("FAIL: opened %s\n", path), exit(1);
t_opens++;
return t_open_ok ? 9 : -1;
}
static FILE* t_fopen(const char*, const char*) { return nullptr; } // (the limit file not readable: 64 MB)
static const char* t_getenv(const char* name) {
if (!strcmp(name, "RX_VK_NO_DMABUF")) return t_no_dmabuf;
if (!strcmp(name, "RX_VK_DMABUF_PIECE_MB")) return t_piece_mb;
if (!strcmp(name, "RX_VK_DMABUF_NVIDIA")) return t_nvidia_too;
printf("FAIL: getenv(%s)\n", name), exit(1);
}
static void logf(const char* fmt, ...) {
char line[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(line, sizeof line, fmt, ap);
va_end(ap);
logged += line, logged += "\n";
}
#define open t_open
#define fopen t_fopen
#define getenv t_getenv
static int dmabuf_memfd = -2, dmabuf_dev = -1;
static uint64_t dmabuf_limit = 0;
static uint64_t piece;
static void first_call() {
@CUT@
}
// One start. Returns the number of opens
static int start(VkDriverId driver, bool open_ok = true, int memfd = 5, bool ext = true, const char* no_dmabuf = nullptr,
const char* nvidia_too = nullptr) {
dev.driver_id = driver, dev.dma_buf_import = ext;
t_open_ok = open_ok, t_memfd = memfd, t_no_dmabuf = no_dmabuf, t_nvidia_too = nvidia_too, t_piece_mb = nullptr;
t_opens = t_dlsyms = 0, logged.clear();
dmabuf_memfd = -2, dmabuf_dev = -1, dmabuf_limit = 0, piece = 0;
first_call();
CHECK(dmabuf_memfd != -2); // (decided once)
return t_opens;
}
static bool said(const char* what) { return logged.find(what) != std::string::npos; }
int main() {
using dmabuf_gate::Facts;
// the rule by itself
Facts f;
f.fn = f.memfd = f.ext = true;
CHECK(dmabuf_gate::try_udmabuf(f) && !strcmp(dmabuf_gate::answer(f, true), "yes"));
CHECK(!strcmp(dmabuf_gate::answer(f, false), "no (/dev/udmabuf cannot be opened)"));
f.nvidia = true;
CHECK(!dmabuf_gate::try_udmabuf(f) && dmabuf_gate::try_udmabuf_before(f)); // (as it was: offered to NVIDIA's too)
CHECK(!strcmp(dmabuf_gate::answer(f, false), "no (NVIDIA's driver refuses them: host pointers)"));
CHECK(strcmp(dmabuf_gate::answer(f, true), "yes")); // (never "yes" where it is not tried)
f.ext = false;
CHECK(!dmabuf_gate::try_udmabuf(f) && !strcmp(dmabuf_gate::answer(f, false), "no (VK_EXT_external_memory_dma_buf)"));
f.memfd = false;
CHECK(!strcmp(dmabuf_gate::answer(f, false), "no (the window is anonymous memory)"));
f.fn = false;
CHECK(!strcmp(dmabuf_gate::answer(f, false), "no (no memfd window, or RX_VK_NO_DMABUF)"));
CHECK(!dmabuf_gate::try_udmabuf(Facts{}) && !dmabuf_gate::try_udmabuf_before(Facts{}));
// the first-call block of import_dma_buf
CHECK(start(VK_DRIVER_ID_MESA_RADV) == 1 && dmabuf_memfd == 5 && dmabuf_dev == 9);
CHECK(said("vk: guest memory as dma-bufs: yes\n") && said("vk: dma-bufs of at most 64 MB (udmabuf's limit 64 MB)"));
CHECK(piece == 64ull << 20 && dmabuf_limit == 64ull << 20);
CHECK(start(VK_DRIVER_ID_MESA_NVK) == 1 && dmabuf_memfd == 5 && said("dma-bufs: yes\n"));
CHECK(start(VK_DRIVER_ID_AMD_OPEN_SOURCE) == 1 && said("dma-bufs: yes\n"));
CHECK(start(VK_DRIVER_ID_NVIDIA_PROPRIETARY) == 0 && dmabuf_memfd == -1 && dmabuf_dev == -1);
CHECK(said("vk: guest memory as dma-bufs: no (NVIDIA's driver refuses them: host pointers)\n") && !said("dma-bufs of at most"));
CHECK(t_dlsyms == 1); // (the window's memfd is still asked for: only the offer is skipped)
CHECK(start(VK_DRIVER_ID_NVIDIA_PROPRIETARY, true, 5, true, nullptr, "1") == 1 && dmabuf_memfd == 5 && said("dma-bufs: yes\n"));
CHECK(start(VK_DRIVER_ID_NVIDIA_PROPRIETARY, true, 5, true, nullptr, "0") == 0 && said("NVIDIA's driver refuses them"));
CHECK(start(VK_DRIVER_ID_MESA_RADV, false) == 1 && dmabuf_memfd == -1 && said("no (/dev/udmabuf cannot be opened)\n"));
CHECK(start(VK_DRIVER_ID_MESA_RADV, true, -1) == 0 && dmabuf_memfd == -1 && said("no (the window is anonymous memory)\n"));
CHECK(start(VK_DRIVER_ID_MESA_RADV, true, 5, false) == 0 && said("no (VK_EXT_external_memory_dma_buf)\n"));
CHECK(start(VK_DRIVER_ID_MESA_RADV, true, 5, true, "1") == 0 && t_dlsyms == 0 && said("no (no memfd window, or RX_VK_NO_DMABUF)\n"));
// NVIDIA's with an anonymous window, or RX_VK_NO_DMABUF: the earlier reason is the one said
CHECK(start(VK_DRIVER_ID_NVIDIA_PROPRIETARY, true, -1) == 0 && said("no (the window is anonymous memory)\n"));
CHECK(start(VK_DRIVER_ID_NVIDIA_PROPRIETARY, true, 5, true, "1") == 0 && said("no (no memfd window, or RX_VK_NO_DMABUF)\n"));
if (fails) printf("dmabuf driver gate: FAILED (%d of %d checks)\n", fails, checks);
else printf("dmabuf driver gate: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check_source():
src = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
if '#include "dmabuf_gate.h"' not in src:
raise SystemExit("FAIL: vk_memory.cpp does not include dmabuf_gate.h")
fn_at = src.find("bool import_dma_buf(uint64_t lo, uint64_t hi, Pool& p) {")
if fn_at < 0:
raise SystemExit("FAIL: vk_memory.cpp has no import_dma_buf")
first = block(src, " if (dmabuf_memfd == -2) {", fn_at)
order = [first.find(n) for n in (
"gate.nvidia = dev.driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&",
'if (dmabuf_gate::try_udmabuf(gate)) dmabuf_dev = open("/dev/udmabuf", O_RDWR | O_CLOEXEC);',
'logf("vk: guest memory as dma-bufs: %s", dmabuf_gate::answer(gate, dmabuf_dev >= 0));')]
if min(order) < 0 or order != sorted(order) or first.count("open(") != 2: # (the open and the limit file's fopen)
raise SystemExit("FAIL: import_dma_buf's first call does not ask dmabuf_gate.h before its one open of "
"/dev/udmabuf, or its line is not the header's answer (%r)" % (order,))
guard = block(src, "VkDeviceMemory dma_buf_memory(uint64_t lo, uint64_t size) {")
if "fcntl(fd, F_DUPFD_CLOEXEC, 1024)" not in guard or "if (fcntl(fd, F_GETFD) != -1) close(fd);" not in guard:
raise SystemExit("FAIL: dma_buf_memory lost the guard for a failed import's fd (other drivers and layers need it)")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `RX_VK_DMABUF_NVIDIA=1`, `RX_VK_NO_DMABUF=1`, `RX_VK_DMABUF_PIECE_MB=<n>` |" not in docs or "`gpu/vk/dmabuf_gate.h`" not in docs:
raise SystemExit("FAIL: docs/development.md: the switches table has no RX_VK_DMABUF_NVIDIA row")
return first
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="g++")
parser.add_argument("--headers", required=True, help="Vulkan-Headers' include folder")
args = parser.parse_args()
first = check_source()
with tempfile.TemporaryDirectory(prefix="dmabuf-gate-") as tmp:
cpp, exe = Path(tmp) / "dmabuf_gate_test.cpp", Path(tmp) / "dmabuf-gate.exe"
cpp.write_text(FIXTURE.replace("@CUT@", first), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function",
"-I" + args.headers, "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the dma-buf driver gate's checks failed")
if __name__ == "__main__":
main()

266
tests/test_dmb_light.py Normal file
View File

@@ -0,0 +1,266 @@
"""DMB of the load or the store kind as a compiler barrier in code lifted with A64_DMB_LIGHT=1 (game-free).
(Triforce, 11-22, the performance audit's LIFT-47, after Source B's.) The kit's translator writes every DMB and DSB as
recomp_barrier(), an out-of-line full fence. x86-64 keeps load-load, load-store and store-store order in hardware, so a
DMB that asks only for those (ISHLD, ISHST and the OSH, NSH and full-system forms: eight words) needs no instruction
there, only that the compiler moves no guest access across it. With A64_DMB_LIGHT=1 at lift time nx2/insn.py writes
A64_DMB_LDST() for those eight, which nx2/rtgen.py's a64.h defines as an empty asm with a "memory" clobber on x86-64 and
as recomp_barrier() on any other host. Off by default: unset, empty or 0 is the kit's text.
The rule (nx2/insn.py emit, dmb_light): with the switch the eight words give the macro; a full DMB (ISH, SY, OSH, NSH),
every DSB (the LD and ST kinds too, SSBB, PSSBB), ISB and CLREX give None, the kit's text; without the switch, or with
it empty or 0, every word gives None.
The lifted text: each word goes through the real translator (a64emit.translate under insn.install(), then
a64_lift.op_text) twice, with A64_DMB_LIGHT=1 and without. The eight must be the macro alone with the switch and the
kit's recomp_barrier() without; the others must be the same text both times. The kit's mark grouping
(a64_lift.mark_groups) must end a run of stores at the macro, as it does at the fence: no mark moves across it.
The run: tests/dmb_light_test.c runs every word both ways, alone and between a store and a load of the same guest
address (STR; DMB; LDR; STR; DMB; LDR), from the same random Ctx (every flag combination, registers and vector
registers random) and guest memory, and compares the whole Ctx and the bytes stored. It also counts the calls of
recomp_barrier: none in the light text on x86-64, one a barrier in the kit's. Built at -march=x86-64 and -march=native.
What a single thread cannot show, the order other threads see, is not tested here.
And reads the source: nx2/rtgen.py's a64.h holds the macro once, guarded by __x86_64__, with the fence for other
hosts; the switch is an A64_ name, which nx2/make_game.py's lift_key takes, and nx2/insn.py and nx2/rtgen.py are files of
the lift key; the switch is in the switches table.
python -I -B tests/test_dmb_light.py [--cc clang] [--cxx clang++] [--random 20000]
Skipped (exit 0) on a host that is not x86-64 or has no C or C++ compiler.
"""
import argparse
import os
import platform
import re
import sys
import tempfile
import time
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
sys.path.insert(0, os.path.join(ROOT, "nx2"))
import test_fpx_sticky as fs # noqa: E402 (the build helpers the kit's other lifted-code tests use)
RUN_ALL_ARGS = ["--cc", "clang", "--cxx", "{cxx}"]
SEED = 20261122
MARCHES = ("x86-64", "native")
SWITCH = "A64_DMB_LIGHT"
MACRO = "A64_DMB_LDST();"
KINDS = {0: "OSH", 1: "NSH", 2: "ISH", 3: "SY"}
STR_X1, LDR_X3, STR_X4_8, LDR_X5_8 = 0xF9000041, 0xF9400043, 0xF9000444, 0xF9400445 # ([x2], [x2], [x2,#8], [x2,#8])
def dmb(crm):
return 0xD50330BF | (crm << 8)
def dsb(crm):
return 0xD503309F | (crm << 8)
def light_words():
"""(name, word): the eight DMBs of the load or store kind"""
return [("DMB %s%s" % (KINDS[crm >> 2], "LD" if crm & 3 == 1 else "ST"), dmb(crm))
for crm in range(16) if crm & 3 in (1, 2)]
def full_words():
"""(name, word): what keeps the fence. A full DMB of every domain, DSBs of every kind, the reserved CRm<1:0> = 00"""
out = [("DMB %s" % KINDS[crm >> 2], dmb(crm)) for crm in (3, 7, 11, 15)]
out += [("DMB crm=%d (reserved)" % crm, dmb(crm)) for crm in (0, 4, 8, 12)]
out += [("DSB %sLD" % KINDS[crm >> 2], dsb(crm)) for crm in (1, 5, 9, 13)]
out += [("DSB %sST" % KINDS[crm >> 2], dsb(crm)) for crm in (2, 6, 10, 14)]
out += [("DSB %s" % KINDS[crm >> 2], dsb(crm)) for crm in (3, 7, 11, 15)]
out += [("SSBB", dsb(0)), ("PSSBB", dsb(4))]
return out
def with_switch(value, fn):
old = os.environ.get(SWITCH)
if value is None:
os.environ.pop(SWITCH, None)
else:
os.environ[SWITCH] = value
try:
return fn()
finally:
if old is None:
os.environ.pop(SWITCH, None)
else:
os.environ[SWITCH] = old
def check_rule(insn):
assert len(light_words()) == 8
assert (dmb(11), dmb(15), dsb(9)) == (0xD5033BBF, 0xD5033FBF, 0xD503399F) # (DMB ISH, DMB SY, DSB ISHLD)
others = full_words() + [("ISB", 0xD5033FDF), ("CLREX", 0xD503305F), ("NOP", 0xD503201F)]
for name, w in light_words():
if with_switch("1", lambda: insn.emit(w)) != MACRO:
raise SystemExit("dmb light: %s (%08x) is not %s with %s=1" % (name, w, MACRO, SWITCH))
for off in (None, "", "0"):
if with_switch(off, lambda: insn.emit(w)) is not None:
raise SystemExit("dmb light: %s (%08x) is rewritten with %s=%r" % (name, w, SWITCH, off))
for name, w in others:
for value in ("1", None):
if with_switch(value, lambda: insn.emit(w)) is not None:
raise SystemExit("dmb light: %s (%08x) is rewritten (%s=%r): it keeps the kit's text" % (name, w, SWITCH, value))
def shapes():
"""(name, guest words, barriers in it, of which light)"""
out = []
for name, w in light_words():
out.append((name, [w], 1, 1))
out.append(("STR; %s; LDR; STR; %s; LDR" % (name, name), [STR_X1, w, LDR_X3, STR_X4_8, w, LDR_X5_8], 2, 2))
for name, w in full_words():
out.append((name, [w], 1, 0))
out.append(("STR; DMB ISHST; LDR; STR; DMB ISH; LDR", [STR_X1, dmb(10), LDR_X3, STR_X4_8, dmb(11), LDR_X5_8], 2, 1))
return out
def lifted(a64emit, a64_lift, used):
"""dmb_lifted.h: each shape as lifted with the switch and without, as functions, and the CASES table"""
all_shapes = shapes()
out = ["/* Generated by tests/test_dmb_light.py: each shape lifted with A64_DMB_LIGHT=1 and without. */"]
for i, (name, words, barriers, light) in enumerate(all_shapes):
items = [(0x1000 + 4 * k, w) for k, w in enumerate(words)]
def lift():
texts, flags = a64emit.translate(items)
if any(flags):
raise SystemExit("dmb light: a64emit did not translate %s: %s" % (name, flags))
return [a64_lift.op_text(t, used).strip() for t in texts]
now, old = with_switch("1", lift), with_switch(None, lift)
for w, n, o in zip(words, now, old):
is_light = (w & 0xFFFFF0FF) == 0xD50330BF and (w >> 8) & 3 in (1, 2)
if is_light and (n != MACRO or "recomp_barrier();" not in o or "A64_DMB_LDST" in o):
raise SystemExit("dmb light: %08x in %s: with the switch %r, without %r" % (w, name, n, o))
if not is_light and (n != o or "A64_DMB_LDST" in n):
raise SystemExit("dmb light: %08x in %s changes with the switch: %r, %r" % (w, name, n, o))
if sum(t.count("recomp_barrier();") for t in old) != barriers or \
sum(t.count("recomp_barrier();") for t in now) != barriers - light:
raise SystemExit("dmb light: %s: the fences in the lifted text are not %d and %d" % (name, barriers, barriers - light))
out.append("static void g_now%d(Ctx *__restrict c) { %s }" % (i, " ".join(now)))
out.append("static void g_old%d(Ctx *__restrict c) { %s }" % (i, " ".join(old)))
out.append("static const struct { const char *name; void (*now)(Ctx *), (*old)(Ctx *); int barriers, light; } CASES[] = {%s};"
% ", ".join('{"%s", g_now%d, g_old%d, %d, %d}' % (s[0], i, i, s[2], s[3]) for i, s in enumerate(all_shapes)))
return "\n".join(out) + "\n", len(all_shapes)
def check_marks(a64_lift):
"""The kit's mark grouping ends a run of stores at the macro, as at the fence: the group's mark stays before it"""
for end in (" " + MACRO, " recomp_barrier();"):
if not a64_lift.mg_ends_run(end):
raise SystemExit("dmb light: mark_groups does not end a run at %r" % end.strip())
body = "\n".join(["void f_80000000(Ctx *A64_NOESC c) {", " recomp_store32(c,c->x[21] + 0,c->x[1]);",
" recomp_store32(c,c->x[21] + 4,c->x[2]);", end, " recomp_store32(c,c->x[21] + 8,c->x[3]);",
" return;", "}", ""])
got = a64_lift.mark_groups(body).split("\n")
at = got.index(end)
if "A64_MARK(" not in got[at - 1] or any("A64_MARK(" in line for line in got[at + 1:at + 2]):
raise SystemExit("dmb light: mark_groups moved a mark across %r:\n%s" % (end.strip(), "\n".join(got)))
def generate(cxx, tmp, flags):
"""a64.h (nx2/rtgen.py's, in tmp/rt64), a64_fpx.h, the kit's runtime C and dmb_lifted.h, by the generators the lift
runs (a64emit built into tmp). Returns (the shapes, rt64, the runtime's C file)"""
dll = os.path.join(tmp, "a64emit.dll" if os.name == "nt" else "liba64emit.so")
fs.build([cxx, "-std=c++20", "-O0", "-shared", "-Wno-everything", "-o", dll, os.path.join(fs.KIT, "lift", "a64emit.cpp")]
+ flags + ([] if os.name == "nt" else ["-fPIC"]), "a64emit")
import kit # noqa: F401 (a64-kit on the path)
import build_emit
build_emit.build = lambda force=False: dll # (not a64emit.dll beside the source)
import a64_lift
import a64_rtgen
import a64emit
import insn
import rtgen
check_rule(insn)
insn.install() # (the override point under test, around the kit's translate)
check_marks(a64_lift)
used = set()
try:
a64_rtgen.write(tmp)
text, n = lifted(a64emit, a64_lift, used)
finally:
if a64emit._lib is not None: # (unloaded, so the folder can go)
import _ctypes
(_ctypes.FreeLibrary if os.name == "nt" else _ctypes.dlclose)(a64emit._lib._handle)
a64emit._lib = None
with open(os.path.join(tmp, "dmb_lifted.h"), "w", newline="\n") as f:
f.write(text)
with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f:
f.write(a64_lift.fpx_header(used))
rt64 = rtgen.write(os.path.join(tmp, "rt64"))
with open(os.path.join(rt64, "a64.h"), encoding="utf-8") as f:
header = f.read()
want = re.compile(r"#if defined\(__x86_64__\)\n#define A64_DMB_LDST\(\) __asm__ __volatile__\(\"\" ::: \"memory\"\)\n"
r"#else\n#define A64_DMB_LDST\(\) recomp_barrier\(\)\n#endif\n")
if len(want.findall(header)) != 1 or header.count("#define A64_DMB_LDST") != 2:
raise SystemExit("dmb light: nx2/rtgen.py's a64.h does not define A64_DMB_LDST once, guarded by __x86_64__")
rt_c = [f for f in sorted(os.listdir(tmp)) if f.startswith("a64_") and f.endswith(".c")] # (what a64_rtgen wrote)
if len(rt_c) != 1:
raise SystemExit("dmb light: a64_rtgen wrote %s, not one C file" % rt_c)
return n, rt64, os.path.join(tmp, rt_c[0])
def check_sources():
with open(os.path.join(ROOT, "nx2", "insn.py"), encoding="utf-8") as f:
insn_text = f.read()
for line in (' return (w & 0xFFFFF0FF) == 0xD50330BF and ((w >> 8) & 3) in (1, 2) and '
'os.environ.get("A64_DMB_LIGHT", "0") not in ("", "0")',
' return "A64_DMB_LDST();" if dmb_light(w) else None'):
if insn_text.count(line) != 1:
raise SystemExit("dmb light: nx2/insn.py no longer has the line %r" % line.strip())
with open(os.path.join(ROOT, "nx2", "rtgen.py"), encoding="utf-8") as f:
if "(RET_MISMATCH, RET_MISMATCH + POLL_DECL + DMB_LDST)," not in f.read():
raise SystemExit("dmb light: nx2/rtgen.py no longer adds DMB_LDST to a64.h")
with open(os.path.join(ROOT, "nx2", "make_game.py"), encoding="utf-8") as f:
make_game = f.read()
# (the lift key: every A64_ variable of the environment, and every file of nx2/ but LIFT_KEY_NOT's)
if 'if k.startswith(("A64_", "OOT_", "RX_"))' not in make_game or not SWITCH.startswith("A64_"):
raise SystemExit("dmb light: nx2/make_game.py's lift_key no longer takes the A64_ variables: %s is not in the key" % SWITCH)
not_keyed = re.search(r"^LIFT_KEY_NOT = \((.*?)\) #", make_game, re.S | re.M)
if not not_keyed or '"insn.py"' in not_keyed.group(1) or '"rtgen.py"' in not_keyed.group(1):
raise SystemExit("dmb light: nx2/insn.py and nx2/rtgen.py must stay files of the lift key")
with open(os.path.join(ROOT, "docs", "development.md"), encoding="utf-8") as f:
if "| `A64_DMB_LIGHT=1` |" not in f.read():
raise SystemExit("dmb light: docs/development.md: A64_DMB_LIGHT is not in the switches table")
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--cc")
p.add_argument("--cxx")
p.add_argument("--random", type=int, default=20000)
a = p.parse_args()
check_sources()
if platform.machine().lower() not in ("x86_64", "amd64"):
fs.skip("not an x86-64 host: %s" % platform.machine())
cc = fs.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang")
cxx = fs.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++")
if not cc or not cxx:
fs.skip("no C compiler" if not cc else "no C++ compiler")
t0 = time.time()
failed = []
with tempfile.TemporaryDirectory(prefix="triforce-dmb-light-") as tmp:
n, rt64, rt_c = generate(cxx, tmp, fs.target_flags(cxx, tmp))
for march in MARCHES:
exe = os.path.join(tmp, "dmb_light_test_%s.exe" % march)
fs.build([cc] + fs.target_flags(cc, tmp) + ["-march=" + march] + fs.FLAGS +
["-D_CRT_SECURE_NO_WARNINGS", "-I" + rt64, "-I" + os.path.join(fs.KIT, "rt"), "-I" + tmp,
os.path.join(ROOT, "tests", "dmb_light_test.c"), rt_c, "-o", exe], "the driver at " + march)
r = fs.run([exe, str(a.random), str(SEED)], timeout=1200)
print("%-10s %s" % (march, r.stdout.rstrip()))
if r.returncode:
failed.append("%s (exit %d)" % (march, r.returncode))
if failed:
raise SystemExit("FAIL: dmb light: %s" % ", ".join(failed))
print("PASS: %d shapes lifted with A64_DMB_LIGHT=1 leave the same registers, flags and memory as the kit's text, "
"the eight light DMBs without a fence (%.0f s)" % (n, time.time() - t0))
if __name__ == "__main__":
main()

View File

@@ -35,6 +35,11 @@ def main():
raise SystemExit("a vertex set remembers an empty size as empty")
if "gpu_bind_stamp() == vstamp" not in draw:
raise SystemExit("a vertex set is remembered across a mirror")
# (a mesh pipeline's bind leaves the vertex strides undefined: the next vertex draw binds all 16 streams again)
bind = draw.index("vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pl)")
rebind = draw.find("if (p.mesh && mesh_vb_rebind) last_vb_valid = false, vertex_set.on = false;", bind)
if rebind < 0 or rebind > draw.index("vertex_set_same(", bind):
raise SystemExit("a mesh pipeline's bind keeps the vertex streams and strides of the draws before it")
if draw.count("bind_index(") != 3:
raise SystemExit("an indexed draw binds its index buffer on its own")
bound = block(source, "void bind_index(")

View File

@@ -25,6 +25,7 @@ FIXTURE = r"""
#include <thread>
#include <vector>
#include "pipe_wait.h"
#include "translate_wait.h"
#define PROF_EXACT() ((void)0)
#define PROF_HOT(x) ((void)0)
#define SKIP_HERE() return
@@ -54,13 +55,14 @@ struct Stages {
};
struct Program {
std::atomic<bool> ready{false};
uint64_t key = 0; // ((Triforce, PIPE-30) read by the bounded wait)
bool failed = false, fallback = false, fallback_task = false;
int layout = 1, fallback_topology = 0;
Stages stages{&ready, {}};
};
struct PipeEntry { std::atomic<bool> ready{false}; VkPipeline pipeline = 1; };
struct Targets { bool persistent = true; };
struct NvnState {};
struct Targets { bool persistent = true; void* depth = nullptr; }; // ((Triforce, PIPE-29) depth: read by the budget)
struct NvnState { struct { bool dt = false; } ds; };
struct ParLock {};
struct DrawArgs { bool indirect = true; int groups[3]{1, 1, 1}; int first_task = 0; };
int fallback_task_groups(const Program&) { std::abort(); }
@@ -101,6 +103,21 @@ uint64_t texture_frame_no() { return 1; }
pipe_wait::Pool& pipe_pool() { static pipe_wait::Pool p(50 * pipe_wait::kMs); return p; }
pipe_wait::Pool& pipe_last() { static pipe_wait::Pool p(40 * pipe_wait::kMs); return p; }
bool pipe_wait_for(const std::atomic<bool>&, int64_t) { std::abort(); }
// ((Triforce, 11-22, PIPE-30) RX_VK_TRANSLATE_WAIT_MS unset, as here by default: the block waits as before.
// tests/test_translate_wait.py runs it set)
int translate_wait_ms() { return 0; }
translate_wait::Notes& translate_notes() { static translate_wait::Notes n; return n; }
pipe_wait::Pool& translate_pool() { static pipe_wait::Pool p; return p; }
bool jobs_wait_for(const std::atomic<bool>&, int) { std::abort(); }
std::atomic<int64_t> st_translate_bounded{0}, st_translate_left{0};
// ((Triforce, 11-22, PIPE-29) RX_VK_PIPE_DEPTH_BUDGET unset, as here by default: depth-tested draws into new passes wait
// as before. tests/test_pipe_depth_budget.py checks the rule set)
bool pipe_depth_budget() { return false; }
bool occ_counting_now() { return false; }
bool pipe_depth_timed_out(const void*) { std::abort(); }
void pipe_depth_time_out(const void*) { std::abort(); }
std::atomic<int64_t> st_pipe_depth_waits{0}, st_pipe_depth_left{0};
void logf(const char*, ...) {}
int64_t st_pipe_new_waits, st_pipe_full_waits, st_pipe_full_held, st_pipe_full_left;
bool exposure_log_on() { return false; }
void exposure_note(NvnState&, const Program&) {}

View File

@@ -35,7 +35,7 @@ def main():
actual += "\n" + block(draw, "BufferRef stream_copy(")
fixture = fixture.replace("/* STREAM */", actual)
flush = block(draw, "void draw_flush_recording(") if "void draw_flush_recording(" in draw else ""
fixture = fixture.replace("/* FLUSH */", flush).replace("/* BEGIN */", block(draw, "void draw_begin_recording("))
fixture = fixture.replace("/* FLUSH */", flush).replace("/* BEGIN */", block(draw, "void slot_reset_locked(") + "\n" + block(draw, "void draw_begin_recording("))
fixture = fixture.replace("/* SUBMIT */", block(replay, "void submit_recording("))
start = replay.index(" if (j->inits < 0) init_lost = true;")
end = replay.index(" if (res == VK_ERROR_DEVICE_LOST) device_lost();", start)

View File

@@ -0,0 +1,188 @@
"""A texture the GPU drew first gets its page map at that draw (gpu/vk/drawn_page_map.h, vk_texture.cpp texture_written and
texture_dirty; no game, no GPU).
(Triforce, 11-21, the performance audit's TEX-21, after Source A's.) A texture in a virtual pool is checked through its
mapping, and its page map was recorded at its first check. One the GPU drew first had none: once the game finalized it,
the first check took its whole storage as mapped since and uploaded it from memory over what the GPU drew.
This compiles drawn_page_map.h and texture_dirty cut out of vk_texture.cpp as it is, over a mapping and write generations
of the test's own:
- without the seed, the first check of a drawn texture gives its whole storage (the fault);
- seeded as texture_written seeds it (generation 0), the same check gives nothing; a CPU write gives its pages alone; a
page mapped anew gives that page alone;
- the rule: which writes seed (a reason, a virtual pool, no map of the storage's size yet) and the switch.
And reads the source: texture_written seeds by the rule, under the parallel replay's lock, with generation 0, before the
texture is marked GPU-written and uploaded; every caller that draws or copies gives a reason; the switch is in the
switches table.
python -I -B tests/test_drawn_page_map.py [--cxx clang++]
"""
import argparse
import re
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def block(text, head):
at = text.index(head)
return text[at:text.index("\n}\n", at) + 3]
FIXTURE = r"""
#include <algorithm>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "drawn_page_map.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using Ranges = std::vector<std::pair<uint64_t, uint64_t>>;
struct WriteGen {
uint64_t v = 0;
constexpr WriteGen() = default;
constexpr explicit WriteGen(uint64_t x) : v(x) {}
constexpr explicit operator bool() const { return v != 0; }
};
struct Texture {
uint32_t id = 1, width = 256, height = 256, layers = 1;
int nvn_format = 0x25;
bool gpu_written = false, uploaded = false;
uint64_t gpu = 0, guest = 0, guest_size = 0, map_sig = 0;
std::vector<uint64_t> page_map;
struct Run { uint64_t off, guest, n; };
std::vector<Run> runs;
uint32_t runs_maps_gen = ~0u;
};
// the test's mapping: a virtual address to guest memory, 64 KB a page; and what the CPU wrote, with its generation
static std::map<uint64_t, uint64_t> mapping;
static uint32_t maps_gen = 1;
struct Host {
uint32_t maps_generation() const { return maps_gen; }
uint32_t vmap_generation() const { return 0; }
uint64_t gpu_guest(uint64_t a) const {
const auto it = mapping.find(a & ~0xFFFFull);
return it == mapping.end() ? 0 : it->second + (a & 0xFFFF);
}
};
static Host host_obj;
static Host* host = &host_obj;
struct Write { uint64_t lo, hi, gen; };
static std::vector<Write> writes;
static void memory_written_runs(uint64_t guest, uint64_t n, WriteGen gen, Ranges& out, bool = false) {
for (const Write& w : writes)
if (w.gen > gen.v && w.lo < guest + n && w.hi > guest)
out.push_back({std::max(w.lo, guest) - guest, std::min(w.hi, guest + n) - guest});
std::sort(out.begin(), out.end());
}
static uint64_t mix(uint64_t h, uint64_t v) { return (h ^ v) * 0x100000001b3ull + 0x9E3779B97F4A7C15ull; }
static std::map<uint64_t, std::unique_ptr<Texture>> textures;
static void logf(const char*, ...) {}
// (vk_texture.cpp's own, cut out by the test)
@TEXTURE_DIRTY@
static Texture drawn() { // (a 256 KB texture in a virtual pool, four pages, mapped to guest memory that is not in one piece)
Texture t;
t.gpu = (1ull << 40) + 0x200000, t.guest_size = 4 * 65536;
return t;
}
int main() {
using namespace drawn_page_map;
// the rule
const uint64_t V = kVirtual + 0x200000;
CHECK(on(nullptr) && on("") && on("1") && !on("0"));
CHECK(pages(1) == 1 && pages(65536) == 1 && pages(65537) == 2 && pages(4 * 65536) == 4);
CHECK(seeds(true, "a draw", V, 4 * 65536, 0));
CHECK(!seeds(false, "a draw", V, 4 * 65536, 0)); // (the switch)
CHECK(!seeds(true, "?", V, 4 * 65536, 0)); // (no reason given: a clear, the movie upscaler)
CHECK(!seeds(true, nullptr, V, 4 * 65536, 0));
CHECK(!seeds(true, "a draw", 0x80000000, 4 * 65536, 0)); // (not a virtual pool: checked in guest memory directly)
CHECK(!seeds(true, "a draw", V, 0, 0)); // (no storage)
CHECK(!seeds(true, "a draw", V, 4 * 65536, 4)); // (the map is there: once a texture)
CHECK(seeds(true, "a draw", V, 4 * 65536, 3)); // (a map of another size is none)
mapping = {{V, 0x10000000}, {V + 65536, 0x10010000}, {V + 2 * 65536, 0x30000000}, {V + 3 * 65536, 0x30010000}};
writes = {{0x10000000, 0x10001000, 5}}; // (the CPU wrote a page of it long ago, generation 5)
Ranges out;
{ // without the seed: drawn, finalized at generation 9, and its first check takes the whole storage
Texture t = drawn();
CHECK(texture_dirty(t, WriteGen{9}, out) && out.size() == 1 && out[0].first == 0 && out[0].second == 4 * 65536);
}
{ // seeded at the draw, as texture_written does: generation 0
Texture t = drawn();
CHECK(seeds(true, "a draw", t.gpu, t.guest_size, t.page_map.size()));
Ranges seeded;
texture_dirty(t, WriteGen{}, seeded);
CHECK(t.page_map.size() == 4 && t.page_map[0] == 0x10000000 && t.page_map[2] == 0x30000000);
CHECK(!seeds(true, "a draw", t.gpu, t.guest_size, t.page_map.size())); // (once)
CHECK(!texture_dirty(t, WriteGen{9}, out) && out.empty()); // (finalized, checked: nothing to upload)
writes.push_back({0x30010000, 0x30012000, 12}); // (the CPU writes two 4 KB pages after that)
CHECK(texture_dirty(t, WriteGen{9}, out) && out.size() == 1 && out[0].first == 3 * 65536 && out[0].second == 3 * 65536 + 0x2000);
mapping[V + 65536] = 0x50000000, maps_gen++; // (and one of its pages is mapped anew)
CHECK(texture_dirty(t, WriteGen{12}, out) && out.size() == 1 && out[0].first == 65536 && out[0].second == 2 * 65536);
CHECK(!texture_dirty(t, WriteGen{12}, out)); // (taken once)
}
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
written = block(tex, "void texture_written(Texture* tex, const char* why) {")
at = 0
for part in ('static const bool drawn_map = drawn_page_map::on(getenv("RX_VK_DRAWN_PAGE_MAP"));',
"if (drawn_page_map::seeds(drawn_map, why, tex->gpu, tex->guest_size, tex->page_map.size())) {",
"ParLock lk;", "texture_dirty(*tex, WriteGen{}, seeded);", "tex->gpu_written = true;", "tex->uploaded = true;"):
at = written.find(part, at)
check(at >= 0, "texture_written does not record a drawn texture's page map by the rule, under the parallel replay's lock, "
"with generation 0, before it marks the texture written and uploaded (missing, or out of order: " + part + ")")
at += len(part)
check('#include "drawn_page_map.h"' in tex, "vk_texture.cpp does not use the rule")
# (the writers that draw or copy say who they are; none of them passes the "?" that keeps the old path)
reasons = 0
for name in ("vk_draw.cpp", "vk_xfer.cpp"):
src = (VK / name).read_text(encoding="utf-8")
for m in re.finditer(r"\btexture_written\(([^;]*)\);", src):
call = m.group(1)
check('"?"' not in call, name + ": a writer passes \"?\" itself: say who writes, or give no reason")
reasons += call.count('"') >= 2
check(reasons >= 8, "the writers that give a reason were not found (%d): the scan no longer reads the source" % reasons)
check('void texture_written(Texture* tex, const char* why = "?");' in (VK / "vk_texture.h").read_text(encoding="utf-8"),
"texture_written's default reason is no longer \"?\": a write without one would seed")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_DRAWN_PAGE_MAP=0`" in dev, "docs/development.md: RX_VK_DRAWN_PAGE_MAP is not in the switches table")
with tempfile.TemporaryDirectory(prefix="drawn-page-map-") as tmp:
c, exe = Path(tmp) / "map.cpp", Path(tmp) / "map.exe"
c.write_text(FIXTURE.replace("@TEXTURE_DIRTY@", block(tex, "bool texture_dirty(Texture& tex, WriteGen gen, Ranges& out, bool same_ok = false) {")),
encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Wno-unused-function", "-Wno-unused-variable", "-Werror",
"-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the map at the draw:\n" + r.stdout)
print("PASS: a texture the GPU drew first gets its page map at that draw")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,215 @@
"""The folder __GL_SHADER_DISK_CACHE_PATH names is made before Vulkan loads; the Linux binary keeps its name (no game).
(Triforce, 11-22, the performance audit's PIPE-98, after Source A's.) NVIDIA's Linux driver keeps no shader cache at
all when __GL_SHADER_DISK_CACHE_PATH names a folder that is not there: every prepared pipeline is compiled again at
each start. The Linux main makes the folder and its parents (0700) and never changes the value. The driver files its
cache by the program's file name, so that name is pinned here too.
Cuts driver_cache_folder out of host/hos_main.c and runs it on real folders under a temporary one (mkdir through a
stand-in that notes the mode, so it runs on Windows too), over an environment of the test's own:
a missing folder three levels deep made, every mkdir with 0700, no line
a folder that is there left as it is, with what it holds, no line
a trailing slash, a double slash made
a path under a file, a file at that name not made, one "is not a folder and cannot be made one" line
an empty value, or unset nothing made, nothing asked of the file system
HOS_DRIVER_CACHE_FOLDER=0 nothing made, no line
And reads the source: the function sets no variable; the last main calls it after the env-file loader and before
hos_display_init; game_main logs the kept line; nx2/build.py still has EXE = "triforce" if LINUX and
__GL_SHADER_DISK_CACHE_APP_NAME appears nowhere in host/ or gpu/vk as something set; the switches table has the row.
python -I -B tests/test_driver_cache_folder.py [--cc clang]
"""
import argparse
import re
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cc", "clang"]
def block(text, marker):
start = text.find(marker)
if start < 0:
raise SystemExit("FAIL: host/hos_main.c has no '%s'" % marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
FIXTURE = r'''#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#endif
#define CHECK(x) do { checks++; if (!(x)) { fprintf(stdout, "FAIL:%d %s (line kept: \"%s\")\n", __LINE__, #x, driver_folder_line); fails++; } } while (0)
static int checks, fails;
static int t_mkdirs, t_other_modes;
static int t_mkdir(const char *path, int mode) { /* (the real folder, and the mode asked for noted) */
t_mkdirs++, t_other_modes += mode != 0700;
#ifdef _WIN32
return _mkdir(path);
#else
return mkdir(path, (mode_t)mode);
#endif
}
/* the test's own environment */
static const char *t_path, *t_off;
static char *t_getenv(const char *name) {
if (!strcmp(name, "__GL_SHADER_DISK_CACHE_PATH")) return (char *)t_path;
if (!strcmp(name, "HOS_DRIVER_CACHE_FOLDER")) return (char *)t_off;
fprintf(stdout, "FAIL: the environment was asked for %s\n", name);
exit(1);
}
#define getenv t_getenv
#define mkdir(path, mode) t_mkdir(path, mode)
@CUT@
#undef getenv
#undef mkdir
static int is_dir(const char *path) { struct stat st; return stat(path, &st) == 0 && S_ISDIR(st.st_mode); }
static int is_file(const char *path) { struct stat st; return stat(path, &st) == 0 && S_ISREG(st.st_mode); }
static const char *root;
static const char *under(const char *rest) {
static char p[8][1024];
static int k;
k = (k + 1) % 8;
snprintf(p[k], sizeof p[k], "%s/%s", root, rest);
return p[k];
}
static void start(const char *path, const char *off) {
t_path = path, t_off = off, t_mkdirs = t_other_modes = 0, driver_folder_line[0] = 0;
driver_cache_folder();
CHECK(t_other_modes == 0); /* (for the user alone) */
}
int main(int argc, char **argv) {
if (argc < 2) return 2;
root = argv[1]; /* (a temporary folder holding "there" with "kept.bin" in it, and the file "afile") */
start(under("a/b/c"), NULL); /* missing, three levels deep */
CHECK(is_dir(under("a")) && is_dir(under("a/b")) && is_dir(under("a/b/c")) && !driver_folder_line[0] && t_mkdirs >= 3);
start(under("a/b/c"), NULL); /* and again: there already */
CHECK(is_dir(under("a/b/c")) && !driver_folder_line[0]);
start(under("there"), NULL); /* a folder that is there, with a file in it */
CHECK(is_dir(under("there")) && is_file(under("there/kept.bin")) && !driver_folder_line[0]);
start(under("t1/t2/"), NULL); /* a trailing slash */
CHECK(is_dir(under("t1/t2")) && !driver_folder_line[0]);
start(under("d1//d2"), NULL); /* a double slash */
CHECK(is_dir(under("d1/d2")) && !driver_folder_line[0]);
start(under("afile/sub"), NULL); /* under a file */
CHECK(!is_dir(under("afile/sub")) && is_file(under("afile")));
CHECK(strstr(driver_folder_line, "boot: __GL_SHADER_DISK_CACHE_PATH=") && strstr(driver_folder_line, "afile/sub is not a folder and cannot be made one"));
start(under("afile"), NULL); /* a file at that name */
CHECK(is_file(under("afile")) && strstr(driver_folder_line, "afile is not a folder and cannot be made one"));
start("", NULL); /* empty, unset: nothing asked of the file system */
CHECK(t_mkdirs == 0 && !driver_folder_line[0]);
start(NULL, NULL);
CHECK(t_mkdirs == 0 && !driver_folder_line[0]);
start(under("off/x"), "0"); /* the switch: as before, nothing made */
CHECK(t_mkdirs == 0 && !is_dir(under("off")) && !driver_folder_line[0]);
start(under("off/x"), "1");
CHECK(is_dir(under("off/x")) && !driver_folder_line[0]);
start(under("afile/sub"), "0"); /* and nothing said */
CHECK(t_mkdirs == 0 && !driver_folder_line[0]);
{ /* a path longer than the function copies: not made, said */
static char longp[2200];
size_t n = (size_t)snprintf(longp, sizeof longp, "%s/long", root);
while (n < 1500) longp[n++] = 'x';
longp[n] = 0;
start(longp, NULL);
CHECK(t_mkdirs == 0 && strstr(driver_folder_line, "is not a folder and cannot be made one"));
}
if (fails) printf("driver cache folder: FAILED (%d of %d checks)\n", fails, checks);
else printf("driver cache folder: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check_source(main_c):
at = main_c.rfind("\nint main(int argc, char **argv) {")
if at < 0:
raise SystemExit("FAIL: host/hos_main.c has no main")
body = main_c[at:main_c.index("\n}\n", at)]
loader = re.search(r"\n load_\w+_env\(\);\n", body) # (the env-file loader's call: the switch can be in that file)
order = [loader.start() if loader else -1] + [body.find(n) for n in (" driver_cache_folder();", "hos_display_init();")]
if min(order) < 0 or order != sorted(order):
raise SystemExit("FAIL: the Linux main does not call driver_cache_folder() after the env-file loader and before "
"hos_display_init() (%r)" % (order,))
game = main_c[main_c.index("static int game_main(int argc, char **argv) {"):at]
order = [game.find(n) for n in ('hos_log("triforce %s",', 'if (driver_folder_line[0]) hos_log("%s", driver_folder_line);',
"rx_prepare_shaders()")]
if min(order) < 0 or order != sorted(order):
raise SystemExit("FAIL: game_main does not log the kept line after the version line, before the preparation")
fn = block(main_c, "static void driver_cache_folder(void) {")
if "setenv" in fn or "putenv" in fn:
raise SystemExit("FAIL: driver_cache_folder() changes the environment (the variable's value must stay)")
if fn.count("mkdir(dir, 0700);") != 2:
raise SystemExit("FAIL: driver_cache_folder() no longer makes each parent and the folder for the user alone")
before = main_c[:main_c.index("static void driver_cache_folder(void) {")]
# (the block for the systems other than Windows: #ifndef _WIN32, or the Linux block driver_env_choose is in)
if max(before.rfind("#ifndef _WIN32\n"), before.rfind("#ifdef __linux__\n")) < before.rfind("#endif"):
raise SystemExit("FAIL: driver_cache_folder() is not in the block for the systems other than Windows")
# (the driver files its cache by the program's file name: a rename starts every Linux NVIDIA player's cache over)
build = (ROOT / "nx2/build.py").read_text(encoding="utf-8")
if '\nEXE = "triforce" if LINUX else ' not in build:
raise SystemExit('FAIL: nx2/build.py no longer has EXE = "triforce" if LINUX: renaming the Linux binary starts '
"every NVIDIA player's driver shader cache over")
for path in sorted((ROOT / "host").rglob("*.c")) + sorted((ROOT / "gpu/vk").glob("*.cpp")):
text = path.read_text(encoding="utf-8", errors="replace")
if re.search(r'(setenv|putenv)\(\s*"__GL_SHADER_DISK_CACHE_APP_NAME', text):
raise SystemExit("FAIL: %s sets __GL_SHADER_DISK_CACHE_APP_NAME: every player's cache would start over" % path.name)
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `HOS_DRIVER_CACHE_FOLDER=0` |" not in docs or "`__GL_SHADER_DISK_CACHE_PATH`" not in docs:
raise SystemExit("FAIL: docs/development.md: the switches table has no HOS_DRIVER_CACHE_FOLDER row")
if "The game builds as `Triforce.exe` + `triforce_vk.dll` (`triforce` + `triforce_vk.so` on Linux)." not in docs:
raise SystemExit("FAIL: docs/development.md does not give the game's file names as nx2/build.py builds them")
return "static char driver_folder_line[1200];\n" + fn
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cc", default="clang")
args = parser.parse_args()
main_c = (ROOT / "host/hos_main.c").read_text(encoding="utf-8")
if "static char driver_folder_line[1200];\nstatic void driver_cache_folder(void) {" not in main_c:
raise SystemExit("FAIL: host/hos_main.c has no driver_cache_folder() with its kept line")
fn = check_source(main_c)
with tempfile.TemporaryDirectory(prefix="driver-cache-folder-") as tmp:
tmp = Path(tmp)
src, exe = tmp / "driver_cache_folder_test.c", tmp / "driver-cache-folder.exe"
src.write_text(FIXTURE.replace("@CUT@", fn), encoding="utf-8")
subprocess.run([args.cc, "-std=c11", "-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-Werror", str(src),
"-o", str(exe)], check=True)
root = tmp / "root"
(root / "there").mkdir(parents=True)
(root / "there" / "kept.bin").write_bytes(b"kept")
(root / "afile").write_bytes(b"a file")
r = subprocess.run([str(exe), root.as_posix()], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the driver cache folder's checks failed")
if (root / "there" / "kept.bin").read_bytes() != b"kept" or (root / "afile").read_bytes() != b"a file":
raise SystemExit("FAIL: a file that was there was changed")
lines = r.stderr.splitlines()
if len(lines) != 3 or not all(l.startswith("boot: __GL_SHADER_DISK_CACHE_PATH=") for l in lines):
raise SystemExit("FAIL: stderr has %d lines, 3 expected (under a file, a file at that name, too long):\n%s"
% (len(lines), r.stderr))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,176 @@
"""Room in the driver's shader cache for the pipelines the game prepares (Linux; no game, no Linux needed to check).
(Triforce, 11-22, the performance audit's PIPE-97, after Source A's and Source B's.) The game builds its ~41,400
predicted pipelines into the driver's own disk cache. NVIDIA's and Mesa's caches hold 1 GiB unless told otherwise and
trim themselves to it, so part of a full preparation is dropped and compiled again in play. The Linux main sets both
drivers' sizes to 10 GiB before Vulkan loads, without overwriting a size the player set.
Cuts driver_cache_room out of host/hos_main.c and runs it over a small environment of the test's own:
nothing set __GL_SHADER_DISK_CACHE_SIZE=10737418240 (at least 4 GiB, in bytes) and
MESA_SHADER_CACHE_MAX_SIZE=10G, both set without overwriting
the player's own sizes kept, each by itself
HOS_DRIVER_CACHE_ROOM=0 neither set, the line says so; =1 or empty: set
__GL_SHADER_DISK_CACHE_SKIP_CLEANUP never set
one line on stderr, kept for the log
And reads the source: the last main calls it after the env-file loader and before hos_display_init, so the builder's
--prepare-shaders run gets it too; game_main logs the kept line; the switches table has the row.
python -I -B tests/test_driver_cache_room.py [--cc clang]
"""
import argparse
import re
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cc", "clang"]
def block(text, marker):
start = text.find(marker)
if start < 0:
raise SystemExit("FAIL: host/hos_main.c has no '%s'" % marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
FIXTURE = r'''#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHECK(x) do { checks++; if (!(x)) { fprintf(stdout, "FAIL:%d %s (line kept: \"%s\")\n", __LINE__, #x, driver_room_line); fails++; } } while (0)
static int checks, fails;
/* the test's own environment */
static const char *const t_names[] = {"HOS_DRIVER_CACHE_ROOM", "__GL_SHADER_DISK_CACHE_SIZE", "MESA_SHADER_CACHE_MAX_SIZE"};
static char t_values[3][128];
static int t_set[3], t_overwrites, t_sets;
static int t_index(const char *name) {
for (int i = 0; i < 3; i++)
if (!strcmp(name, t_names[i])) return i;
fprintf(stdout, "FAIL: the environment was asked for %s\n", name); /* (SKIP_CLEANUP, APP_NAME or any other) */
exit(1);
}
static char *t_getenv(const char *name) { const int i = t_index(name); return t_set[i] ? t_values[i] : NULL; }
static int t_setenv(const char *name, const char *value, int overwrite) {
const int i = t_index(name);
t_sets++, t_overwrites += overwrite != 0;
if (t_set[i] && !overwrite) return 0;
t_set[i] = 1;
snprintf(t_values[i], sizeof t_values[i], "%s", value);
return 0;
}
static void put(const char *name, const char *value) {
const int i = t_index(name);
t_set[i] = value != NULL;
if (value) snprintf(t_values[i], sizeof t_values[i], "%s", value);
}
#define getenv t_getenv
#define setenv t_setenv
@CUT@
#undef getenv
#undef setenv
static int is(const char *name, const char *want) {
const char *v = t_getenv(name);
return want ? v && !strcmp(v, want) : !v;
}
static void start(const char *room, const char *nv, const char *mesa) {
put("HOS_DRIVER_CACHE_ROOM", room), put("__GL_SHADER_DISK_CACHE_SIZE", nv), put("MESA_SHADER_CACHE_MAX_SIZE", mesa);
driver_room_line[0] = 0, t_sets = t_overwrites = 0;
driver_cache_room();
CHECK(t_overwrites == 0); /* (never over a size the player set) */
CHECK(is("HOS_DRIVER_CACHE_ROOM", room));
}
int main(void) {
start(NULL, NULL, NULL); /* out of the box */
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "10737418240") && is("MESA_SHADER_CACHE_MAX_SIZE", "10G") && t_sets == 2);
CHECK(strtoull(t_getenv("__GL_SHADER_DISK_CACHE_SIZE"), NULL, 10) >= 4ull << 30); /* (in bytes, well past the 1 GiB default) */
CHECK(strstr(driver_room_line, "__GL_SHADER_DISK_CACHE_SIZE=10737418240, MESA_SHADER_CACHE_MAX_SIZE=10G"));
CHECK(!strncmp(driver_room_line, "boot: ", 6));
start(NULL, "2147483648", NULL); /* the player's own size for NVIDIA's stays; Mesa's is still set */
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "2147483648") && is("MESA_SHADER_CACHE_MAX_SIZE", "10G"));
CHECK(strstr(driver_room_line, "__GL_SHADER_DISK_CACHE_SIZE=2147483648, MESA_SHADER_CACHE_MAX_SIZE=10G"));
start(NULL, NULL, "2G");
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "10737418240") && is("MESA_SHADER_CACHE_MAX_SIZE", "2G"));
start(NULL, "1", "1K");
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "1") && is("MESA_SHADER_CACHE_MAX_SIZE", "1K"));
start("0", NULL, NULL); /* the switch: as before, nothing set */
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", NULL) && is("MESA_SHADER_CACHE_MAX_SIZE", NULL) && t_sets == 0);
CHECK(strstr(driver_room_line, "left as they are (HOS_DRIVER_CACHE_ROOM=0)"));
start("0", "5", "6");
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "5") && is("MESA_SHADER_CACHE_MAX_SIZE", "6") && t_sets == 0);
start("1", NULL, NULL);
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "10737418240") && is("MESA_SHADER_CACHE_MAX_SIZE", "10G"));
start("", NULL, NULL);
CHECK(is("__GL_SHADER_DISK_CACHE_SIZE", "10737418240") && is("MESA_SHADER_CACHE_MAX_SIZE", "10G"));
if (fails) printf("driver cache room: FAILED (%d of %d checks)\n", fails, checks);
else printf("driver cache room: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check_source(main_c):
at = main_c.rfind("\nint main(int argc, char **argv) {")
if at < 0:
raise SystemExit("FAIL: host/hos_main.c has no main")
body = main_c[at:main_c.index("\n}\n", at)]
loader = re.search(r"\n load_\w+_env\(\);\n", body) # (the env-file loader's call: the switch can be in that file)
order = [loader.start() if loader else -1] + [body.find(n) for n in (" driver_cache_room();", "hos_display_init();")]
if min(order) < 0 or order != sorted(order):
raise SystemExit("FAIL: the Linux main does not call driver_cache_room() after the env-file loader and before "
"hos_display_init() (%r)" % (order,))
if body.find(" driver_cache_room();") > body.find("if (!strcmp(argv[i], \"--prepare-shaders\")) prepare_shaders = 1;"):
raise SystemExit("FAIL: driver_cache_room() is called after the --prepare-shaders parse")
game = main_c[main_c.index("static int game_main(int argc, char **argv) {"):at]
order = [game.find(n) for n in ('hos_log("triforce %s",', 'if (driver_room_line[0]) hos_log("%s", driver_room_line);',
"rx_prepare_shaders()")]
if min(order) < 0 or order != sorted(order):
raise SystemExit("FAIL: game_main does not log the kept line after the version line, before the preparation")
fn = block(main_c, "static void driver_cache_room(void) {")
for needle in ('setenv("__GL_SHADER_DISK_CACHE_SIZE", "10737418240", 0);', 'setenv("MESA_SHADER_CACHE_MAX_SIZE", "10G", 0);'):
if needle not in fn:
raise SystemExit("FAIL: driver_cache_room() no longer has: " + needle)
if "SKIP_CLEANUP" in fn or "APP_NAME" in fn:
raise SystemExit("FAIL: driver_cache_room() sets more than the two sizes")
before = main_c[:main_c.index("static void driver_cache_room(void) {")]
# (the block for the systems other than Windows: #ifndef _WIN32, or the Linux block driver_env_choose is in)
if max(before.rfind("#ifndef _WIN32\n"), before.rfind("#ifdef __linux__\n")) < before.rfind("#endif"):
raise SystemExit("FAIL: driver_cache_room() is not in the block for the systems other than Windows")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `HOS_DRIVER_CACHE_ROOM=0` |" not in docs or "`__GL_SHADER_DISK_CACHE_SIZE`" not in docs:
raise SystemExit("FAIL: docs/development.md: the switches table has no HOS_DRIVER_CACHE_ROOM row")
return "static char driver_room_line[320];\n" + fn
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cc", default="clang")
args = parser.parse_args()
main_c = (ROOT / "host/hos_main.c").read_text(encoding="utf-8")
if "static char driver_room_line[320];\nstatic void driver_cache_room(void) {" not in main_c:
raise SystemExit("FAIL: host/hos_main.c has no driver_cache_room() with its kept line")
fn = check_source(main_c)
with tempfile.TemporaryDirectory(prefix="driver-cache-room-") as tmp:
tmp = Path(tmp)
src, exe = tmp / "driver_cache_room_test.c", tmp / "driver-cache-room.exe"
src.write_text(FIXTURE.replace("@CUT@", fn), encoding="utf-8")
subprocess.run([args.cc, "-std=c11", "-D_POSIX_C_SOURCE=200809L", "-Wall", "-Wextra", "-Werror", str(src),
"-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the driver cache room's checks failed")
lines = r.stderr.splitlines()
if len(lines) != 8 or not all(l.startswith("boot: ") for l in lines):
raise SystemExit("FAIL: stderr has %d lines, one for each of the 8 starts expected:\n%s" % (len(lines), r.stderr))
if __name__ == "__main__":
main()

243
tests/test_driver_faults.py Normal file
View File

@@ -0,0 +1,243 @@
"""Programs the driver faulted on kept out of it, and one build of a program at a time, on AMD's Windows driver
(gpu/vk/driver_faults.h; vk_draw.cpp fault_quarantine, driver_fault_file_note, guarded_graphics, build_graphics; no game,
no GPU).
(Triforce, 11-22, the performance audit's PIPE-57, after Source A's.) The guards caught a fault in the driver's compiler and
a fault during the start restarted the game, but nothing remembered the program: the next start faulted on it again, and in
play a second build of the same program could go into the driver while the first was still inside. Now, on AMD's own
Windows driver (or everywhere with RX_VK_DRIVER_FAULTS=2), a faulted program is kept out of the driver for the run and, by
a line the fault's handler writes to triforce_driver_faults.txt, for later starts with that driver; and a program has one
build inside the driver at a time. Other drivers: nothing of it. 0: nowhere.
The header is compiled and run:
- the switch, and whether it is on for many made-up PCs (Windows and Linux; AMD's own driver, RADV, AMD's open driver,
NVIDIA's, Intel's, Mesa's, MoltenVK's): on for AMD's own driver on Windows alone at 1, everywhere at 2, nowhere at 0;
this PC (NVIDIA on Windows) has it off, as before;
- the file's line (35 bytes, made without the C library) and its parse: this vendor's and driver's lines only, CRLF
taken, broken lines passed over;
- the gate with 8 threads on one program: never two inside; a probe that may not wait is told busy; a fault keeps the
program out for every later build, said once; a key from the file is kept out from the start.
Then driver_fault_file_note, cut out of vk_draw.cpp, appends the line to a file with the system's own calls, and nothing
without a program in the gate. And the source: the graphics guard clears the handle, notes the fault and writes the line
only where the gate set a program, before driver_fault_restart; build_graphics goes through the gate around the creation
and its retry (none after a fault), ends it before the probe's return, and the rule asks for AMD's own driver; the
switches table has the row.
python -I -B tests/test_driver_faults.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
RULES = r"""
#include <atomic>
#include <cstdio>
#include <cstring>
#include <thread>
#include "driver_faults.h"
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
using namespace driver_faults;
struct Pc { const char* what; bool windows; bool amd_proprietary; bool want1; };
int main() {
CHECK(mode(nullptr) == 1 && mode("") == 1 && mode("0") == 0 && mode("1") == 1 && mode("2") == 2 && mode("5") == 2 && mode("-1") == 0);
const Pc pcs[] = {
{"this PC: NVIDIA RTX, Windows", true, false, false},
{"RX 6800 XT, AMD's Windows driver", true, true, true},
{"RX 5700 XT, AMD's Windows driver", true, true, true},
{"RX 7900 XT, AMD's Windows driver", true, true, true},
{"RX 6600, RADV on Linux", false, false, false},
{"RX 6600, AMD's own driver on Linux", false, true, false},
{"Arc B580, Intel's Windows driver", true, false, false},
{"Iris Xe, Mesa on Linux", false, false, false},
{"GTX 1060, NVIDIA on Linux", false, false, false},
{"RTX 4070, NVIDIA on Windows", true, false, false},
};
for (const Pc& pc : pcs) {
CHECK(on(1, pc.windows, pc.amd_proprietary) == pc.want1);
CHECK(on(2, pc.windows, pc.amd_proprietary));
CHECK(!on(0, pc.windows, pc.amd_proprietary));
}
// the line and its parse
char l[kLine + 1];
CHECK(line(l, 0x1002, 0x800161, 0x6eaa646d07ea4e43ull) == kLine && !strcmp(l, "00001002 00800161 6eaa646d07ea4e43\n"));
std::string file = l;
line(l, 0x1002, 0x800161, 0x5fa56485a6b07619ull);
file += std::string(l, kLine - 1) + "\r\n"; // (CRLF)
line(l, 0x1002, 0x800170, 0x1111ull); // (another driver version)
file += l;
line(l, 0x10de, 0x800161, 0x2222ull); // (another vendor)
file += l;
file += "garbage line\n00001002 00800161 zz\n00001002 00800161 4e5ce183a33b0855"; // (broken, then one with no newline)
const std::vector<uint64_t> keys = parse(file, 0x1002, 0x800161);
CHECK(keys == (std::vector<uint64_t>{0x6eaa646d07ea4e43ull, 0x5fa56485a6b07619ull, 0x4e5ce183a33b0855ull}));
CHECK(parse(file, 0x1002, 0x800170) == std::vector<uint64_t>{0x1111ull} && parse("", 1, 2).empty());
// the gate: 8 threads on one program, never two inside
Quarantine q;
std::atomic<int> inside{0}, most{0}, went{0};
std::vector<std::thread> threads;
for (int t = 0; t < 8; t++)
threads.emplace_back([&] {
for (int i = 0; i < 200; i++) {
if (q.begin(42, true) != Quarantine::Begin::Go) continue;
const int now = ++inside;
int m = most.load();
while (now > m && !most.compare_exchange_weak(m, now)) {}
std::this_thread::yield();
--inside, went++;
q.end(42, false);
}
});
for (auto& th : threads) th.join();
CHECK(most == 1 && went == 1600 && !q.faulted(42));
// a probe that may not wait is told busy; another program goes in beside it
CHECK(q.begin(7, true) == Quarantine::Begin::Go && q.begin(7, false) == Quarantine::Begin::Busy && q.begin(8, false) == Quarantine::Begin::Go);
// a fault: kept out from then on, said once
CHECK(q.end(7, true) && q.faulted(7) && q.begin(7, true) == Quarantine::Begin::Faulted && q.begin(7, false) == Quarantine::Begin::Faulted);
CHECK(!q.end(8, false) && q.count() == 1);
// a waiting build whose program faults while it waits is told so
{
CHECK(q.begin(9, true) == Quarantine::Begin::Go);
std::atomic<int> got{-1};
std::thread waiter([&] { got = (int)q.begin(9, true); });
std::this_thread::sleep_for(std::chrono::milliseconds(30));
CHECK(got == -1); // (waiting at the gate)
q.end(9, true);
waiter.join();
CHECK(got == (int)Quarantine::Begin::Faulted);
}
// keys from the file: kept out from the start
Quarantine f;
f.load(keys);
CHECK(f.begin(0x5fa56485a6b07619ull, true) == Quarantine::Begin::Faulted && f.begin(0x1111ull, true) == Quarantine::Begin::Go && f.count() == 3);
std::printf("ok\n");
return 0;
}
"""
WRITER = r"""
#include <windows.h>
#include <cstdint>
#include <cstdio>
#include <string>
#include "driver_faults.h"
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
thread_local uint64_t fault_key = 0;
uint32_t fault_vendor = 0x1002, fault_driver = 0x800161;
wchar_t fault_file_w[MAX_PATH * 2] = L"";
/* CUT */
static std::string read(const char* path) {
std::string s;
if (FILE* f = fopen(path, "rb")) {
char b[256];
for (size_t n; (n = fread(b, 1, sizeof b, f)) > 0;) s.append(b, n);
fclose(f);
}
return s;
}
int main(int argc, char** argv) {
if (argc < 2) return 2;
const std::string path = std::string(argv[1]) + "/triforce_driver_faults.txt";
MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, fault_file_w, MAX_PATH * 2);
driver_fault_file_note(); // (no program in the gate: nothing)
CHECK(read(path.c_str()).empty());
fault_key = 0x6eaa646d07ea4e43ull;
driver_fault_file_note();
fault_key = 0x5fa56485a6b07619ull;
driver_fault_file_note();
const std::string got = read(path.c_str());
CHECK(got == "00001002 00800161 6eaa646d07ea4e43\n00001002 00800161 5fa56485a6b07619\n");
CHECK(driver_faults::parse(got, 0x1002, 0x800161).size() == 2);
std::printf("ok\n");
return 0;
}
"""
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def check_source(draw):
need('#include "driver_faults.h"' in draw, "vk_draw.cpp does not include driver_faults.h")
rule = block(draw, "bool driver_faults_on() {")
need(rule.count("dev.driver_id == VK_DRIVER_ID_AMD_PROPRIETARY") == 2 and "driver_faults::on(driver_faults_mode(), true," in rule
and "driver_faults::on(driver_faults_mode(), false," in rule, "the rule does not ask for AMD's own driver, on Windows")
need('static const int mode = driver_faults::mode(getenv("RX_VK_DRIVER_FAULTS"));' in block(draw, "int driver_faults_mode() {"),
"the switch is not RX_VK_DRIVER_FAULTS, read once")
guard = block(draw, "__declspec(noinline) VkResult guarded_graphics(")
ordered(guard, ['__except (driver_fault_note(GetExceptionInformation(), "graphics")) {',
"if (fault_key) *pl = VK_NULL_HANDLE, driver_fault_hit = true, driver_fault_file_note();",
"driver_fault_restart();", "return VK_ERROR_UNKNOWN;"],
"the graphics guard does not clear the handle and write the line before the restart, only for a gated build")
quar = block(draw, "driver_faults::Quarantine& fault_quarantine() {")
ordered(quar, ["std::call_once(once,", '"triforce_driver_faults.txt"', "fault_vendor = dev.props.vendorID, fault_driver = dev.props.driverVersion;",
"driver_faults::parse(text, fault_vendor, fault_driver);", "q.load(keys);", "MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, fault_file_w,",
'logf("vk: driver faults: %zu programs'],
"fault_quarantine does not load the file once, make the handler's path or say it")
build = block(draw, "VkPipeline build_graphics(const Program& p, const NvnState& s, const Targets& at, VkPrimitiveTopology topo, int part, bool* cold) {")
ordered(build, ["const bool fault_gated = driver_faults_on();", "if (fault_gated) {",
"fault_quarantine().begin(p.key, cold == nullptr);",
"if (b == driver_faults::Quarantine::Begin::Busy) return *cold = true, VK_NULL_HANDLE;",
"if (b == driver_faults::Quarantine::Begin::Faulted) {", "return VK_NULL_HANDLE;", "fault_key = p.key, driver_fault_hit = false;",
"const PcacheUse use(pcache_door, wanted);", "VkResult r = cached_graphics(cache, &ci, &pl);",
"&& !driver_fault_hit) {", "r = cached_graphics(cache, &ci, &pl);", "if (fault_gated) {", "fault_key = 0;",
"if (fault_quarantine().end(p.key, driver_fault_hit))", "driver_fault_hit = false;",
"if (cold && r == VK_PIPELINE_COMPILE_REQUIRED) return *cold = true, VK_NULL_HANDLE;"],
"build_graphics does not go through the gate around the creation and its retry, or ends it after a return")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
need("`RX_VK_DRIVER_FAULTS=0` or `2`" in docs, "docs/development.md: the switches table has no RX_VK_DRIVER_FAULTS row")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
need("void driver_fault_file_note() {" in draw, "vk_draw.cpp has no driver_fault_file_note")
env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_")}
with tempfile.TemporaryDirectory(prefix="driver-faults-") as tmp:
tmp = Path(tmp)
tests = [("rules", RULES)]
if os.name == "nt": # (the handler's writer is Windows code)
tests.append(("writer", WRITER.replace("/* CUT */", block(draw, "void driver_fault_file_note() {"))))
for name, text in tests:
src, exe = tmp / (name + ".cpp"), tmp / (name + ".exe")
src.write_text(text, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(src), "-o", str(exe)],
check=True)
work = tmp / ("work-" + name)
work.mkdir()
r = subprocess.run([str(exe), str(work).replace("\\", "/")], capture_output=True, text=True, timeout=120, env=env)
need(r.returncode == 0 and r.stdout.strip() == "ok", "driver faults (%s): %s" % (name, r.stdout.strip() or r.returncode))
check_source(draw)
print("PASS: on AMD's Windows driver a faulted program is kept out of the driver (and in the file for later starts), one build "
"of a program at a time; other drivers as before; RX_VK_DRIVER_FAULTS=0 nowhere")
if __name__ == "__main__":
main()

121
tests/test_evict_excess.py Normal file
View File

@@ -0,0 +1,121 @@
"""The 90% and 97% rules take their excess, not an eighth of the cache (gpu/vk/vram_compact.h rule_target's three-argument
form, vk_texture.cpp texture_frame, RX_VK_EVICT_EXCESS; no game/GPU).
With the compaction on, the hold's pass let textures unused for 60 presents go down to 7/8 of the budget the 90% and 97%
rules left. Those budgets are "the textures less the excess", not sizes: 62 MB over the line on an 8 GB card took about
400 MB, all on screen again within the camera's turn, made again, and over the line again. Now the budget's size is kept
apart: 7/8 of the size when over it, and for the two rules their budget less 64 MB. Checked:
1. The rule, compiled from the header: the Market on an 8 GB card (2,873 MB held, a size of 3,285 MB, the 90% rule's budget
2,811 MB) lets 126 MB go, not 413; the size alone as before; both over, the lower; never under nothing.
2. texture_frame: the size taken after the live line and before the 90% rule; the rules' budget after the 90% rule and
through the 97% one; rule_to from both with the switch on, the old form with RX_VK_EVICT_EXCESS=0 or the lab's knob
noexcess, read the way the other switches are (on unless 0); the switch has its docs row.
python -I -B tests/test_evict_excess.py [--cxx clang++]
"""
import argparse
import os
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)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def fail(message):
raise SystemExit("FAIL: " + message)
FIXTURE = r'''#include <cstdint>
#include <cstdio>
#include "vram_compact.h"
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
using namespace vram_compact;
int main() {
const uint64_t MB = 1ull << 20;
// (the Market entered from Hyrule Field, an 8 GB card: 62 MB over the 90% line)
const uint64_t total = 2873 * MB, size = 3285 * MB, rule = 2811 * MB;
CHECK(total - rule_target(total, size, rule) == 126 * MB);
CHECK(total - rule_target(total, rule) == total - rule / 8 * 7); // (the old form: 413 MB)
CHECK(total - rule / 8 * 7 > 413 * MB - MB && total - rule / 8 * 7 < 414 * MB);
// (under every line: nothing; the size alone: 7/8 of it, as the old form; both: the lower)
CHECK(rule_target(rule, size, rule) == UINT64_MAX && rule_target(size, size, size) == UINT64_MAX);
CHECK(rule_target(3500 * MB, size, size) == rule_target(3500 * MB, size));
CHECK(rule_target(3500 * MB, size, 3000 * MB) == size / 8 * 7);
CHECK(rule_target(3500 * MB, size, 2600 * MB) == 2600 * MB - 64 * MB);
CHECK(rule_target(3500 * MB, size, 10 * MB) == 0 && rule_target(3500 * MB, size, 0) == 0);
std::printf("OK\n");
return 0;
}
'''
def check_source(tex, dev):
frame = block(tex, "void texture_frame(uint64_t completed)")
order = ["if (press.live) budget = tex_pressure::budget(", "const uint64_t size_budget = budget;",
"if (known && pressure > allowed / 10 * 9) {", "uint64_t rule_budget = budget;",
"rule_budget = std::min(rule_budget, total_bytes > over ? total_bytes - over : 0);",
"const uint64_t rule_to = ", "texture_sweep(completed, "]
at = -1
for needle in order:
i = frame.find(needle, at + 1)
if i < 0:
fail("texture_frame: %r missing or out of order (the size after the live line and before the 90%% rule, the "
"rules' budget after both rules, rule_to after them)" % needle)
at = i
rule = frame[frame.index("const uint64_t rule_to = "):]
rule = rule[:rule.index(";\n") + 1]
if "vram_compact::rule_target(total_bytes, size_budget, rule_budget)" not in rule or \
"vram_compact::rule_target(total_bytes, rule_budget)" not in rule:
fail("rule_to is not the three-argument rule with the old form for the switch off")
if 'lab_ab::on("noexcess")' not in rule:
fail("the lab's knob noexcess does not turn the rule back")
env = frame[frame.index('getenv("RX_VK_EVICT_EXCESS")') - 120:frame.index("const uint64_t rule_to = ")]
if "return !e || atoi(e) != 0;" not in env:
fail("RX_VK_EVICT_EXCESS is not on unless 0")
if "`RX_VK_EVICT_EXCESS=0`" not in dev:
fail("docs/development.md has no RX_VK_EVICT_EXCESS row")
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
vk = ROOT / "gpu" / "vk"
tex = (vk / "vk_texture.cpp").read_text(encoding="utf-8")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
try:
check_source(tex, dev)
except ValueError as e:
fail("a text this test reads moved (%s)" % e)
with tempfile.TemporaryDirectory(prefix="evict-excess-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / ("fixture" + (".exe" if os.name == "nt" else ""))
cpp.write_text(FIXTURE, encoding="utf-8")
built = subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I", str(vk), str(cpp), "-o", str(exe)],
capture_output=True, text=True)
if built.returncode:
print(built.stdout + built.stderr)
fail("the fixture did not build")
done = subprocess.run([str(exe)], capture_output=True, text=True, timeout=30)
if done.returncode or done.stdout.strip() != "OK":
fail("the rule: " + (done.stdout + done.stderr).strip())
print("PASS: the 90% and 97% rules go down to their budget less 64 MB, the size to 7/8 of it; RX_VK_EVICT_EXCESS=0 and "
"noexcess as before")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,77 @@
// The eviction's stale write-back rule (gpu/vk/evict_reason.h); run by tests/test_evict_reason.py.
// (Triforce, 11-22, the performance audit's TEX-R404, after Source A's.)
#include "../gpu/vk/evict_reason.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 evict_reason;
// the switch: unset, empty and 0 leave a stale write-back out
CHECK("the switch unset", !flush_stale(nullptr) && !flush_stale("") && !flush_stale("0"));
CHECK("the switch set", flush_stale("1") && flush_stale("2"));
// Source A's frames: a target last drawn in frame 3804, a label drawn inside it in 3987
CHECK("a later drawing makes it stale", write_back_stale(3804, 3987));
CHECK("no other drawing", !write_back_stale(3804, 0));
CHECK("an older drawing", !write_back_stale(3804, 1468));
CHECK("a drawing in the same frame keeps it", !write_back_stale(3804, 3804));
CHECK("the frame after", write_back_stale(3804, 3805));
// the trace's addresses: a 1920 x 1080 target of 8,640 KB and a label inside it
const uint64_t target = 0x1001e88fc00ull, target_end = target + 8640ull * 1024;
const uint64_t label = 0x1001f039400ull, label_end = label + 110ull * 110 * 4;
CHECK("the label is inside the target", label > target && label_end < target_end);
CHECK("they overlap, either way round", overlaps(target, target_end, label, label_end) && overlaps(label, label_end, target, target_end));
CHECK("ranges that touch are apart", !overlaps(target, target_end, target_end, target_end + 4096) &&
!overlaps(target_end, target_end + 4096, target, target_end));
CHECK("one byte shared", overlaps(target, target_end, target_end - 1, target_end + 4096));
CHECK("apart", !overlaps(target, target_end, target_end + 4096, target_end + 8192));
CHECK("an empty range overlaps nothing", !overlaps(target, target, target - 1, target_end) && !overlaps(target, target_end, label, label));
CHECK("the same range", overlaps(target, target_end, target, target_end));
// the walk: candidates in any order, the others noted one by one
int self_target = 0, self_label = 0, self_far = 0, other = 0;
std::vector<Candidate> c;
c.push_back({label, label_end, 7, &self_label, 0});
c.push_back({target_end + (64ull << 20), target_end + (65ull << 20), 9, &self_far, 0});
c.push_back({target, target_end, 3, &self_target, 0});
const uint64_t longest = by_start(c);
CHECK("in the order of their starts", c[0].lo == target && c[1].lo == label && c[2].self == &self_far);
CHECK("each keeps its place in the caller's list", c[0].index == 3 && c[1].index == 7 && c[2].index == 9);
CHECK("the longest", longest == 8640ull * 1024);
note(c, longest, target, target_end, 3804, &self_target); // (the target itself)
CHECK("a candidate is not another image over its own storage", c[0].newest == 0);
CHECK("but it is one over the label's", c[1].newest == 3804 && c[2].newest == 0);
note(c, longest, label, label_end, 3987, &self_label); // (the label itself)
CHECK("the label, drawn later, is the target's newest other", c[0].newest == 3987 && c[1].newest == 3804);
CHECK("the target's write-back is stale, the label's is not",
write_back_stale(3804, c[0].newest) && !write_back_stale(3987, c[1].newest));
note(c, longest, target + 4096, target + 8192, 1468, &other); // (an older image inside the target)
CHECK("an older one does not lower it", c[0].newest == 3987);
note(c, longest, target_end, target_end + 4096, 5000, &other); // (one that touches the target's end)
CHECK("one that only touches counts nothing", c[0].newest == 3987 && c[1].newest == 3804 && c[2].newest == 0);
note(c, longest, target - 4096, target, 5000, &other); // (and its start)
CHECK("nor one that ends where it starts", c[0].newest == 3987);
note(c, longest, target - (16ull << 20), target + 1, 4100, &other); // (a long one from far before, a byte into it)
CHECK("a long image from before the start is found", c[0].newest == 4100 && c[1].newest == 3804);
note(c, longest, 0, ~0ull, 4200, &other); // (one over everything)
CHECK("one over everything reaches all", c[0].newest == 4200 && c[1].newest == 4200 && c[2].newest == 4200);
note(c, longest, label, label, 9000, &other); // (an empty one)
CHECK("an empty image counts nothing", c[1].newest == 4200);
{ // (no candidates: nothing to do, nothing read)
std::vector<Candidate> none;
const uint64_t l = by_start(none);
note(none, l, target, target_end, 1, &other);
CHECK("no candidates", l == 0 && none.empty());
}
{ // (many candidates side by side, one other over three of them)
std::vector<Candidate> v;
static int selves[64];
for (int i = 63; i >= 0; i--) v.push_back({0x1000ull * (uint64_t)i, 0x1000ull * (uint64_t)i + 0x1000, (size_t)i, &selves[i], 0});
const uint64_t l = by_start(v);
note(v, l, 0x1000ull * 10 + 0x800, 0x1000ull * 12 + 1, 77, &other);
for (int i = 0; i < 64; i++) CHECK("only the three it covers", v[(size_t)i].newest == (i >= 10 && i <= 12 ? 77u : 0u));
}
std::printf("evict reason: all checks passed\n");
return 0;
}

221
tests/test_evict_reason.py Normal file
View File

@@ -0,0 +1,221 @@
"""An eviction leaves out the write-back of a texture another image drew into since (gpu/vk/evict_reason.h,
vk_texture.cpp texture_sweep and drawn_others; no game, no GPU).
(Triforce, 11-22, the performance audit's TEX-R404, after Source A's.) Over the budget the eviction writes each chosen
texture the GPU drew back into the game's memory. The game draws its HUD labels inside the storage of a 1920 x 1080
target it no longer uses; when the target went, its old texels landed over the labels, and the labels were uploaded from
them as noise.
Three parts:
1. tests/test_evict_reason.cpp against the header: Source A's frames (3804 against 3987 stale; 0, 1468 and 3804 not),
the overlap on the trace's addresses (the target at 0x1001e88fc00, 8,640 KB; the label at 0x1001f039400 inside it;
ranges that touch are apart), the walk's candidates and the switch's values.
2. drawn_others and texture_sweep's block, cut out of vk_texture.cpp and compiled with stand-ins: the target's
write-back is left out and the label's kept, one drawn in the same frame, one that touches, an image the GPU never
drew and one of no size change nothing, the skips are counted and logged; with RX_VK_EVICT_STALE_FLUSH=1 every
write-back stays, as before.
3. the source: the block sits between the loop that chooses and the batches that write back, the bookkeeping loop is
as it was, drawn_others walks the cache and not the pass's ordered list, the switch is in the switches table.
python -I -B tests/test_evict_reason.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
BLOCK_HEAD = ' static const bool stale_flush = evict_reason::flush_stale(getenv("RX_VK_EVICT_STALE_FLUSH"));\n'
BATCHES = " { // (the write-backs: batches of up to 64 textures or ~512 MB of staging memory, each one submission and one wait)\n"
HARNESS = r'''
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <unordered_map>
#include <vector>
#include "evict_reason.h"
struct Texture {
uint64_t gpu = 0, guest_size = 0, last_written = 0;
bool gpu_written = false;
uint32_t id = 0, width = 0, height = 0;
};
static std::unordered_map<int, std::unique_ptr<Texture>> textures;
static int logged;
static void logf(const char* fmt, ...) {
char line[512];
va_list ap;
va_start(ap, fmt);
std::vsnprintf(line, sizeof line, fmt, ap);
va_end(ap);
logged++;
std::printf("log: %s\n", line);
}
struct Evict { Texture* tex; bool flush, shifted; };
@DRAWN_OTHERS@
static uint64_t pass(std::vector<Evict>& evict) {
@BLOCK@
return st_stale_skipped;
}
static int fails;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
static Texture* add(uint64_t gpu, uint64_t size, uint64_t written, bool drawn = true) {
static int ids;
auto t = std::make_unique<Texture>();
t->gpu = gpu, t->guest_size = size, t->last_written = written, t->gpu_written = drawn, t->id = (uint32_t)++ids;
t->width = t->height = 16;
Texture* p = t.get();
textures[ids] = std::move(t);
return p;
}
int main(int argc, char** argv) {
const bool as_before = argc > 1 && argv[1][0] == '1'; // (run with RX_VK_EVICT_STALE_FLUSH=1)
const uint64_t target_at = 0x1001e88fc00ull, target_size = 8640ull * 1024;
Texture* target = add(target_at, target_size, 3804);
Texture* label = add(0x1001f039400ull, 110ull * 110 * 4, 3987); // (inside the target, drawn later)
Texture* beside = add(target_at + target_size, 1 << 20, 3804); // (touches the target's end)
Texture* touching = add(target_at + target_size + (1 << 20), 4096, 3990); // (touches `beside`, drawn later)
Texture* pair_a = add(0x20000000000ull, 1 << 20, 500); // (two drawn in the same frame, overlapping)
Texture* pair_b = add(0x20000000000ull + 4096, 1 << 20, 500);
Texture* cpu = add(0x30000000000ull, 1 << 20, 100);
add(0x30000000000ull + 4096, 4096, 900, false); // (over `cpu`, but never drawn by the GPU)
Texture* sized = add(0x40000000000ull, 1 << 20, 100);
add(0x40000000000ull, 0, 900); // (an image of no size at its start)
Texture* unflushed = add(0x50000000000ull, 1 << 20, 100); // (chosen, not written back: not asked)
add(0x50000000000ull + 4096, 4096, 900);
Texture* older = add(0x60000000000ull, 1 << 20, 800);
add(0x60000000000ull + 8192, 4096, 799); // (inside it, drawn a frame before)
std::vector<Evict> evict = {{beside, true, false}, {label, true, false}, {pair_b, true, false}, {target, true, false},
{pair_a, true, false}, {cpu, true, false}, {sized, true, false}, {unflushed, false, true},
{older, true, false}};
(void)touching;
const uint64_t skipped = pass(evict);
const auto flush_of = [&](const Texture* t) {
for (const Evict& e : evict)
if (e.tex == t) return e.flush;
return false;
};
CHECK(evict.size() == 9 && evict[0].tex == beside && evict[3].tex == target && evict[7].tex == unflushed); // (the list as it was)
CHECK(evict[7].shifted && !evict[7].flush);
if (as_before) {
CHECK(skipped == 0 && logged == 0);
CHECK(flush_of(target) && flush_of(label) && flush_of(beside) && flush_of(pair_a) && flush_of(pair_b) && flush_of(cpu) &&
flush_of(sized) && flush_of(older));
} else {
CHECK(!flush_of(target)); // (the label was drawn inside it since)
CHECK(flush_of(label)); // (the target over it was drawn before it)
CHECK(flush_of(beside)); // (its neighbours only touch it)
CHECK(flush_of(pair_a) && flush_of(pair_b)); // (the same frame keeps both)
CHECK(flush_of(cpu) && flush_of(sized) && flush_of(older));
CHECK(skipped == 1 && logged == 1);
// a second pass: the count goes on, and a write-back already left out is not asked again
std::vector<Evict> again = {{target, false, false}, {older, true, false}};
add(0x60000000000ull + 16384, 4096, 801); // (now a later image inside `older`)
CHECK(pass(again) == 2 && !again[1].flush && !again[0].flush && logged == 2);
// the first 20 are logged, the rest counted
for (int i = 0; i < 30; i++) {
std::vector<Evict> more = {{older, true, false}};
pass(more);
}
std::vector<Evict> last = {{older, true, false}};
CHECK(pass(last) == 33 && logged == 20);
}
if (!fails) std::printf("the eviction's stale write-backs (%s): all checks passed\n", as_before ? "the switch set" : "the default");
return fails != 0;
}
'''
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def function(text, head):
start = text.index(head)
return text[start:text.index("\n}\n", start) + 3]
def check_source(tex):
check('#include "evict_reason.h"' in tex, "vk_texture.cpp does not include evict_reason.h")
others = function(tex, "void drawn_others(std::vector<evict_reason::Candidate>& cands) {")
check("for (auto& kv : textures) {" in others and "order" not in others,
"drawn_others does not walk the cache itself (a pass's ordered list is filtered)")
check("if (!o->gpu_written || !o->guest_size) continue;" in others and
"evict_reason::note(cands, longest, o->gpu, o->gpu + o->guest_size, o->last_written, o);" in others,
"drawn_others does not note every sized image the GPU drew with its last drawing")
start = tex.index("void texture_sweep(uint64_t completed, bool sweep, uint64_t budget, uint64_t evict_to, bool known, uint64_t used, "
"uint64_t allowed,\n bool dead_only, uint64_t hold_to, uint64_t rule_to) {")
sweep = tex[start:]
at = [sweep.index(n) for n in (
" for (auto& [last, tex] : order) {\n // (total_bytes can have drifted", # the loop that chooses
" evict.push_back({tex, !no_flush && !overwritten && tex->gpu_written && !tex->released && flushable(*tex), shifted});",
" come_back_noted.store(true, std::memory_order_relaxed);",
BLOCK_HEAD,
" if (evict[i].flush && evict[i].tex->guest_size)",
" if (!cands.empty()) drawn_others(cands);",
" if (!evict_reason::write_back_stale(tex->last_written, c.newest)) continue;\n evict[c.index].flush = false;",
BATCHES,
" if (!e.flush) continue;\n batch.push_back(Flush{e.tex, e.tex->gpu, e.tex->gpu + e.tex->guest_size});",
" for (const Evict& e : evict) { // (and each one's bookkeeping and its end, as before)",
" destroy_later(*tex);\n forget_gpu(tex);\n gone.push_back(tex);")]
check(at == sorted(at), "the stale write-backs are not left out between the loop that chooses and the batches that write back")
check(sweep.count("drawn_others(") == 1 and sweep.count("for (auto& [last, tex] : order)") == 6,
"texture_sweep asks drawn_others more than once, or walks its ordered list a new time")
check("(%llu not: \"\n" in sweep[sweep.index(BATCHES):] and "(unsigned long long)st_stale_skipped);" in sweep[sweep.index(BATCHES):],
"the write-backs' line no longer counts the ones left out")
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("| `RX_VK_EVICT_STALE_FLUSH=1` |" in docs and "`gpu/vk/evict_reason.h`" in docs,
"docs/development.md: RX_VK_EVICT_STALE_FLUSH is not in the switches table")
block = sweep[sweep.index(BLOCK_HEAD):sweep.index(BATCHES)]
return others, block
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
check((VK / "evict_reason.h").is_file(), "gpu/vk/evict_reason.h is not there")
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8")
others, block = check_source(tex)
flags = ["-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror"]
with tempfile.TemporaryDirectory(prefix="evict-reason-") as tmp:
rule = Path(tmp) / "rule.exe"
subprocess.run([args.cxx, *flags, str(ROOT / "tests" / "test_evict_reason.cpp"), "-o", str(rule)], check=True)
r = subprocess.run([str(rule)], capture_output=True, text=True)
print((r.stdout + r.stderr).rstrip())
check(r.returncode == 0, "the rule's checks failed")
cpp, exe = Path(tmp) / "pass.cpp", Path(tmp) / "pass.exe"
cpp.write_text(HARNESS.replace("@DRAWN_OTHERS@", others).replace("@BLOCK@", block), encoding="utf-8")
subprocess.run([args.cxx, *flags, "-I" + str(VK), str(cpp), "-o", str(exe)], check=True)
env = {k: v for k, v in os.environ.items() if k != "RX_VK_EVICT_STALE_FLUSH"}
for value, arg in ((None, "0"), ("0", "0"), ("1", "1")):
e = dict(env)
if value is not None:
e["RX_VK_EVICT_STALE_FLUSH"] = value
r = subprocess.run([str(exe), arg], capture_output=True, text=True, env=e)
tail = [line for line in r.stdout.splitlines() if not line.startswith("log: ")]
print("\n".join(tail))
check(r.returncode == 0, "the cut-out pass with RX_VK_EVICT_STALE_FLUSH %s:\n%s" % ("unset" if value is None else "=" + value, r.stdout))
if value is None:
check("log: vk: texture #1 0x1001e88fc00 16x16 the GPU last drew in frame 3804 goes without its write-back: another image "
"was drawn over its storage in frame 3987 (1 so far; RX_VK_EVICT_STALE_FLUSH=1: written back, as before)" in r.stdout,
"the skip's log line does not name the texture and both frames:\n" + r.stdout)
print("PASS: an eviction leaves out the write-back of a texture another image drew into since")
if __name__ == "__main__":
main()

View File

@@ -10,10 +10,17 @@ Game-free: build.py's own lines, authored.
python -I -B tests/test_failed_compile_keeps_objects.py
"""
import ast
import contextlib
import importlib.util
import io
import os
from pathlib import Path
import sys
import tempfile
import types
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
@@ -51,8 +58,140 @@ class WhichFailureKeepsTheObjects(unittest.TestCase):
body = body[body.index('run(PY + ["nx2/build.py", "--standalone"'):body.index("compile_seconds = time.time() - compile_started")]
asked, deleted = body.index("if compile_alone_failed(said):"), body.index('os.path.join(work, "objcache")')
self.assertLess(asked, deleted)
# (the lift is forgotten either way: a lifted file cut short fails the same way at every try otherwise)
# (the lift is forgotten before that, a lifted file cut short fails the same way at every try otherwise; unless the
# compile failed only in the program's own files, once: the performance audit's LIFT-27, KeptLift below)
self.assertLess(body.index("os.remove(key_file)"), asked)
self.assertLess(body.index("lift_kept = keep_lift_after_failure(said, work)"), body.index("os.remove(key_file)"))
self.assertIn("if os.path.exists(key_file) and not lift_kept:", body)
def failed_build_handler():
"""The statements main() runs when nx2/build.py failed, cut out of make_game.py: its except block, behind a raise"""
source = (ROOT / "nx2/make_game.py").read_text(encoding="utf-8")
main = next(n for n in ast.parse(source).body if isinstance(n, ast.FunctionDef) and n.name == "main")
tries = [n for n in ast.walk(main) if isinstance(n, ast.Try) and any(
isinstance(c, ast.Constant) and c.value == "nx2/build.py" for s in n.body for c in ast.walk(s))]
assert len(tries) == 1 and len(tries[0].handlers) == 1
stop = ast.parse("raise SystemExit(1)").body
block = ast.Try(body=stop, handlers=tries[0].handlers, orelse=[], finalbody=[])
return compile(ast.fix_missing_locations(ast.Module(body=[block], type_ignores=[])), "make_game.py", "exec")
class KeptLift(unittest.TestCase):
"""(11-22, the performance audit's LIFT-27, after Source A's) A compile that failed only in the program's own files
keeps the translated game too, once: failed_only_own_sources, keep_lift_after_failure, and main()'s own except block
run on a made-up work folder"""
OWN = ["FAILED vk_draw.cpp (1 MB, 12s, alone too):", "vk_draw.cpp:10:1: error: a made-up error", "compiled in 412s, 1 failed"]
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix="triforce-kept-lift-")
self.addCleanup(self.temporary.cleanup)
self.work = Path(self.temporary.name) / "work"
self.gen = self.work / "sa" / "gen"
self.gen.mkdir(parents=True)
(self.work / "sa" / "build" / "gen").mkdir(parents=True)
for name in ("code_000.c", "code_hot_0.c", "a64_table.c", "a64_fpx.h"):
(self.gen / name).write_text("// lifted\n")
(self.work / "sa" / "build" / "gen" / "ui_fonts.cpp").write_text("// generated\n")
self.key = self.gen / make_game.LIFT_KEY
self.key.write_text("key\nprint\n")
self.objects = self.work / "objcache"
self.objects.mkdir()
(self.objects / "one.o").write_bytes(b"object")
self.mark = self.work / "sa" / make_game.LIFT_KEPT_ONCE
environ = {k: v for k, v in os.environ.items() if k != "HOOKSHOT_KEEP_LIFT"}
patched = mock.patch.dict(os.environ, environ, clear=True)
patched.start()
self.addCleanup(patched.stop)
def lines(self, *names, count=None):
return ["FAILED %s (3 MB, 40s, alone too):" % n for n in names] + \
["compiled in 412s, %d failed" % (len(names) if count is None else count)]
def test_only_the_programs_own_files(self):
own = lambda *names, **k: make_game.failed_only_own_sources(self.lines(*names, **k), str(self.work))
self.assertTrue(own("vk_draw.cpp"))
self.assertTrue(own("hos_main.c", "vk_shader.cpp", "imgui.cpp"))
self.assertTrue(make_game.failed_only_own_sources(self.OWN, str(self.work)))
self.assertFalse(own("code_021.c")) # (a lifted file)
self.assertFalse(own("code_hot_3.c"))
self.assertFalse(own("vk_draw.cpp", "code_021.c")) # (one of them is)
self.assertFalse(own("a64_table.c")) # (a file of the lift's folder)
self.assertFalse(own("ui_fonts.cpp")) # (a file the build generated)
self.assertFalse(own("vk_draw.cpp", count=2)) # (a failed file that was not named)
self.assertFalse(own()) # (none named)
self.assertFalse(make_game.failed_only_own_sources([], str(self.work)))
self.assertFalse(make_game.failed_only_own_sources(["FAILED vk_draw.cpp (1 MB, 2s, alone too):"], str(self.work)))
def test_kept_once_and_not_twice_in_a_row(self):
keep = lambda lines, environ=None: make_game.keep_lift_after_failure(lines, str(self.work), environ)
self.assertTrue(keep(self.OWN))
self.assertTrue(self.mark.exists())
self.assertFalse(keep(self.OWN)) # (the same kind of failure again: the lift is forgotten this time)
self.assertFalse(self.mark.exists())
self.assertTrue(keep(self.OWN)) # (and after that lift, once again)
self.mark.unlink()
self.assertFalse(keep(self.lines("code_021.c")))
self.assertFalse(self.mark.exists())
self.assertFalse(keep(["compiled in 412s, 0 failed", "link FAILED (3s), 20 undefined symbols"]))
self.assertFalse(keep(self.OWN + ["link FAILED (3s), 1 undefined symbols"]))
self.assertFalse(keep([]))
self.assertFalse(keep(self.OWN, {"HOOKSHOT_KEEP_LIFT": "0"})) # (the switch: forgotten after every failed build)
self.assertFalse(self.mark.exists())
self.assertTrue(keep(self.OWN, {"HOOKSHOT_KEEP_LIFT": "1"}))
def fail_build(self, said):
"""main()'s own except block after a failed build.py that said these lines: what it printed"""
stopped = []
printed = io.StringIO()
scope = dict(make_game.__dict__, said=list(said), work=str(self.work),
prompts=types.SimpleNamespace(stop=lambda: stopped.append(True)))
with contextlib.redirect_stdout(printed), self.assertRaises(SystemExit):
exec(failed_build_handler(), scope)
self.assertEqual(stopped, [True])
return printed.getvalue()
def test_the_failed_builds_own_statements(self):
# (the program's own file failed: the key and the objects stay)
said = self.fail_build(self.OWN)
self.assertTrue(self.key.exists() and (self.objects / "one.o").exists() and self.mark.exists())
self.assertIn("the translated game and the files that did compile are kept", said)
# (the same again at the next build: the key goes, the objects stay)
said = self.fail_build(self.OWN)
self.assertFalse(self.key.exists() or self.mark.exists())
self.assertTrue((self.objects / "one.o").exists())
self.assertIn("the next build translates the game again and compiles only the rest", said)
# (a lifted file failed: the key goes at once)
self.key.write_text("key\nprint\n")
said = self.fail_build(self.lines("code_021.c"))
self.assertFalse(self.key.exists() or self.mark.exists())
self.assertTrue((self.objects / "one.o").exists())
self.assertIn("the next build translates the game again and compiles only the rest", said)
# (a link failed: the key and the objects go, as before)
self.key.write_text("key\nprint\n")
said = self.fail_build(["compiled in 412s, 0 failed", "link FAILED (3s), 20 undefined symbols"])
self.assertFalse(self.key.exists() or self.objects.exists())
self.assertIn("nothing of this one is reused", said)
def test_the_switch_forgets_after_every_failed_build(self):
with mock.patch.dict(os.environ, {"HOOKSHOT_KEEP_LIFT": "0"}):
said = self.fail_build(self.OWN)
self.assertFalse(self.key.exists() or self.mark.exists())
self.assertTrue((self.objects / "one.o").exists())
self.assertIn("the next build translates the game again and compiles only the rest", said)
def test_the_lines_and_the_marks_place(self):
build = (ROOT / "nx2/build.py").read_text(encoding="utf-8")
self.assertIn('print("FAILED %s (%d MB, %.0fs, alone too):\\n%s" % (os.path.basename(src), size, secs, err), flush=True)', build)
source = (ROOT / "nx2/make_game.py").read_text(encoding="utf-8")
self.assertIn('if line.startswith("compiled in ") or "link FAILED" in line or line.startswith("FAILED "):', source)
# (the mark is not in sa/gen: gen_print vouches for every file there, and a new one would be a new lift)
self.assertIn('mark = os.path.join(work, "sa", LIFT_KEPT_ONCE)', source)
after = source[source.index(" prompts.stop()\n raise\n"):source.index("compile_seconds = time.time() - compile_started")]
self.assertIn('os.remove(os.path.join(work, "sa", LIFT_KEPT_ONCE))', after)
before = make_game.gen_print(str(self.gen))
make_game.keep_lift_after_failure(self.OWN, str(self.work))
self.assertEqual(make_game.gen_print(str(self.gen)), before)
if __name__ == "__main__":

View File

@@ -11,7 +11,9 @@ RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
CASES = ("initial_allocation_failure", "payload_allocation_failure", "records_address_zero",
"records_only_address_zero", "payload_address_zero", "records_only_success", "task_success",
"invalid_program", "program_isolation")
"invalid_program", "program_isolation",
# ((11-21, the performance audit's MEM-154) a payload growth keeps the thread's records buffer)
"payload_growth_keeps_records", "shared_records_payload_failure")
def block(source, signature, semicolon=False):

View File

@@ -0,0 +1,199 @@
"""The mesh fallback's programs already in memory are handed to the precompile too (gpu/vk/vk_shader.cpp
program_cache_load_some; vk_draw.cpp precompile_run; no game, no GPU).
(Triforce, 11-22, the performance audit's PIPE-62, after Source A's.) On the task and mesh fallback the precompile builds
the fallback programs' compute pipelines on workers before the title, for the programs program_cache_load_some hands it
with `fallbacks` set. That call handed over only the records still on the disk; in a start that prepares,
state_list_prepare has taken the state lists' records before it (with fallbacks off), so most fallback programs play draws
got no pipelines before the title, and each one's first draw waited up to 50 ms and was left out until they were built.
The call now also hands over every fallback program already in memory that is ready and not failed.
RX_VK_FALLBACK_LOADED=0: not, as before.
Cuts the end of program_cache_load_some (from its lock to its return) out of vk_shader.cpp and runs it over authored
programs: what was read from the disk is handed over as before, with what was in memory under the same key, each once;
a fallback program in memory under another key, ready and not failed, is handed over too, one that is not ready, failed
or no fallback program is not; the count returned and the log line say so; without `fallbacks`, or with the switch at 0,
only what came from the disk.
Reads the source: the walk is after the loop over what was read, under the same shader_mu, skips that loop's keys, tests
fallback, failed and ready (acquire), and is behind the switch; precompile_run still asks with fallbacks true and keeps
the fallback programs for its workers, which take compute_state first; the edit lies inside vk_shader.cpp's part marked
as outside the translation stamp (tests/test_program_carry.py holds the stamp itself); the switches table has the row.
python -I -B tests/test_fallback_loaded.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = r"""
#include <atomic>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define CHECK(x) do { if (!(x)) { std::printf("line %d: %s\n", __LINE__, #x); return 1; } } while (0)
struct Program {
uint64_t key = 0;
int id = 0;
bool fallback = false, failed = false;
std::atomic<bool> ready{false};
};
struct DiskRecord { int kind; };
std::mutex shader_mu;
std::unordered_map<uint64_t, std::vector<std::unique_ptr<Program>>> programs;
std::string said;
int lines;
void logf(const char* f, ...) {
char b[512];
va_list ap;
va_start(ap, f);
vsnprintf(b, sizeof b, f, ap);
va_end(ap);
said = b, lines++;
}
static std::unique_ptr<Program> make(uint64_t key, int id, bool fallback, bool ready = true, bool failed = false) {
auto p = std::make_unique<Program>();
p->key = key, p->id = id, p->fallback = fallback, p->failed = failed, p->ready = ready;
return p;
}
// program_cache_load_some's end, as it is: `work` and `made` are what its workers read from the disk
size_t tail(std::vector<std::pair<uint64_t, std::vector<DiskRecord>>>& work, std::vector<std::vector<std::unique_ptr<Program>>>& made,
const std::function<void(Program&)>& fn, bool fallbacks) {
/* TAIL */
}
int main(int argc, char** argv) {
const bool off = argc > 1 && argv[1][0] == '0';
// in memory before the call (what state_list_prepare and the draws read in)
programs[0xA].push_back(make(0xA, 1, true)); // (and a record of the same key still on the disk: below)
programs[0xB].push_back(make(0xB, 2, true)); // a fallback program, ready
programs[0xC].push_back(make(0xC, 3, true, false)); // one still being translated
programs[0xD].push_back(make(0xD, 4, true, true, true)); // one that failed
programs[0xE].push_back(make(0xE, 5, false)); // a compute or native program
programs[0xF].push_back(make(0xF, 6, true)); // a key with two variants, one of them a fallback's
programs[0xF].push_back(make(0xF, 7, false));
const auto run = [&](bool fallbacks, std::vector<int>* got) {
std::vector<std::pair<uint64_t, std::vector<DiskRecord>>> work;
work.push_back({0xA, {DiskRecord{4}}});
work.push_back({0x9, {DiskRecord{4}}});
std::vector<std::vector<std::unique_ptr<Program>>> made(2);
made[0].push_back(make(0xA, 10 + (int)got->size(), true));
made[1].push_back(make(0x9, 20 + (int)got->size(), true));
got->clear();
return tail(work, made, [&](Program& p) { got->push_back(p.id); }, fallbacks);
};
const auto count = [](const std::vector<int>& v, int id) {
int n = 0;
for (const int x : v) n += x == id;
return n;
};
std::vector<int> got;
// without `fallbacks` (the state list's call): only what was read, with what its keys had in memory
size_t n = run(false, &got);
CHECK(n == 3 && got.size() == 3 && count(got, 1) == 1 && count(got, 10) == 1 && count(got, 20) == 1 && lines == 0);
// with it (the precompile's)
n = run(true, &got);
if (off) { // (RX_VK_FALLBACK_LOADED=0: as before)
CHECK(n == 5 && got.size() == 5 && count(got, 2) == 0 && count(got, 6) == 0 && lines == 0);
std::printf("ok off\n");
return 0;
}
// (0xA has three variants now: the one in memory and the two read; 0x9 two)
CHECK(n == 7 && got.size() == 7);
CHECK(count(got, 1) == 1 && count(got, 10) == 1 && count(got, 13) == 1); // (a key that was read: each of its variants once, not again)
CHECK(count(got, 2) == 1 && count(got, 6) == 1); // (the fallback programs in memory under other keys)
CHECK(count(got, 3) == 0 && count(got, 4) == 0 && count(got, 5) == 0 && count(got, 7) == 0); // (not ready, failed, no fallback)
CHECK(lines == 1 && said == "vk: 2 mesh fallback programs read in earlier in this start are made ready with the rest");
// one that became ready since is handed over at a later call
programs[0xC][0]->ready = true;
n = run(true, &got);
CHECK(count(got, 3) == 1 && count(got, 2) == 1 && lines == 2 && said.rfind("vk: 3 mesh fallback programs", 0) == 0);
std::printf("ok on\n");
return 0;
}
"""
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 need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8")
load = block(shader, "size_t program_cache_load_some(")
ordered(load, ["std::lock_guard lk(shader_mu);", "for (size_t i = 0; i < work.size(); i++) {", "for (auto& p : variants) n++, fn(*p);",
"if (fallbacks) {", 'static const bool loaded = !(getenv("RX_VK_FALLBACK_LOADED") && atoi(getenv("RX_VK_FALLBACK_LOADED")) == 0);',
"if (loaded) {", "for (const auto& w : work) taken.insert(w.first);", "for (auto& kv : programs) {",
"if (taken.count(kv.first)) continue;",
"if (p->fallback && !p->failed && p->ready.load(std::memory_order_acquire)) found++, n++, fn(*p);",
'logf("vk: %zu mesh fallback programs read in earlier in this start are made ready with the rest", found);', "return n;"],
"program_cache_load_some does not hand over the fallback programs already in memory after what it read, under the same lock")
need(load.count("std::lock_guard lk(shader_mu);") == 1 and load.count("return n;") == 1, "the walk is not under the loop's own lock")
need("Only a GPU without mesh shaders has such programs" not in load, "the comment that only a GPU without mesh shaders has such programs is back")
# the edit is inside the part of vk_shader.cpp marked as outside the translation stamp
out_mark, in_mark = "// ---- (Triforce) Outside the translation stamp", "// ---- (Triforce) Inside the translation stamp again"
at = shader.index("size_t program_cache_load_some(")
need(shader.rfind(in_mark, 0, at) < shader.rfind(out_mark, 0, at) < at < at + len(load) < shader.index(in_mark, at),
"program_cache_load_some is no longer inside a part marked as outside the translation stamp")
need(shader.count("RX_VK_FALLBACK_LOADED") == load.count("RX_VK_FALLBACK_LOADED"), "the switch is read outside program_cache_load_some")
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
pre = block(draw, "void precompile_run(void (*progress)(size_t done, size_t total)) {")
ordered(pre, ["const size_t programs_n = program_cache_load_some(wanted, [&](Program& p) {", "ensure_layout(p);",
"else if (p.fallback) fallbacks.push_back(&p);", "}, all_computes, true);",
"Program& p = *fallbacks[i - computes.size()];", "if (p.compute_state.compare_exchange_strong(none, 1)) {",
"fallback_compute_pipelines(p);", "p.compute_state.store(2, std::memory_order_release);"],
"precompile_run does not ask with fallbacks true, keep the fallback programs, or take compute_state before it builds")
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
need("`RX_VK_FALLBACK_LOADED=0`" in docs, "docs/development.md: the switches table has no RX_VK_FALLBACK_LOADED row")
tail = load[load.index(" std::lock_guard lk(shader_mu);"):load.rindex("}")]
env = {k: v for k, v in os.environ.items() if not k.startswith("RX_VK_")}
with tempfile.TemporaryDirectory(prefix="fallback-loaded-") as tmp:
cpp, exe = Path(tmp) / "fixture.cpp", Path(tmp) / "fixture.exe"
cpp.write_text(FIXTURE.replace("/* TAIL */", tail), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
for arg, more, want in (("on", {}, "ok on"), ("0", {"RX_VK_FALLBACK_LOADED": "0"}, "ok off")):
r = subprocess.run([str(exe), arg], capture_output=True, text=True, timeout=60, env={**env, **more})
need(r.returncode == 0 and r.stdout.strip() == want, "program_cache_load_some's end (%s): %s" % (arg, r.stdout.strip() or r.returncode))
print(r.stdout.strip())
print("PASS: the mesh fallback's programs already in memory are handed to the precompile; RX_VK_FALLBACK_LOADED=0 as before")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,185 @@
"""Mesh-fallback compute stages too big for RADV on GCN or Mesa's Intel driver are not built (gpu/vk/fallback_size_policy.h).
(Triforce, 11-22, the performance audit's PIPE-56, after Source A's.) No game, no GPU. Every fallback stage went to the
driver; on those two drivers the largest ones took tens of GB and minutes and ended with the game killed. A program with
a stage over its driver's limit is now left out before anything is built, as a program the driver refused is.
Compiles a small driver against the header and checks:
the counters OpPhi is opcode 245 and OpFunctionCall 57; a made-up module with 3 phis and 2 calls counts 3 and 2;
a cut-off module, a wrong magic word and no module count 0 of both;
the driver RADV on GCN first, then Mesa's Intel driver, any other: none;
the limits GCN: 50,000 phis, no call limit; Mesa's Intel driver: 20,000 phis, 200 calls; any other driver: none,
whatever the switches say;
the switches unset, empty or not a number: the driver's own; 0: none; a number: that number;
refused just over a limit yes, at the limit no; 79,947 phis on GCN yes, on Mesa's Intel driver yes, on any other
driver no; 251 calls on Mesa's Intel driver yes, on GCN no; nothing with both limits 0.
And reads the source: fallback_compute_pipelines asks the rule after the failed-set lookup and before the first stage is
built, only where fallback_size_driver() names one of the two drivers, and puts the program into the failed set;
vk_shader.cpp counts in make_module, moves the counts in program_pack and forgets them in stage_module_destroy, all
between its first pair of "Outside the translation stamp" marks; and the switches table has the two rows.
python -I -B tests/test_fallback_size_policy.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
DRIVER = r'''
#include "fallback_size_policy.h"
#include <cstdio>
#include <initializer_list>
#include <vector>
using namespace fallback_size;
static int checks, fails;
static void want(const char* what, bool got, bool wanted) {
checks++;
if (got != wanted) fails++, printf("FAIL %s: %d, want %d\n", what, (int)got, (int)wanted);
}
int main() {
want("OpPhi is 245", kOpPhi == 245, true);
want("OpFunctionCall is 57", kOpFunctionCall == 57, true);
std::vector<uint32_t> m{0x07230203, 0x00010600, 0, 100, 0};
const auto op = [&m](uint32_t code, std::initializer_list<uint32_t> words) {
m.push_back((uint32_t)((words.size() + 1) << 16) | code);
for (uint32_t w : words) m.push_back(w);
};
op(17, {1});
for (int i = 0; i < 3; i++) op(245, {2, 20u + i, 7, 8, 9, 10}); // OpPhi
op(57, {3, 30, 4}); // OpFunctionCall
op(57, {3, 31, 4, 30});
op(253, {});
want("3 phis", spirv_phis(m.data(), m.size()) == 3, true);
want("2 calls", spirv_calls(m.data(), m.size()) == 2, true);
std::vector<uint32_t> cut = m;
cut.back() = (5u << 16) | 245; // (the last instruction, one word, made one longer than what is left)
want("a cut-off module counts 0", count(cut.data(), cut.size()).phis == 0 && count(cut.data(), cut.size()).calls == 0, true);
std::vector<uint32_t> wrong = m;
wrong[0] = 0;
want("a wrong magic word counts 0", spirv_phis(wrong.data(), wrong.size()) == 0 && spirv_calls(wrong.data(), wrong.size()) == 0, true);
want("no module counts 0", spirv_phis(nullptr, 0) == 0, true);
want("RADV on GCN", driver_of(true, false) == Driver::RadvGcn, true);
want("Mesa's Intel driver", driver_of(false, true) == Driver::MesaIntel, true);
want("any other driver", driver_of(false, false) == Driver::Other, true);
const Limits gcn = limits(Driver::RadvGcn, nullptr, nullptr), intel = limits(Driver::MesaIntel, nullptr, nullptr);
want("GCN: 50,000 phis, no call limit", gcn.phis == 50000 && gcn.calls == 0, true);
want("Mesa's Intel driver: 20,000 phis, 200 calls", intel.phis == 20000 && intel.calls == 200, true);
for (const char* v : {(const char*)nullptr, "", "0", "5", "100000"}) {
const Limits other = limits(Driver::Other, v, v);
want("no limit on any other driver, whatever is set", other.phis == 0 && other.calls == 0, true);
want("nothing refused on any other driver", refused(other, Size{4000000, 4000000}), false);
}
want("unset: the driver's own", parse_limit(nullptr, 50000) == 50000, true);
want("empty: the driver's own", parse_limit("", 50000) == 50000, true);
want("not a number: the driver's own", parse_limit("many", 50000) == 50000 && parse_limit("12x", 50000) == 50000, true);
want("0: none", parse_limit("0", 50000) == 0, true);
want("a number: that number", parse_limit("30000", 50000) == 30000, true);
want("a number too large: the largest", parse_limit("99999999999", 5) == 0xffffffffu, true);
const Limits set = limits(Driver::MesaIntel, "0", "300");
want("the switches on Mesa's Intel driver", set.phis == 0 && set.calls == 300, true);
want("just over the phi limit", refused(gcn, Size{50001, 0}), true);
want("at the phi limit", refused(gcn, Size{50000, 0}), false);
want("79,947 phis on GCN", refused(gcn, Size{79947, 12}), true);
want("79,947 phis on Mesa's Intel driver", refused(intel, Size{79947, 12}), true);
want("251 calls on Mesa's Intel driver", refused(intel, Size{100, 251}), true);
want("200 calls on Mesa's Intel driver", refused(intel, Size{100, 200}), false);
want("251 calls on GCN: no call limit there", refused(gcn, Size{100, 251}), false);
want("both limits 0: nothing refused", refused(limits(Driver::RadvGcn, "0", "0"), Size{4000000, 4000000}), false);
want("a stage nobody counted is built", refused(intel, Size{}) || refused(gcn, Size{}), false);
if (fails) printf("fallback size policy: FAILED (%d of %d checks)\n", fails, checks);
else printf("fallback size policy: all %d checks passed\n", checks);
return fails != 0;
}
'''
def check_source():
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
at = draw.index("bool fallback_compute_pipelines(Program& p) {")
body = draw[at:draw.index("\n}\n", at)]
order = [body.find(n) for n in (
"if (failed.count({p.key, p.variant})) return false;",
"if (const fallback_size::Driver size_driver = fallback_size_driver(); size_driver != fallback_size::Driver::Other) {",
'fallback_size::limits(size_driver, getenv("RX_VK_FALLBACK_PHI_LIMIT"), getenv("RX_VK_FALLBACK_CALL_LIMIT"));',
"const fallback_size::Size size = stage_size(st);",
"if (!fallback_size::refused(limits, size)) continue;",
"failed.insert({p.key, p.variant});",
"std::vector<VkPipeline> made(p.stages.size(), VK_NULL_HANDLE);",
"guarded_compute(pcache_use().cache, &ci, &made[i])")]
if min(order) < 0 or order != sorted(order):
raise SystemExit("fallback_compute_pipelines does not ask the size rule after the failed-set lookup and before the first "
"build, putting a program over the limit into the failed set")
if "return false;" not in body[order[5]:order[6]]:
raise SystemExit("fallback_compute_pipelines goes on to build a program the size rule put into the failed set")
if body.count("failed.insert({p.key, p.variant});") != 2:
raise SystemExit("fallback_compute_pipelines: wanted the failed set written by the size rule and by a refused build")
if "size_told++ < 8" not in body:
raise SystemExit("fallback_compute_pipelines: the size rule's log line is not capped at 8")
shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8")
out_mark, in_mark = "// ---- (Triforce) Outside the translation stamp", "// ---- (Triforce) Inside the translation stamp again"
lo = shader.index(out_mark)
hi = shader.index(in_mark, lo)
between = shader[lo:hi]
for needle in ("void size_note(VkShaderModule m, const std::vector<uint32_t>& code) {",
"if (!m || fallback_size_driver() == fallback_size::Driver::Other) return;",
"void size_move(VkShaderModule from, VkShaderModule to) {",
"size_move(st.module, (VkShaderModule)(uintptr_t)code.get());",
"fallback_size::Driver fallback_size_driver() {",
"fallback_size::driver_of(dev.radv_gcn, dev.driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA);",
"fallback_size::Size stage_size(const Stage& st) {",
"const auto it = size_of.find(stage_exact_key(st));",
"size_of.erase(m);"):
if needle not in between:
raise SystemExit("vk_shader.cpp: not between the first pair of translation stamp marks: " + needle)
if shader.count("fallback_size") != between.count("fallback_size") or shader.count("size_of") != between.count("size_of"):
raise SystemExit("vk_shader.cpp names the size rule outside the marks: the translation stamp would move")
make = between[between.index("VkShaderModule make_module("):]
if make.count("size_note(m, code);") != 2:
raise SystemExit("make_module does not count each module it makes")
pack = between[between.index("void program_pack(Program& p) {"):]
pack = pack[:pack.index("\n}\n")]
if pack.index("exact_move(st.module,") > pack.index("size_move(st.module,") or \
pack.index("size_move(st.module,") > pack.index("vkDestroyShaderModule(dev.device, st.module, nullptr);"):
raise SystemExit("program_pack does not move a stage's counts with its key before the module goes")
destroy = between[between.index("void stage_module_destroy(VkShaderModule m) {"):]
if "size_of.erase(m);" not in destroy[:destroy.index("\n}\n")]:
raise SystemExit("stage_module_destroy does not forget a module's counts")
header = (ROOT / "gpu/vk/vk_shader.h").read_text(encoding="utf-8")
for needle in ('#include "fallback_size_policy.h"', "fallback_size::Driver fallback_size_driver();",
"fallback_size::Size stage_size(const Stage& st);"):
if needle not in header:
raise SystemExit("vk_shader.h: no " + needle)
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
if "| `RX_VK_FALLBACK_PHI_LIMIT=<n>` |" not in docs or "| `RX_VK_FALLBACK_CALL_LIMIT=<n>` |" not in docs or \
"`gpu/vk/fallback_size_policy.h`" not in docs:
raise SystemExit("docs/development.md: the switches table lacks the two rows for the fallback's size limits")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="fallback-size-") as tmp:
cpp, exe = Path(tmp) / "fallback_size_test.cpp", Path(tmp) / "fallback-size.exe"
cpp.write_text(DRIVER, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the fallback size policy's checks failed")
if __name__ == "__main__":
main()

96
tests/test_fg_motion.py Normal file
View File

@@ -0,0 +1,96 @@
"""NVIDIA DLSS frame generation is given the camera's motion alone, not the upscaler chain's (gpu/vk/fg_motion.h,
vk_fsr3_inputs.cpp chain_motion; no game, no GPU).
(Triforce, 11-22, the owner's DLSS frame generation flicker.) With the chain's motion copied over FSR's inputs (FG-4) the
whole picture flickered again while the camera moved, with the clip-space fix of 11-20 in the build. The owner's runs:
RX_VK_FG_OWN_MOTION=0 (the camera's alone) had no flicker; the chain's motion with the upscaler audit's commits all set back
had it.
This compiles fg_motion.h alone and checks the rule: with DLSS frame generation the chain's motion is not given, unless
RX_VK_DLSSG_OWN_MOTION is set and not 0; without it (AMD's frame generation, FSR 3.1's interpolation) it is, as before;
RX_VK_FG_OWN_MOTION=0 keeps the camera's alone for every generator, over RX_VK_DLSSG_OWN_MOTION too; the switches read as
the renderer reads them (unset, empty, 0).
And reads the source: chain_motion asks the rule with dlssg::active() and both switches before it takes the lock or copies
anything, and returns false then; the old switch's line stays (tests/test_framegen_amd.py reads it); and the switch is in
the switches table.
python -I -B tests/test_fg_motion.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdio>
#include "fg_motion.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace fg_motion;
int main() {
CHECK(!set_on(nullptr) && !set_on("") && !set_on("0") && set_on("1") && set_on("2"));
CHECK(!camera_alone(nullptr) && !camera_alone("") && camera_alone("0") && !camera_alone("1"));
// (DLSS frame generation: the camera's alone by default; the chain's with RX_VK_DLSSG_OWN_MOTION=1)
CHECK(!chain(true, nullptr, nullptr));
CHECK(!chain(true, nullptr, "0") && !chain(true, nullptr, ""));
CHECK(chain(true, nullptr, "1"));
// (AMD's frame generation, FSR 3.1's interpolation: the chain's, as before; the DLSS switch says nothing to them)
CHECK(chain(false, nullptr, nullptr) && chain(false, nullptr, "0") && chain(false, nullptr, "1"));
CHECK(chain(false, "1", nullptr));
// (RX_VK_FG_OWN_MOTION=0: the camera's alone for every generator, over the DLSS switch too)
CHECK(!chain(false, "0", nullptr) && !chain(true, "0", nullptr) && !chain(true, "0", "1") && !chain(false, "0", "1"));
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
src = (VK / "vk_fsr3_inputs.cpp").read_text(encoding="utf-8")
check('#include "fg_motion.h"' in src, "vk_fsr3_inputs.cpp does not use the rule")
motion = block(src, "bool chain_motion(VkCommandBuffer cmd, VkImage motion, uint32_t width, uint32_t height) {")
ask = 'if (!fg_motion::chain(dlssg::active(), getenv("RX_VK_FG_OWN_MOTION"), getenv("RX_VK_DLSSG_OWN_MOTION"))) return false;'
check(ask in motion, "chain_motion does not ask the rule with DLSS frame generation's state and both switches")
check(motion.index(ask) < motion.index("std::lock_guard lk(s.mu);") < motion.index("vkCmdCopyImage(cmd, motion, VK_IMAGE_LAYOUT_GENERAL, s.motion.image"),
"chain_motion copies or takes its lock before the rule says whether the chain's motion is given")
check(motion.count("vkCmdCopyImage(") == 1 and "s.frame.chain_motion = true;" in motion,
"chain_motion copies more than once, or no longer notes the chain's motion")
check('env_switch("RX_VK_FG_OWN_MOTION") == 0' in motion, "the old switch's line is gone (tests/test_framegen_amd.py reads it)")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_DLSSG_OWN_MOTION=1`" in dev, "docs/development.md: RX_VK_DLSSG_OWN_MOTION=1 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="fg-motion-") as tmp:
c, exe = Path(tmp) / "motion.cpp", Path(tmp) / "motion.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: NVIDIA DLSS frame generation is given the camera's motion alone, the chain's only with RX_VK_DLSSG_OWN_MOTION=1")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,144 @@
"""A finalized texture the GPU drew goes once the game has rewritten its storage (gpu/vk/finalized_obsolete.h,
vk_texture.cpp texture_sweep; no game, no GPU).
(Triforce, 11-21, the performance audit's TEX-43, after Source A's.) A texture the GPU drew in one frame is kept after
the game finalizes it, for being made again over that storage. Within the budget only a draw over one let it go: the
game's own CPU writes to that memory kept each for good, about 200 render target images an hour in players' logs.
This compiles finalized_obsolete.h alone: which textures can be one (finalized, GPU-written, in no virtual pool, 32 KB
or more), three quarters of the storage as the line, the look again every 60 presents, the switch; and 30 laps of 60
tile-sized targets whose storage the game then reuses: 1,800 alive without the rule, 120 with it.
And reads the source: the sweep's look uses the texture's own fields and write generations since its release, keeps
its answer with the texture, and a release starts it afresh; keep_finalized refuses an obsolete one; it counts as dead
in the hold's walk and as overwritten in the pass, so it is never written back; what goes is counted; the switch is in
the switches table.
python -I -B tests/test_finalized_obsolete.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def in_order(text, parts, why):
at = 0
for p in parts:
at = text.find(p, at)
check(at >= 0, why + " (missing, or out of order: " + p + ")")
at += len(p)
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <vector>
#include "finalized_obsolete.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace finalized_obsolete;
struct Tex { bool released, gpu_written; uint64_t gpu, guest_size, written; bool obsolete; uint64_t checked; };
// the sweep's look, as vk_texture.cpp makes it of the rule
static bool look(Tex& t, uint64_t frame, bool keep_all) {
if (keep_all || !candidate(t.released, t.gpu_written, t.gpu, t.guest_size)) return false;
if (!t.obsolete && due(frame, t.checked)) t.checked = frame, t.obsolete = rewritten(t.written, t.guest_size);
return t.obsolete;
}
int main() {
CHECK(!keep(nullptr) && !keep("") && !keep("0") && keep("1"));
CHECK(candidate(true, true, 0x8000000000ull, 32u << 10) && candidate(true, true, 1, 135200));
CHECK(!candidate(false, true, 1, 135200)); // (not finalized: the game still has it)
CHECK(!candidate(true, false, 1, 135200)); // (uploaded from memory: its own rules)
CHECK(!candidate(true, true, 1ull << 40, 135200)); // (a virtual pool: its pages are mapped, not written)
CHECK(!candidate(true, true, 1, (32u << 10) - 1)); // (a small one shares its pages)
CHECK(rewritten(3, 4) && rewritten(4, 4) && rewritten(101400, 135200) && !rewritten(101399, 135200) && !rewritten(0, 135200));
CHECK(!rewritten(0, 0));
CHECK(due(100, 0) && !due(100, 41) && due(100, 40) && due(1000, 1) && !due(5, 5));
// 30 laps, each makes 60 tile-sized targets (260 x 260 RG8) the GPU draws and the game finalizes; a lap later the game
// has put other data in the storage of all but the newest two laps'
for (const bool keep_all : {true, false}) {
std::vector<Tex> alive;
uint64_t frame = 1;
for (int lap = 0; lap < 30; lap++) {
for (int i = 0; i < 60; i++) alive.push_back({true, true, 0x100000ull + alive.size() * 0x40000, 135200, 0, false, 0});
for (size_t i = 0; i + 120 < alive.size() + 0 && i < alive.size(); i++) alive[i].written = 135200; // (older than two laps: rewritten)
frame += 600;
std::vector<Tex> kept;
for (Tex& t : alive)
if (!look(t, frame, keep_all)) kept.push_back(t);
alive.swap(kept);
}
if (keep_all) CHECK(alive.size() == 1800);
else CHECK(alive.size() == 120);
}
{ // one not rewritten yet is not looked at again before 60 presents; once rewritten it stays so
Tex t{true, true, 1, 135200, 0, false, 0};
CHECK(!look(t, 100, false) && t.checked == 100);
t.written = 135200;
CHECK(!look(t, 159, false) && t.checked == 100);
CHECK(look(t, 160, false) && t.checked == 160);
t.written = 0;
CHECK(look(t, 400, false) && t.checked == 160);
}
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8").replace("\r\n", "\n")
start = tex.rindex("void texture_sweep(")
sweep = tex[start:]
check('#include "finalized_obsolete.h"' in tex, "vk_texture.cpp does not use the rule")
in_order(sweep, ['static const bool keep_obsolete = finalized_obsolete::keep(getenv("RX_VK_KEEP_OBSOLETE"));',
"const auto obsolete = [&](const Texture* tex) {",
"if (keep_obsolete || !finalized_obsolete::candidate(tex->released, tex->gpu_written, tex->gpu, tex->guest_size)) return false;",
"if (!tex->obsolete && finalized_obsolete::due(frame_no, tex->obsolete_checked)) {",
"tex->obsolete_checked = frame_no;",
"memory_written_runs(tex->guest, tex->guest_size, tex->release_gen, written, true);",
"tex->obsolete = finalized_obsolete::rewritten(sum, tex->guest_size);", "return tex->obsolete;",
"const auto keep_finalized = [&](const Texture* tex) {",
"tex->bytes > (4ull << 20) || obsolete(tex))",
"if (shifted || drawn_over(tex) || obsolete(tex)) hold_dead += tex->bytes + tex->pad;",
"const bool stale = obsolete(tex);", "const bool overwritten = shifted || drawn_over(tex) || stale;",
"if (sweep && !overwritten && !gone_early) continue;",
"!no_flush && !overwritten && tex->gpu_written && !tex->released && flushable(*tex)",
"if (stale && !shifted && !drawn_over(tex)) {", "st_obsolete_n"],
"texture_sweep does not look at a finalized texture's storage by the rule, keep its answer, refuse it among the kept, "
"count it dead in the hold's walk and overwritten in the pass, or count what goes")
check("it->second->obsolete = false, it->second->obsolete_checked = 0;" in tex and
# ((Triforce, 11-22, the performance audit's MEM-85) the release's generation is texture_released_at's gen)
tex.index("it->second->released = true, it->second->release_gen = gen;") <
tex.index("it->second->obsolete = false, it->second->obsolete_checked = 0;"),
"a release does not start the look afresh: a texture rewritten before it stays obsolete after it")
head = (VK / "vk_texture.h").read_text(encoding="utf-8")
check("mutable bool obsolete = false;" in head and "mutable uint64_t obsolete_checked = 0;" in head,
"the texture does not keep the look's answer")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_KEEP_OBSOLETE=1`" in dev, "docs/development.md: RX_VK_KEEP_OBSOLETE is not in the switches table")
with tempfile.TemporaryDirectory(prefix="finalized-obsolete-") as tmp:
c, exe = Path(tmp) / "obsolete.cpp", Path(tmp) / "obsolete.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: a finalized texture the GPU drew goes once the game has rewritten its storage")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,380 @@
"""The finish passes over files the game folder already has from the same source (nx2/make_game.py INSTALLED_NOTE).
(Triforce, 11-22, the performance audit's IO-45, after Source A's.) Every build copied every file into the game folder
as <name>.new and swapped it in: the exe, the renderer, the prebuilt DLLs and image.bin, changed or not. The finish now
notes, per installed file, its source's SHA-1 and the installed copy's size, time, file number and mode, and the next
finish passes over a file whose source hashes the same and whose installed copy is still as noted. The installed file
is never read. HOOKSHOT_FINISH_COPY_ALL=1 copies everything and keeps no note, as before.
Runs the production finish statements themselves (tests/test_finish_output.py's finish_statements cuts them out of
main()) on made-up files in a temporary folder:
- a first finish copies everything and writes the note; a second with the same sources calls neither shutil.copy2 nor
os.replace, opens no installed file, and leaves the files and the note as they were;
- a changed source is copied, alone, also when its size and time are the old ones; a source only touched is not;
- an installed file changed by hand, a note that is broken or of another format, a file the note does not have, and a
file system that gives no file number (st_ino 0) all copy;
- the launcher layout (the game in bin, the launcher beside it) is passed over file by file;
- a <name>.new left beside a file that is passed over is removed, and a read-only one is left locked;
- a swap that is refused leaves no note saying the new file is there, and the retry installs it;
- the switch copies everything and removes the note;
- after every finish the installed files are the sources byte for byte.
The exe and the renderer are linked again at every build, and lld writes the link's time into them, so they are still
copied. HOOKSHOT_LINK_REPRO=1 (nx2/build.py repro_link, off unless asked: it changes the linked files' bytes) adds
/Brepro to those two links. Checked here: the rule and its two hooks, and, with --cc, that two links of one tiny object
a second apart are the same bytes with the flag and are not without it.
python -I -B tests/test_finish_installed.py [--cc clang]
"""
import ast
import contextlib
import hashlib
import io
import json
import os
from pathlib import Path
import shutil
import stat
import subprocess
import sys
import tempfile
import time
import types
import unittest
from unittest import mock
ROOT = Path(__file__).resolve().parents[1]
RUN_ALL_ARGS = ["--cc", "clang"]
sys.path.insert(0, str(ROOT / "tests"))
from test_finish_output import FINISH, make_game # (the production statements, and the module they came from)
class NoNumber:
"""An os.stat result on a file system that gives no file number"""
def __init__(self, result):
self._result = result
st_ino = 0
def __getattr__(self, name):
return getattr(self._result, name)
class FinishInstalled(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory(prefix="triforce-finish-installed-")
self.addCleanup(self.directory.cleanup)
self.root = Path(self.directory.name).resolve()
self.addCleanup(self.unlock)
self.work, self.out = self.root / "work", self.root / "game"
self.built = self.work / "sa" / "build"
self.built.mkdir(parents=True)
self.out.mkdir()
(self.work / "sa" / "image").mkdir()
self.exe, self.dll = self.built / "Triforce.exe", self.built / "triforce_vk.dll"
self.image = self.work / "sa" / "image" / "image.bin"
self.exe.write_bytes(b"a made-up executable")
self.dll.write_bytes(b"a made-up renderer")
self.image.write_bytes(b"a made-up image")
self.sources = {"Triforce.exe": self.exe, "triforce_vk.dll": self.dll, "image.bin": self.image}
self.note = self.out / make_game.INSTALLED_NOTE
environ = {k: v for k, v in os.environ.items() if k != "HOOKSHOT_FINISH_COPY_ALL"}
patched = mock.patch.dict(os.environ, environ, clear=True)
patched.start()
self.addCleanup(patched.stop)
def unlock(self):
for path in self.root.rglob("*"):
if path.is_file():
path.chmod(stat.S_IREAD | stat.S_IWRITE)
def finish(self, launcher=False):
"""The production finish; returns (the sources copied, the targets swapped in, what it printed)"""
if launcher:
(self.root / "HookshotBuilder-Setup.exe").write_bytes(b"a made-up launcher")
copied, swapped, said = [], [], io.StringIO()
copy2, replace = make_game.shutil.copy2, os.replace
def copy(src, dst):
copied.append(Path(src).name)
return copy2(src, dst)
def swap(src, dst):
swapped.append(Path(dst).name)
return replace(src, dst)
with mock.patch.object(make_game, "BESIDE", ["Triforce.exe", "triforce_vk.dll"]), \
mock.patch.object(make_game.shutil, "copy2", side_effect=copy), \
mock.patch.object(make_game.os, "replace", side_effect=swap), contextlib.redirect_stdout(said):
namespace = dict(make_game.__dict__, built=str(self.built), out=str(self.out), work=str(self.work),
ROOT=str(self.root / "tools"), LINUX=False if launcher else make_game.LINUX)
exec(FINISH, namespace)
return copied, swapped, said.getvalue()
def assert_installed(self, folder=None):
for name, source in self.sources.items():
where = self.out / "image.bin" if name == "image.bin" else (folder or self.out) / name
self.assertEqual(where.read_bytes(), source.read_bytes(), name)
def test_second_finish_copies_and_swaps_nothing(self):
copied, swapped, said = self.finish()
self.assertEqual(copied, ["Triforce.exe", "triforce_vk.dll", "image.bin"])
self.assertEqual(swapped, ["Triforce.exe", "triforce_vk.dll", "image.bin", make_game.INSTALLED_NOTE])
self.assert_installed()
note = json.loads(self.note.read_text(encoding="utf-8"))
self.assertEqual(sorted(note["files"]), ["Triforce.exe", "image.bin", "triforce_vk.dll"])
self.assertEqual(note["files"]["Triforce.exe"]["sha1"], make_game.file_sha1(str(self.exe)))
before = {p.name: (p.read_bytes(), p.stat().st_mtime_ns, p.stat().st_ino) for p in self.out.iterdir()}
opened = []
real_open = open
def watch(path, *a, **k):
opened.append(os.path.normcase(os.path.abspath(path)))
return real_open(path, *a, **k)
with mock.patch("builtins.open", side_effect=watch):
copied, swapped, said = self.finish()
self.assertEqual((copied, swapped), ([], []))
self.assertIn("the game folder has 3 of this build's 3 files already", said)
# (the installed files are never read: only the sources, for their hashes, and the note)
inside = [p for p in opened if p.startswith(os.path.normcase(str(self.out)) + os.sep)]
self.assertEqual(inside, [os.path.normcase(str(self.note))])
self.assertEqual(before, {p.name: (p.read_bytes(), p.stat().st_mtime_ns, p.stat().st_ino) for p in self.out.iterdir()})
def test_changed_source_is_copied_alone(self):
self.finish()
self.dll.write_bytes(b"a made-up renderer, built again")
copied, swapped, said = self.finish()
self.assertEqual(copied, ["triforce_vk.dll"])
self.assertEqual(swapped, ["triforce_vk.dll", make_game.INSTALLED_NOTE])
self.assertIn("2 of this build's 3 files already (Triforce.exe, image.bin)", said)
self.assert_installed()
self.assertEqual(self.finish()[:2], ([], []))
def test_changed_source_of_the_same_size_and_time_is_copied(self):
self.finish()
times = self.exe.stat()
self.exe.write_bytes(b"A MADE-UP EXECUTABLE") # (the same length)
os.utime(self.exe, ns=(times.st_atime_ns, times.st_mtime_ns))
self.assertEqual(self.finish()[0], ["Triforce.exe"])
self.assert_installed()
def test_touched_source_is_not_copied(self):
self.finish()
times = self.image.stat()
os.utime(self.image, ns=(times.st_atime_ns, times.st_mtime_ns + 5_000_000_000))
self.assertEqual(self.finish()[:2], ([], []))
def test_installed_file_changed_by_hand_is_copied(self):
self.finish()
(self.out / "image.bin").write_bytes(b"another image, longer than the one installed")
self.assertEqual(self.finish()[0], ["image.bin"])
self.assert_installed()
# the same length, written later: its time is not the noted one
installed = self.out / "Triforce.exe"
times = installed.stat()
installed.write_bytes(b"x" * times.st_size)
os.utime(installed, ns=(times.st_atime_ns, times.st_mtime_ns + 2_000_000_000))
self.assertEqual(self.finish()[0], ["Triforce.exe"])
self.assert_installed()
# deleted
(self.out / "triforce_vk.dll").unlink()
self.assertEqual(self.finish()[0], ["triforce_vk.dll"])
self.assert_installed()
def test_broken_or_foreign_note_copies_everything(self):
self.finish()
good = self.note.read_text(encoding="utf-8")
for text in ("", "{ not json", "[1, 2]", json.dumps({"format": 2, "files": json.loads(good)["files"]}),
json.dumps({"format": 1, "files": [1]}), json.dumps({"format": 1, "files": {"Triforce.exe": "x"}})):
with self.subTest(note=text[:24]):
self.note.write_text(text, encoding="utf-8")
self.assertEqual(self.finish()[0], ["Triforce.exe", "triforce_vk.dll", "image.bin"])
self.assert_installed()
self.assertEqual(self.finish()[:2], ([], []))
self.note.unlink() # (deleting the note copies everything)
self.assertEqual(self.finish()[0], ["Triforce.exe", "triforce_vk.dll", "image.bin"])
def test_file_the_note_does_not_have_is_copied(self):
self.finish()
note = json.loads(self.note.read_text(encoding="utf-8"))
del note["files"]["image.bin"]
self.note.write_text(json.dumps(note), encoding="utf-8")
self.assertEqual(self.finish()[0], ["image.bin"])
self.assertEqual(sorted(json.loads(self.note.read_text(encoding="utf-8"))["files"]),
["Triforce.exe", "image.bin", "triforce_vk.dll"])
def test_no_file_number_always_copies(self):
real = os.stat
with mock.patch.object(make_game.os, "stat", side_effect=lambda p, *a, **k: NoNumber(real(p, *a, **k))):
self.assertIsNone(make_game.installed_stat(str(self.exe)))
for _ in range(2):
self.assertEqual(self.finish()[0], ["Triforce.exe", "triforce_vk.dll", "image.bin"])
self.assert_installed()
self.assertFalse(self.note.exists() and json.loads(self.note.read_text(encoding="utf-8"))["files"])
def test_launcher_layout_is_passed_over_file_by_file(self):
copied, swapped, said = self.finish(launcher=True)
self.assertEqual(copied, ["Triforce.exe", "triforce_vk.dll", "image.bin", "HookshotBuilder-Setup.exe"])
self.assertEqual((self.out / "Triforce.exe").read_bytes(), b"a made-up launcher")
self.assert_installed(self.out / "bin")
note = json.loads(self.note.read_text(encoding="utf-8"))["files"]
self.assertEqual(sorted(note), ["Triforce.exe", "bin/Triforce.exe", "bin/triforce_vk.dll", "image.bin"])
self.assertEqual(self.finish(launcher=True)[:2], ([], []))
self.exe.write_bytes(b"a made-up executable, built again")
copied, swapped, said = self.finish(launcher=True)
self.assertEqual(copied, ["Triforce.exe"])
self.assertEqual((self.out / "Triforce.exe").read_bytes(), b"a made-up launcher") # (the launcher: passed over)
self.assert_installed(self.out / "bin")
def test_leftover_new_beside_a_passed_over_file_goes_unless_locked(self):
self.finish()
left = self.out / "image.bin.new"
left.write_bytes(b"an interrupted build's copy")
self.assertEqual(self.finish()[:2], ([], []))
self.assertFalse(left.exists())
if os.name == "nt": # (a read-only file is not deleted there, and the finish never unlocks one)
left.write_bytes(b"an interrupted build's copy")
left.chmod(stat.S_IREAD)
with mock.patch.object(make_game.os, "chmod", wraps=os.chmod) as chmod:
self.assertEqual(self.finish()[:2], ([], []))
chmod.assert_not_called()
self.assertEqual(left.read_bytes(), b"an interrupted build's copy")
self.assertFalse(left.stat().st_mode & stat.S_IWRITE)
def test_refused_swap_notes_nothing_and_the_retry_installs(self):
self.finish()
old_note = self.note.read_text(encoding="utf-8")
self.exe.write_bytes(b"a made-up executable, built again")
said = io.StringIO()
with mock.patch.object(make_game, "BESIDE", ["Triforce.exe", "triforce_vk.dll"]), \
mock.patch.object(make_game.os, "replace", side_effect=PermissionError("a made-up refusal")), \
contextlib.redirect_stdout(said), self.assertRaises(SystemExit):
exec(FINISH, dict(make_game.__dict__, built=str(self.built), out=str(self.out), work=str(self.work),
ROOT=str(self.root / "tools")))
self.assertIn("@@error Could not replace", said.getvalue())
self.assertEqual(self.note.read_text(encoding="utf-8"), old_note)
self.assertEqual((self.out / "Triforce.exe").read_bytes(), b"a made-up executable")
copied, swapped, said = self.finish()
self.assertEqual(copied, ["Triforce.exe"])
self.assert_installed()
def test_switch_copies_everything_and_keeps_no_note(self):
self.finish()
self.assertTrue(self.note.exists())
with mock.patch.dict(os.environ, {"HOOKSHOT_FINISH_COPY_ALL": "1"}):
for _ in range(2):
copied, swapped, said = self.finish()
self.assertEqual(copied, ["Triforce.exe", "triforce_vk.dll", "image.bin"])
self.assertEqual(swapped, ["Triforce.exe", "triforce_vk.dll", "image.bin"])
self.assertFalse(self.note.exists())
self.assertNotIn("not copied again", said)
self.assert_installed()
with mock.patch.dict(os.environ, {"HOOKSHOT_FINISH_COPY_ALL": "0"}):
self.assertEqual(len(self.finish()[0]), 3)
self.assertEqual(self.finish()[:2], ([], []))
def test_helpers(self):
self.assertEqual(make_game.file_sha1(str(self.exe)), hashlib.sha1(b"a made-up executable").hexdigest())
self.assertIsNone(make_game.file_sha1(str(self.root / "missing")))
self.assertIsNone(make_game.installed_stat(str(self.root / "missing")))
self.assertIsNone(make_game.installed_stat(str(self.out))) # (a folder is no installed file)
st = self.exe.stat()
self.assertEqual(make_game.installed_stat(str(self.exe)), [st.st_size, st.st_mtime_ns, st.st_ino, st.st_mode])
self.assertEqual(make_game.installed_name(str(self.out), str(self.out / "bin" / "Triforce.exe")), "bin/Triforce.exe")
self.assertEqual(make_game.read_installed(str(self.out)), {})
note = {"Triforce.exe": {"sha1": "abc", "stat": make_game.installed_stat(str(self.exe))}}
self.assertFalse(make_game.installed_same(note, str(self.built), str(self.exe), None))
self.assertFalse(make_game.installed_same(note, str(self.built), str(self.exe), "abd"))
self.assertTrue(make_game.installed_same(note, str(self.built), str(self.exe), "abc"))
self.assertFalse(make_game.installed_same({}, str(self.built), str(self.exe), "abc"))
def test_source_and_docs(self):
source = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
main = source[source.index("def main(a=None):"):]
for needle in ('copy_all = os.environ.get("HOOKSHOT_FINISH_COPY_ALL", "0") not in ("", "0")',
"installed_note = {} if copy_all else read_installed(out)",
"if installed_same(installed_note, out, dst, digest):",
"write_installed(out, installed_now)"):
self.assertTrue(needle in main, needle)
self.assertTrue(main.index("if installed_same(installed_note, out, dst, digest):") <
main.index("shutil.copy2(src, dst + \".new\")") < main.index("write_installed(out, installed_now)"))
self.assertEqual(make_game.INSTALLED_NOTE, ".triforce_installed.json")
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
rows = [line for line in docs.splitlines() if line.startswith("| `HOOKSHOT_FINISH_COPY_ALL=1` |")]
self.assertEqual(len(rows), 1)
self.assertTrue("IO-45" in rows[0] and ".triforce_installed.json" in rows[0])
class ReproLink(unittest.TestCase):
"""nx2/build.py repro_link: HOOKSHOT_LINK_REPRO=1 takes the link's time out of the exe and the renderer (off unless
asked: it changes the linked files' bytes), so that the finish above can pass an unchanged exe over"""
def rule(self, link_opt=lambda o: "<" + o + ">"):
source = (ROOT / "nx2" / "build.py").read_text(encoding="utf-8")
tree = ast.parse(source)
function = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "repro_link"]
scope = {"os": os, "LINUX": False, "toolchain": types.SimpleNamespace(link_opt=link_opt)}
exec(compile(ast.Module(body=function, type_ignores=[]), "build.py", "exec"), scope)
return scope["repro_link"], source
def test_rule_and_hooks(self):
repro, source = self.rule()
self.assertEqual(repro(""), [])
self.assertEqual(repro("0"), [])
self.assertEqual(repro("yes"), [])
self.assertEqual(repro("1"), ["</Brepro>"])
self.assertEqual(repro("1", linux=True), [])
with mock.patch.dict(os.environ, {"HOOKSHOT_LINK_REPRO": "1"}):
self.assertEqual(repro(), ["</Brepro>"])
environ = {k: v for k, v in os.environ.items() if k != "HOOKSHOT_LINK_REPRO"}
with mock.patch.dict(os.environ, environ, clear=True):
self.assertEqual(repro(), []) # (the default: the link is the command it was)
# the two Windows links that the finish installs ask it: the exe's and the renderer's
self.assertEqual(source.count("repro_link()"), 2)
exe = source[source.index('exe = os.path.join(out, "Triforce.exe")'):]
self.assertTrue("repro_link() +" in exe[:exe.index("capture_output=True")])
dll = source[source.index('["-shared", "-o", vdll, toolchain.link_opt("/map:" + vdll[:-4] + ".map")'):]
self.assertTrue("+ repro_link()" in dll[:dll.index("\n")])
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
self.assertEqual(len([l for l in docs.splitlines() if l.startswith("| `HOOKSHOT_LINK_REPRO=1` |") and "IO-45" in l]), 1)
def test_two_links_of_one_object_are_the_same_bytes_only_with_the_flag(self):
cc = CC and shutil.which(CC)
if os.name != "nt" or not cc:
self.skipTest("needs --cc <an llvm-mingw clang> on Windows")
target = subprocess.run([cc, "-dumpmachine"], capture_output=True, text=True).stdout.strip()
if "windows-gnu" not in target and "mingw" not in target:
self.skipTest("not llvm-mingw: %s" % target)
repro, _ = self.rule(lambda o: "-Wl,-Xlink=" + o) # (toolchain.link_opt's form for llvm-mingw)
with tempfile.TemporaryDirectory(prefix="triforce-repro-link-") as folder:
source = os.path.join(folder, "tiny.c")
with open(source, "w") as f:
f.write("int main(void) { return 0; }\n")
subprocess.run([cc, "-c", source, "-o", os.path.join(folder, "tiny.o")], check=True)
def link(flags):
digests = []
for i in range(2):
exe = os.path.join(folder, "tiny.exe")
subprocess.run([cc, os.path.join(folder, "tiny.o"), "-o", exe] + flags, check=True)
digests.append(make_game.file_sha1(exe))
os.remove(exe)
if i == 0:
time.sleep(1.2) # (the link's time is in whole seconds)
return digests
first, second = link(repro("1"))
self.assertEqual(first, second)
first, second = link(repro("0"))
self.assertNotEqual(first, second, "this linker writes no link time: the switch has nothing to do")
CC = None
if __name__ == "__main__":
if "--cc" in sys.argv:
at = sys.argv.index("--cc")
CC = sys.argv[at + 1]
del sys.argv[at:at + 2]
unittest.main()

View File

@@ -1,6 +1,9 @@
"""Exercise actual production stage-all/copy/swap statements with authored temporary files.
python -I -B tests/test_finish_output.py
(The fixture's program is Triforce.exe, the name the build gives it: under its old name the finish's own cleanup of old
programs removed it, and five of these tests stopped on a missing file.)
"""
import ast
import contextlib
@@ -49,7 +52,7 @@ class FinishOutputTests(unittest.TestCase):
self.built = self.work / "sa" / "build"
self.built.mkdir(parents=True)
self.out.mkdir()
self.source, self.destination = self.built / "hookshot.exe", self.out / "hookshot.exe"
self.source, self.destination = self.built / "Triforce.exe", self.out / "Triforce.exe"
self.source.write_bytes(b"new synthetic executable")
self.destination.write_bytes(b"existing synthetic executable")
(self.work / "sa" / "image").mkdir()
@@ -67,7 +70,7 @@ class FinishOutputTests(unittest.TestCase):
def finish(self, launcher=False):
if launcher:
(self.root / "HookshotBuilder-Setup.exe").write_bytes(b"new synthetic launcher")
with mock.patch.object(make_game, "BESIDE", ["hookshot.exe"]):
with mock.patch.object(make_game, "BESIDE", ["Triforce.exe"]):
# Comprehensions use globals. Keep one namespace, as in production, and discover only authored setup files.
namespace = dict(make_game.__dict__, built=str(self.built), out=str(self.out), work=str(self.work),
ROOT=str(self.root / "tools"), LINUX=False if launcher else make_game.LINUX)
@@ -78,7 +81,10 @@ class FinishOutputTests(unittest.TestCase):
self.assertEqual((self.out / "image.bin").read_bytes(), b"existing synthetic image")
def assert_no_staging_files(self):
self.assertEqual(sorted(path.name for path in self.out.iterdir()), ["hookshot.exe", "image.bin"])
# (the note of what was installed is the finish's own, not a staging file: the performance audit's IO-45,
# tests/test_finish_installed.py)
self.assertEqual(sorted(path.name for path in self.out.iterdir() if path.name != make_game.INSTALLED_NOTE),
["Triforce.exe", "image.bin"])
def stopped_output(self):
output = io.StringIO()
@@ -124,7 +130,7 @@ class FinishOutputTests(unittest.TestCase):
self.assert_no_staging_files()
def test_launcher_stages_all_three_files_before_replacement(self):
binary = self.out / "bin" / "hookshot.exe"
binary = self.out / "bin" / "Triforce.exe"
binary.parent.mkdir()
binary.write_bytes(b"existing synthetic game in bin")
copy = make_game.shutil.copy2
@@ -148,7 +154,7 @@ class FinishOutputTests(unittest.TestCase):
self.assertEqual(list(self.out.rglob("*.new")), [])
def test_partial_launcher_copy_preserves_game_image_and_launcher(self):
binary = self.out / "bin" / "hookshot.exe"
binary = self.out / "bin" / "Triforce.exe"
binary.parent.mkdir()
binary.write_bytes(b"existing synthetic game in bin")
copy = make_game.shutil.copy2
@@ -164,9 +170,9 @@ class FinishOutputTests(unittest.TestCase):
self.finish(launcher=True)
self.assert_installed_unchanged()
self.assertEqual(binary.read_bytes(), b"existing synthetic game in bin")
self.assertEqual(binary.with_name("hookshot.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual(binary.with_name("Triforce.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual((self.out / "image.bin.new").read_bytes(), self.image.read_bytes())
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), b"partial launcher")
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), b"partial launcher")
def test_partial_executable_copy_preserves_both_installed_files(self):
def partial_copy(source, destination):
@@ -177,7 +183,7 @@ class FinishOutputTests(unittest.TestCase):
with self.assertRaises(OSError):
self.finish()
self.assert_installed_unchanged()
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), b"incomplete replacement")
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), b"incomplete replacement")
self.assertFalse((self.out / "image.bin.new").exists())
def test_partial_image_copy_preserves_both_installed_files(self):
@@ -194,7 +200,7 @@ class FinishOutputTests(unittest.TestCase):
with self.assertRaises(OSError):
self.finish()
self.assert_installed_unchanged()
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual((self.out / "image.bin.new").read_bytes(), b"incomplete image")
def test_readonly_source_mode_is_preserved_on_success(self):
@@ -212,8 +218,8 @@ class FinishOutputTests(unittest.TestCase):
self.assertIn("writable", message)
self.assertNotIn("the game is running", message)
self.assert_installed_unchanged()
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), self.source.read_bytes())
self.assertFalse((self.out / "hookshot.exe.new").stat().st_mode & stat.S_IWRITE)
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), self.source.read_bytes())
self.assertFalse((self.out / "Triforce.exe.new").stat().st_mode & stat.S_IWRITE)
self.assertEqual((self.out / "image.bin.new").read_bytes(), self.image.read_bytes())
def test_stage_copy_permission_failure_has_actionable_paths(self):
@@ -224,13 +230,13 @@ class FinishOutputTests(unittest.TestCase):
with mock.patch.object(make_game.shutil, "copy2", side_effect=denied_copy):
message = self.stopped_output()
self.assertIn("stage", message)
self.assertIn(str(self.out / "hookshot.exe.new"), message)
self.assertIn(str(self.out / "Triforce.exe.new"), message)
self.assertIn(str(self.source), message)
self.assertIn("readable", message)
self.assertIn("writable", message)
self.assertIn("No installed files were replaced", message)
self.assert_installed_unchanged()
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), b"partial staged copy")
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), b"partial staged copy")
@unittest.skipUnless(os.name == "nt", "Windows file-sharing contract")
def test_locked_destination_reports_action_and_retains_staged_build(self):
@@ -250,7 +256,7 @@ class FinishOutputTests(unittest.TestCase):
self.assertIn(str(self.destination), message)
self.assertIn(str(self.source), message)
self.assert_installed_unchanged()
self.assertEqual((self.out / "hookshot.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual((self.out / "Triforce.exe.new").read_bytes(), self.source.read_bytes())
self.assertEqual((self.out / "image.bin.new").read_bytes(), self.image.read_bytes())
finally:
self.assertTrue(kernel.CloseHandle(handle))
@@ -270,7 +276,7 @@ class FinishOutputTests(unittest.TestCase):
@unittest.skipUnless(os.name == "nt", "Windows read-only staging-file contract")
def test_readonly_existing_stage_is_not_removed_or_unlocked(self):
staged = self.out / "hookshot.exe.new"
staged = self.out / "Triforce.exe.new"
staged.write_bytes(b"pre-existing staged content")
staged.chmod(stat.S_IREAD)
with mock.patch.object(make_game.os, "chmod", wraps=make_game.os.chmod) as chmod:

165
tests/test_first_start.py Normal file
View File

@@ -0,0 +1,165 @@
"""A first start sized to the card: with RX_VK_FIRST_START_AUTO, the Render resolution row set once on a weak GPU
(gpu/vk/first_start.h, vk_menu.cpp menu_first_start, vk_main.cpp mkwvk_init; no game, no GPU).
(Triforce, 11-22, the performance audit's PRESENT-46, after Source A's.) With no options file every card started with
the Render resolution at Auto. Source A's tier 1 (an integrated GPU, a card without mesh shaders, 4.5 GB of video memory
or less) starts with the 3D at 50%, or 33% for an integrated GPU beside 4 threads or fewer. Here that is behind a
switch that is off unless set: what a first start on a weak card gets is the owner's to decide.
Compiles first_start.h and checks the rule for many cards:
integrated GPUs (a handheld's, a laptop's, a desktop's with 2 to 32 threads) 50%, and 33% at 4 threads or fewer
discrete cards without mesh shaders (2 to 12 GB) 50%
discrete cards with them: 2, 3, 4 and 4.5 GB 50%
4.5 GB and a megabyte, 6, 8, 11, 12, 16, 24, 32 GB no change
a card that reports no memory, or no thread count never tier 1 for that alone
and that nothing in the rule reads the SteamDeck variable (a desktop card started from that launcher stays as it is);
the switch (off when unset or 0, on at 1, a larger number plays that many MB); and that the choice is made only with
the switch on, no options file, RX_VK_LOW_SPEC not asked and HOS_DYNRES not set.
Then the source: menu_init only notes that there was no file; menu_first_start reads the switch and the card, sets the
row through the upscaler's own steps, moves the restart row's start value with it, saves nothing and logs one line;
mkwvk_init calls it after the device is made and its line logged; and the switch's row is in the development guide.
python -I -B tests/test_first_start.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <initializer_list>
#include "first_start.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace first_start;
static Choice card(bool integrated, bool mesh, uint64_t mb, unsigned threads) {
Card c;
c.integrated = integrated, c.mesh_shaders = mesh, c.vram_mb = mb, c.threads = threads;
return choose(c);
}
int main() {
CHECK(kRowHalf == 4 && kRowThird == 6 && kSmallVramMb == 4608 && kFewThreads == 4);
// integrated GPUs: tier 1 whatever they report; 33% beside 4 threads or fewer
for (uint64_t mb : {0ull, 512ull, 2048ull, 3840ull, 8192ull, 16384ull, 32768ull})
for (int mesh = 0; mesh < 2; mesh++) {
for (unsigned t : {1u, 2u, 4u}) CHECK(card(true, mesh, mb, t).row == kRowThird && card(true, mesh, mb, t).why == kIntegrated);
for (unsigned t : {0u, 5u, 6u, 8u, 12u, 16u, 32u})
CHECK(card(true, mesh, mb, t).row == kRowHalf && card(true, mesh, mb, t).why == kIntegrated);
}
CHECK(card(true, true, 3840, 8).row == kRowHalf); // (a handheld: 4 cores, 8 threads, its own GPU)
CHECK(card(true, true, 8192, 4).row == kRowThird); // (Source A's note: a 2-core, 4-thread laptop's own GPU)
// discrete cards without mesh shaders: 50%, whatever their memory and threads
for (uint64_t mb : {0ull, 2048ull, 4096ull, 8192ull, 11264ull, 12288ull})
for (unsigned t : {0u, 2u, 4u, 8u, 16u}) CHECK(card(false, false, mb, t).row == kRowHalf && card(false, false, mb, t).why == kNoMeshShaders);
// discrete cards with mesh shaders: by their video memory alone, 4.5 GB the last of tier 1
for (unsigned t : {0u, 2u, 4u, 6u, 8u, 16u, 32u}) {
for (uint64_t mb : {1024ull, 2048ull, 3072ull, 4018ull, 4096ull, 4607ull, 4608ull})
CHECK(card(false, true, mb, t).row == kRowHalf && card(false, true, mb, t).why == kSmallVram);
for (uint64_t mb : {4609ull, 5892ull, 6144ull, 8188ull, 8192ull, 10240ull, 11264ull, 12288ull, 16376ull, 24564ull, 32607ull})
CHECK(card(false, true, mb, t).row == 0 && card(false, true, mb, t).why == kKeep);
CHECK(card(false, true, 0, t).row == 0); // (no memory reported: not tier 1 for that)
}
// the switch
CHECK(switch_mb(nullptr) == -1 && switch_mb("") == -1 && switch_mb("0") == -1 && switch_mb("-5") == -1 && switch_mb("on") == -1);
CHECK(switch_mb("1") == 0 && switch_mb("2") == 2 && switch_mb("4096") == 4096 && switch_mb("24564") == 24564);
// when the choice is made at all: (has_options, low_spec, resolution_env, switch)
CHECK(applies(false, false, false, 0) && applies(false, false, false, 4096));
CHECK(!applies(false, false, false, -1)); // (the switch off: the default)
CHECK(!applies(true, false, false, 0) && !applies(true, false, false, 4096)); // (an options file: the player's own)
CHECK(!applies(false, true, false, 0)); // (RX_VK_LOW_SPEC sets the row itself)
CHECK(!applies(false, false, true, 0)); // (HOS_DYNRES sets it for the run)
for (int m = 0; m < 8; m++)
for (int64_t sw : {(int64_t)-1, (int64_t)0, (int64_t)4096})
CHECK(applies(m & 1, m & 2, m & 4, sw) == (m == 0 && sw >= 0));
// the lab's number plays a small card on a large one
CHECK(card(false, true, (uint64_t)switch_mb("4096"), 32).row == kRowHalf && card(false, true, (uint64_t)switch_mb("6144"), 32).row == 0);
CHECK(std::strlen(why_text(kIntegrated)) && std::strlen(why_text(kNoMeshShaders)) && std::strlen(why_text(kSmallVram)) &&
std::strlen(why_text(kKeep)));
std::puts("first start: tier 1 for integrated GPUs, cards without mesh shaders and 4.5 GB or less; off unless asked");
return fails ? 1 : 0;
}
"""
def check_source():
header = (VK / "first_start.h").read_text(encoding="utf-8")
code = "\n".join(l for l in header.splitlines() if not l.lstrip().startswith("//"))
check("getenv" not in code and "SteamDeck" not in code and "#include <vulkan" not in code,
"first_start.h reads the environment (a desktop card under SteamDeck=1 must stay as it is) or needs Vulkan")
menu = (VK / "vk_menu.cpp").read_text(encoding="utf-8")
init = block(menu, "void menu_init() {")
check("first_start_no_file = !has_options;" in init and "first_start::" not in init and "RX_VK_FIRST_START_AUTO" not in init,
"menu_init does more than note that there was no options file (the card is not known there)")
check(init.index("first_start_no_file = !has_options;") < init.index('getenv("RX_VK_LOW_SPEC")'), "the note is not before RX_VK_LOW_SPEC's block")
first = block(menu, "void menu_first_start() {")
for part in ('first_start::switch_mb(getenv("RX_VK_FIRST_START_AUTO"))',
'const bool low_spec = getenv("RX_VK_LOW_SPEC") && atoi(getenv("RX_VK_LOW_SPEC")) != 0;',
'if (!first_start::applies(!first_start_no_file, low_spec, getenv("HOS_DYNRES") != nullptr, asked)) return;',
"card.integrated = dev.props.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;",
"card.mesh_shaders = dev.mesh_shader;",
"card.vram_mb = asked > 0 ? (uint64_t)asked : device_vram_bytes() >> 20;",
"card.threads = cpu_count();", # ((Triforce, 11-22, OS-49) cpu_count.h
"opts.resolution = ur::snap(upscaler(), choice.row);",
"restart_state.restart_value(restart_slot_of(r), restart_value_of(r));",
'logf("menu: first start: tier 1 ('):
check(part in first, "menu_first_start: missing " + part)
check(first.index("first_start::applies(") < first.index("first_start::choose(card)") < first.index("opts.resolution ="),
"menu_first_start sets the row before it has asked whether it applies")
check(first.count("opts.") == first.count("opts.resolution") and "SteamDeck" not in first and "fopen" not in first
and "save" not in first.replace("nothing is saved", ""),
"menu_first_start sets another row than the Render resolution, reads SteamDeck, or saves the options")
check("void menu_first_start();" in (VK / "vk_menu.h").read_text(encoding="utf-8"), "vk_menu.h does not declare menu_first_start")
main = (VK / "vk_main.cpp").read_text(encoding="utf-8")
start = main[main.index("const MkwVkRenderer* mkwvk_init(const MkwVkHost* h) {"):]
order = [start.index(p) for p in ("if (!create_instance() || !pick_device() || !create_device()) return nullptr;",
'logf("vk: %s, Vulkan %u.%u, driver %#x; mesh shaders %s', " menu_first_start();\n",
"table.start = start;")]
check(order == sorted(order) and main.count("menu_first_start()") == 1,
"mkwvk_init does not call menu_first_start once, after the device is made and before the renderer's table is given out")
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("| `RX_VK_FIRST_START_AUTO=1`, `RX_VK_FIRST_START_AUTO=<MB>` |" in docs, "docs/development.md has no row for RX_VK_FIRST_START_AUTO")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="first-start-") as tmp:
src, exe = Path(tmp) / "first.cpp", Path(tmp) / "first.exe"
src.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I", str(VK), str(src), "-o", str(exe)], check=True)
out = subprocess.run([str(exe)], stdout=subprocess.PIPE, text=True, timeout=60)
print(out.stdout.strip())
if out.returncode:
raise SystemExit("FAIL: the rule")
print("PASS: a first start on a weak card takes its Render resolution only with RX_VK_FIRST_START_AUTO, no options file "
"and no RX_VK_LOW_SPEC; the hook is after the device")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,223 @@
"""Flushes with nothing held skip the queue's lock (gpu/vk/vk_replay.cpp pend_held, RX_VK_PEND_HELD).
queue_flush_for and queue_flush_poll took dev.present_mu to find nothing held, at every fence wait and progress poll (with
batching off, always), behind a present or a late page's fix waiting on the GPU. pend_held is raised under the lock when a
submit is held and lowered when the batch goes out; both flushes look at it first. A held submit's recording is registered
for the waiters only after it is in pend with the flag raised, so a waiter that finds the registration finds it held (or
already sent) and flushes it.
Checked, compiled (pend_held, pend_held_on, queue_flush_for and queue_flush_poll cut out of vk_replay.cpp unchanged, with a
counting mutex for dev.present_mu): nothing held, no lock; held, the lock and the flush; RX_VK_PEND_HELD=0 and the lab knob
nopendheld, the lock every time; four waiters against a submitter that holds, raises and then registers: every waiter's
flush after it found a registration leaves that recording sent. In the source: the lowering after pend.cbs.clear(); in
submit_recording the registration before the lock only when not registered late, after the push and the raise when held,
before the direct submit otherwise; wait_recorded's counter read before its flush; vk_timeline_wait's second flush once its
lookup finds the recording; the docs row.
python -I -B tests/test_flush_nothing_held.py [--cxx clang++]
"""
import argparse
import os
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
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 fail(message):
raise SystemExit("FAIL: " + message)
def in_order(text, parts, where):
at = 0
for part in parts:
found = text.find(part, at)
if found < 0:
fail("%s: %r is missing, or not after the part before it" % (where, part))
at = found + len(part)
FIXTURE = r"""
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <thread>
#include <vector>
namespace lab_ab {
bool knob = false;
bool on(const char* name) { return knob && !strcmp(name, "nopendheld"); }
}
struct CountingMutex {
std::mutex m;
std::atomic<long> locks{0};
void lock() { locks.fetch_add(1); m.lock(); }
void unlock() { m.unlock(); }
};
struct { CountingMutex present_mu; } dev;
struct { std::vector<int> cbs; } pend;
int flush_reason;
enum { FR_OTHER, FR_POLL, FR_WAIT, FR_VALUE };
std::chrono::steady_clock::time_point pend_since;
DECL
uint64_t held_seq; // (the newest recording in pend; dev.present_mu)
std::atomic<uint64_t> sent_seq{0}; // (the newest one sent)
long flushes;
void queue_flush_locked() { // (the real one's end: the batch out, then the flag lowered)
if (pend.cbs.empty()) return;
flushes++;
sent_seq.store(held_seq, std::memory_order_relaxed);
pend.cbs.clear();
pend_held.store(false, std::memory_order_release);
}
void hold(uint64_t seq) { // (submit_recording's held path)
std::lock_guard lk(dev.present_mu);
pend.cbs.push_back(1);
held_seq = seq;
pend_held.store(true, std::memory_order_release);
}
FLUSH
int main(int argc, char** argv) {
const char* mode = argc > 1 ? argv[1] : "";
int bad = 0;
auto check = [&](bool ok, const char* what) { if (!ok) { std::printf("FAIL %s\n", what); bad++; } };
if (!strcmp(mode, "basic") || !strcmp(mode, "off") || !strcmp(mode, "knob")) {
lab_ab::knob = !strcmp(mode, "knob");
const bool skip = !strcmp(mode, "basic");
const long l0 = dev.present_mu.locks;
for (int i = 0; i < 1000; i++) queue_flush_for(FR_WAIT), queue_flush_poll();
check(skip ? dev.present_mu.locks == l0 : dev.present_mu.locks == l0 + 2000, "nothing held: the lock as the mode says");
check(flushes == 0, "nothing held: no flush");
hold(7);
const long l1 = dev.present_mu.locks;
queue_flush_for(FR_WAIT);
check(dev.present_mu.locks == l1 + 1 && flushes == 1 && sent_seq == 7 && !pend_held.load(), "held: the lock and the flush");
hold(8);
queue_flush_poll();
check(flushes == 2 && sent_seq == 8 && pend.cbs.empty(), "held: the poll flushes (RX_VK_BATCH_POLL_US=0)");
const long l2 = dev.present_mu.locks;
queue_flush_for(FR_WAIT), queue_flush_poll();
check(skip ? dev.present_mu.locks == l2 : dev.present_mu.locks == l2 + 2, "after the flush: nothing held again");
std::printf("%s: %ld locks, %ld flushes\n", mode, (long)dev.present_mu.locks, flushes);
return bad;
}
if (!strcmp(mode, "race")) {
std::mutex map_mu; // (rec_map_mu)
uint64_t registered = 0;
std::atomic<bool> stop{false};
std::atomic<long> late{0}, looks{0};
std::thread submitter([&] {
for (uint64_t s = 1; s <= 200000; s++) {
{
std::lock_guard lk(dev.present_mu);
pend.cbs.push_back(1);
held_seq = s;
pend_held.store(true, std::memory_order_release);
std::lock_guard mk(map_mu); // (registered after the raise, under the lock)
registered = s;
}
if (s % 64 == 0) { std::lock_guard lk(dev.present_mu); queue_flush_locked(); } // (the cap)
}
stop = true;
});
std::vector<std::thread> waiters;
for (int w = 0; w < 4; w++)
waiters.emplace_back([&] {
while (!stop) {
uint64_t s;
{
std::lock_guard mk(map_mu);
s = registered;
}
if (!s) continue;
queue_flush_for(FR_WAIT);
looks++;
if (sent_seq.load(std::memory_order_relaxed) < s) late++;
}
});
submitter.join();
for (auto& t : waiters) t.join();
std::printf("race: %ld looks, %ld left a found recording held\n", (long)looks, (long)late);
return late != 0;
}
return 1;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
src = (ROOT / "gpu" / "vk" / "vk_replay.cpp").read_text(encoding="utf-8")
if "std::atomic<bool> pend_held{false};" not in src:
fail("vk_replay.cpp has no pend_held")
decl = src[src.index("std::atomic<bool> pend_held{false};"):]
decl = decl[:decl.index(block(decl, "bool pend_held_on() {")) + len(block(decl, "bool pend_held_on() {"))]
flush = block(src, "void queue_flush_for(int why) {") + "\n" + block(src, "void queue_flush_poll() {")
for name in ("queue_flush_for", "queue_flush_poll"):
body = block(src, "void %s(" % name)
if body.index("pend_held.load(std::memory_order_acquire)") > body.index("std::lock_guard lk(dev.present_mu);"):
fail("%s: the look is not before the lock" % name)
locked = block(src, "void queue_flush_locked() {")
in_order(locked, ["pend.cbs.clear()", "pend_held.store(false, std::memory_order_release);"], "queue_flush_locked")
sub = block(src, "void submit_recording(Recording& r, bool signal_value = true, uint64_t signal_at = 0, bool part = false) {")
in_order(sub, ["seq = ++rec_seq;", "const auto register_recording = [&] {", "std::lock_guard mk(rec_map_mu);",
"fin_rec.submitted(rec_open_values, seq, !part);", "};",
"const bool batching = batch_on(), registered_late = batching && pend_held_on();",
"if (!registered_late) register_recording();", "std::lock_guard lk(dev.present_mu);", "if (batch_on()) {",
"for (uint32_t i = 0; i < n; i++) pend.cbs.push_back(cb[i]);", "pend.recs++;",
"pend_held.store(true, std::memory_order_release);", "if (registered_late) register_recording();",
"queue_flush_locked();", "return;", "if (registered_late) register_recording();",
"vkQueueSubmit2(dev.queue, 1, &si, r.fence);"], "submit_recording")
if sub.count("register_recording();") != 3:
fail("submit_recording: the registration is made somewhere this test does not know")
wr = block(src, "bool wait_recorded(uint64_t f, uint64_t timeout_ns, int* ok) {")
in_order(wr, ["seq = it->second;", "pend_held_on() && vkGetSemaphoreCounterValue(dev.device, rec_timeline, &rec_done) == VK_SUCCESS && rec_done >= seq",
"*ok = 1;", "return true;", "queue_flush_for(FR_WAIT);"], "wait_recorded")
tw = block(src, "void vk_timeline_wait(uint64_t v) {")
in_order(tw, ["queue_flush_for(FR_VALUE);", "seq = fin_rec.of(v);", "if (seq && pend_held_on()) queue_flush_for(FR_VALUE);"],
"vk_timeline_wait")
docs = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
if "`RX_VK_PEND_HELD=0`" not in docs:
fail("docs/development.md: no RX_VK_PEND_HELD row")
print("ok the source: the look before the lock, the lowering after the clear, the registration after the raise, the "
"counter read, the second flush")
with tempfile.TemporaryDirectory(prefix="flush-nothing-held-") as tmp:
c, exe = Path(tmp) / "fixture.cpp", Path(tmp) / "fixture.exe"
c.write_text(FIXTURE.replace("DECL", decl).replace("FLUSH", flush), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-pthread", "-Wall", "-Wextra", "-Werror", str(c), "-o", str(exe)], check=True)
for mode, env in (("basic", {}), ("off", {"RX_VK_PEND_HELD": "0"}), ("knob", {}), ("race", {})):
e = dict(os.environ, RX_VK_BATCH_POLL_US="0")
e.pop("RX_VK_PEND_HELD", None)
e.update(env)
r = subprocess.run([str(exe), mode], capture_output=True, text=True, env=e, timeout=300)
print(("ok " if r.returncode == 0 else "FAIL ") + r.stdout.strip().replace("\n", "; "))
if r.returncode:
fail("the fixture's %s mode" % mode)
print("PASS: flushes with nothing held skip the queue's lock")
if __name__ == "__main__":
main()

220
tests/test_fma_soft.py Normal file
View File

@@ -0,0 +1,220 @@
"""The exact fused multiply-add bodies' short way out on a CPU without FMA3, bit-exact with the bodies (game-free).
(Triforce, 11-22, the performance audit's LIFT-06, after Source A's.) Where the build has no hardware FMA (x86-64-v2)
every double-precision FMADD, and each single-precision one its binary64 fast path cannot prove, runs the kit's exact
body: integer code of many words. nx2/fma_soft.py puts a short way out at the head of recomp_fpe_fma64 and
recomp_fpe_fma32 for the plain case (default FPCR with or without FZ, three normal operands, a normal result): an exact
product and sum in binary64, rounded to odd, then rounded once, FPSR.IXC set when the result is inexact. Everything
else goes on into the body.
The text: the kit's runtime C from a64_rtgen.runtime_c() under fma_soft.install() must be the kit's own text with
three pieces put in (the header after `#include "a64.h"`, one group of lines after each body's head) and nothing else
changed; with A64_NO_FMA_SOFT=1 it must be the kit's text byte for byte.
The run: tests/fma_soft_test.c calls both bodies with the way out and without it (the same body past its first lines,
so the kit's code) from the same Ctx, and compares the value and every byte of the Ctx:
- random: 10 million operand triples a width (zeros, subnormals, infinities, NaNs of both kinds with payloads, the
extreme normals, exponents everywhere, at the way out's limits and matched to the product; addends that cancel the
product within a few ulps, sit at its half ulp, or have the product at their half ulp), under random FPCR (FZ, DN,
every rounding mode, FIZ/AH/NEP, the trap enables, FZ16, the host's kill switch) and FPSR states;
- sweep: about 7 million: edge exponents and fractions for both factors, each with 71 addends placed against the
product, under FZ on and off with IXC set and clear.
Built at -march=x86-64-v2 and -march=x86-64 (no FMA: the way out is compiled, and must answer calls, set IXC on some,
leave it clear on exact ones, and answer under FZ), and at -march=x86-64-v3 and -march=native (with FMA3 it is compiled
out: no call may be answered). A build without the test's hook checks HOS_FMA_FAST as the game reads it: unset and 1
answer, 0 answers none.
And reads the source: nx2/lift.py installs it, A64_NO_FMA_SOFT is an A64_ name (in nx2/make_game.py's lift key, as
nx2/fma_soft.py is), and both switches are in the switches table.
python -I -B tests/test_fma_soft.py [--cc clang] [--cxx clang++] [--random 10000000] [--time]
Skipped (exit 0) on a host that is not x86-64 or has no C or C++ compiler.
"""
import argparse
import os
import platform
import re
import subprocess
import sys
import tempfile
import time
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
sys.path.insert(0, os.path.join(ROOT, "nx2"))
import test_fpx_sticky as fs # noqa: E402 (the build helpers the kit's other lifted-code tests use)
RUN_ALL_ARGS = ["--cc", "clang", "--cxx", "{cxx}"]
SEED = 20261122
MARCHES = ("x86-64-v2", "x86-64", "x86-64-v3", "native")
HOOK = """/* tests/test_fma_soft.py: the way out switched by the driver, and its answers counted */
extern int fma_soft_test_on;
extern long long fma_soft_test_exits;
#define RECOMP_FMA_SOFT_PROBE() (fma_soft_test_exits++)
#ifndef FMA_SOFT_ENV
#define RECOMP_FMA_SOFT_ON() fma_soft_test_on
#endif
"""
def generate(cxx, tmp, flags):
"""The kit's runtime header and C (the C with the way out), a64_fpx.h and the hook header in tmp. Returns the C file"""
dll = os.path.join(tmp, "a64emit.dll" if os.name == "nt" else "liba64emit.so")
fs.build([cxx, "-std=c++20", "-O0", "-shared", "-Wno-everything", "-o", dll, os.path.join(fs.KIT, "lift", "a64emit.cpp")]
+ flags + ([] if os.name == "nt" else ["-fPIC"]), "a64emit")
import kit # noqa: F401 (a64-kit on the path)
import build_emit
build_emit.build = lambda force=False: dll # (not a64emit.dll beside the source)
import a64_lift
import a64_rtgen
import a64emit
import fma_soft
try:
kit_text = a64_rtgen.runtime_c()
fma_soft.install() # (the wrapper under test, around the kit's runtime_c)
now = a64_rtgen.runtime_c()
os.environ["A64_NO_FMA_SOFT"] = "1"
try:
off = a64_rtgen.runtime_c()
finally:
del os.environ["A64_NO_FMA_SOFT"]
before = set(os.listdir(tmp))
a64_rtgen.write(tmp)
finally:
if a64emit._lib is not None: # (unloaded, so the folder can go)
import _ctypes
(_ctypes.FreeLibrary if os.name == "nt" else _ctypes.dlclose)(a64emit._lib._handle)
a64emit._lib = None
if off != kit_text:
raise SystemExit("fma soft: with A64_NO_FMA_SOFT=1 the runtime C is not the kit's text")
pieces = [fma_soft.HEADER] + [fma_soft.EARLY % n for n in (32, 64)]
stripped = now
for piece in pieces:
if stripped.count(piece) != 1:
raise SystemExit("fma soft: the runtime C does not hold this piece once: %s" % piece.strip()[:80])
stripped = stripped.replace(piece, "")
if stripped != kit_text:
raise SystemExit("fma soft: the runtime C differs from the kit's by more than the header and the two ways out")
for n in (32, 64): # (the way out is the body's first lines: right after its head)
if (fma_soft.HEAD % n) + (fma_soft.EARLY % n) not in now:
raise SystemExit("fma soft: recomp_fpe_fma%d does not begin with the way out" % n)
rt_c = [f for f in sorted(set(os.listdir(tmp)) - before) if f.endswith(".c")]
if len(rt_c) != 1:
raise SystemExit("fma soft: a64_rtgen wrote %s, not one C file" % rt_c)
with open(os.path.join(tmp, rt_c[0]), encoding="utf-8") as f:
if f.read() != now:
raise SystemExit("fma soft: a64_rtgen.write did not write the runtime C with the way out")
with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f:
f.write(a64_lift.fpx_header(set()))
with open(os.path.join(tmp, "fma_soft_hook.h"), "w", newline="\n") as f:
f.write(HOOK)
return os.path.join(tmp, rt_c[0])
def check_sources():
with open(os.path.join(ROOT, "nx2", "lift.py"), encoding="utf-8") as f:
lift = f.read()
at = [lift.find(p) for p in ("import fma_soft", "import a64_rtgen", "fma_soft.install()", "def main():")]
if min(at) < 0 or not at[1] < at[2] < at[3]:
raise SystemExit("fma soft: nx2/lift.py does not install nx2/fma_soft.py at module level")
with open(os.path.join(ROOT, "nx2", "make_game.py"), encoding="utf-8") as f:
make_game = f.read()
if 'if k.startswith(("A64_", "OOT_", "RX_"))' not in make_game:
raise SystemExit("fma soft: nx2/make_game.py's lift_key no longer takes the A64_ variables: A64_NO_FMA_SOFT is not in the key")
not_keyed = re.search(r"^LIFT_KEY_NOT = \((.*?)\) #", make_game, re.S | re.M)
if not not_keyed or '"fma_soft.py"' in not_keyed.group(1):
raise SystemExit("fma soft: nx2/fma_soft.py must stay a file of the lift key")
with open(os.path.join(ROOT, "docs", "development.md"), encoding="utf-8") as f:
if "| `HOS_FMA_FAST=0`, `A64_NO_FMA_SOFT=1` |" not in f.read():
raise SystemExit("fma soft: docs/development.md: HOS_FMA_FAST and A64_NO_FMA_SOFT are not in the switches table")
def counts(text, mode):
"""{(mode, width): {name: number}} from the driver's lines"""
out = {}
for line in text.splitlines():
m = re.match(r"%s fma(\d+) (.*)" % mode, line)
if m:
out[int(m.group(1))] = {k: int(v) for k, v in (kv.split("=") for kv in m.group(2).split())}
return out
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--cc")
p.add_argument("--cxx")
p.add_argument("--random", type=int, default=10000000)
p.add_argument("--time", action="store_true", help="also time a plain call with the way out and without, at x86-64-v2")
a = p.parse_args()
check_sources()
if platform.machine().lower() not in ("x86_64", "amd64"):
fs.skip("not an x86-64 host: %s" % platform.machine())
cc = fs.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang")
cxx = fs.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++")
if not cc or not cxx:
fs.skip("no C compiler" if not cc else "no C++ compiler")
t0 = time.time()
failed, total = [], 0
with tempfile.TemporaryDirectory(prefix="triforce-fma-soft-") as tmp:
rt_c = generate(cxx, tmp, fs.target_flags(cxx, tmp))
def build(march, extra, name):
exe = os.path.join(tmp, "%s_%s.exe" % (name, march))
fs.build([cc] + fs.target_flags(cc, tmp) + ["-march=" + march] + fs.FLAGS + extra +
["-D_CRT_SECURE_NO_WARNINGS", "-include", os.path.join(tmp, "fma_soft_hook.h"),
"-I" + os.path.join(fs.KIT, "rt"), "-I" + tmp, os.path.join(ROOT, "tests", "fma_soft_test.c"), rt_c,
"-o", exe], "the driver at " + march)
return exe
for march in MARCHES:
exe = build(march, [], "fma_soft_test")
for mode, args in (("random", ["random", str(a.random), str(SEED)]), ("sweep", ["sweep"])):
r = fs.run([exe] + args, timeout=3600)
print("%-10s %s" % (march, r.stdout.rstrip().replace("\n", "\n ")))
if r.returncode == 3:
print("fma soft: %s skipped (this CPU cannot run it)" % march)
break
got = counts(r.stdout, mode)
if r.returncode or sorted(got) != [32, 64]:
failed.append("%s %s (exit %d)" % (march, mode, r.returncode))
continue
for width, c in sorted(got.items()):
total += c["checks"]
if c["fma64"]: # (FMA3: compiled out)
if c["exits"]:
failed.append("%s %s fma%d: the way out answered %d calls in a build with FMA3" % (march, mode, width, c["exits"]))
elif not (c["exits"] and c["new_ixc"] and c["exact"] and c["fz"]):
failed.append("%s %s fma%d: the way out is not exercised: %s" % (march, mode, width, c))
if not c["fma64"] and march in ("x86-64-v3",):
failed.append("%s: built without FMA3" % march)
if c["fma64"] and march in ("x86-64-v2", "x86-64"):
failed.append("%s: built with FMA3" % march)
# HOS_FMA_FAST as the game reads it (the build without the hook's switch): unset and 1 answer, 0 answers none
exe = build("x86-64-v2", ["-DFMA_SOFT_ENV"], "fma_soft_env")
sums = set()
for value, want in ((None, True), ("1", True), ("0", False)):
env = {k: v for k, v in os.environ.items() if k != "HOS_FMA_FAST"}
if value is not None:
env["HOS_FMA_FAST"] = value
r = subprocess.run([exe, "env"], stdout=subprocess.PIPE, text=True, env=env, timeout=120)
m = re.match(r"fma64=0 exits=(\d+) sum=(\w+)", r.stdout)
if r.returncode == 3:
break
if not m or (int(m.group(1)) == 2000) != want or (int(m.group(1)) == 0) == want:
failed.append("HOS_FMA_FAST=%s: %s" % (value, r.stdout.strip()))
elif m:
sums.add(m.group(2))
if len(sums) > 1:
failed.append("HOS_FMA_FAST changes a result: %s" % sorted(sums))
if a.time and not failed:
print("timing at -O2 -march=x86-64-v2:")
print(fs.run([build("x86-64-v2", [], "fma_soft_test"), "time"], timeout=1200).stdout.rstrip())
if failed:
raise SystemExit("FAIL: fma soft: %s" % "; ".join(failed))
print("PASS: %d calls of the exact FMA bodies give the same value and context with the short way out as without (%.0f s)"
% (total, time.time() - t0))
if __name__ == "__main__":
main()

246
tests/test_fma_steps.py Normal file
View File

@@ -0,0 +1,246 @@
"""The single-precision FRECPS and FRSQRTS steps as one fused instruction on x86-64-v3, bit-exact with the binary64 form
and with the exact bodies (game-free).
(Triforce, 11-22, the performance audit's LIFT-05, after Source A's.) The kit's fast path for the Newton-Raphson steps
(recomp_fpx_recps32: 2 - a*b; recomp_fpx_rsqrts32: (3 - a*b) / 2) computes them in binary64 on x86-64 and declines a
binary32 midpoint, because the baseline has no FMA. nx2/fma_steps.py gives both the guard the kit's fused multiply-add
already has: where the build has FMA3, one fused instruction, the step rounded once.
The text: the kit's runtime header from a64_rtgen.runtime_h() under fma_steps.install() must be the kit's own with those
two #if lines changed and nothing else, all three functions (fma32, recps32, rsqrts32) under the same guard; with
A64_NO_FMA_STEPS=1 it must be the kit's text byte for byte.
The run: guest words (FRECPS and FRSQRTS: scalar S, 2S and 4S, with Vd apart, Vd = Vn, Vd = Vm, one register for all
and registers above 15; and the D forms, which do not change) go through a64emit.translate and a64_lift.op_text once,
and that one text is compiled twice, against the header as written now and against the kit's (A64_NO_FMA_STEPS=1),
tests/fma_steps_side.c. tests/fma_steps_test.c runs each word as lifted and as a hot function has it on both sides,
and the exact body inline (the word translated with the fast path off), from the same random Ctx: operands biased to
zeros, infinities, NaNs of both kinds with payloads, subnormals, the extreme normals, steps that cancel and steps that are
exactly a binary32 midpoint; random FPCR (FZ, DN, the rounding modes, the other control bits) and FPSR (the gate open and
closed), every flag combination. The whole Ctx, FPSR in it, must be the same in all five. Then the two fast paths by
themselves: what both sides keep must be the same bits, what either keeps must be the exact body's value with FPSR
untouched, and the kit's side must keep nothing the new one declines, with one exception that the driver checks by its
size: an FRSQRTS step of 2^127 or more, whose fused 3 - a*b is past binary32's range before the halving, so the new side
declines it and the exact body answers (the same value).
Built at -march=x86-64-v3 and -march=native (with FMA3 the new side must keep midpoints the kit's declines, the audit's
two named pairs among them), at -march=x86-64-v2 and -march=x86-64 (no FMA3: both sides must decide alike on every pair),
and at -march=x86-64-v3 with -DA64_FPX_NO_FMA32 (the binary64 form again: alike too).
And reads the source: nx2/lift.py installs it, A64_NO_FMA_STEPS is an A64_ name (in nx2/make_game.py's lift key, as
nx2/fma_steps.py is), and the switch is in the switches table.
python -I -B tests/test_fma_steps.py [--cc clang] [--cxx clang++] [--random 20000] [--direct 4000000]
Skipped (exit 0) on a host that is not x86-64 or has no C or C++ compiler.
"""
import argparse
import os
import platform
import re
import sys
import tempfile
import time
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
sys.path.insert(0, os.path.join(ROOT, "nx2"))
import test_fpx_sticky as fs # noqa: E402 (the build helpers the kit's other lifted-code tests use)
RUN_ALL_ARGS = ["--cc", "clang", "--cxx", "{cxx}"]
SEED = 20261122
# (-march, more flags, whether the steps are the fused instruction there; None: as this CPU has it)
BUILDS = (("x86-64-v3", [], True), ("x86-64-v2", [], False), ("x86-64", [], False), ("native", [], None),
("x86-64-v3", ["-DA64_FPX_NO_FMA32"], False))
REGS = ((0, 5, 1), (5, 5, 1), (1, 5, 1), (2, 2, 2), (3, 30, 17)) # (rd, rn, rm)
def cases():
"""(name, guest word, rn, rm, double)"""
out = []
for name, base, dbl in (("FRECPS s", 0x5E20FC00, 0), ("FRSQRTS s", 0x5EA0FC00, 0), ("FRECPS .2s", 0x0E20FC00, 0),
("FRSQRTS .2s", 0x0EA0FC00, 0), ("FRECPS .4s", 0x4E20FC00, 0), ("FRSQRTS .4s", 0x4EA0FC00, 0),
("FRECPS d", 0x5E60FC00, 1), ("FRSQRTS d", 0x5EE0FC00, 1), ("FRECPS .2d", 0x4E60FC00, 1),
("FRSQRTS .2d", 0x4EE0FC00, 1)):
for rd, rn, rm in (REGS[:2] if dbl else REGS):
out.append(("%s v%d, v%d, v%d" % (name, rd, rn, rm), base | (rm << 16) | (rn << 5) | rd, rn, rm, dbl))
return out
def lifted(a64emit, a64_lift, used):
"""fma_steps_lifted.h: each guest word as lifted, as a hot function has it and with the exact body inline"""
all_cases = cases()
words = [(0x1000 + 4 * i, c[1]) for i, c in enumerate(all_cases)]
texts, flags = a64emit.translate(words)
if any(flags):
raise SystemExit("fma steps: a64emit did not translate the guest words: %s" % flags)
texts = [a64_lift.op_text(t, used) for t in texts]
a64emit.lib().a64_config(1, 0) # (the fast path off: the exact body inline, the reference)
try:
exact, _ = a64emit.translate(words)
finally:
a64emit.lib().a64_config(1, 1)
out = ["/* Generated by tests/test_fma_steps.py from guest words, as a64_lift.py lifts them. */"]
for i, (t, ex, c) in enumerate(zip(texts, exact, all_cases)):
helper = "a64_fpx_%s%d_" % ("rsqrts" if "FRSQRTS" in c[0] else "recps", 64 if c[4] else 32)
if helper not in t or "recomp_fpx_" in ex:
raise SystemExit("fma steps: %s is not lifted through %s, or its exact form has a fast path" % (c[0], helper))
out.append("static void g_lifted%d(Ctx *__restrict c) { %s }" % (i, t))
out.append("static void g_hot%d(Ctx *__restrict c) { %s }" % (i, a64_lift.hot_text(t)))
out.append("static void g_exact%d(Ctx *__restrict c) { %s }" % (i, ex))
out.append("static const struct { const char *name; int rn, rm, dbl; void (*lifted)(Ctx *), (*hot)(Ctx *), (*exact)(Ctx *); }"
" CASES[] = {%s};" % ", ".join('{"%s", %d, %d, %d, g_lifted%d, g_hot%d, g_exact%d}' % (c[0], c[2], c[3], c[4], i, i, i)
for i, c in enumerate(all_cases)))
return "\n".join(out) + "\n", len(all_cases)
def generate(cxx, tmp, flags):
"""tmp/now and tmp/old: the kit's runtime header and C as written now and with A64_NO_FMA_STEPS=1; tmp: a64_fpx.h and
fma_steps_lifted.h. Returns (the shapes, the runtime C file)"""
dll = os.path.join(tmp, "a64emit.dll" if os.name == "nt" else "liba64emit.so")
fs.build([cxx, "-std=c++20", "-O0", "-shared", "-Wno-everything", "-o", dll, os.path.join(fs.KIT, "lift", "a64emit.cpp")]
+ flags + ([] if os.name == "nt" else ["-fPIC"]), "a64emit")
import kit # noqa: F401 (a64-kit on the path)
import build_emit
build_emit.build = lambda force=False: dll # (not a64emit.dll beside the source)
import a64_lift
import a64_rtgen
import a64emit
import fma_steps
now_dir, old_dir = os.path.join(tmp, "now"), os.path.join(tmp, "old")
used = set()
try:
kit_h = a64_rtgen.runtime_h()
fma_steps.install() # (the wrapper under test, around the kit's runtime_h)
now_h = a64_rtgen.runtime_h()
a64_rtgen.write(now_dir)
os.environ["A64_NO_FMA_STEPS"] = "1"
try:
old_h = a64_rtgen.runtime_h()
a64_rtgen.write(old_dir)
finally:
del os.environ["A64_NO_FMA_STEPS"]
text, n = lifted(a64emit, a64_lift, used)
finally:
if a64emit._lib is not None: # (unloaded, so the folder can go)
import _ctypes
(_ctypes.FreeLibrary if os.name == "nt" else _ctypes.dlclose)(a64emit._lib._handle)
a64emit._lib = None
if old_h != kit_h:
raise SystemExit("fma steps: with A64_NO_FMA_STEPS=1 the runtime header is not the kit's text")
want = kit_h
for old, new in ((fma_steps.RECPS32_OLD, fma_steps.RECPS32_NEW), (fma_steps.RSQRTS32_OLD, fma_steps.RSQRTS32_NEW)):
if want.count(old) != 1:
raise SystemExit("fma steps: the kit's header no longer has %r once" % old.split("\n")[0])
want = want.replace(old, new)
if now_h != want or now_h == kit_h:
raise SystemExit("fma steps: the runtime header is not the kit's with the two steps' guards changed and nothing else")
for name in ("fma32", "recps32", "rsqrts32"): # (all three under one guard, so -DA64_FPX_NO_FMA32 returns all three)
if not re.search(r"static RECOMP_INLINE int recomp_fpx_%s\([^\n]*\{\n%s" % (name, re.escape(fma_steps.GUARD_NEW)), now_h):
raise SystemExit("fma steps: recomp_fpx_%s is not under the FMA3 guard" % name)
if now_h.count(fma_steps.GUARD_NEW) != 3 or kit_h.count(fma_steps.GUARD_NEW) != 1:
raise SystemExit("fma steps: the FMA3 guard is not on three functions now and one in the kit's text")
files = {}
for side, text_h in ((now_dir, now_h), (old_dir, old_h)):
names = sorted(os.listdir(side))
heads, srcs = [f for f in names if f.endswith(".h")], [f for f in names if f.endswith(".c")]
if len(heads) != 1 or len(srcs) != 1:
raise SystemExit("fma steps: a64_rtgen wrote %s" % names)
with open(os.path.join(side, heads[0]), encoding="utf-8") as f:
if f.read() != text_h:
raise SystemExit("fma steps: a64_rtgen.write did not write the header under test into %s" % side)
with open(os.path.join(side, srcs[0]), encoding="utf-8") as f:
files[side] = (srcs[0], f.read())
if files[now_dir] != files[old_dir]:
raise SystemExit("fma steps: the runtime C differs between the two sides (only the header should)")
with open(os.path.join(tmp, "fma_steps_lifted.h"), "w", newline="\n") as f:
f.write(text)
with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f:
f.write(a64_lift.fpx_header(used))
return n, os.path.join(now_dir, files[now_dir][0])
def check_sources():
with open(os.path.join(ROOT, "nx2", "lift.py"), encoding="utf-8") as f:
lift = f.read()
at = [lift.find(p) for p in ("import fma_steps", "import a64_rtgen", "fma_steps.install()", "def main():")]
if min(at) < 0 or not at[1] < at[2] < at[3]:
raise SystemExit("fma steps: nx2/lift.py does not install nx2/fma_steps.py at module level")
with open(os.path.join(ROOT, "nx2", "make_game.py"), encoding="utf-8") as f:
make_game = f.read()
if 'if k.startswith(("A64_", "OOT_", "RX_"))' not in make_game:
raise SystemExit("fma steps: nx2/make_game.py's lift_key no longer takes the A64_ variables: A64_NO_FMA_STEPS is not in the key")
not_keyed = re.search(r"^LIFT_KEY_NOT = \((.*?)\) #", make_game, re.S | re.M)
if not not_keyed or '"fma_steps.py"' in not_keyed.group(1):
raise SystemExit("fma steps: nx2/fma_steps.py must stay a file of the lift key")
with open(os.path.join(ROOT, "docs", "development.md"), encoding="utf-8") as f:
if "| `A64_NO_FMA_STEPS=1` |" not in f.read():
raise SystemExit("fma steps: docs/development.md: A64_NO_FMA_STEPS is not in the switches table")
def fields(line):
return {k: int(v) for k, v in (kv.split("=") for kv in line.split()[1:])}
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--cc")
p.add_argument("--cxx")
p.add_argument("--random", type=int, default=20000)
p.add_argument("--direct", type=int, default=4000000)
a = p.parse_args()
check_sources()
if platform.machine().lower() not in ("x86_64", "amd64"):
fs.skip("not an x86-64 host: %s" % platform.machine())
cc = fs.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang")
cxx = fs.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++")
if not cc or not cxx:
fs.skip("no C compiler" if not cc else "no C++ compiler")
t0 = time.time()
failed, states, pairs, shapes = [], 0, 0, 0
with tempfile.TemporaryDirectory(prefix="triforce-fma-steps-") as tmp:
shapes, rt_c = generate(cxx, tmp, fs.target_flags(cxx, tmp))
for at, (march, more, fused) in enumerate(BUILDS):
what = "%s%s" % (march, " " + " ".join(more) if more else "")
common = [cc] + fs.target_flags(cc, tmp) + ["-march=" + march] + fs.FLAGS + more + \
["-D_CRT_SECURE_NO_WARNINGS", "-Wno-everything", "-I" + os.path.join(fs.KIT, "rt"), "-I" + tmp]
objs = []
for side in ("now", "old"): # (the same lifted text against each side's runtime header)
obj = os.path.join(tmp, "side_%s_%d.o" % (side, at))
fs.build(common + ["-DSIDE=" + side, "-I" + os.path.join(tmp, side), "-c",
os.path.join(ROOT, "tests", "fma_steps_side.c"), "-o", obj], "the %s side at %s" % (side, what))
objs.append(obj)
exe = os.path.join(tmp, "fma_steps_test_%d.exe" % at)
fs.build(common + ["-I" + os.path.join(tmp, "now"), os.path.join(ROOT, "tests", "fma_steps_test.c"), rt_c] + objs +
["-o", exe], "the driver at " + what)
for mode, n in (("lifted", a.random), ("direct", a.direct)):
r = fs.run([exe, mode, str(n), str(SEED)], timeout=3600)
print("%-28s %s" % (what, r.stdout.rstrip()))
if r.returncode == 3:
print("fma steps: %s skipped (this CPU cannot run it)" % what)
break
line = [l for l in r.stdout.splitlines() if l.startswith(mode + " ")]
if r.returncode or len(line) != 1:
failed.append("%s %s (exit %d)" % (what, mode, r.returncode))
continue
c = fields(line[0])
if fused is not None and bool(c["fused"]) != fused:
failed.append("%s: the steps are %sfused there" % (what, "" if c["fused"] else "not "))
if mode == "lifted":
states += c["checks"]
continue
pairs += c["checks"]
if not c["named"] or not c["kept_old"]:
failed.append("%s direct: %s" % (what, c))
elif c["fused"] and not c["now_only"]:
failed.append("%s direct: the fused form kept no pair the binary64 form declines" % what)
elif not c["fused"] and (c["now_only"] or c["kept_now"] != c["kept_old"]):
failed.append("%s direct: the sides decide differently without FMA3: %s" % (what, c))
if failed:
raise SystemExit("FAIL: fma steps: %s" % "; ".join(failed))
print("PASS: %d guest words, %d random states (each five ways) and %d operand pairs: the fused steps leave what the "
"kit's and the exact bodies leave (%.0f s)" % (shapes, states, pairs, time.time() - t0))
if __name__ == "__main__":
main()

View File

@@ -162,7 +162,9 @@ def main():
loop = vsync[vsync.index("for (;;)"):]
check("1000000000LL / fps_max()" in loop, "the vsync event's period is taken once, before Monitor's rate is known")
presented = block(vi, "void hos_vi_native_presented(void) {")
check("const int interval = fps_max() < 60 ? 1 : present_interval;" in presented and "fps_max() * interval" in presented,
# ((Triforce, 11-22, PRESENT-30) the period is pace_fps()'s, which is the cap unless a movie plays at a cap of 60)
check("const int interval = fps_max() < 60 ? 1 : present_interval;" in presented and "pace_fps() * interval" in presented
and "return movie_pace_fps(movie_raise, fps_max());" in block(vi, "static int pace_fps(void) {"),
"a cap under 60 that settles late is paced with the game's own interval on top")
readme = (ROOT / "readme.md").read_text(encoding="utf-8")
check("| Frame rate cap | 30, 40, 45, 60, 90, 120, 144, 165, 240, Monitor's rate" in readme,

View File

@@ -89,6 +89,10 @@ int page = 0; // ((Triforce) the menu's page: not t
constexpr int kModsRowPage = 9;
void draw_mods_row_shut(ui::Ctx&) { calls += "M"; }
void draw_notice(ui::Ctx&) { calls += "n"; }
int ach_notice = -1; // ((Triforce) no achievement earned here: its notice is tests/test_achievements.py's)
int64_t ach_notice_at = 0;
constexpr int64_t kAchNoticeMs = 4000;
void draw_ach_notice(ui::Ctx&) { calls += "a"; }
''' + fn + r'''
int main() {
for (bool startup : {false, true}) for (int fps : {0, 1, 2}) for (bool menu : {false, true}) for (bool can : {true, false}) {

View File

@@ -52,8 +52,10 @@ FIXTURE = r'''#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include "step_steady.h"
#include "movie_pace.h" /* ((Triforce, 11-22, PRESENT-30) realtime_step goes by pace_fps: the cap here, no movie plays) */
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
static int64_t pace_period_ns; /* (the period the presents are paced by: hos_vi_native_presented) */
static int movie_raise; /* (nn_vi.c's, set once a present by movie_pace_decide: tests/test_movie_pace.py) */
static uint8_t guest[0x4000];
#define GP(a) ((void *)(guest + (a)))
#define MOD 0x1000u
@@ -181,8 +183,8 @@ def main():
"a second run left one divide's hook from the first")
vi = (ROOT / "host" / "nn_vi.c").read_text(encoding="utf-8")
host = "\n".join(block(vi, m) for m in ("static float text_scale(", "static int fps_cap_of(", "static int fps_max(void) {", "static int realtime_rate(",
"static void realtime_step("))
host = "\n".join(block(vi, m) for m in ("static float text_scale(", "static int fps_cap_of(", "static int fps_max(void) {", "static int pace_fps(void) {",
"static int realtime_rate(", "static void realtime_step("))
fixture = FIXTURE.replace("@HOST@", host).replace("@HOOK@", "\n".join(oot_patches.STEP_HOOK.split("\n")[:2]))
with tempfile.TemporaryDirectory(prefix="cap-step-") as tmp:
c = Path(tmp) / "fixture.c"

View File

@@ -23,15 +23,15 @@ int main() {
// as many values as columns, in their order; outside: the interval less the replay and the present
ft::Split s;
s.interval = 16.0, s.replay = 9.5, s.present = 0.5, s.dispatch = 0.75, s.qsubmit = 0.25, s.par_wait = 1.5, s.game_wait = 2.0;
s.captures = 1.25, s.wb_wait = 3.0, s.wb_held = 2.5, s.qs_bg = 0.1;
s.captures = 1.25, s.wb_wait = 3.0, s.wb_held = 2.5, s.qs_bg = 0.1, s.gpu_waits = 1.75;
CHECK("outside", ft::outside(s) == 6.0);
char l[512];
const int n = ft::line(s, l, sizeof l);
CHECK("a line", n > 0 && (size_t)n < sizeof l);
CHECK("as many values as columns", commas(l) == commas(ft::kColumns));
CHECK("the values in the columns' order", !std::strcmp(l, "9.50,0.50,0.75,0.25,1.50,6.00,2.00,1.25,3.00,2.50,0.10"));
CHECK("the values in the columns' order", !std::strcmp(l, "9.50,0.50,0.75,0.25,1.50,6.00,2.00,1.25,3.00,2.50,0.10,1.75"));
CHECK("the columns", std::string(ft::kColumns) == "replay_ms,present_ms,dispatch_ms,qsubmit_ms,par_wait_ms,outside_ms,"
"game_wait_ms,captures_ms,wb_wait_ms,wb_held_ms,qs_bg_ms");
"game_wait_ms,captures_ms,wb_wait_ms,wb_held_ms,qs_bg_ms,gpu_waits_ms"); // ((Triforce, 11-22, SYNC-27) the last one)
// (a replay that began before the present before, a submit across it: outside below 0, written as it is)
ft::Split o;
o.interval = 10.0, o.replay = 11.0, o.present = 0.5;

147
tests/test_framegen_idle.py Normal file
View File

@@ -0,0 +1,147 @@
"""Frame generation's images are let go 120 presents after it stops (gpu/vk/framegen_idle.h, vk_framegen.cpp
framegen_idle, vk_replay.cpp's present; no game, no GPU).
(Triforce, 11-21, the performance audit's PRESENT-18, after Source A's.) The built-in interpolation's frames and
made-frame sets were kept until a resize: turned off, or left for NVIDIA's own frame generation, they stayed in VRAM.
This compiles framegen_idle.h alone and checks the rule: 119 presents off let nothing go and the 120th does; a present
with it on, or one that recorded made frames while the row already reads off, starts the count again; nothing goes with
no images; the images of the picture without the HUD have a count of their own, which only AMD FSR 4's row holds at
zero; RX_VK_FG_KEEP keeps everything while the counts go on.
And reads the source: framegen_idle asks the rule with the row read again, what this present recorded and what there
is; before anything is dropped the display thread is done with its sets and every queue is idle; the present calls it
right after framegen_queue, with what it recorded; the switch is in the switches table.
python -I -B tests/test_framegen_idle.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include "framegen_idle.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace framegen_idle;
int main() {
CHECK(kPresents == 120);
{ // off for 119 presents: nothing; the 120th lets them go
Counts c;
Release r{};
for (int i = 0; i < 119; i++) { r = step(c, {false, false, false, true, false}, false); CHECK(!r.shared && !r.hudless); }
r = step(c, {false, false, false, true, false}, false);
CHECK(r.shared && !r.hudless && c.off == 120);
}
{ // 119 off, then a present that recorded made frames though the row reads off by then: the count starts again
Counts c;
for (int i = 0; i < 119; i++) step(c, {false, false, false, true, false}, false);
Release r = step(c, {false, true, false, true, false}, false);
CHECK(!r.shared && c.off == 0);
for (int i = 0; i < 119; i++) { r = step(c, {false, false, false, true, false}, false); CHECK(!r.shared); }
CHECK(step(c, {false, false, false, true, false}, false).shared);
}
{ // on: never, however long
Counts c;
for (int i = 0; i < 1000; i++) CHECK(!step(c, {true, i % 2 == 0, false, true, false}, false).shared);
CHECK(c.off == 0);
}
{ // no images: nothing to let go, and the count goes on for when there are
Counts c;
for (int i = 0; i < 500; i++) CHECK(!step(c, {false, false, false, false, false}, false).shared);
CHECK(step(c, {false, false, false, true, false}, false).shared);
}
{ // the picture without the HUD: held by AMD FSR 4's row alone; another kind of frame generation lets it go
Counts c;
for (int i = 0; i < 300; i++) CHECK(!step(c, {true, true, true, true, true}, false).hudless);
Release r{};
for (int i = 0; i < 119; i++) { r = step(c, {true, true, false, true, true}, false); CHECK(!r.hudless && !r.shared); }
r = step(c, {true, true, false, true, true}, false);
CHECK(r.hudless && !r.shared);
CHECK(!step(c, {true, true, false, true, false}, false).hudless); // (gone: nothing more to let go)
Counts d; // (wanted by the row, but frame generation off)
for (int i = 0; i < 119; i++) step(d, {false, false, true, true, true}, false);
r = step(d, {false, false, true, true, true}, false);
CHECK(r.hudless && r.shared);
}
{ // RX_VK_FG_KEEP: kept, the counts going on
Counts c;
for (int i = 0; i < 400; i++) {
const Release r = step(c, {false, false, false, true, true}, true);
CHECK(!r.shared && !r.hudless);
}
CHECK(c.off == 400 && step(c, {false, false, false, true, true}, false).shared);
}
CHECK(next_off(UINT32_MAX, false, false) == UINT32_MAX); // (a long run off does not wrap round to on)
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
fg = (VK / "vk_framegen.cpp").read_text(encoding="utf-8")
replay = (VK / "vk_replay.cpp").read_text(encoding="utf-8")
check('#include "framegen_idle.h"' in fg, "vk_framegen.cpp does not use the rule")
idle = block(fg, "void framegen_idle(bool recorded) {")
check("const bool on = framegen_on();" in idle and
"framegen_idle::step(counts, {on, recorded, fsr4fg::frames_wanted(), fg.w != 0, have_hudless}, keep);" in idle,
"framegen_idle does not ask the rule with the row read again, what this present recorded and what there is")
check('getenv("RX_VK_FG_KEEP") && atoi(getenv("RX_VK_FG_KEEP")) != 0' in idle, "the switch is not RX_VK_FG_KEEP")
at = [idle.index(part) for part in (
"if (!go.shared && !go.hudless) return;",
"fg.cv.wait(lk, [] { return fg.jobs.empty() && !fg.sets[0].busy && !fg.sets[1].busy && !fg.sets[2].busy; });",
"device_wait_idle_locked();", "drop_size();", "drop_img(hl.srgb), drop_img(hl.out)")]
check(at == sorted(at), "an image is dropped before the display thread is done with its sets and every queue is idle")
check("if (go.shared) {" in idle and "if (go.hudless) {" in idle and idle.index("if (go.hudless) {") < idle.index("std::lock_guard lk(hl.mu);", idle.index("if (go.hudless) {")),
"the two kinds of images are not let go each by its own count, the HUD-less ones under their lock")
start = replay.index("if (interpolated) framegen_queue(value);")
after = replay[start:start + 900]
# (after the queue and the else that belongs to it, which counts the frame for the FPS line)
check("framegen_idle(interpolated);" in after and replay.count("framegen_idle(") == 1 and
after.index("else if (!held) menu_note_frame(") < after.index("framegen_idle(interpolated);"),
"the present does not count after framegen_queue and its else, with what it recorded")
check("void framegen_idle(bool recorded);" in (VK / "vk_framegen.h").read_text(encoding="utf-8"), "vk_framegen.h does not declare it")
# (unchanged, as the tests that compile or read them need: the remake after a resize and AMD's context's own release)
check("void drop_size() {" in fg and "bool ensure_size(VkCommandBuffer cmd, uint32_t w, uint32_t h) {" in fg and
"void framegen_present_tick() { fsr4fg::present_tick(); }" in fg, "ensure_size, drop_size or framegen_present_tick changed")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_FG_KEEP=1`" in dev, "docs/development.md: RX_VK_FG_KEEP=1 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="framegen-idle-") as tmp:
c, exe = Path(tmp) / "idle.cpp", Path(tmp) / "idle.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: frame generation's images are let go 120 presents after it stops, the GPU idle first")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,119 @@
"""With RX_VK_FRAMEGEN_REFRESH=1 a fixed frame generation multiplier makes no more frames than the display can show
(gpu/vk/framegen_refresh.h, vk_framegen.cpp framegen_record; no game, no GPU).
(Triforce, 11-21, the upscaler audit's UPS-05.) The Frame generation row's 2x to 8x made that many frames for each of the
game's whatever the display's refresh rate: the owner's log has 5x on a 165 Hz display with the game near 66 frames a
second, 331 frames a second presented, half of them never on the screen, each present call 2.2 to 3.0 ms.
This compiles framegen_refresh.h alone and checks the rule: the frames are the display's refreshes in a frame of the game,
rounded up, a tenth of a refresh forgiven; never more than the row's number, never fewer than the game's own frame; an
unknown frame time or refresh rate leaves the row's number; and the switch is off unless set.
And reads the source: framegen_record takes the row's multiplier through set_multiplier as before, and only then, with
the switch on and the row at 2x to 8x, through the rule, with the game's recent frame time and the display's period; the
row's Auto and AMD FSR's are not touched; and the switch is in the switches table.
python -I -B tests/test_framegen_refresh.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstdint>
#include <cstdio>
#include <initializer_list>
#include "framegen_refresh.h"
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
using namespace framegen_refresh;
static int64_t ns(double fps) { return (int64_t)(1e9 / fps); }
int main() {
CHECK(!on(nullptr) && !on("") && !on("0") && on("1"));
CHECK(period_ns(165000) == 6060606 && period_ns(60000) == 16666666 && period_ns(0) == 0 && period_ns(-1) == 0);
const int64_t hz165 = period_ns(165000), hz120 = period_ns(120000), hz60 = period_ns(60000), hz240 = period_ns(240000);
// (the owner's log: 5x, the game near 66 frames a second, 165 Hz: 2.5 refreshes a frame, 3 frames, not 5)
CHECK(frames(5, ns(66), hz165) == 3);
CHECK(frames(2, ns(66), hz165) == 2 && frames(3, ns(66), hz165) == 3 && frames(8, ns(66), hz165) == 3);
// (a game at a whole fraction of the refresh rate is not given one more for its frames' jitter)
CHECK(frames(4, ns(60), hz120) == 2 && frames(4, ns(59.5), hz120) == 2 && frames(4, ns(60.5), hz120) == 2);
CHECK(frames(4, ns(30), hz120) == 4 && frames(8, ns(30), hz120) == 4 && frames(8, ns(30), hz240) == 8);
CHECK(frames(4, ns(60), hz60) == 1 && frames(2, ns(60), hz60) == 1); // (at the display's rate: the game's frames alone)
CHECK(frames(4, ns(200), hz60) == 1 && frames(4, ns(165), hz165) == 1); // (faster than the display: never under 1)
CHECK(frames(2, ns(40), hz60) == 2 && frames(4, ns(40), hz60) == 2 && frames(4, ns(45), hz60) == 2);
CHECK(frames(8, ns(20), hz165) == 8 && frames(8, ns(24), hz165) == 7);
// (nothing known, or nothing to cap: the row's number)
CHECK(frames(5, 0, hz165) == 5 && frames(5, ns(66), 0) == 5 && frames(5, -1, hz165) == 5);
CHECK(frames(1, ns(66), hz165) == 1 && frames(0, ns(66), hz165) == 0);
// (never more than the row asks, never fewer than one, over every row and a sweep of rates)
for (int m = 1; m <= 8; m++)
for (int fps = 10; fps <= 400; fps += 7)
for (int64_t p : {hz60, hz120, hz165, hz240}) {
const int f = frames(m, ns(fps), p);
CHECK(f >= 1 && f <= m);
// (what is shown never falls short of the display where the row's number would have reached it)
if (m > 1 && (double)m * fps >= 1e9 / (double)p) CHECK((double)f * fps >= 0.9 * 1e9 / (double)p || f == m);
}
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
fg = (VK / "vk_framegen.cpp").read_text(encoding="utf-8")
check('#include "framegen_refresh.h"' in fg, "vk_framegen.cpp does not use the rule")
rec = block(fg, "bool framegen_record(VkCommandBuffer cmd, VkImageView src, uint32_t sw, uint32_t sh, const int crop[4]) {")
at = [rec.index(part) for part in (
"fg.cv.wait(lk, [] { return !fg.sets[fg.next_set].busy && (!pace::display_yields() || fg.jobs.empty()); });",
"int m = framegen_rows::set_multiplier(multiplier(), fg.mult);",
'static const bool refresh_cap = framegen_refresh::on(getenv("RX_VK_FRAMEGEN_REFRESH"));',
"if (refresh_cap && opts.framegen >= 2 && opts.framegen <= kMaxMult) {",
"m = framegen_refresh::frames(m, fg.game_ns, framegen_refresh::period_ns(display_refresh_mhz()));",
"const bool blend = fg.have_prev && !gap;",
"if (opts.framegen == 1) {",
"if (set->made == 0 && !none && blend && m > 1) {",
"for (int k = 1; k < m; k++) make_frame(cmd, prev, cur, set->frame[k - 1], (float)k / (float)m, uw, uh);")]
check(at == sorted(at), "framegen_record does not take the row's multiplier through set_multiplier and then, with the switch, the rule")
check(rec.count("framegen_refresh::frames(") == 1 and "framegen_refresh::" not in fg.replace(rec, "").replace('#include "framegen_refresh.h"', ""),
"the rule is asked somewhere else than framegen_record's fixed multipliers")
auto = rec[rec.index("if (opts.framegen == 1) {"):rec.index("bool none = false;")]
check("framegen_refresh" not in auto, "the row's Auto goes through the rule: it has its own, by the display's refreshes")
dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8")
check("`RX_VK_FRAMEGEN_REFRESH=1`" in dev, "docs/development.md: RX_VK_FRAMEGEN_REFRESH=1 is not in the switches table")
with tempfile.TemporaryDirectory(prefix="framegen-refresh-") as tmp:
c, exe = Path(tmp) / "refresh.cpp", Path(tmp) / "refresh.exe"
c.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(c), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the rule as made:\n" + r.stdout)
print("PASS: with RX_VK_FRAMEGEN_REFRESH=1 a fixed multiplier makes no more frames than the display can show")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,116 @@
"""Frame interpolation never writes past the frames its sets were made with, whatever the Frame generation row does while
a present waits (gpu/vk/framegen_rows.h set_multiplier and game_slot, vk_framegen.cpp framegen_record and ensure_size; no
game).
(Triforce, 11-20) The owner's "upscaling crash": three runs of 11-20 ended in an access violation reading 0x590 inside
NVIDIA's driver, in a vkCmdCopyImage run by the recorder thread while a present waited for it. Each had frame
interpolation on, and the Frame generation row had been stepped up in the options (Auto, then 3x at the next start, then
5x). The sets' frames are made for the row's multiplier when a present begins; the present then waits for the display
thread to let a set go, and while frames are made that thread runs the menu, so a step of the row lands in the wait. The
multiplier read again after it was more than the frames made: the made frames went into the frames there were and the
game's own into an image that was never made.
This compiles framegen_rows.h alone and checks the two rules: the multiplier a present records with is the row's and no
more than the frames made; the game's frame goes after the made ones and into the set's last frame at most. Then, for
every number of frames a set can be made with and every multiplier the row can go to in the wait, that every frame a
present writes is one the set has.
And reads the source for what only the game shows: the sets made with as many frames as the multiplier read there, and
that number kept; framegen_record taking its multiplier through set_multiplier with the frames made, after the wait, and
no bare multiplier(); the game's frame's place through game_slot before the copy; Auto's frames counted from the frames
made.
python -I -B tests/test_framegen_set_bounds.py [--cxx clang++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu" / "vk"
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
def check(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
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]
PROGRAM = r"""
#include <cstddef>
#include <cstdio>
#include "framegen_rows.h"
using namespace mkwvk::framegen_rows;
static int fails = 0;
#define CHECK(x) do { if (!(x)) { std::printf("FAIL:%d %s\n", __LINE__, #x); fails++; } } while (0)
int main() {
// the multiplier a present records with
CHECK(set_multiplier(3, 3) == 3 && set_multiplier(2, 3) == 2 && set_multiplier(8, 8) == 8);
CHECK(set_multiplier(4, 3) == 3 && set_multiplier(5, 3) == 3 && set_multiplier(8, 4) == 4); // (stepped up in the wait)
CHECK(set_multiplier(0, 3) == 1 && set_multiplier(-2, 3) == 1); // (Off in the wait: the game's frame)
CHECK(set_multiplier(5, 0) == 1 && set_multiplier(5, 1) == 1);
// the game's frame's place
CHECK(game_slot(2, 3) == 2 && game_slot(0, 3) == 0 && game_slot(7, 8) == 7);
CHECK(game_slot(3, 3) == 2); // (the crash: three made frames over sets of three, the game's into a fourth)
CHECK(game_slot(1, 1) == 0); // (AMD FSR 4 in the wait, over sets made for the game's frames alone)
CHECK(game_slot(0, 0) == 0 && game_slot(-1, 3) == 0);
// a present, as framegen_record writes it: the made frames into 0 to m - 2, the game's after them
for (int made_with = 1; made_with <= kMaxMult; made_with++)
for (int row = 0; row <= kMaxMult; row++) {
const int m = set_multiplier(row, made_with);
for (int k = 1; k < m; k++) CHECK(k - 1 < made_with);
const int game = game_slot(m - 1, made_with);
CHECK(game >= 0 && game < made_with);
CHECK(row > made_with || row < 1 || (m == row && game == row - 1)); // (a row the sets were made for: as it asks)
}
CHECK(game_slot(1, 2) == 1 && game_slot(1, 8) == 1); // (AMD's one made frame, sets of two or more: the game's second)
return fails ? 1 : 0;
}
"""
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
fg = (VK / "vk_framegen.cpp").read_text(encoding="utf-8")
size = block(fg, "bool ensure_size(VkCommandBuffer cmd, uint32_t w, uint32_t h) {")
check("const int mult = multiplier();" in size and "for (int k = 0; k < mult; k++)" in size and "fg.mult = mult;" in size,
"ensure_size: the sets' frames are not made for one reading of the multiplier, kept as fg.mult")
record = block(fg, "bool framegen_record(VkCommandBuffer cmd, VkImageView src, uint32_t sw, uint32_t sh, const int crop[4]) {")
taken = "m = framegen_rows::set_multiplier(multiplier(), fg.mult);"
check(taken in record, "framegen_record: the multiplier is not held to the frames the sets were made with")
check(record.count("multiplier()") == 1, "framegen_record reads the row's multiplier again, past the frames made")
wait = record.index("fg.cv.wait(lk,")
check(record.index("ensure_size(cmd,") < wait < record.index(taken),
"framegen_record: the multiplier is taken before the wait for a set, where the menu may still step the row")
slot, copy = "set->made = framegen_rows::game_slot(set->made, fg.mult);", "set->frame[set->made].image"
check(slot in record and record.index(slot) < record.index(copy), "framegen_record: the game's frame may go past the set's last frame")
check(record.count(copy) == 1, "framegen_record copies the game's frame into a set in a second place")
check("n = auto_slots(before, now, period, fg.slot_next, t, at, std::min(fg.mult, kMaxMult));" in record,
"Auto's frames are not counted from the frames the sets were made with")
with tempfile.TemporaryDirectory(prefix="framegen-set-bounds-") as tmp:
src, exe = Path(tmp) / "bounds.cpp", Path(tmp) / "bounds.exe"
src.write_text(PROGRAM, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(src), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
check(r.returncode == 0, "the frames a present writes:\n" + r.stdout)
print("PASS: a present writes only frames its sets were made with, whatever the Frame generation row does in its wait")
if __name__ == "__main__":
main()

View File

@@ -25,7 +25,7 @@ def main():
build = between(draw, "VkPrimitiveTopology topo, int part, bool* cold) {", "\n}\n")
for line, why in (
("const bool lib_cached = (part == 1 || part == 2) && gpl_libs_cached();", "only the two shader libraries are kept"),
("const VkPipelineCache wanted = lib_cached || (part < 0 && !driver_cache_only) ? pcache : VK_NULL_HANDLE;",
("const VkPipelineCache wanted = lib_cached || (part < 0 && !driver_cache_only && !pcache_libs_only) ? pcache : VK_NULL_HANDLE;", # ((Triforce, 11-22, PIPE-78) whole pipelines out when lean)
"the libraries are not built into the pipeline cache"),
# ((Triforce) through the cache's gate, held over the creation: none once it is let go for the RAM, pcache_gate.h)
("const PcacheUse use(pcache_door, wanted);", "the creation does not hold the cache's gate"),

View File

@@ -26,5 +26,25 @@ int main() {
const std::vector<std::string> twins = {"NVIDIA GeForce RTX 3090", "NVIDIA GeForce RTX 3090"};
CHECK("two cards of one name: the numbered one when it has that name", pick("NVIDIA GeForce RTX 3090", twins, 2) == 1);
CHECK("a number past the list with a name: the first of that name", pick("NVIDIA GeForce RTX 3090", twins, 5) == 0);
// ((Triforce) 11-22) a number alone on the processor graphics while a card of its own is there: Auto (RX 6600 + 5600G: the
// launcher's "1: AMD Radeon RX 6600" in Windows' order saved 2, the game's 2 is the 5600G's graphics)
using mkwvk::gpu_choice::unnamed_integrated;
const std::vector<std::string> apu = {"AMD Radeon RX 6600", "AMD Radeon(TM) Graphics"};
const std::vector<int> apu_types = {2, 1};
CHECK("no name, the number on the processor graphics: Auto", unnamed_integrated("", apu, apu_types, 2));
CHECK("a name no card has, the number on the processor graphics: Auto",
unnamed_integrated("AMD Radeon RX 6600 XT", apu, apu_types, 2));
CHECK("the processor graphics chosen by its name: taken", !unnamed_integrated("AMD Radeon(TM) Graphics", apu, apu_types, 2));
CHECK("the name finds the card under another number: pick's", !unnamed_integrated("AMD Radeon RX 6600", apu, apu_types, 2) &&
pick("AMD Radeon RX 6600", apu, 2) == 0);
CHECK("no name, the number on the card: taken", !unnamed_integrated("", apu, apu_types, 1));
CHECK("Auto: Auto as before", !unnamed_integrated("", apu, apu_types, 0));
CHECK("processor graphics alone (no card of its own): the number", !unnamed_integrated("", {"Intel(R) Iris(R) Xe Graphics"}, {1}, 1));
const std::vector<std::string> twice = {"AMD Radeon RX 7900 XTX", "AMD Radeon RX 7900 XTX", "AMD Radeon(TM) Graphics"};
CHECK("the card listed twice, the number on the processor graphics: Auto", unnamed_integrated("", twice, {2, 2, 1}, 3));
CHECK("two processor graphics and no card (a software device, type 4): the number",
!unnamed_integrated("", {"AMD Radeon(TM) Graphics", "llvmpipe (LLVM 22.1.8, 256 bits)"}, {1, 4}, 1));
CHECK("types shorter than names: the number", !unnamed_integrated("", apu, {2}, 2));
std::puts("gpu choice: all checks passed");
}

View File

@@ -38,6 +38,18 @@ def main():
raise SystemExit("pick_device reads the GPU row before menu_init")
if pick.index('getenv("RX_VK_DEVICE")') > pick.index("gpu_choice::pick("):
raise SystemExit("pick_device takes the GPU row before RX_VK_DEVICE")
# ((Triforce) 11-22) a number saved without a name on the processor graphics: Auto, after RX_VK_DEVICE and before pick, the
# types beside the names, and the row set to the card Auto took
unnamed = "gpu_choice::unnamed_integrated(menu_gpu_name(), gpu_device_names, valid_types, opts.gpu_device)"
if unnamed not in pick:
raise SystemExit("pick_device: a number without a name on the processor graphics is taken (no unnamed_integrated)")
if not pick.index('getenv("RX_VK_DEVICE")') < pick.index(unnamed) < pick.index("gpu_choice::pick("):
raise SystemExit("pick_device: unnamed_integrated is not between RX_VK_DEVICE and the row's pick")
if "valid_types.push_back((int)p.deviceType)" not in pick or "gpu_device_names.push_back" not in pick:
raise SystemExit("pick_device: the device types are not listed beside the names")
fix = pick.index("if (unnamed_integrated) {")
if not pick.index("best = (int)i") < fix < pick.index("dev.physical = all[best]") or "opts.gpu_device = (int)k + 1" not in pick[fix:]:
raise SystemExit("pick_device: the row does not follow the card Auto took for an unnamed number")
menu = (vk / "vk_menu.cpp").read_text(encoding="utf-8")
if not preset_exempt(menu, "gpu_device"):
raise SystemExit("vk_menu.cpp: changing the GPU row resets the quality preset (and its VRAM budget) to Custom")

146
tests/test_gpu_clock.py Normal file
View File

@@ -0,0 +1,146 @@
"""The Steam Deck's GPU held at amdgpu's peak stable power state while the game runs (gpu/vk/gpu_clock.h, vk_gpu_clock.cpp,
vk_main.cpp create_device; RX_VK_GPU_CLOCK; Linux; no game/GPU).
The rule is compiled from the header and run on made-up devices: unset, the peak state on the Deck's APU (its LCD and OLED Vulkan
names, or SteamDeck=1 on an integrated GPU) only while the level is "auto"; a level set by hand, or one not read, left alone;
SteamDeck=1 on a discrete GPU, another AMD GPU and any other vendor left alone; =0 nothing; =peak, =1 and =standard on any AMD GPU
at any level, never on NVIDIA. The ioctl's numbers against what amdgpu_drm.h gives them (written out here, as it was before and
since Linux 5.18; on Linux the system's own header too, when it is there). The source: create_device calls it after vkCreateDevice,
on Linux alone, with VK_EXT_physical_device_drm's presence; vk_gpu_clock.cpp is Linux's alone, checks the node it opens is the
device Vulkan names, sets the state on a context of its own and keeps the file open; the docs row is there.
python -I -B tests/test_gpu_clock.py [--cxx clang++]
"""
import argparse
import os
from pathlib import Path
import subprocess
import sys
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = r'''#include <cstdio>
#include "gpu_clock.h"
#ifdef CHECK_SYSTEM_HEADER
#include <drm/amdgpu_drm.h>
static_assert(mkwvk::gpu_clock::kCtxIoctl == DRM_IOCTL_AMDGPU_CTX, "DRM_IOCTL_AMDGPU_CTX");
static_assert(mkwvk::gpu_clock::kCtxArgBytes == sizeof(union drm_amdgpu_ctx), "union drm_amdgpu_ctx");
static_assert(mkwvk::gpu_clock::kCtxOpAlloc == AMDGPU_CTX_OP_ALLOC_CTX, "AMDGPU_CTX_OP_ALLOC_CTX");
#ifdef AMDGPU_CTX_OP_SET_STABLE_PSTATE
static_assert(mkwvk::gpu_clock::kCtxOpSetStablePstate == AMDGPU_CTX_OP_SET_STABLE_PSTATE, "AMDGPU_CTX_OP_SET_STABLE_PSTATE");
static_assert(mkwvk::gpu_clock::kPstatePeak == AMDGPU_CTX_STABLE_PSTATE_PEAK, "AMDGPU_CTX_STABLE_PSTATE_PEAK");
static_assert(mkwvk::gpu_clock::kPstateStandard == AMDGPU_CTX_STABLE_PSTATE_STANDARD, "AMDGPU_CTX_STABLE_PSTATE_STANDARD");
#endif
#endif
using namespace mkwvk::gpu_clock;
// (amdgpu_drm.h's numbers, written out: DRM_COMMAND_BASE 0x40, DRM_AMDGPU_CTX 0x02, _IOC_READ | _IOC_WRITE 3, a 16-byte union;
// the ops and the states of Linux 5.18's)
static_assert(kCtxIoctl == 0xC0106442ul, "DRM_IOCTL_AMDGPU_CTX");
static_assert(kCtxOpAlloc == 1 && kCtxOpSetStablePstate == 6 && kPstateStandard == 1 && kPstatePeak == 4, "the ops and states");
static int bad = 0;
static void expect(Want got, Want want, const char* what) {
if (got != want) { std::printf("FAIL %s: %d, wanted %d\n", what, (int)got, (int)want); bad++; }
}
int main() {
const char* lcd = "AMD Custom GPU 0405 (RADV VANGOGH)";
const char* oled = "AMD Custom GPU 0932 (RADV VANGOGH)";
expect(choose(nullptr, 0x1002, true, lcd, nullptr, "auto").want, kPeak, "Deck LCD, auto");
expect(choose(nullptr, 0x1002, true, oled, nullptr, "auto").want, kPeak, "Deck OLED, auto");
expect(choose(nullptr, 0x1002, true, "AMD Radeon Graphics (RADV VANGOGH)", "1", "auto").want, kPeak, "SteamDeck=1, integrated");
expect(choose(nullptr, 0x1002, false, "AMD Radeon RX 6800 XT (RADV NAVI21)", "1", "auto").want, kNone, "SteamDeck=1, discrete");
expect(choose(nullptr, 0x1002, true, "AMD Radeon 780M (RADV PHOENIX)", nullptr, "auto").want, kNone, "another AMD APU");
expect(choose(nullptr, 0x1002, true, "AMD Radeon 780M (RADV PHOENIX)", "0", "auto").want, kNone, "SteamDeck=0");
expect(choose(nullptr, 0x1002, true, oled, nullptr, "manual").want, kNone, "Deck, level manual");
expect(choose(nullptr, 0x1002, true, oled, nullptr, "profile_peak").want, kNone, "Deck, level profile_peak");
expect(choose(nullptr, 0x1002, true, oled, nullptr, nullptr).want, kNone, "Deck, level unread");
expect(choose("0", 0x1002, true, oled, nullptr, "auto").want, kNone, "=0 on the Deck");
expect(choose("peak", 0x1002, false, "AMD Radeon RX 7900 XTX (RADV NAVI31)", nullptr, "manual").want, kPeak, "=peak, any AMD");
expect(choose("1", 0x1002, false, "AMD Radeon RX 7900 XTX", nullptr, nullptr).want, kPeak, "=1, any AMD");
expect(choose("standard", 0x1002, true, oled, nullptr, "auto").want, kStandard, "=standard");
expect(choose("peak", 0x10DE, false, "NVIDIA GeForce RTX 4070", nullptr, "auto").want, kNone, "=peak on NVIDIA");
expect(choose(nullptr, 0x10DE, true, lcd, "1", "auto").want, kNone, "NVIDIA, a Deck's name");
expect(choose(nullptr, 0x8086, true, "Intel(R) Graphics", "1", "auto").want, kNone, "Intel");
if (bad) return 1;
std::printf("OK\n");
return 0;
}
'''
def fail(message):
raise SystemExit("FAIL: " + message)
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 check_source():
main = (ROOT / "gpu/vk/vk_main.cpp").read_text(encoding="utf-8")
clock = (ROOT / "gpu/vk/vk_gpu_clock.cpp").read_text(encoding="utf-8")
dev = (ROOT / "docs/development.md").read_text(encoding="utf-8")
create = block(main, "bool create_device() {")
call = " gpu_clock_device_created(has_extension(exts, VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME));\n"
at = create.index(call)
if create.index("vkCreateDevice(") > at:
fail("create_device calls gpu_clock_device_created before vkCreateDevice")
if "#ifdef __linux__" not in "\n".join(create[:at].splitlines()[-2:]) or not create[at + len(call):].lstrip().startswith("#endif"):
fail("the call is not Linux's alone")
body = clock[clock.index('#include "vk_gpu_clock.h"') + len('#include "vk_gpu_clock.h"'):].strip()
if not body.startswith("#ifdef __linux__") or not body.endswith("#endif"):
fail("vk_gpu_clock.cpp is not Linux's alone")
fn = block(clock, "void gpu_clock_device_created(bool drm_ext) {")
for needle in ("major(st.st_rdev) != (unsigned)drm.renderMajor", "minor(st.st_rdev) != (unsigned)drm.renderMinor",
"a.in.op = gpu_clock::kCtxOpAlloc;", "s.in.op = gpu_clock::kCtxOpSetStablePstate;", "s.in.ctx_id = a.alloc.ctx_id;",
"held_fd = fd;", "gpu_clock::choose(sw, dev.props.vendorID, integrated, dev.props.deviceName, getenv(\"SteamDeck\")"):
if needle not in fn:
fail("gpu_clock_device_created no longer has: " + needle)
tail = fn[fn.index("held_fd = fd;"):]
if "close(fd)" in tail:
fail("gpu_clock_device_created closes the file it holds the state with")
if fn.index("if (dev.props.vendorID != 0x1002) return;") > fn.index("logf("):
fail("gpu_clock_device_created logs on GPUs that are not AMD's")
if "`RX_VK_GPU_CLOCK=0`" not in dev:
fail("docs/development.md has no RX_VK_GPU_CLOCK row")
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
try:
check_source()
except ValueError as e:
fail("a text this test reads moved (%s)" % e)
flags = subprocess.BELOW_NORMAL_PRIORITY_CLASS if os.name == "nt" else 0
system_header = sys.platform.startswith("linux") and any(
Path(d, "drm/amdgpu_drm.h").is_file() for d in ("/usr/include", "/usr/include/libdrm"))
with tempfile.TemporaryDirectory(prefix="gpu-clock-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / ("fixture" + (".exe" if os.name == "nt" else ""))
cpp.write_text(FIXTURE, encoding="utf-8")
extra = ["-DCHECK_SYSTEM_HEADER", "-I/usr/include/libdrm"] if system_header else []
built = subprocess.run([a.cxx, "-std=c++20", "-O1", "-Wall", "-Werror", "-I" + str(ROOT / "gpu/vk")] + extra +
[str(cpp), "-o", str(exe)], capture_output=True, text=True, creationflags=flags)
if built.returncode:
print(built.stdout + built.stderr)
fail("the fixture did not build")
done = subprocess.run([str(exe)], capture_output=True, text=True, timeout=60, creationflags=flags)
if done.returncode or done.stdout.strip() != "OK":
fail((done.stdout + done.stderr).strip())
print("PASS: the GPU's stable power state: peak on the Steam Deck's APU at level auto, the switch's states on any AMD GPU, "
"nothing elsewhere; the ioctl's numbers%s; create_device calls it on Linux after vkCreateDevice and the file stays open"
% (" (and the system's amdgpu_drm.h)" if system_header else ""))
if __name__ == "__main__":
main()

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