mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-22 17:19:58 -04:00
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:
176
host/achievements.c
Normal file
176
host/achievements.c
Normal file
@@ -0,0 +1,176 @@
|
||||
/* (Triforce) Achievements, as a console's or a store's: things done in the game that are marked as earned, once, with a
|
||||
notice on the screen and a sound when one is, and a page of the options that lists them. host/achievements.h has what
|
||||
an achievement is, the list every game has, the form of a player's own list and of the file that keeps the earned
|
||||
ones; this file has what is the game folder's and the screen's.
|
||||
|
||||
- On by default (11-22, the owner's word); HOS_ACHIEVEMENTS=0 (triforce_env.txt) turns them off. While off nothing is
|
||||
read, of the game or of the disk.
|
||||
- The lists are read at the game's first present (hos_achievements_frame, host/nvn_native.c): the built-in one, then
|
||||
achievements.txt in the game folder when there is one. Once the renderer answers they are named to the menu
|
||||
(HOS_OPT_ACH_TEXT, _ADD, _COUNT: gpu/vk/achievement_rows.h), which lists them on the System tab's Achievements page.
|
||||
- host/hos_main.c asks once a second at Link's update, on the game's thread, while a save is loaded, and hands over
|
||||
how the save data is read (hos_achievements_check).
|
||||
- An achievement earned is written to triforce_achievements.txt in the game folder at once (the file whole, under
|
||||
another name first), said in the log, and shown: the menu's notice (HOS_OPT_ACH_EARNED) with the system menu's
|
||||
decide sound (HOS_MENU_SOUND_SYS_DECIDE, at the Menu sounds row's volume; silent where that row is Off. Not the
|
||||
item fanfare: most are earned as the game plays that itself). One at a time: a save that earns several at once
|
||||
shows them one after the other.
|
||||
- The present's thread and the game's both come here: what they share is under one lock, held for a list's naming
|
||||
or a check and no longer.
|
||||
- Nothing of the game's is written here, and nothing is changed in the game: the save data is only read. */
|
||||
#include "hos.h"
|
||||
#include "hos_paths.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include "achievements.h"
|
||||
#include "menu_sound.h"
|
||||
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
|
||||
#define EARNED_FILE "triforce_achievements.txt"
|
||||
#define OWN_FILE "achievements.txt"
|
||||
enum { OWN_MOST = 1 << 18, SHOWN_MS = 4500 /* (a notice stays 4 s: gpu/vk/vk_menu.cpp) */ };
|
||||
|
||||
static AchList list;
|
||||
static int ready; /* the lists were read */
|
||||
static int named; /* and named to the menu: it shows them, and a notice for one earned */
|
||||
static int waiting[ACH_MAX], nwaiting; /* earned, not yet shown */
|
||||
static int64_t next_shown;
|
||||
static volatile char busy; /* the lock: the present's thread names the list, the game's checks it */
|
||||
|
||||
static void lock(void) {
|
||||
while (__atomic_test_and_set(&busy, __ATOMIC_ACQUIRE)) {}
|
||||
}
|
||||
static void unlock(void) { __atomic_clear(&busy, __ATOMIC_RELEASE); }
|
||||
|
||||
static const char *last_sep(const char *s) {
|
||||
const char *b = strrchr(s, '\\'), *f = strrchr(s, '/');
|
||||
return b > f ? b : f;
|
||||
}
|
||||
/* a file of the game folder */
|
||||
static int game_file(char *out, size_t cap, const char *name) {
|
||||
char exe[MAX_PATH];
|
||||
const DWORD n = hos_game_path_a(exe, sizeof exe);
|
||||
const char *slash = n && n < sizeof exe ? last_sep(exe) : NULL;
|
||||
if (!slash) return 0;
|
||||
return snprintf(out, cap, "%.*s%s", (int)(slash + 1 - exe), exe, name) < (int)cap;
|
||||
}
|
||||
|
||||
/* a text file of the game folder whole, its end a 0 (freed by the caller); NULL when it is not there or over `most` */
|
||||
static char *read_text(const char *name, long most) {
|
||||
char path[MAX_PATH + 40];
|
||||
if (!game_file(path, sizeof path, name)) return NULL;
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
char *text = NULL;
|
||||
long size = 0;
|
||||
if (!fseek(f, 0, SEEK_END) && (size = ftell(f)) >= 0 && size <= most && !fseek(f, 0, SEEK_SET) && (text = (char *)malloc((size_t)size + 1))) {
|
||||
const size_t got = fread(text, 1, (size_t)size, f);
|
||||
text[got] = 0;
|
||||
for (size_t i = 0; i < got; i++)
|
||||
if (!text[i]) text[i] = ' '; /* (a 0 in the middle is not the text's end) */
|
||||
}
|
||||
fclose(f);
|
||||
return text;
|
||||
}
|
||||
|
||||
static void earned_save(void) {
|
||||
static char text[ACH_MAX * (ACH_NAME + 40) + ACH_ORPHANS * ACH_LINE + 200];
|
||||
char path[MAX_PATH + 40], fresh[MAX_PATH + 48];
|
||||
const size_t n = ach_earned_write(&list, text, sizeof text);
|
||||
if (!n || !game_file(path, sizeof path, EARNED_FILE)) return;
|
||||
snprintf(fresh, sizeof fresh, "%s.new", path);
|
||||
FILE *f = fopen(fresh, "wb"); /* (its lines end the same on Windows and Linux) */
|
||||
const int ok = f && fwrite(text, 1, n, f) == n;
|
||||
if ((f && fclose(f)) || !ok || !MoveFileExA(fresh, path, MOVEFILE_REPLACE_EXISTING)) {
|
||||
remove(fresh);
|
||||
hos_log("achievements: %s could not be written: what was earned is kept until the game ends", path);
|
||||
}
|
||||
}
|
||||
|
||||
static void lists_read(void) {
|
||||
int refused = 0;
|
||||
const int built_in = ach_parse(&list, ACH_BUILTIN, &refused);
|
||||
char *own = read_text(OWN_FILE, OWN_MOST);
|
||||
const int own_n = own ? ach_parse(&list, own, &refused) : 0;
|
||||
free(own);
|
||||
char *earned = read_text(EARNED_FILE, OWN_MOST);
|
||||
const int marked = earned ? ach_earned_read(&list, earned) : 0;
|
||||
free(earned);
|
||||
hos_log("achievements: %d (%d of " OWN_FILE "%s), %d earned so far%s", list.n, own_n, own ? "" : ", which is not there", marked,
|
||||
built_in + own_n ? "" : ": none to earn");
|
||||
if (refused)
|
||||
hos_log("achievements: %d left out: a name that is empty, over %d bytes or another's, no condition, over %d of them, or a "
|
||||
"line that is not about:, icon: <shape> [<tone>], item <name> [<least>] or flag <name>", refused, ACH_NAME, ACH_CONDS);
|
||||
}
|
||||
|
||||
/* ---- the menu's side: gpu/vk/achievement_rows.h ---- */
|
||||
static void to_menu(const char *text) {
|
||||
for (const char *p = text; *p; p++) rx_option_set(HOS_OPT_ACH_TEXT, (unsigned char)*p);
|
||||
}
|
||||
/* the day of a unix time, as the page shows it ("" when it is not known) */
|
||||
static void day_of(int64_t when, char *out, size_t cap) {
|
||||
const time_t t = (time_t)when;
|
||||
const struct tm *tm = when > 0 ? localtime(&t) : NULL;
|
||||
if (!tm || !strftime(out, cap, "%Y-%m-%d", tm)) out[0] = 0;
|
||||
}
|
||||
static void name_list(void) {
|
||||
for (int i = 0; i < list.n; i++) {
|
||||
const Ach *a = &list.a[i];
|
||||
char day[16];
|
||||
day_of(a->earned ? a->when : 0, day, sizeof day);
|
||||
to_menu(a->name), to_menu("\n"), to_menu(a->about), to_menu("\n"), to_menu(day);
|
||||
rx_option_set(HOS_OPT_ACH_ADD, (a->icon & 255) | (a->earned ? HOS_ACH_EARNED : 0));
|
||||
}
|
||||
rx_option_set(HOS_OPT_ACH_COUNT, list.n);
|
||||
}
|
||||
|
||||
/* On unless HOS_ACHIEVEMENTS=0 (11-22: on by default). Read once */
|
||||
int hos_achievements_on(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
const char *e = getenv("HOS_ACHIEVEMENTS");
|
||||
on = !(e && e[0] == '0' && !e[1]);
|
||||
}
|
||||
return on;
|
||||
}
|
||||
|
||||
/* host/nvn_native.c, at each present of the game's: the lists read at the first, and named to the menu once it answers */
|
||||
void hos_achievements_frame(void) {
|
||||
static int done; /* (the present's thread alone) */
|
||||
if (done || !hos_achievements_on()) return;
|
||||
lock();
|
||||
if (!ready) ready = 1, lists_read();
|
||||
if (rx_option(HOS_OPT_ACH_ASK, 0) > 0) named = done = 1, name_list();
|
||||
unlock();
|
||||
}
|
||||
|
||||
/* host/hos_main.c, once a second at Link's update while a save is loaded (now_ns: hos_now_ns): what the save data has
|
||||
earned since the last call is marked, kept and shown */
|
||||
void hos_achievements_check(AchProbe probe, void *ctx, int64_t now_ns) {
|
||||
if (!hos_achievements_on()) return;
|
||||
lock();
|
||||
if (!ready) ready = 1, lists_read();
|
||||
int fresh[ACH_MAX];
|
||||
const int got = ach_check(&list, probe, ctx, (int64_t)time(NULL), fresh, ACH_MAX);
|
||||
for (int i = 0; i < got; i++) {
|
||||
hos_log("achievements: earned \"%s\" (%d of %d)", list.a[fresh[i]].name, ach_earned_count(&list) - (got - 1 - i), list.n);
|
||||
if (nwaiting < ACH_MAX) waiting[nwaiting++] = fresh[i];
|
||||
}
|
||||
if (got) earned_save();
|
||||
if (nwaiting && named && now_ns >= next_shown) { /* (not before the menu has the list: it shows them by their place in it) */
|
||||
const int at = waiting[0];
|
||||
char day[16];
|
||||
memmove(waiting, waiting + 1, sizeof waiting[0] * (size_t)--nwaiting);
|
||||
next_shown = now_ns + SHOWN_MS * 1000000LL;
|
||||
day_of(list.a[at].when, day, sizeof day);
|
||||
to_menu(day);
|
||||
rx_option_set(HOS_OPT_ACH_EARNED, at);
|
||||
hos_menu_sound_play(HOS_MENU_SOUND_SYS_DECIDE, now_ns);
|
||||
}
|
||||
unlock();
|
||||
}
|
||||
300
host/achievements.h
Normal file
300
host/achievements.h
Normal file
@@ -0,0 +1,300 @@
|
||||
/* (Triforce) Achievements: what one is, the list's text, when one is earned, and the file that keeps the earned ones.
|
||||
host/achievements.c has what is the game folder's and the screen's; host/hos_main.c asks the game. Nothing of the host is
|
||||
in here: tests/test_achievements.py compiles it alone.
|
||||
|
||||
An achievement has a name, a line about it, and one to ACH_CONDS conditions, all of which must hold:
|
||||
item <pouch name> [<least>] Link has that item, <least> of it or more (1 without a number)
|
||||
flag <name> that flag of the game's save data is set
|
||||
The list is text:
|
||||
# a comment
|
||||
[A Sword of Your Own]
|
||||
about: Find a blade in the forest.
|
||||
icon: sword green
|
||||
item Weapon_Sword_Kokiri
|
||||
(icon: its picture on the Achievements page and in its notice, a shape and a tone the menu draws: ach_icon below.)
|
||||
ACH_BUILTIN below is the list every game has. Its conditions are items only, by the pouch names gpu/vk/oot_cheat_items.h
|
||||
already has for the Inventory page: no other name of the game's is in this file, and none of its flags' names (the
|
||||
Debug tab reads those from the player's own game files). A player's own list, achievements.txt beside the game, is read
|
||||
after it in the same form, and may ask for flags by the names the Game flags page shows.
|
||||
|
||||
An achievement is earned once, for the game folder and not for a save slot, and stays earned: triforce_achievements.txt
|
||||
has a line for each, "<key> <unix time> <name>", the key the FNV-1a 32 of the name. A line whose achievement the lists
|
||||
no longer have is kept as it is, so that a list taken away and put back loses nothing. */
|
||||
#ifndef HOS_ACHIEVEMENTS_H
|
||||
#define HOS_ACHIEVEMENTS_H
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { ACH_MAX = 128, ACH_CONDS = 16, ACH_NAME = 40 /* (a name's bytes: gpu/vk/achievement_rows.h kName holds it whole) */, ACH_ABOUT = 160,
|
||||
ACH_WHAT = 96 /* (a flag's or an item's name: gpu/vk/oot_debug_link.h OOT_NAME_MAX and its end) */, ACH_ORPHANS = 64,
|
||||
ACH_LINE = 256 };
|
||||
enum { ACH_ITEM = 'I', ACH_FLAG = 'F' };
|
||||
|
||||
typedef struct AchCond {
|
||||
int kind; /* ACH_ITEM or ACH_FLAG */
|
||||
int least; /* ACH_ITEM: how many of it */
|
||||
char what[ACH_WHAT];
|
||||
} AchCond;
|
||||
typedef struct Ach {
|
||||
char name[ACH_NAME + 1], about[ACH_ABOUT + 1];
|
||||
uint32_t key;
|
||||
int icon; /* its picture: a shape and a tone, ach_icon */
|
||||
AchCond conds[ACH_CONDS];
|
||||
int nconds;
|
||||
int earned;
|
||||
int64_t when; /* unix time it was earned, 0 not known */
|
||||
} Ach;
|
||||
typedef struct AchList {
|
||||
Ach a[ACH_MAX];
|
||||
int n;
|
||||
char orphans[ACH_ORPHANS][ACH_LINE]; /* (the earned file's lines of achievements no list has now) */
|
||||
int norphans;
|
||||
} AchList;
|
||||
|
||||
/* What the game's save data has of a condition's name: an item's count, a flag's 1 or 0; -1 it has no such name */
|
||||
typedef int (*AchProbe)(void *ctx, int kind, const char *what);
|
||||
|
||||
#define ACH_BUILTIN \
|
||||
"[A Sword of Your Own]\nabout: Find a blade in the forest.\nicon: sword green\nitem Weapon_Sword_Kokiri\n" \
|
||||
"[First Melody]\nabout: Learn the royal family's song.\nicon: note\nitem Item_Melody_ZeldaLullaby\n" \
|
||||
"[Forest's Thanks]\nabout: Earn the first Spiritual Stone.\nicon: gem green\nitem Item_Stone_Forest\n" \
|
||||
"[Heart of the Mountain]\nabout: Earn the second Spiritual Stone.\nicon: gem red\nitem Item_Stone_Fire\n" \
|
||||
"[Jewel of the River]\nabout: Earn the third Spiritual Stone.\nicon: gem blue\nitem Item_Stone_Water\n" \
|
||||
"[Three Stones]\nabout: Hold all three Spiritual Stones.\nicon: gem\nitem Item_Stone_Forest\nitem Item_Stone_Fire\nitem Item_Stone_Water\n" \
|
||||
"[Seven Years Later]\nabout: Draw the blade from its pedestal.\nicon: sword blue\nitem Weapon_Sword_Master\n" \
|
||||
"[Sage of Forest]\nabout: Earn the Forest Medallion.\nicon: medal green\nitem Item_Medal_Forest\n" \
|
||||
"[Sage of Fire]\nabout: Earn the Fire Medallion.\nicon: medal red\nitem Item_Medal_Fire\n" \
|
||||
"[Sage of Water]\nabout: Earn the Water Medallion.\nicon: medal blue\nitem Item_Medal_Water\n" \
|
||||
"[Sage of Shadow]\nabout: Earn the Shadow Medallion.\nicon: medal purple\nitem Item_Medal_Shadow\n" \
|
||||
"[Sage of Spirit]\nabout: Earn the Spirit Medallion.\nicon: medal orange\nitem Item_Medal_Spirit\n" \
|
||||
"[Sage of Light]\nabout: Earn the Light Medallion.\nicon: medal yellow\nitem Item_Medal_Light\n" \
|
||||
"[Six Awakened]\nabout: Hold all six medallions.\nicon: medal\nitem Item_Medal_Forest\nitem Item_Medal_Fire\nitem Item_Medal_Water\n" \
|
||||
"item Item_Medal_Shadow\nitem Item_Medal_Spirit\nitem Item_Medal_Light\n" \
|
||||
"[Full Songbook]\nabout: Learn all twelve songs.\nicon: note yellow\nitem Item_Melody_ZeldaLullaby\nitem Item_Melody_EponaSong\n" \
|
||||
"item Item_Melody_SariaSong\nitem Item_Melody_SunSong\nitem Item_Melody_TimeSong\nitem Item_Melody_StormSong\n" \
|
||||
"item Item_Melody_ForestMinuet\nitem Item_Melody_FireBolero\nitem Item_Melody_WaterSerenade\n" \
|
||||
"item Item_Melody_ShadowNocturne\nitem Item_Melody_SpiritRequiem\nitem Item_Melody_LightPrelude\n" \
|
||||
"[Spider Hunter]\nabout: Collect 10 Gold Skulltula Tokens.\nicon: token orange\nitem Item_Token_Gold 10\n" \
|
||||
"[Spider Slayer]\nabout: Collect 50 Gold Skulltula Tokens.\nicon: token silver\nitem Item_Token_Gold 50\n" \
|
||||
"[Curse Broken]\nabout: Collect all 100 Gold Skulltula Tokens.\nicon: token\nitem Item_Token_Gold 100\n" \
|
||||
"[Long Reach]\nabout: Find the Longshot.\nicon: star blue\nitem Item_Longshot\n" \
|
||||
"[Seeing Clearly]\nabout: Find the Lens of Truth.\nicon: star purple\nitem Armor_GlassTruth\n" \
|
||||
"[Honest Face]\nabout: Earn the Mask of Truth.\nicon: star orange\nitem Armor_Child_Mask_Truth\n" \
|
||||
"[A Giant's Blade]\nabout: Earn Biggoron's Sword.\nicon: sword\nitem Weapon_Sword_Biggoron\n" \
|
||||
"[Deep Pockets]\nabout: Carry the Giant's Wallet.\nicon: token green\nitem Item_Wallet_Large\n" \
|
||||
"[Light Against the Dark]\nabout: Receive the Light Arrow.\nicon: star yellow\nitem Item_Arrow_Light\n" \
|
||||
"[Twice as Tough]\nabout: Receive Double Defense.\nicon: shield silver\nitem Item_FairyBless_DefenseDouble\n"
|
||||
|
||||
/* FNV-1a 32 of a name's bytes */
|
||||
static inline uint32_t ach_key(const char *name) {
|
||||
uint32_t h = 2166136261u;
|
||||
for (const unsigned char *p = (const unsigned char *)name; *p; p++) h = (h ^ *p) * 16777619u;
|
||||
return h;
|
||||
}
|
||||
|
||||
/* a character of a flag's or an item's name, as the game's are: letters, digits, _ . and - */
|
||||
static inline int ach_what_char(int ch) {
|
||||
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '.' || ch == '-';
|
||||
}
|
||||
|
||||
static inline int ach_find(const AchList *l, uint32_t key) {
|
||||
for (int i = 0; i < l->n; i++)
|
||||
if (l->a[i].key == key) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* a line's text between its blanks: its start, and its length through *n */
|
||||
static inline const char *ach_trim(const char *s, size_t *n) {
|
||||
while (*n && (*s == ' ' || *s == '\t')) s++, (*n)--;
|
||||
while (*n && (s[*n - 1] == ' ' || s[*n - 1] == '\t' || s[*n - 1] == '\r')) (*n)--;
|
||||
return s;
|
||||
}
|
||||
|
||||
/* An achievement's picture, drawn by the menu and not a file (gpu/vk/achievement_rows.h has the same shapes and tones in
|
||||
the same order, and draws them): a shape in the low four bits, a tone in the next four. "icon: <shape> [<tone>]" in
|
||||
a list; without the line, a gold star */
|
||||
#define ACH_SHAPES "star", "sword", "gem", "medal", "note", "token", "shield"
|
||||
#define ACH_TONES "gold", "green", "red", "blue", "purple", "orange", "yellow", "silver"
|
||||
static inline int ach_icon(int shape, int tone) { return shape | tone << 4; }
|
||||
|
||||
/* the words after "icon:" read: the icon, -1 when they are not a shape and at most a tone */
|
||||
static inline int ach_icon_parse(const char *s, size_t n) {
|
||||
static const char *const shapes[] = {ACH_SHAPES}, *const tones[] = {ACH_TONES};
|
||||
size_t w = 0;
|
||||
while (w < n && s[w] != ' ' && s[w] != '\t') w++;
|
||||
int shape = -1, tone = 0;
|
||||
for (size_t i = 0; i < sizeof shapes / sizeof shapes[0]; i++)
|
||||
if (strlen(shapes[i]) == w && !memcmp(shapes[i], s, w)) shape = (int)i;
|
||||
if (shape < 0) return -1;
|
||||
size_t rest = n - w;
|
||||
const char *t = ach_trim(s + w, &rest);
|
||||
if (!rest) return ach_icon(shape, 0);
|
||||
tone = -1;
|
||||
for (size_t i = 0; i < sizeof tones / sizeof tones[0]; i++)
|
||||
if (strlen(tones[i]) == rest && !memcmp(tones[i], t, rest)) tone = (int)i;
|
||||
return tone < 0 ? -1 : ach_icon(shape, tone);
|
||||
}
|
||||
|
||||
/* A condition's line read ("item <name> [<least>]", "flag <name>"): 1 when it is one */
|
||||
static inline int ach_cond_parse(const char *s, size_t n, AchCond *out) {
|
||||
memset(out, 0, sizeof *out);
|
||||
if (n < 6 || (memcmp(s, "item", 4) && memcmp(s, "flag", 4)) || (s[4] != ' ' && s[4] != '\t')) return 0;
|
||||
out->kind = s[0] == 'i' ? ACH_ITEM : ACH_FLAG;
|
||||
out->least = 1;
|
||||
size_t at = 4, w = 0;
|
||||
while (at < n && (s[at] == ' ' || s[at] == '\t')) at++;
|
||||
while (at < n && ach_what_char((unsigned char)s[at])) {
|
||||
if (w + 1 >= ACH_WHAT) return 0;
|
||||
out->what[w++] = s[at++];
|
||||
}
|
||||
if (!w) return 0;
|
||||
if (at == n) return 1;
|
||||
if (out->kind != ACH_ITEM || (s[at] != ' ' && s[at] != '\t')) return 0;
|
||||
while (at < n && (s[at] == ' ' || s[at] == '\t')) at++;
|
||||
int least = 0, digits = 0;
|
||||
for (; at < n && s[at] >= '0' && s[at] <= '9' && digits < 5; at++, digits++) least = least * 10 + (s[at] - '0');
|
||||
if (!digits || at != n || least < 1 || least > 9999) return 0;
|
||||
out->least = least;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* A list's text added to the list. An achievement is taken whole or not at all: one with a line that is none of the
|
||||
form's, without a condition, with more than ACH_CONDS, with a name that is empty, too long or another's, or past
|
||||
ACH_MAX is left out, and counted in *refused (may be NULL). Returns how many were added */
|
||||
static inline int ach_parse(AchList *l, const char *text, int *refused) {
|
||||
int added = 0, bad = 0, open = 0, broken = 0;
|
||||
static Ach cur; /* (too large for a guest worker's stack; the lists are read by one thread, at the first check) */
|
||||
for (const char *line = text; ; ) {
|
||||
const char *end = line;
|
||||
while (*end && *end != '\n') end++;
|
||||
size_t n = (size_t)(end - line);
|
||||
const char *s = ach_trim(line, &n);
|
||||
const int head = n && s[0] == '[';
|
||||
if (open && (head || (!*end && !n))) { /* (the achievement before this line is whole now) */
|
||||
if (broken || !cur.nconds || ach_find(l, cur.key) >= 0 || l->n >= ACH_MAX) bad++;
|
||||
else l->a[l->n++] = cur, added++;
|
||||
open = 0;
|
||||
}
|
||||
if (head) {
|
||||
memset(&cur, 0, sizeof cur);
|
||||
open = 1, broken = 0;
|
||||
size_t nn = s[n - 1] == ']' ? n - 2 : 0;
|
||||
const char *name = ach_trim(s + 1, &nn);
|
||||
if (!nn || nn > ACH_NAME) broken = 1;
|
||||
else {
|
||||
memcpy(cur.name, name, nn);
|
||||
for (size_t i = 0; i < nn; i++)
|
||||
if ((unsigned char)cur.name[i] < 32) broken = 1;
|
||||
cur.key = ach_key(cur.name);
|
||||
}
|
||||
} else if (open && n && s[0] != '#') { /* (a line before the first name is not an achievement's) */
|
||||
AchCond c;
|
||||
if (n >= 6 && !memcmp(s, "about:", 6)) {
|
||||
size_t an = n - 6;
|
||||
const char *a = ach_trim(s + 6, &an);
|
||||
if (an > ACH_ABOUT) broken = 1;
|
||||
else memcpy(cur.about, a, an), cur.about[an] = 0;
|
||||
} else if (n >= 5 && !memcmp(s, "icon:", 5)) {
|
||||
size_t in = n - 5;
|
||||
const char *i = ach_trim(s + 5, &in);
|
||||
if ((cur.icon = ach_icon_parse(i, in)) < 0) broken = 1;
|
||||
} else if (cur.nconds < ACH_CONDS && ach_cond_parse(s, n, &c)) cur.conds[cur.nconds++] = c;
|
||||
else broken = 1;
|
||||
}
|
||||
if (!*end) {
|
||||
if (!n) break;
|
||||
line = end; /* (the text's last line had no line feed: once more, at its end, for the achievement it closes) */
|
||||
} else line = end + 1;
|
||||
}
|
||||
if (refused) *refused += bad;
|
||||
return added;
|
||||
}
|
||||
|
||||
/* 1 when every condition of the achievement holds in the save data the probe reads */
|
||||
static inline int ach_met(const Ach *a, AchProbe probe, void *ctx) {
|
||||
for (int i = 0; i < a->nconds; i++) {
|
||||
const AchCond *c = &a->conds[i];
|
||||
const int have = probe(ctx, c->kind, c->what);
|
||||
if (have < (c->kind == ACH_ITEM ? c->least : 1)) return 0;
|
||||
}
|
||||
return a->nconds > 0;
|
||||
}
|
||||
|
||||
/* The achievements not yet earned whose conditions hold are earned now (when: unix time): their places in the list go
|
||||
to fresh, cap of them at most a call (the rest at the next), and how many is returned. Nothing is asked of the probe
|
||||
for an achievement that is earned already */
|
||||
static inline int ach_check(AchList *l, AchProbe probe, void *ctx, int64_t when, int *fresh, int cap) {
|
||||
int got = 0;
|
||||
for (int i = 0; i < l->n && got < cap; i++) {
|
||||
Ach *a = &l->a[i];
|
||||
if (a->earned || !ach_met(a, probe, ctx)) continue;
|
||||
a->earned = 1, a->when = when;
|
||||
fresh[got++] = i;
|
||||
}
|
||||
return got;
|
||||
}
|
||||
|
||||
static inline int ach_earned_count(const AchList *l) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < l->n; i++) n += l->a[i].earned != 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
/* The earned file's text read: "<key, 8 hex digits> <unix time> <name>" a line. A line of an achievement the list has
|
||||
marks it earned; any other line of that form is kept for the next write (ACH_ORPHANS at most); the rest is dropped.
|
||||
Returns how many of the list's were marked */
|
||||
static inline int ach_earned_read(AchList *l, const char *text) {
|
||||
int marked = 0;
|
||||
for (const char *line = text; *line; ) {
|
||||
const char *end = line;
|
||||
while (*end && *end != '\n') end++;
|
||||
size_t n = (size_t)(end - line);
|
||||
const char *s = ach_trim(line, &n);
|
||||
char *after = NULL;
|
||||
const unsigned long key = n >= 10 && s[0] != '#' ? strtoul(s, &after, 16) : 0;
|
||||
if (after == s + 8 && *after == ' ') {
|
||||
const long long when = strtoll(after + 1, &after, 10);
|
||||
const int at = ach_find(l, (uint32_t)key);
|
||||
if (at >= 0) {
|
||||
if (!l->a[at].earned) marked++;
|
||||
l->a[at].earned = 1, l->a[at].when = when > 0 ? when : 0;
|
||||
} else if (l->norphans < ACH_ORPHANS && n < ACH_LINE) {
|
||||
int dup = 0;
|
||||
for (int i = 0; i < l->norphans; i++) dup |= !strncmp(l->orphans[i], s, 8);
|
||||
if (!dup) memcpy(l->orphans[l->norphans], s, n), l->orphans[l->norphans++][n] = 0;
|
||||
}
|
||||
}
|
||||
line = *end ? end + 1 : end;
|
||||
}
|
||||
return marked;
|
||||
}
|
||||
|
||||
/* The earned file's text written into out: its length, 0 when cap is too small for it */
|
||||
static inline size_t ach_earned_write(const AchList *l, char *out, size_t cap) {
|
||||
size_t at = 0;
|
||||
int n = snprintf(out, cap, "# The achievements earned in this game folder, a line each: key, when (unix time), name.\n");
|
||||
if (n < 0 || (size_t)n >= cap) return 0;
|
||||
at = (size_t)n;
|
||||
for (int i = 0; i < l->n; i++) {
|
||||
const Ach *a = &l->a[i];
|
||||
if (!a->earned) continue;
|
||||
n = snprintf(out + at, cap - at, "%08x %lld %s\n", (unsigned)a->key, (long long)a->when, a->name);
|
||||
if (n < 0 || (size_t)n >= cap - at) return 0;
|
||||
at += (size_t)n;
|
||||
}
|
||||
for (int i = 0; i < l->norphans; i++) {
|
||||
n = snprintf(out + at, cap - at, "%s\n", l->orphans[i]);
|
||||
if (n < 0 || (size_t)n >= cap - at) return 0;
|
||||
at += (size_t)n;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
/* host/achievements.c */
|
||||
int hos_achievements_on(void);
|
||||
void hos_achievements_frame(void);
|
||||
void hos_achievements_check(AchProbe probe, void *ctx, int64_t now_ns);
|
||||
#endif
|
||||
@@ -262,6 +262,16 @@ static void typed_text(HWND w, WPARAM wp) {
|
||||
rx_option_set(HOS_OPT_TEXT_CHAR, (int)u);
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, after Casual Gamer's fix) A lone Alt: DefWindowProc enters 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, and the screen froze until a key
|
||||
was pressed. The window has no menu bar: nothing to enter. Alt+Space (the system menu, lp ' ') still goes on to Windows;
|
||||
Alt+Enter, F10 and Alt+F4 are WM_SYSKEYDOWN's and WM_SYSKEYUP's, below. HOS_ALT_MENU=1: as before */
|
||||
static int keymenu_dropped(WPARAM wp, LPARAM lp) {
|
||||
static int before = -1;
|
||||
if (before < 0) before = getenv("HOS_ALT_MENU") && atoi(getenv("HOS_ALT_MENU")) != 0;
|
||||
return !before && (wp & 0xFFF0) == SC_KEYMENU && lp != ' ';
|
||||
}
|
||||
|
||||
static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
switch (msg) {
|
||||
case WM_MOUSEMOVE:
|
||||
@@ -329,6 +339,7 @@ static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
return 0;
|
||||
}
|
||||
case WM_SYSCOMMAND:
|
||||
if (keymenu_dropped(wp, lp)) return 0; /* (a lone Alt: no menu loop, keymenu_dropped) */
|
||||
if ((wp & 0xFFF0) == SC_SCREENSAVE && !(wp & SCF_ISSECURE) &&
|
||||
renderer && GetForegroundWindow() == w && !IsIconic(w)) {
|
||||
BOOL secure = TRUE; /* unknown or protected policy keeps Windows' normal handling */
|
||||
|
||||
52
host/heap_commit.h
Normal file
52
host/heap_commit.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* (Triforce, 11-21, the performance audit's MEM-09, after Source A's) The game's heap committed as it is touched.
|
||||
|
||||
The guest window is reserved, and what the runtime hands out is committed (hos_commit): the game asks for a heap of
|
||||
~6.9 GB at its start and all of it was committed there, though the game touches a part of it. Committed memory counts
|
||||
against the PC's RAM plus page file whether touched or not: on a 16 GB PC with a small page file that is where "out
|
||||
of memory" came from, with GBs of RAM free. On Windows the heap is now left reserved, and the first-touch handler
|
||||
that was there already for pages nothing handed out (mkw_glue.c commit_on_touch) commits it, 64 KB at a touch.
|
||||
|
||||
What had to be there first, as Source A found:
|
||||
- two threads touching one new 64 KB at once: the second's fault finds the pages committed, and is tried again
|
||||
(heap_touch_again) where it went on to the crash report;
|
||||
- a commit refused at Windows' commit limit is tried again for 5 s (mkw_glue.c commit_wait), while the renderer's
|
||||
textures give commit back under their own rule: a first touch has no result to give the game;
|
||||
- the kernel does not fault pages in: a file read into heap pages nothing touched yet failed without an exception.
|
||||
The calls that hand guest memory to the kernel read each page of their range once first (mkw_glue.c
|
||||
hos_guest_touch), so that the handler commits the reserved ones.
|
||||
And a first touch commits only the reserved pages of its 64 KB (heap_touch_range): a commit over committed pages
|
||||
resets their protection, the lazy write-back's and the watches' too.
|
||||
|
||||
HOS_HEAP_COMMIT=1: the heap committed whole at the start, as before. HOS_LARGE_PAGES: whole, as it is reserved so.
|
||||
Linux: as it was (its window does not charge untouched pages).
|
||||
|
||||
The rules alone: tests/test_heap_commit_lazy.py compiles this file. */
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Whether SetMemoryHeapSize commits the new heap whole. asked: HOS_HEAP_COMMIT's value (not 0: whole); large: the heap
|
||||
is in large pages; windows: 0 on Linux */
|
||||
static inline int heap_commit_whole(const char *asked, int large, int windows) {
|
||||
if (!windows || large) return 1;
|
||||
return asked && atoi(asked) != 0;
|
||||
}
|
||||
|
||||
/* Whether a fault in the guest window is tried again with nothing done: its page is committed and read-write by now,
|
||||
another thread's first touch having got there between the fault and the look. Not an execute fault (kind 8): a jump
|
||||
into guest memory would fault again at once, for ever; it goes on to the crash report */
|
||||
static inline int heap_touch_again(int committed, int read_write, uint64_t kind) {
|
||||
return committed && read_write && kind != 8;
|
||||
}
|
||||
|
||||
/* The pages a first touch at `at` commits, [*lo, *hi): of the 64 KB around it, the run of reserved pages it lies in
|
||||
([run_lo, run_hi): VirtualQuery's region) */
|
||||
static inline void heap_touch_range(uint64_t at, uint64_t run_lo, uint64_t run_hi, uint64_t *lo, uint64_t *hi) {
|
||||
const uint64_t c_lo = at & ~0xFFFFULL, c_hi = c_lo + 0x10000;
|
||||
*lo = run_lo > c_lo ? run_lo : c_lo;
|
||||
*hi = run_hi < c_hi ? run_hi : c_hi;
|
||||
}
|
||||
|
||||
/* Whether the n-th first touch of the heap gets a log line: the first, then each doubling from 4096 (256 MB) */
|
||||
static inline int heap_touch_said(long n) { return n == 1 || (n >= 4096 && !(n & (n - 1))); }
|
||||
19
host/hos.h
19
host/hos.h
@@ -54,6 +54,10 @@ int hos_game_is_mkw(void);
|
||||
/* (mkw_glue.c) guest pages made usable by host APIs. (Triforce) 1: committed, 0: the commit failed (logged), -1: not in
|
||||
the guest window (logged) */
|
||||
int hos_commit(uint64_t addr, uint64_t size);
|
||||
/* ((Triforce, 11-21, MEM-09) mkw_glue.c) before guest memory goes to the kernel: each of its pages read once, so that one
|
||||
only reserved is committed (the heap committed as it is touched: hos_heap_on_touch, set by SetMemoryHeapSize) */
|
||||
extern int hos_heap_on_touch;
|
||||
void hos_guest_touch(uint64_t addr, uint64_t size);
|
||||
/* (Triforce) the guest memory census (hos_mem.c, host/guest_census.h): each place that commits, maps or lets go of guest
|
||||
pages says what it did. COMMITTED: handed out and committed (before the commit itself); TOUCHED: committed on a first
|
||||
touch; PAGES, MAPPED: the game's own pages and physical maps; DECOMMITTED: let go and decommitted; ZEROED: let go and
|
||||
@@ -84,6 +88,18 @@ void web_mark_written(const void *p, size_t n); /* (hos/web/win32.c: guest mem
|
||||
#else
|
||||
#define HOS_WRITTEN(p, n) ((void)0)
|
||||
#endif
|
||||
/* ((Triforce, 11-22, the performance audit's MEM-13, after Source A's; HOS_MEM_ZERO_MARK=1, off unless set) Guest memory
|
||||
filled by host code: the freed pages a Vulkan import holds, which are zeroed in place and not discarded. The renderer
|
||||
does not see a bare memset (the soft write watch knows what guest code and HOS_WRITTEN mark), so its mirror, its
|
||||
captures and the write-back's shadow kept the freed bytes, and a later write-back could put them back over the zeros.
|
||||
With the switch the fill is marked written: the memset first and the mark after it, since a capture takes the mark
|
||||
before it copies the page. Without it: a plain memset, as before. Off until it is measured at arrivals: the marked
|
||||
zeros are pages the captures and the mirror syncs then copy */
|
||||
int hos_mem_zero_mark(void); /* (hos_mem.c: HOS_MEM_ZERO_MARK, read once) */
|
||||
static inline void hos_guest_fill(uint64_t a, int v, uint64_t n) {
|
||||
memset(GP(a), v, (size_t)n);
|
||||
if (hos_mem_zero_mark()) HOS_WRITTEN(GP(a), (size_t)n);
|
||||
}
|
||||
#ifdef A64_MEM_ZERO
|
||||
extern uint64_t hos_space_end; /* web: the guest window ends where the host's own data starts (hos_main.c) */
|
||||
#define HOS_SPACE_END hos_space_end
|
||||
@@ -128,6 +144,7 @@ void hos_thread_join(HosThread *t);
|
||||
void hos_thread_destroy(HosThread *t);
|
||||
void hos_thread_set_name(HosThread *t, const char *name);
|
||||
void hos_thread_pin(void *h, const char *name); /* HOS_PIN: host CPU affinity by thread name */
|
||||
void hos_sim_cpu_apply(void); /* ((Triforce, 11-22, OS-49) HOS_SIM_CPU, a lab switch: hos_sim_cpu.h; before any thread */
|
||||
int hos_game_cores(void); /* totk_cores.c: HOS_GAME_CORES, or 0 */
|
||||
const char *hos_thread_name(HosThread *t);
|
||||
void hos_thread_affinity(HosThread *t, int ideal, uint64_t mask);
|
||||
@@ -142,6 +159,7 @@ int hos_vcore_away(void);
|
||||
void hos_vcore_back(void);
|
||||
void hos_vcore_checkpoint(void);
|
||||
void hos_vcore_yield(void);
|
||||
int hos_vcore_asked(void); /* a waiter asked this thread for its core (vc_kick) */
|
||||
int hos_cv_sleep(void *cv, void *srw_lock, unsigned ms); /* SleepConditionVariableSRW (exclusive) for guest threads */
|
||||
void hos_thread_boost(int level);
|
||||
void hos_thread_stack(HosThread *t, uint64_t *addr, uint64_t *size);
|
||||
@@ -152,6 +170,7 @@ Ctx *hos_boot_main_thread(uint64_t stack_size); /* the calling host th
|
||||
uint32_t hos_thread_handle(HosThread *t);
|
||||
HosThread *hos_thread_by_handle(uint32_t h);
|
||||
int hos_thread_prio(HosThread *t);
|
||||
int hos_thread_serial(HosThread *t); /* ((Triforce, OS-33) a thread the frame waits on: host/short_sleep.h) */
|
||||
void hos_thread_wait(HosThread *t, int64_t timeout_ns, int *timed_out); /* < 0: forever */
|
||||
void hos_thread_wake(HosThread *t);
|
||||
|
||||
|
||||
148
host/hos_main.c
148
host/hos_main.c
@@ -15,6 +15,7 @@
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include "../gpu/vk/oot_cheat_items.h"
|
||||
#include "../gpu/vk/oot_debug_link.h" /* ((Triforce) the Game flags and Cutscenes pages' lines) */
|
||||
#include "achievements.h" /* ((Triforce) what an achievement asks of the save data: host/achievements.c) */
|
||||
#include "../gpu/vk/ultrawide_rows.h" /* ((Triforce) the Ultrawide row's shapes: ultrawide_ratio) */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -34,6 +35,7 @@ const char *hos_version(void) { return HOOKSHOT_VERSION; } /* (discord_rpc.c's
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <sys/stat.h> /* ((Triforce) driver_env_choose) */
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@@ -183,24 +185,30 @@ static int reserve_guest(void) {
|
||||
/* (only where /dev/udmabuf can be opened: a shared mapping's pages cannot be host-pointer imports (RADV:
|
||||
VK_ERROR_INVALID_EXTERNAL_HANDLE), so a pool udmabuf refused would be no import at all. A pool larger than udmabuf's
|
||||
size_limit_mb (64 MB unless raised) is imported as several dma-bufs bound into one sparse buffer, so no setting is
|
||||
needed. HOS_GUEST_MEMFD=1: the memfd all the same) */
|
||||
needed. HOS_GUEST_MEMFD=1: the memfd all the same; =0: anonymous memory.
|
||||
(Triforce, 11-21) A size_limit_mb that cannot be read counts as udmabuf's default, 64 MB, as the renderer takes it
|
||||
(gpu/vk/vk_memory.cpp import_dma_buf): Flatpak and other sandboxes hide /sys/module while /dev/udmabuf opens, and the
|
||||
window stayed anonymous there, 15-45 ms a submission on AMD. Only a limit that reads 0 (udmabuf then refuses every
|
||||
dma-buf) keeps it anonymous) */
|
||||
const char *e = getenv("HOS_GUEST_MEMFD");
|
||||
int usable = 0;
|
||||
unsigned long long limit_mb = 0;
|
||||
{
|
||||
const int u = open("/dev/udmabuf", O_RDWR | O_CLOEXEC);
|
||||
if (u >= 0) {
|
||||
close(u);
|
||||
usable = 1;
|
||||
FILE *f = fopen("/sys/module/udmabuf/parameters/size_limit_mb", "r");
|
||||
if (f) {
|
||||
if (fscanf(f, "%llu", &limit_mb) != 1) limit_mb = 0;
|
||||
unsigned long long limit_mb = 0;
|
||||
if (fscanf(f, "%llu", &limit_mb) == 1 && limit_mb == 0) usable = 0;
|
||||
fclose(f);
|
||||
} else {
|
||||
hos_log("mem: udmabuf's size_limit_mb cannot be read (in a container?): its default, 64 MB, assumed");
|
||||
}
|
||||
usable = limit_mb > 0;
|
||||
}
|
||||
if (!usable && !e)
|
||||
hos_log("mem: guest memory stays host-pointer imports for the GPU (slow submissions on AMD): %s",
|
||||
u < 0 ? "/dev/udmabuf cannot be opened" : "udmabuf's size_limit_mb cannot be read");
|
||||
u < 0 ? "/dev/udmabuf cannot be opened" : "udmabuf's size_limit_mb is 0");
|
||||
}
|
||||
if (e ? atoi(e) != 0 : usable) {
|
||||
const int fd = (int)syscall(SYS_memfd_create, "hookshot-guest", 1u /* MFD_CLOEXEC */ | 2u /* MFD_ALLOW_SEALING */);
|
||||
@@ -467,6 +475,96 @@ static void load_hookshot_env(void) {
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
/* (Triforce, 11-21, Linux) Steam's Game Mode gives every game RADV_FORCE_VRS_CONFIG_FILE (its Half Rate Shading). While that
|
||||
file exists, RADV on RDNA 2 and newer puts a forced shading rate into every pipeline's key, whatever the file holds, 1x1 too:
|
||||
the pipelines prepared in Desktop Mode, or by the builder, missed the driver's caches and were all compiled again (on a Deck,
|
||||
over 25 minutes to the title, against under one). The variable goes before Vulkan is loaded, when it names a file that
|
||||
exists, unless HOS_HALF_RATE_SHADING=1 (triforce_env.txt can set it) keeps it; RADV_FORCE_VRS is left alone. What was done is
|
||||
said on stderr now and in the log with the boot: lines (driver_env_note). The slider is worth little here: RADV keeps fragment
|
||||
programs that read their position at full rate, and nearly all of this game's do */
|
||||
static char driver_env_note[640];
|
||||
static void driver_env_choose(void) {
|
||||
const char *v = getenv("RADV_FORCE_VRS_CONFIG_FILE");
|
||||
struct stat st;
|
||||
if (!v || !*v || stat(v, &st) != 0) return;
|
||||
const char *keep = getenv("HOS_HALF_RATE_SHADING");
|
||||
if (keep && !strcmp(keep, "1")) {
|
||||
snprintf(driver_env_note, sizeof driver_env_note,
|
||||
"boot: RADV_FORCE_VRS_CONFIG_FILE=%s kept (HOS_HALF_RATE_SHADING=1: Steam's Half Rate Shading changes every "
|
||||
"pipeline's key, so the driver's caches prepared without it miss)", v);
|
||||
} else {
|
||||
snprintf(driver_env_note, sizeof driver_env_note,
|
||||
"boot: RADV_FORCE_VRS_CONFIG_FILE=%s dropped (Steam's Half Rate Shading changes every pipeline's key; "
|
||||
"HOS_HALF_RATE_SHADING=1 keeps it)", v);
|
||||
unsetenv("RADV_FORCE_VRS_CONFIG_FILE");
|
||||
}
|
||||
fprintf(stderr, "%s\n", driver_env_note);
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's PIPE-97, after Source A's and Source B's) Room in the driver's own shader
|
||||
cache for the pipelines the game prepares. The ~41,400 predicted pipelines are built into the driver's disk cache
|
||||
alone, and NVIDIA's and Mesa's caches hold 1 GiB unless told otherwise and trim themselves to it: Source A saw a
|
||||
full preparation stop at 1,026 MB with about 45% of it dropped, to be compiled again in play. Both drivers read
|
||||
their size from the environment when they load, so the Linux main sets 10 GiB for each before Vulkan loads, in the
|
||||
game's run and the builder's preparation alike, without overwriting: a size the player set stays. The cache only
|
||||
grows by what is built. The env file cannot set these two names (env_line_allowed). Not set here:
|
||||
__GL_SHADER_DISK_CACHE_SKIP_CLEANUP (the cache would then grow with every update's preparation).
|
||||
HOS_DRIVER_CACHE_ROOM=0, in the environment or triforce_env.txt: neither is set, as before. Windows' NVIDIA driver
|
||||
takes its size from its control panel: nothing is done there. One line for the log, as driver_env_choose's */
|
||||
static char driver_room_line[320];
|
||||
static void driver_cache_room(void) {
|
||||
const char *off = getenv("HOS_DRIVER_CACHE_ROOM");
|
||||
if (off && !strcmp(off, "0")) {
|
||||
snprintf(driver_room_line, sizeof driver_room_line,
|
||||
"boot: the drivers' shader cache sizes left as they are (HOS_DRIVER_CACHE_ROOM=0)");
|
||||
} else {
|
||||
setenv("__GL_SHADER_DISK_CACHE_SIZE", "10737418240", 0); /* (NVIDIA's, in bytes: 10 GiB) */
|
||||
setenv("MESA_SHADER_CACHE_MAX_SIZE", "10G", 0); /* (Mesa's: RADV, the Steam Deck) */
|
||||
const char *nv = getenv("__GL_SHADER_DISK_CACHE_SIZE"), *mesa = getenv("MESA_SHADER_CACHE_MAX_SIZE");
|
||||
snprintf(driver_room_line, sizeof driver_room_line,
|
||||
"boot: room in the drivers' shader caches: __GL_SHADER_DISK_CACHE_SIZE=%.64s, MESA_SHADER_CACHE_MAX_SIZE=%.64s "
|
||||
"(a size set before the game stays; HOS_DRIVER_CACHE_ROOM=0 sets neither)", nv ? nv : "", mesa ? mesa : "");
|
||||
}
|
||||
fprintf(stderr, "%s\n", driver_room_line);
|
||||
}
|
||||
|
||||
/* (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: it neither makes it nor falls back to
|
||||
~/.cache/nvidia, and every one of the game's prepared pipelines is compiled again at each start (Source A: a
|
||||
preparation of 830.7 s against 51.0 s from a filled cache). So before Vulkan loads the Linux main makes the folder
|
||||
the variable names, with its parents, for the user alone (0700). The value is never changed, and nothing is made
|
||||
when the variable is not set: only a folder the player or a launcher named. One line when it cannot be made (a
|
||||
file in the way, no permission), on stderr and kept for the log. HOS_DRIVER_CACHE_FOLDER=0, in the environment or
|
||||
triforce_env.txt: nothing is made, as before. The driver files its cache by the program's file name, so the Linux
|
||||
binary's name stays "triforce" (nx2/build.py EXE; tests/test_driver_cache_folder.py pins it), and
|
||||
__GL_SHADER_DISK_CACHE_APP_NAME is never set: every player's cache would start over */
|
||||
static char driver_folder_line[1200];
|
||||
static void driver_cache_folder(void) {
|
||||
const char *off = getenv("HOS_DRIVER_CACHE_FOLDER");
|
||||
const char *path = getenv("__GL_SHADER_DISK_CACHE_PATH");
|
||||
char dir[1024];
|
||||
struct stat st;
|
||||
if ((off && !strcmp(off, "0")) || !path || !*path) return;
|
||||
const size_t len = strlen(path);
|
||||
if (len < sizeof dir) {
|
||||
memcpy(dir, path, len + 1);
|
||||
for (size_t i = 1; i < len; i++) { /* (each parent in turn; one that is there already is no failure) */
|
||||
if (dir[i] != '/') continue;
|
||||
dir[i] = 0;
|
||||
mkdir(dir, 0700);
|
||||
dir[i] = '/';
|
||||
}
|
||||
mkdir(dir, 0700);
|
||||
}
|
||||
if (len < sizeof dir && stat(path, &st) == 0 && S_ISDIR(st.st_mode)) return; /* (made, or there already) */
|
||||
snprintf(driver_folder_line, sizeof driver_folder_line,
|
||||
"boot: __GL_SHADER_DISK_CACHE_PATH=%.900s is not a folder and cannot be made one: NVIDIA's driver keeps no "
|
||||
"shader cache without it", path);
|
||||
fprintf(stderr, "%s\n", driver_folder_line);
|
||||
}
|
||||
#endif
|
||||
|
||||
int hos_oot_protect_hearts;
|
||||
volatile int hos_oot_live_action; /* (test runs) the live input file's "!cheat <action>": host/nn_hid.c */
|
||||
|
||||
@@ -1237,6 +1335,24 @@ static void oot_link_tick(Ctx *c, uint64_t actor, int64_t now) {
|
||||
oot_link_say(OOT_SAID_FLAGS, states);
|
||||
}
|
||||
|
||||
/* ---- (Triforce) Achievements (host/achievements.c, on unless HOS_ACHIEVEMENTS=0): what the save data has of a
|
||||
condition's name, read as the pages above and the Inventory page read it, and nothing written. Asked once a second at
|
||||
Link's update, and only while a save is loaded: before that GameData holds a new game's values */
|
||||
static int oot_achievement_probe(void *ctx, int kind, const char *what) {
|
||||
Ctx *c = (Ctx *)ctx;
|
||||
if (kind == ACH_ITEM) return oot_item_get(c, what);
|
||||
const char state = oot_flag_state(c, 1, oot_name_hash(what, strlen(what)));
|
||||
return state == '1' ? 1 : state == '0' ? 0 : -1;
|
||||
}
|
||||
|
||||
static void oot_achievements_tick(Ctx *c, int64_t now) {
|
||||
static int64_t next;
|
||||
if (!hos_achievements_on() || now < next) return;
|
||||
next = now + 1000000000LL;
|
||||
if (oot_pouch_count(c, oot_item_names[0]) < 0) return;
|
||||
hos_achievements_check(oot_achievement_probe, c, now);
|
||||
}
|
||||
|
||||
static uint64_t oot_gameplay_view(Ctx *c) {
|
||||
const uint64_t singleton = recomp_load64(c, 0x84ca6bc0ULL);
|
||||
const uint64_t manager = singleton ? recomp_load64(c, singleton) : 0;
|
||||
@@ -1292,6 +1408,7 @@ static void oot_update_player_cheats(Ctx *c, uint64_t actor, uint64_t components
|
||||
oot_bench_tick(c, actor, now); /* ((Triforce, 11-19) benchmark mode: its tour, host/hos_bench.c */
|
||||
oot_inventory_refresh(c, now);
|
||||
oot_link_tick(c, actor, now); /* ((Triforce) the Game flags and Cutscenes pages) */
|
||||
oot_achievements_tick(c, now); /* ((Triforce) host/achievements.c) */
|
||||
if (action >= OOT_CHEAT_ITEM_ACTION && action < OOT_CHEAT_ITEM_ACTION + 256 * (int)OOT_ITEM_COUNT) {
|
||||
oot_inventory_edit(c, action);
|
||||
} else if (action == OOT_AGE_ACTION) {
|
||||
@@ -1843,7 +1960,13 @@ static int game_main(int argc, char **argv) {
|
||||
}
|
||||
#endif
|
||||
HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
|
||||
if (!err || err == INVALID_HANDLE_VALUE || GetFileType(err) == FILE_TYPE_UNKNOWN) {
|
||||
/* ((Triforce, 11-21, ticket "Linux: games started with the launcher's Play write no triforce_log.txt") HOS_LOG_FILE=1:
|
||||
the log file whatever stderr is. The launcher's Play on Linux gives the game pipes it reads and drops (so a full pipe
|
||||
never blocks it), and a pipe is a known stderr: the whole log went into it and no triforce_log.txt was written, so a
|
||||
session started from the launcher left nothing to send with a report, nor with an F8 capture) */
|
||||
const char *lf = getenv("HOS_LOG_FILE");
|
||||
const int log_file = lf && *lf && *lf != '0';
|
||||
if (log_file || !err || err == INVALID_HANDLE_VALUE || GetFileType(err) == FILE_TYPE_UNKNOWN) {
|
||||
char path[MAX_PATH + 32];
|
||||
DWORD n = hos_game_path_a(path, MAX_PATH);
|
||||
while (n && path[n - 1] != 92 && path[n - 1] != 47) n--;
|
||||
@@ -1861,6 +1984,10 @@ static int game_main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
hos_log("triforce %s", HOOKSHOT_VERSION); /* (the log's first line: which build a report comes from) */
|
||||
#ifdef __linux__
|
||||
if (driver_room_line[0]) hos_log("%s", driver_room_line); /* ((Triforce, 11-22) the performance audit's PIPE-97: decided in main, before this log was open */
|
||||
if (driver_folder_line[0]) hos_log("%s", driver_folder_line); /* ((Triforce, 11-22) the performance audit's PIPE-98: likewise */
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
if (instance_held >= 0)
|
||||
hos_log("instance: %s", instance_held ? "this game folder is this run's (a second copy started on it says so and ends)"
|
||||
@@ -1876,6 +2003,7 @@ static int game_main(int argc, char **argv) {
|
||||
: "Windows' throttling of a window not in front left as it is (HOS_POWER_THROTTLING=1)");
|
||||
}
|
||||
#endif
|
||||
hos_sim_cpu_apply(); /* ((Triforce, 11-22, the performance audit's OS-49) HOS_SIM_CPU, a lab switch: before any thread */
|
||||
const char *image = NULL;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "--romfs") && i + 1 < argc) hos_romfs_dir = argv[++i];
|
||||
@@ -2039,6 +2167,9 @@ static int game_main(int argc, char **argv) {
|
||||
(unsigned long long)(hos_total_memory >> 20));
|
||||
#ifndef A64_MEM_ZERO
|
||||
memory_notice();
|
||||
#endif
|
||||
#ifdef __linux__
|
||||
if (driver_env_note[0]) hos_log("%s", driver_env_note); /* ((Triforce) driver_env_choose's line) */
|
||||
#endif
|
||||
void hos_mods_init(const char *image_path);
|
||||
hos_mods_init(image); /* mods.c: <exe folder>\mods (RomFS files) */
|
||||
@@ -2090,6 +2221,11 @@ int main(int argc, char **argv) {
|
||||
int hos_display_init(void);
|
||||
void hos_display_loop(void);
|
||||
load_hookshot_env();
|
||||
#ifdef __linux__
|
||||
driver_env_choose(); /* ((Triforce) after the env file, which may set HOS_HALF_RATE_SHADING; before Vulkan, the builder's run too) */
|
||||
driver_cache_room(); /* ((Triforce, 11-22) the performance audit's PIPE-97: 10 GiB for the driver's shader cache, likewise */
|
||||
driver_cache_folder(); /* ((Triforce, 11-22) the performance audit's PIPE-98: the folder __GL_SHADER_DISK_CACHE_PATH names, made */
|
||||
#endif
|
||||
game_argc = argc, game_argv = argv;
|
||||
int prepare_shaders = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
|
||||
115
host/hos_mem.c
115
host/hos_mem.c
@@ -1,6 +1,8 @@
|
||||
/* Guest memory map: the system region (our own objects), the heap, address regions.
|
||||
The whole 4 GB window is committed up front (hos_main.c), so "mapping" here is only bookkeeping and zeroing. */
|
||||
#include "hos.h"
|
||||
#include "heap_commit.h" /* ((Triforce, 11-21, MEM-09) the heap committed as it is touched) */
|
||||
#include "stall_host.h" /* ((Triforce, 11-22, OS-51) the host's part of a long frame, counted where it is spent) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -166,6 +168,57 @@ static void pages_mark_used_locked(uint64_t addr, uint64_t size) {
|
||||
for (uint64_t p = addr >> 12; p < (addr + size + 0xFFF) >> 12 && p < (1ULL << 20); p++) pages_used[p >> 6] |= 1ULL << (p & 63);
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-22, the performance audit's MEM-13) HOS_MEM_ZERO_MARK=1: hos_guest_fill (hos.h) marks what it fills as
|
||||
written. Read once, off unless set */
|
||||
int hos_mem_zero_mark(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
const char *e = getenv("HOS_MEM_ZERO_MARK");
|
||||
const int v = e && *e && atoi(e) != 0;
|
||||
if (v) hos_log("mem: freed pages zeroed under an import are marked written for the renderer (HOS_MEM_ZERO_MARK=1)");
|
||||
on = v;
|
||||
}
|
||||
return on;
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's OS-40, after Source A's) Whether a thread keeps its virtual core while it
|
||||
discards. A discard is milliseconds in the kernel for a region of some MB, and before it the lazy write-back's pages
|
||||
there are paid and pages a GPU import holds are zeroed; all the while the thread runs no guest code and the game
|
||||
threads allowed only on its core wait. Source A gives the core up before a freed region's discard and takes it back
|
||||
once, after the region is unlisted: at its Market the core waits of 50 ms and more went from 287 to 19 (275 of them
|
||||
loader threads'), the longest from 177 to 45 ms, with the frames unchanged; the freeing thread once waited 124 ms for
|
||||
its core back, in a load.
|
||||
HOS_DISCARD_KEEP_CORE, read once:
|
||||
- not set: as before. The core is given up for the discard's own kernel call, and only with the limit on
|
||||
(HOS_MEM_LIMIT), where that was measured; hos_region_free itself gives nothing up;
|
||||
- 0: Source A's. hos_region_free gives the core up before the whole of its discard (the write-back's payment and the
|
||||
import's zero-fill too) and takes it back once the region is unlisted, never under the regions' lock; the discard's
|
||||
own call gives it up as well, limit or not, which is what FreeMemoryPages' discard then has (under a region's
|
||||
give-up it finds the core gone already and does nothing);
|
||||
- 1 (any other number): the core kept through every discard, with the limit too.
|
||||
Not the default: it reverses the choice made here to stay on upstream's path without the limit, it changes when a game
|
||||
thread waits, and it was not measured here */
|
||||
enum { DISCARD_CORE_AS_BEFORE, DISCARD_CORE_GIVEN_UP, DISCARD_CORE_KEPT };
|
||||
static int discard_core_rule(const char *env) {
|
||||
if (!env || !*env) return DISCARD_CORE_AS_BEFORE;
|
||||
return atoi(env) != 0 ? DISCARD_CORE_KEPT : DISCARD_CORE_GIVEN_UP;
|
||||
}
|
||||
/* (a freed region's give-up around its whole discard; and the discard call's own) */
|
||||
static int discard_core_region_away(int mode) { return mode == DISCARD_CORE_GIVEN_UP; }
|
||||
static int discard_core_call_away(int mode, int limit_on) {
|
||||
return mode == DISCARD_CORE_GIVEN_UP || (mode == DISCARD_CORE_AS_BEFORE && limit_on);
|
||||
}
|
||||
static int discard_core(void) {
|
||||
static int mode = -1;
|
||||
if (mode < 0) {
|
||||
mode = discard_core_rule(getenv("HOS_DISCARD_KEEP_CORE"));
|
||||
if (mode != DISCARD_CORE_AS_BEFORE)
|
||||
hos_log("mem: a thread %s while it discards freed memory (HOS_DISCARD_KEEP_CORE)",
|
||||
mode == DISCARD_CORE_GIVEN_UP ? "gives its virtual core up for the whole of a freed region's discard" : "keeps its virtual core");
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
if (!hos_guest_range(addr, size)) return; /* (hookshot: guest pages only; Linux's discard zeroes what it is given) */
|
||||
const uint64_t lo = (addr + 0xFFF) & ~0xFFFULL, hi = (addr + size) & ~0xFFFULL;
|
||||
@@ -181,12 +234,16 @@ static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
(QueryMemoryInfo above), with the game's frames waiting on it (Hyrule Field 34 fps, Castle Town 15). Zeroed: the pages
|
||||
read as zero, as freed pages; the census keeps them for the give-back once no import holds them) */
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
|
||||
{
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo);
|
||||
const int held = rx_guest_imported(lo, hi);
|
||||
if (held) memset(GP(lo), 0, (size_t)(hi - lo)), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
if (held) hos_guest_fill(lo, 0, hi - lo), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo); /* ((Triforce, 11-22, MEM-13) marked with HOS_MEM_ZERO_MARK=1) */
|
||||
hos_guest_io_end(io);
|
||||
if (held) return; /* (lab tour: 5.2 GB zeroed in 0.47 s in all) */
|
||||
if (held) { /* (lab tour: 5.2 GB zeroed in 0.47 s in all) */
|
||||
stall_host_end(STALL_HOST_ZERO, stall0, hi - lo);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* (Triforce, the census: let go and still committed. Not the large pages above, left as they were, nor Linux's below,
|
||||
noted zeroed or decommitted there) */
|
||||
@@ -203,9 +260,11 @@ static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
in which the thread runs no guest code and cannot see a better thread's ask for its core (hos_thread.c vc_kick). Held to
|
||||
its memory (HOS_MEM_LIMIT) the game frees regions by the hundred while an area loads, on its Resource Memory thread.
|
||||
Only with the limit on, where it was measured: without it the game frees few regions and the path is upstream's) */
|
||||
const int away = hos_mem_limit() ? hos_vcore_away() : 0;
|
||||
/* ((Triforce, 11-22, OS-40) HOS_DISCARD_KEEP_CORE: 0 gives it up without the limit too, 1 never: discard_core above) */
|
||||
const int away = discard_core_call_away(discard_core(), hos_mem_limit() != 0) ? hos_vcore_away() : 0;
|
||||
if (!discard || discard(GP(lo), (SIZE_T)(hi - lo)) != ERROR_SUCCESS)
|
||||
VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_RESET, PAGE_READWRITE);
|
||||
stall_host_end(STALL_HOST_DISCARD, stall0, hi - lo); /* ((Triforce, OS-51) with its write-back wait, not the wait for the core) */
|
||||
if (away) hos_vcore_back();
|
||||
hos_guest_io_end(io);
|
||||
#elif !defined(__EMSCRIPTEN__)
|
||||
@@ -213,15 +272,18 @@ static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
/* (pages a dma-buf import holds stay where they are, zeroed: a hole punched under one would leave the GPU on
|
||||
the old pages and the game on new ones) */
|
||||
if (rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo)), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
if (rx_guest_imported(lo, hi)) hos_guest_fill(lo, 0, hi - lo), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo); /* ((Triforce, 11-22, MEM-13) as above) */
|
||||
else hos_posix_guest_discard(GP(lo), (size_t)(hi - lo)), hos_mem_note(HOS_MEM_DECOMMITTED, lo, hi - lo);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int64_t region_frees_bare; /* ((Triforce) regions freed without a discard: hos_region_discard_needed; the refusal line's) */
|
||||
/* ((Triforce, 11-22, OS-40) the frees that gave their core up for the discard (HOS_DISCARD_KEEP_CORE=0), and how long the
|
||||
longest waited for its core back; a line every 1,000) */
|
||||
static int64_t region_frees_away, region_back_longest_ns;
|
||||
void hos_region_free(uint64_t addr) {
|
||||
uint64_t freed_size = 0;
|
||||
int game = 0;
|
||||
int game = 0, away = 0;
|
||||
LOCK();
|
||||
for (int i = 0; i < nregions; i++) {
|
||||
if (regions[i].addr == addr && !regions[i].freeing) {
|
||||
@@ -244,6 +306,9 @@ void hos_region_free(uint64_t addr) {
|
||||
region is discarded, as upstream does) */
|
||||
const uint64_t game_pages = addr + freed_size <= GC_SPACE ? gc_count(&census, GC_GAME, addr, addr + freed_size) : ~0ULL;
|
||||
if (hos_region_discard_needed(game && hos_mem_limit(), hos_mem_census_on(), game_pages)) {
|
||||
/* ((Triforce, 11-22, OS-40) HOS_DISCARD_KEEP_CORE=0: the core given up from here, with the region still listed and
|
||||
marked, and taken back below once it is unlisted. A bare free, which makes no kernel call, keeps it) */
|
||||
if (discard_core_region_away(discard_core())) away = hos_vcore_away();
|
||||
discard_pages(addr, freed_size);
|
||||
} else {
|
||||
hos_mem_note(HOS_MEM_DISCARDED, addr, freed_size);
|
||||
@@ -258,6 +323,20 @@ void hos_region_free(uint64_t addr) {
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
/* ((Triforce, 11-22, OS-40) the core back, after the lock is let go: a thread that queued for its core with the lock
|
||||
held could wait on one that holds a core and waits for the lock) */
|
||||
if (away) {
|
||||
const int64_t t0 = hos_now_ns();
|
||||
hos_vcore_back();
|
||||
const int64_t waited = hos_now_ns() - t0;
|
||||
int64_t longest = __atomic_load_n(®ion_back_longest_ns, __ATOMIC_RELAXED);
|
||||
while (waited > longest &&
|
||||
!__atomic_compare_exchange_n(®ion_back_longest_ns, &longest, waited, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {}
|
||||
const int64_t n = __atomic_add_fetch(®ion_frees_away, 1, __ATOMIC_RELAXED);
|
||||
if (n % 1000 == 0)
|
||||
hos_log("mem: %lld freed regions discarded with the thread's virtual core given up; the longest waited %.1f ms for it "
|
||||
"back (HOS_DISCARD_KEEP_CORE)", (long long)n, __atomic_exchange_n(®ion_back_longest_ns, 0, __ATOMIC_RELAXED) / 1e6);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- nn::os memory -------------------------------------------------------------------------------------- */
|
||||
@@ -299,7 +378,24 @@ HLE(_ZN2nn2os17SetMemoryHeapSizeEm) {
|
||||
for (uint64_t a = HOS_HEAP_BASE + hos_heap_size; a < HOS_HEAP_BASE + size; a += 4096) *(volatile uint8_t *)GP(a) = 0;
|
||||
hos_log("heap: %llu MB prefaulted in %.0f ms", (unsigned long long)((size - hos_heap_size) >> 20), (hos_now_ns() - t0) / 1e6);
|
||||
}
|
||||
if (size > hos_heap_size) hos_commit(HOS_HEAP_BASE + hos_heap_size, size - hos_heap_size);
|
||||
/* ((Triforce, 11-21, the performance audit's MEM-09, after Source A's) Windows: the heap is left reserved and committed
|
||||
as it is touched (mkw_glue.c commit_on_touch), not all at once: the game asks for ~6.9 GB and touches a part of it, and
|
||||
committed, all of it counted against RAM plus the page file. heap_commit.h has the rule and what it needed first.
|
||||
HOS_HEAP_COMMIT=1: whole, as before; and in large pages (HOS_LARGE_PAGES), reserved committed) */
|
||||
{
|
||||
extern uint64_t hos_large_hi;
|
||||
#if defined(_WIN32)
|
||||
const int windows = 1;
|
||||
#else
|
||||
const int windows = 0;
|
||||
#endif
|
||||
const int whole = heap_commit_whole(getenv("HOS_HEAP_COMMIT"), hos_large_hi != 0, windows);
|
||||
if (size > hos_heap_size && whole) hos_commit(HOS_HEAP_BASE + hos_heap_size, size - hos_heap_size);
|
||||
if (size > hos_heap_size && !whole) {
|
||||
hos_heap_on_touch = 1; /* (the kernel calls on guest memory touch their buffers first: hos_guest_touch) */
|
||||
hos_log("heap: left reserved, committed as it is touched (HOS_HEAP_COMMIT=1: all of it now, as before)");
|
||||
}
|
||||
}
|
||||
hos_heap_size = size;
|
||||
hos_log("heap: %llu MB at %#llx", (unsigned long long)(size >> 20), (unsigned long long)HOS_HEAP_BASE);
|
||||
RET(0);
|
||||
@@ -433,9 +529,11 @@ HLE(_ZN2nn2os15FreeMemoryPagesEmm) {
|
||||
still maps them, and decommitted pages under an import faulted the device. Read as zero, as freed pages) */
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
/* (Triforce: zeroed or decommitted: the lazy write-back's pages there first, rx_backend.c) */
|
||||
const int64_t stall0 = hi > lo ? stall_host_begin() : 0; /* ((Triforce, OS-51) the host's part of a long frame) */
|
||||
const int io = hi > lo ? hos_guest_io_begin(GP(lo), hi - lo) : 0;
|
||||
/* ((Triforce) the census: zeroed pages stay committed, kept for the give-back once no import holds them) */
|
||||
if (hi > lo && rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo)), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
/* ((Triforce, 11-22, MEM-13) hos_guest_fill: the zeros marked written with HOS_MEM_ZERO_MARK=1) */
|
||||
if (hi > lo && rx_guest_imported(lo, hi)) hos_guest_fill(lo, 0, hi - lo), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
#if defined(_WIN32)
|
||||
else if (hi > lo) VirtualFree(GP(lo), (SIZE_T)(hi - lo), MEM_DECOMMIT), hos_mem_note(HOS_MEM_DECOMMITTED, lo, hi - lo);
|
||||
#else
|
||||
@@ -446,6 +544,7 @@ HLE(_ZN2nn2os15FreeMemoryPagesEmm) {
|
||||
}
|
||||
#endif
|
||||
hos_guest_io_end(io);
|
||||
stall_host_end(STALL_HOST_DECOMMIT, stall0, hi > lo ? hi - lo : 0);
|
||||
RET(0);
|
||||
return;
|
||||
}
|
||||
@@ -736,7 +835,9 @@ int64_t hos_mem_give_back(uint64_t max_bytes) {
|
||||
uint64_t runs = 0, refused = 0;
|
||||
/* (from 4 GB up: below it AllocateMemoryPages zeroes pages without committing them, and a host call handed one that was
|
||||
given back would fail on it without a fault) */
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
|
||||
const uint64_t given = gc_give_back(&census, 1ULL << 32, &cursor, 16ULL << 20, max_bytes, 64, census_offer, NULL, &runs, &refused);
|
||||
stall_host_end(STALL_HOST_GIVE_BACK, stall0, given);
|
||||
census_given += given, census_given_runs += runs, census_refused += refused;
|
||||
return census_unsupported && !given ? -1 : (int64_t)given;
|
||||
}
|
||||
|
||||
75
host/hos_sim_cpu.h
Normal file
75
host/hos_sim_cpu.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/* (Triforce, 11-22, the performance audit's OS-49, after Source B's) A lab switch: the process held to fewer of the PC's
|
||||
cores, so that a lab PC plays as one with fewer does: the game's threads, and the renderer's pools.
|
||||
|
||||
HOS_SIM_CPU=<cores>/<threads> ("6/12"; "6" alone: as many threads as cores). The process's affinity becomes <cores> of
|
||||
the PC's physical cores (hos_sim_cpu_mask): the ones on the die with the largest L3 first where there is one (hos_x3d.h),
|
||||
the fastest class first, the last ones first, so that the core kept for the replay (the last of the fastest class,
|
||||
hos_thread.c last_fastest_core) is among them; at least one of the PC's cores always stays out. The renderer sizes its
|
||||
pools by <threads> (gpu/vk/cpu_count.h): an affinity mask alone does not change what std::thread::hardware_concurrency
|
||||
answers. Set in host/hos_main.c before any thread (hos_thread.c hos_sim_cpu_apply). Windows only. Unset, or a value that
|
||||
cannot be read or asks for all of the PC's cores: nothing changes, and the variable is cleared so the renderer agrees.
|
||||
|
||||
The rules alone, plain C: tests/test_sim_cores.py compiles them. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int cores, threads;
|
||||
} HosSimCpu;
|
||||
|
||||
/* 1 and *out for "<cores>/<threads>" or "<cores>": each 1 to 256, threads not under cores; else 0 */
|
||||
static inline int hos_sim_cpu_parse(const char *e, HosSimCpu *out) {
|
||||
if (!e || !*e) return 0;
|
||||
char *end = NULL;
|
||||
const long c = strtol(e, &end, 10);
|
||||
if (end == e) return 0;
|
||||
long t = c;
|
||||
if (*end == '/') {
|
||||
const char *s = end + 1;
|
||||
t = strtol(s, &end, 10);
|
||||
if (end == s) return 0;
|
||||
}
|
||||
if (*end || c < 1 || c > 256 || t < c || t > 256) return 0;
|
||||
out->cores = (int)c, out->threads = (int)t;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The process's mask: `want` of the n cores listed (masks[i]: a core's logical processors; classes[i]: its efficiency class,
|
||||
higher is faster), the ones on `die` first when die is not 0, then the fastest class, then the last ones (the highest
|
||||
processors). 0 when want is not from 1 to n - 1: one of the PC's cores always stays out */
|
||||
static inline uint64_t hos_sim_cpu_mask(const uint64_t *masks, const unsigned char *classes, int n, uint64_t die, int want) {
|
||||
if (n > 256 || want < 1 || want > n - 1) return 0;
|
||||
unsigned char taken[256] = {0};
|
||||
uint64_t mask = 0;
|
||||
for (int k = 0; k < want; k++) {
|
||||
int best = -1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (taken[i]) continue;
|
||||
if (best < 0) {
|
||||
best = i;
|
||||
continue;
|
||||
}
|
||||
const int on_i = die && (masks[i] & die), on_best = die && (masks[best] & die);
|
||||
if (on_i != on_best) {
|
||||
if (on_i) best = i;
|
||||
continue;
|
||||
}
|
||||
if (classes[i] != classes[best]) {
|
||||
if (classes[i] > classes[best]) best = i;
|
||||
continue;
|
||||
}
|
||||
if (masks[i] > masks[best]) best = i;
|
||||
}
|
||||
taken[best] = 1;
|
||||
mask |= masks[best];
|
||||
}
|
||||
return mask;
|
||||
}
|
||||
|
||||
/* The count of logical processors in a mask (vc_spin_max sizes by it while the switch holds the process) */
|
||||
static inline int hos_sim_cpu_count(uint64_t mask) {
|
||||
int n = 0;
|
||||
for (; mask; mask &= mask - 1) n++;
|
||||
return n;
|
||||
}
|
||||
@@ -357,6 +357,8 @@ static void query_memory(uint64_t out, uint64_t addr) {
|
||||
#define CUR_THREAD 0xFFFF8000u
|
||||
static HosThread *thread_handle(uint32_t h) { return h == CUR_THREAD ? hos_self() : hos_thread_by_handle(h); }
|
||||
|
||||
int hos_core_count_on(void);
|
||||
void hos_core_count_svc(void);
|
||||
void a64_svc(Ctx *c, uint32_t imm) {
|
||||
hos_vcore_checkpoint();
|
||||
switch (imm) {
|
||||
@@ -445,6 +447,7 @@ void a64_svc(Ctx *c, uint32_t imm) {
|
||||
return;
|
||||
}
|
||||
case 0x10: /* GetCurrentProcessorNumber */
|
||||
if (hos_core_count_on()) hos_core_count_svc(); /* (TICK-062 A10, HOS_CORE_COUNT) */
|
||||
c->x[0] = (uint32_t)hos_thread_core(hos_self());
|
||||
return;
|
||||
case 0x1E: /* GetSystemTick */
|
||||
@@ -503,8 +506,8 @@ void a64_svc(Ctx *c, uint32_t imm) {
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
const int io = hos_guest_io_begin(GP(c->x[0]), c->x[1]); /* (Triforce: the lazy write-back's pages first: rx_backend.c) */
|
||||
/* ((Triforce) the census: zeroed pages stay committed, kept for the give-back once no import holds them) */
|
||||
if (rx_guest_imported(c->x[0], c->x[0] + c->x[1]))
|
||||
memset(GP(c->x[0]), 0, (size_t)c->x[1]), hos_mem_note(HOS_MEM_ZEROED, c->x[0], c->x[1]);
|
||||
if (rx_guest_imported(c->x[0], c->x[0] + c->x[1])) /* ((Triforce, 11-22, MEM-13) the zeros marked written with HOS_MEM_ZERO_MARK=1) */
|
||||
hos_guest_fill(c->x[0], 0, c->x[1]), hos_mem_note(HOS_MEM_ZEROED, c->x[0], c->x[1]);
|
||||
else VirtualFree(GP(c->x[0]), (SIZE_T)c->x[1], MEM_DECOMMIT), hos_mem_note(HOS_MEM_DECOMMITTED, c->x[0], c->x[1]);
|
||||
hos_guest_io_end(io);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
+0x1F8, so lifted SDK code that reads it inline finds the same thread the HLE knows. */
|
||||
#include "hos.h"
|
||||
#include "hos_x3d.h"
|
||||
#include "hos_sim_cpu.h" /* ((Triforce, 11-22, OS-49) HOS_SIM_CPU, a lab switch: fewer of the PC's cores */
|
||||
#include "pace_wait.h" /* ((Triforce) hos_sleep_until_ns: the frame wait to its time) */
|
||||
#include "short_sleep.h" /* ((Triforce, 11-21) how sleeps under a millisecond wait, by the PC's processors) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -29,6 +31,7 @@ struct HosThread {
|
||||
uint64_t core_mask;
|
||||
uint64_t tls_slots[HOS_TLS_SLOTS];
|
||||
char name[48];
|
||||
int serial; /* ((Triforce, OS-33) a thread the frame waits on, by its name: short_sleep.h short_sleep_serial_name) */
|
||||
HANDLE handle;
|
||||
int started;
|
||||
int bg_kind; int64_t bg_since; /* (HOS_BG_THROTTLE: bg_throttle) */
|
||||
@@ -40,6 +43,8 @@ struct HosThread {
|
||||
int vc_held, vc_last, vc_queued;
|
||||
volatile int vc_kick;
|
||||
int64_t vc_since, vc_grant_at;
|
||||
/* (HOS_CORE_COUNT, TICK-062 A10) calls that ask for the core number (HLE, svc 0x10), waits for a virtual core and their time */
|
||||
int64_t cc_hle, cc_svc, cc_vcw, cc_vcw_ns;
|
||||
CONDITION_VARIABLE vc_cv;
|
||||
/* (hookshot) fibers: the context running now (NULL: ctx, the thread's own), the thread's host fiber once it
|
||||
switched to a guest fiber */
|
||||
@@ -49,6 +54,7 @@ struct HosThread {
|
||||
|
||||
/* The SDK's own mutexes (InternalCriticalSection) tag a lock word with the owner's handle, read from
|
||||
ThreadType+0x1B0 through TLR+0x1F8 (sdk+0x13186c). Every thread gets a unique handle there. */
|
||||
static void thread_gen_bump(void);
|
||||
#define TT_HANDLE 0x1B0
|
||||
static volatile LONG next_handle = 0x10000;
|
||||
uint32_t hos_thread_handle(HosThread *t) { return t->kernel_handle; }
|
||||
@@ -59,10 +65,13 @@ void hos_thread_adopt_guest(HosThread *t) {
|
||||
uint64_t tt = G64(t->tlr + 0x1F8);
|
||||
if (tt && tt != t->guest_tt) {
|
||||
t->guest_tt = tt;
|
||||
thread_gen_bump();
|
||||
G32(tt + TT_HANDLE) = t->kernel_handle;
|
||||
}
|
||||
}
|
||||
int hos_thread_prio(HosThread *t) { return t->priority; }
|
||||
/* ((Triforce, 11-22, the performance audit's OS-33) set where the thread is named, read by its own short sleeps) */
|
||||
int hos_thread_serial(HosThread *t) { return __atomic_load_n(&t->serial, __ATOMIC_RELAXED); }
|
||||
|
||||
int hos_trace;
|
||||
extern const uint64_t hos_thread_start_hook, hos_thread_exit_hook; /* gen/hos_image.c */
|
||||
@@ -136,7 +145,12 @@ int hos_thread_priority(HosThread *t, int set, int value) {
|
||||
return old;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
||||
/* (Triforce, 11-22, the performance audit's THREAD-56, after Source A's) The log's writer thread on Linux too: it was
|
||||
built for Windows only, so on Linux and the Steam Deck every hos_log wrote stderr in the thread that logged, a game
|
||||
thread with its virtual core held (Source A saw a 717 ms frame: the audio thread waited 321 ms for a core whose
|
||||
thread sat in such a write). Everything the writer calls is in the POSIX shim (host/posix/windows.h). Only the web
|
||||
build has no writer. HOS_LOG_SYNC=1: each line written by the thread that logs it, as Linux did */
|
||||
#if !defined(__EMSCRIPTEN__)
|
||||
static SRWLOCK log_lock = SRWLOCK_INIT, log_write_lock = SRWLOCK_INIT;
|
||||
static CONDITION_VARIABLE log_cv = CONDITION_VARIABLE_INIT;
|
||||
static char *log_q[2048];
|
||||
@@ -271,6 +285,10 @@ static int log_queue(const char *line) {
|
||||
if (on && !CreateThread(NULL, 0, log_main, NULL, 0, NULL)) on = 0;
|
||||
if (on) log_prev_filter = SetUnhandledExceptionFilter(log_crash_filter);
|
||||
if (on) atexit(log_flush);
|
||||
#ifndef _WIN32
|
||||
/* ((Triforce, 11-22) THREAD-56: the shim's ExitProcess is quick_exit, which runs these and not atexit's) */
|
||||
if (on) at_quick_exit(log_flush);
|
||||
#endif
|
||||
log_async = on;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&log_lock);
|
||||
@@ -398,6 +416,7 @@ static void setup_guest(HosThread *t) {
|
||||
static void track(HosThread *t) {
|
||||
AcquireSRWLockExclusive(&threads_lock);
|
||||
if (nthreads < 1024) threads[nthreads++] = t;
|
||||
thread_gen_bump();
|
||||
ReleaseSRWLockExclusive(&threads_lock);
|
||||
}
|
||||
|
||||
@@ -483,7 +502,15 @@ void hos_thread_wait(HosThread *t, int64_t timeout_ns, int *timed_out) {
|
||||
}
|
||||
void hos_thread_wake(HosThread *t) { SetEvent(t->wake); }
|
||||
|
||||
HosThread *hos_thread_of(uint64_t guest_tt) {
|
||||
/* TICK-062 B2: hos_thread_of is a linear scan of every thread under a shared lock. HOS_THREAD_OF_CACHE=1: each thread keeps
|
||||
a few (ThreadType, thread) pairs, valid while thread_gen has not changed. thread_gen is bumped by everything that can
|
||||
change what the scan returns: a thread added (track) or removed (hos_thread_destroy), or a guest_tt rewritten (adopt).
|
||||
A reader takes the generation BEFORE the scan and stores the pair under it: a change during the scan leaves the pair
|
||||
stale at once, never served. The answer is then the scan's own (the first matching thread in the table), and a cached
|
||||
pointer to a destroyed thread cannot be served (destroy bumps the generation before the free). */
|
||||
static volatile LONG thread_gen;
|
||||
static void thread_gen_bump(void) { InterlockedIncrement(&thread_gen); }
|
||||
static HosThread *thread_of_scan(uint64_t guest_tt) {
|
||||
HosThread *r = NULL;
|
||||
AcquireSRWLockShared(&threads_lock);
|
||||
for (int i = 0; i < nthreads; i++)
|
||||
@@ -491,6 +518,18 @@ HosThread *hos_thread_of(uint64_t guest_tt) {
|
||||
ReleaseSRWLockShared(&threads_lock);
|
||||
return r;
|
||||
}
|
||||
HosThread *hos_thread_of(uint64_t guest_tt) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = !(getenv("HOS_THREAD_OF_CACHE") && atoi(getenv("HOS_THREAD_OF_CACHE")) == 0); /* ((Triforce) on by default since 11-21, lab +5.8%; =0: off */
|
||||
if (!on) return thread_of_scan(guest_tt);
|
||||
static _Thread_local struct { uint64_t tt; HosThread *t; LONG gen; } cache[8];
|
||||
const unsigned i = (unsigned)((guest_tt >> 4) * 0x9E3779B1u >> 7) & 7;
|
||||
const LONG g = *(volatile LONG *)&thread_gen;
|
||||
if (cache[i].tt == guest_tt && cache[i].gen == g && cache[i].t) return cache[i].t;
|
||||
HosThread *r = thread_of_scan(guest_tt);
|
||||
cache[i].tt = guest_tt, cache[i].t = r, cache[i].gen = g;
|
||||
return r;
|
||||
}
|
||||
|
||||
Ctx *hos_boot_main_thread(uint64_t stack_size) {
|
||||
HosThread *t = (HosThread *)calloc(1, sizeof *t);
|
||||
@@ -502,6 +541,7 @@ Ctx *hos_boot_main_thread(uint64_t stack_size) {
|
||||
t->ideal_core = 0;
|
||||
t->started = 1;
|
||||
snprintf(t->name, sizeof t->name, "MainThread");
|
||||
__atomic_store_n(&t->serial, short_sleep_serial_name(t->name), __ATOMIC_RELAXED);
|
||||
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &t->handle, 0, FALSE,
|
||||
DUPLICATE_SAME_ACCESS); /* a real handle, for the profiler */
|
||||
SetThreadDescription(GetCurrentThread(), L"MainThread"); /* (HOS_SAMPLE finds threads by it) */
|
||||
@@ -604,6 +644,7 @@ void hos_thread_destroy(HosThread *t) {
|
||||
AcquireSRWLockExclusive(&threads_lock);
|
||||
for (int i = 0; i < nthreads; i++)
|
||||
if (threads[i] == t) { threads[i] = threads[--nthreads]; break; }
|
||||
thread_gen_bump();
|
||||
ReleaseSRWLockExclusive(&threads_lock);
|
||||
if (t->handle) CloseHandle(t->handle);
|
||||
if (t->wake) CloseHandle(t->wake);
|
||||
@@ -612,6 +653,7 @@ void hos_thread_destroy(HosThread *t) {
|
||||
|
||||
void hos_thread_set_name(HosThread *t, const char *name) {
|
||||
snprintf(t->name, sizeof t->name, "%s", name);
|
||||
__atomic_store_n(&t->serial, short_sleep_serial_name(t->name), __ATOMIC_RELAXED);
|
||||
if (t->handle) {
|
||||
wchar_t w[48];
|
||||
MultiByteToWideChar(CP_UTF8, 0, t->name, -1, w, 48);
|
||||
@@ -680,7 +722,10 @@ static uint64_t pick_reserved_core(const uint64_t *masks, const unsigned char *c
|
||||
finds such a die; everywhere else 0, and nothing changes. HOS_X3D=0: off there too. Other threads of the process (the
|
||||
driver's, the shader workers) stay where Windows puts them. Windows only: Linux as before.
|
||||
NX2navi keeps the whole process on the die and reserves no core there; here the replay's core stays, on the die, as
|
||||
the replay is pinned to it by default (gpu/vk/vk_replay.cpp ReplayPin). HOS_RESERVE_CORE=0 keeps none, as ever */
|
||||
the replay is pinned to it by default (gpu/vk/vk_replay.cpp ReplayPin). HOS_RESERVE_CORE=0 keeps none, as ever.
|
||||
(Triforce, 11-22, the performance audit's THREAD-49, after Source A's) That holds where the die's cores have two
|
||||
logical processors each. With SMT off no core of the die is kept unless HOS_RESERVE_CORE=1 asks for one or the replay
|
||||
has a thread of its own (RX_VK_ASYNC=1): hos_x3d.h hos_x3d_keep_replay_core has the rule and Source A's numbers */
|
||||
#if defined(_WIN32)
|
||||
/* (hos_x3d_pick over what Windows lists for the calling thread's processor group) */
|
||||
static uint64_t x3d_detect(HosX3d *out) {
|
||||
@@ -738,7 +783,7 @@ static uint64_t hos_x3d_cpus(void) {
|
||||
(unsigned long long)x.die, (unsigned long long)(x.big >> 20), (unsigned long long)(x.small >> 20), x.cores,
|
||||
x.why == HOS_X3D_FEW ? "fewer than 8, so the game's threads are not kept there"
|
||||
: off ? "not used (HOS_X3D=0)"
|
||||
: "the game's threads and the replay's core are kept there (HOS_X3D=0: as before)");
|
||||
: "the game's threads are kept there, and the replay's core when one is kept (HOS_X3D=0: as before)");
|
||||
#endif
|
||||
}
|
||||
const uint64_t m = mask;
|
||||
@@ -746,6 +791,8 @@ static uint64_t hos_x3d_cpus(void) {
|
||||
return m;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-22, the performance audit's OS-49) the mask HOS_SIM_CPU holds the process to, 0: none (hos_sim_cpu.h) */
|
||||
static uint64_t sim_cpu_mask;
|
||||
#if defined(_WIN32)
|
||||
/* (pick_reserved_core over the calling thread's group's cores; *cores: how many, 0 when they could not be read) */
|
||||
static uint64_t last_fastest_core(int *cores) {
|
||||
@@ -765,7 +812,8 @@ static uint64_t last_fastest_core(int *cores) {
|
||||
const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(buf + at);
|
||||
if (!e->Size) break;
|
||||
if (e->Relationship == RelationProcessorCore && e->Processor.GroupCount && e->Processor.GroupMask[0].Group == group &&
|
||||
(!die || ((uint64_t)e->Processor.GroupMask[0].Mask & die))) {
|
||||
(!die || ((uint64_t)e->Processor.GroupMask[0].Mask & die)) &&
|
||||
(!sim_cpu_mask || ((uint64_t)e->Processor.GroupMask[0].Mask & sim_cpu_mask))) { /* ((Triforce, OS-49) inside it) */
|
||||
masks[n] = (uint64_t)e->Processor.GroupMask[0].Mask;
|
||||
classes[n] = e->Processor.EfficiencyClass;
|
||||
n++;
|
||||
@@ -777,15 +825,72 @@ static uint64_t last_fastest_core(int *cores) {
|
||||
return pick_reserved_core(masks, classes, n);
|
||||
}
|
||||
#endif
|
||||
int rx_async_replay(void); /* (host/rx_backend.c: the replay on a thread of its own, RX_VK_ASYNC=1) */
|
||||
/* ((Triforce, 11-22, the performance audit's OS-49, after Source B's) HOS_SIM_CPU=<cores>/<threads>, a lab switch: the process
|
||||
held to that many of the PC's cores (hos_sim_cpu.h), from host/hos_main.c before any thread. Not applied (unset, a value that
|
||||
cannot be read, as many cores as the PC has, or the call refused): the variable is cleared, so that the renderer's pools
|
||||
(gpu/vk/cpu_count.h) size by the whole PC too. Windows only: elsewhere the variable is left and the renderer reads it there
|
||||
as nothing */
|
||||
void hos_sim_cpu_apply(void) {
|
||||
#if defined(_WIN32)
|
||||
const char *e = getenv("HOS_SIM_CPU");
|
||||
if (!e || !*e) return;
|
||||
HosSimCpu s;
|
||||
uint64_t mask = 0;
|
||||
int n = 0;
|
||||
if (hos_sim_cpu_parse(e, &s)) {
|
||||
static uint64_t masks[256];
|
||||
static unsigned char classes[256];
|
||||
GROUP_AFFINITY ga;
|
||||
const WORD group = GetThreadGroupAffinity(GetCurrentThread(), &ga) ? ga.Group : 0;
|
||||
DWORD len = 0;
|
||||
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, NULL, &len) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
||||
char *buf = (char *)malloc(len);
|
||||
if (buf && GetLogicalProcessorInformationEx(RelationProcessorCore, (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)buf, &len))
|
||||
for (DWORD at = 0; at < len && n < 256;) {
|
||||
const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *x = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(buf + at);
|
||||
if (!x->Size) break;
|
||||
if (x->Relationship == RelationProcessorCore && x->Processor.GroupCount && x->Processor.GroupMask[0].Group == group) {
|
||||
masks[n] = (uint64_t)x->Processor.GroupMask[0].Mask;
|
||||
classes[n] = x->Processor.EfficiencyClass;
|
||||
n++;
|
||||
}
|
||||
at += x->Size;
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
mask = hos_sim_cpu_mask(masks, classes, n, hos_x3d_cpus(), s.cores);
|
||||
if (mask && !SetProcessAffinityMask(GetCurrentProcess(), (DWORD_PTR)mask)) mask = 0;
|
||||
}
|
||||
if (!mask) {
|
||||
hos_log("cpu: HOS_SIM_CPU=%s not applied (<cores>/<threads>, fewer cores than the %d here): the whole PC, as without it", e, n);
|
||||
SetEnvironmentVariableA("HOS_SIM_CPU", NULL);
|
||||
_putenv("HOS_SIM_CPU=");
|
||||
return;
|
||||
}
|
||||
sim_cpu_mask = mask;
|
||||
hos_log("cpu: HOS_SIM_CPU=%s (a lab switch): the process held to logical processors %#llx, %d of the %d cores; the renderer's "
|
||||
"pools sized for %d threads", e, (unsigned long long)mask, s.cores, n, s.threads);
|
||||
#endif
|
||||
}
|
||||
uint64_t hos_reserved_cpus(void) {
|
||||
static uint64_t mask = ~0ull;
|
||||
if (mask != ~0ull) return mask;
|
||||
mask = 0;
|
||||
if (getenv("HOS_RESERVE_CORE") && !atoi(getenv("HOS_RESERVE_CORE"))) return mask;
|
||||
const char *e = getenv("HOS_RESERVE_CORE");
|
||||
const int asked = e ? atoi(e) != 0 : -1; /* ((Triforce, THREAD-49) -1: not set) */
|
||||
if (!asked) return mask;
|
||||
#if defined(_WIN32)
|
||||
{
|
||||
int cores = 0;
|
||||
const uint64_t fast = last_fastest_core(&cores);
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-49, after Source A's) on the cache die of a two-die X3D with
|
||||
SMT off none is kept: hos_x3d.h. cores: the die's, where there is one) */
|
||||
const uint64_t die = hos_x3d_cpus();
|
||||
if (cores && fast && !hos_x3d_reserved(fast, die, cores, asked, rx_async_replay())) {
|
||||
hos_log("cpu: no core kept for the replay on the cache die (HOS_RESERVE_CORE=1: one is)");
|
||||
return mask;
|
||||
}
|
||||
if (cores) return mask = fast;
|
||||
}
|
||||
#endif
|
||||
@@ -853,19 +958,38 @@ int64_t hos_now_ns(void) {
|
||||
return (int64_t)((now.QuadPart / freq.QuadPart) * 1000000000LL + (now.QuadPart % freq.QuadPart) * 1000000000LL / freq.QuadPart);
|
||||
}
|
||||
|
||||
static void sleep_ns(int64_t ns);
|
||||
/* (virtual cores: a sleep gives the core up, SleepThread(0) offers it to a waiter as good) */
|
||||
void hos_sleep_ns(int64_t ns) {
|
||||
static void sleep_for(int64_t ns, int guest);
|
||||
/* ((Triforce, 11-22, the performance audit's OS-33) the game threads yielding through a short sleep now, and the short
|
||||
sleeps that took the timer because the cap's worth already were: short_sleep.h, HOS_SHORT_SLEEP_POLLERS) */
|
||||
static int short_pollers;
|
||||
static int64_t short_capped;
|
||||
/* ((Triforce, 11-21, the performance audit's OS-31) the logical processors this process may run on (its affinity mask, as
|
||||
the game starts with it): Windows with one processor group; 0, not known (more than one group, where the mask is one
|
||||
group's and not the process's, or elsewhere) */
|
||||
static int process_threads(void) {
|
||||
int threads = 0;
|
||||
#if defined(_WIN32)
|
||||
DWORD_PTR proc = 0, sys = 0;
|
||||
if (GetActiveProcessorGroupCount() == 1 && GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys))
|
||||
threads = __builtin_popcountll((uint64_t)proc);
|
||||
#endif
|
||||
return threads;
|
||||
}
|
||||
/* (virtual cores: a sleep gives the core up, SleepThread(0) offers it to a waiter as good) guest: the game's own sleep */
|
||||
static void sleep_away(int64_t ns, int guest) {
|
||||
if (ns <= 0) {
|
||||
hos_vcore_yield();
|
||||
SwitchToThread();
|
||||
return;
|
||||
}
|
||||
const int away = hos_vcore_away();
|
||||
sleep_ns(ns);
|
||||
sleep_for(ns, guest);
|
||||
if (away) hos_vcore_back();
|
||||
}
|
||||
static void sleep_ns(int64_t ns) {
|
||||
void hos_sleep_ns(int64_t ns) { sleep_away(ns, 0); }
|
||||
/* (the host's own sleep, as pace_wait_until takes it) */
|
||||
static void sleep_ns(int64_t ns) { sleep_for(ns, 0); }
|
||||
static void sleep_for(int64_t ns, int guest) {
|
||||
/* A high-resolution waitable timer (one per thread) sleeps to within ~0.5 ms without spinning; Sleep() rounds to
|
||||
the timer resolution, so without one the last millisecond is spun (threads that sleep in short steps, the
|
||||
vibration thread for one, used 15% of a core spinning). */
|
||||
@@ -893,13 +1017,57 @@ static void sleep_ns(int64_t ns) {
|
||||
return;
|
||||
#endif
|
||||
/* Under a millisecond: yielded away until the time (the timer's ~0.5 ms granularity made TOTK's 20 us waits for
|
||||
its job workers 25x longer, ~2000 a second on the frame's critical path). HOS_SHORT_SLEEP_TIMER=1: as before */
|
||||
static int short_timer = -1;
|
||||
if (short_timer < 0) short_timer = getenv("HOS_SHORT_SLEEP_TIMER") != NULL;
|
||||
if (ns < 1000000 && !short_timer) {
|
||||
while (hos_now_ns() < until)
|
||||
if (!SwitchToThread()) YieldProcessor();
|
||||
return;
|
||||
its job workers 25x longer, ~2000 a second on the frame's critical path). ((Triforce, 11-21, OS-31) Not the game's
|
||||
own sleeps where the processors are few: short_sleep.h. HOS_SHORT_SLEEP_TIMER=1: the timer always; 0: never; not
|
||||
set: by the processors on Windows. Chosen once. (Triforce, 11-22, OS-33) The same word holds the cap on the game
|
||||
threads that yield at once, HOS_SHORT_SLEEP_POLLERS: 0, no limit, unless that switch asks for one) */
|
||||
static volatile LONG how_now = -1;
|
||||
int word = (int)InterlockedCompareExchange(&how_now, -1, -1);
|
||||
if (word < 0) {
|
||||
const int threads = process_threads();
|
||||
/* ((Triforce, 11-22, the performance audit's OS-32, after Source A's) off Windows the processors are not counted:
|
||||
with the switch not set, short_sleep.h's SHORT_SLEEP_ELSEWHERE (all yield, as before, until a Steam Deck A/B).
|
||||
HOS_SHORT_SLEEP_TIMER=game: the game's own on the timer at any count, on any system) */
|
||||
#if defined(_WIN32)
|
||||
const int windows = 1;
|
||||
#else
|
||||
const int windows = 0;
|
||||
#endif
|
||||
const int how = short_sleep_timer_os(getenv("HOS_SHORT_SLEEP_TIMER"), threads, windows, SHORT_SLEEP_ELSEWHERE);
|
||||
const int cap = short_sleep_pollers(getenv("HOS_SHORT_SLEEP_POLLERS"), getenv("HOS_SHORT_SLEEP_TIMER"), how, threads);
|
||||
word = short_sleep_word(how, cap);
|
||||
if (InterlockedCompareExchange(&how_now, word, -1) == -1) {
|
||||
hos_log("sleep: sleeps under 1 ms: %s (%d logical processors for this process%s; HOS_SHORT_SLEEP_TIMER=1: all on the "
|
||||
"timer, game: the game's own on the timer, 0: all yield%s)", how == SHORT_SLEEP_ALL_TIMER ? "all on the timer"
|
||||
: how == SHORT_SLEEP_GUEST_TIMER ? "the game's own on the timer, the host's yield" : "all yield until their time",
|
||||
threads, threads ? "" : ": not known",
|
||||
windows ? "" : SHORT_SLEEP_ELSEWHERE == SHORT_SLEEP_GUEST_TIMER ? "; not set, off Windows: the game's own on the timer"
|
||||
: "; not set, off Windows: all yield");
|
||||
if (cap)
|
||||
hos_log("sleep: at most %d game threads yield through a short sleep at once, the next waits on the timer; the "
|
||||
"main and presentation threads always yield (HOS_SHORT_SLEEP_POLLERS)", cap);
|
||||
} else word = (int)InterlockedCompareExchange(&how_now, -1, -1); /* (another thread chose first: its choice) */
|
||||
}
|
||||
const int how = short_sleep_how(word);
|
||||
if (short_sleep_yields(ns, how, guest)) {
|
||||
/* ((Triforce, OS-33) the cap's gate: a counted sleeper over the cap takes the timer below, its count given back) */
|
||||
int took;
|
||||
if (short_sleep_poll_enter(&short_pollers, short_sleep_cap(word), guest, cur ? hos_thread_serial(cur) : 0, &took)) {
|
||||
/* TICK-062 B4, HOS_SHORT_SLEEP_PAUSE=1: one yield, then a PAUSE loop to the deadline (SwitchToThread in a loop
|
||||
keeps calling into the kernel and bounces the SMT sibling's work) */
|
||||
static int pause_spin = -1;
|
||||
if (pause_spin < 0) pause_spin = !(getenv("HOS_SHORT_SLEEP_PAUSE") && atoi(getenv("HOS_SHORT_SLEEP_PAUSE")) == 0); /* ((Triforce) on by default since 11-21, lab +5.8%; =0: off */
|
||||
if (pause_spin) {
|
||||
if (hos_now_ns() < until) SwitchToThread();
|
||||
while (hos_now_ns() < until) { YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); }
|
||||
} else {
|
||||
while (hos_now_ns() < until)
|
||||
if (!SwitchToThread()) YieldProcessor();
|
||||
}
|
||||
short_sleep_poll_leave(&short_pollers, took);
|
||||
return;
|
||||
}
|
||||
__atomic_add_fetch(&short_capped, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
if (!tried) {
|
||||
tried = 1;
|
||||
@@ -988,13 +1156,56 @@ HLE(_ZN2nn2os20ChangeThreadPriorityEPNS0_10ThreadTypeEi) {
|
||||
RET((uint32_t)hos_thread_priority(thread_arg(c, ARG(0)), 1, (int)ARG(1)));
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's GAME-22, after Source B's) What the game asks for, in the log: a mask that
|
||||
names two or more cores, which is what HOS_VCORE_WIDE=1 widens (vc_allowed), always; every mask when that switch is set
|
||||
to anything or with the trace. Whether the switch can do anything in a game is read here: with no such line no thread
|
||||
has a mask of more than one core and it changes nothing. The first 64 */
|
||||
static void core_mask_note(HosThread *t, int ideal, uint64_t mask) {
|
||||
static volatile LONG logged;
|
||||
static int every = -1;
|
||||
if (every < 0) every = getenv("HOS_VCORE_WIDE") != NULL;
|
||||
const int cores = hos_cores() > 8 ? 8 : hos_cores();
|
||||
const int named = __builtin_popcountll(mask & ((1ull << cores) - 1));
|
||||
if (named < 2 && !every && !hos_trace) return;
|
||||
if (InterlockedIncrement(&logged) > 64) return;
|
||||
hos_log("vcores: %s (priority %d) asks for ideal core %d and core mask %#llx, %d of the %d cores%s", t->name, t->priority,
|
||||
ideal, (unsigned long long)mask, named, cores, named > 1 ? " (HOS_VCORE_WIDE=1: any core)" : "");
|
||||
}
|
||||
/* nn::os::SetThreadCoreMask(ThreadType*, int idealCore, unsigned long mask) */
|
||||
HLE(_ZN2nn2os17SetThreadCoreMaskEPNS0_10ThreadTypeEim) {
|
||||
HosThread *t = thread_arg(c, ARG(0));
|
||||
hos_thread_affinity(t, (int)ARG(1), ARG(2));
|
||||
core_mask_note(t, (int)ARG(1), ARG(2));
|
||||
}
|
||||
|
||||
HLE(_ZN2nn2os20GetCurrentCoreNumberEv) { RET((uint32_t)hos_thread_core(cur)); }
|
||||
/* TICK-062 A10, HOS_CORE_COUNT=1 (diagnostic, no behaviour change): per guest thread, the calls that ask for the core number
|
||||
(nn::os::GetCurrentCoreNumber, svc 0x10) and the waits for a virtual core, logged every 5 s (cumulative) */
|
||||
int hos_core_count_on(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = getenv("HOS_CORE_COUNT") != NULL;
|
||||
return on;
|
||||
}
|
||||
void hos_core_count_svc(void) { if (cur) cur->cc_svc++; }
|
||||
static void core_count_log(void) {
|
||||
static volatile LONG64 last;
|
||||
const int64_t now = hos_now_ns();
|
||||
const int64_t l = last;
|
||||
if (now - l < 5000000000LL || InterlockedCompareExchange64(&last, now, l) != l) return;
|
||||
if (!l) return;
|
||||
AcquireSRWLockShared(&threads_lock);
|
||||
for (int i = 0; i < nthreads; i++) {
|
||||
const HosThread *t = threads[i];
|
||||
if (t->cc_hle + t->cc_svc + t->cc_vcw)
|
||||
hos_log("corecount: %-24s core-number calls %lld (hle %lld, svc %lld), virtual-core waits %lld (%.1f ms)", t->name,
|
||||
(long long)(t->cc_hle + t->cc_svc), (long long)t->cc_hle, (long long)t->cc_svc, (long long)t->cc_vcw,
|
||||
t->cc_vcw_ns / 1e6);
|
||||
}
|
||||
ReleaseSRWLockShared(&threads_lock);
|
||||
}
|
||||
HLE(_ZN2nn2os20GetCurrentCoreNumberEv) {
|
||||
if (hos_core_count_on()) { cur->cc_hle++; core_count_log(); }
|
||||
RET((uint32_t)hos_thread_core(cur));
|
||||
}
|
||||
HLE(_ZN2nn2os26GetThreadAvailableCoreMaskEv) { RET((1u << hos_cores()) - 1); } /* cores 0-2: what an application gets */
|
||||
|
||||
HLE(_ZN2nn2os19GetCurrentStackInfoEPmS1_) {
|
||||
@@ -1002,9 +1213,28 @@ HLE(_ZN2nn2os19GetCurrentStackInfoEPmS1_) {
|
||||
if (ARG(1)) G64(ARG(1)) = cur->stack_size;
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's THREAD-41, after Source A's) The game's job system yields after every job
|
||||
and actor callback, thousands of times a second. A yield is answered twice: hos_vcore_yield hands the virtual core to
|
||||
a waiting game thread of equal or better priority, which is the console's yield, and SwitchToThread then gives the
|
||||
processor up as well, a call into the kernel each time. Source A, with the game held to 4 processors, found half of
|
||||
its six executors' CPU time inside that call and dropped it (mean frame 0.26 to 0.34 ms shorter there, nothing with
|
||||
16); Windows still takes the processor from a thread at the end of its time slice.
|
||||
HOS_OS_YIELD=0 drops the call here. Off unless asked for: not set or 1, both answers as before. It changes how the
|
||||
game's threads wait and was not measured here, and a thread that yields in a loop for one waiting on its processor
|
||||
then keeps it to the end of its slice, the stall to watch with 2 to 4 processors. Without virtual cores (HOS_VCORES=0)
|
||||
the processor's yield is the only one, so it stays whatever the switch says. SleepThread(0) (sleep_away) and the
|
||||
short sleeps' yields are not this and keep theirs. Read once */
|
||||
static int os_yield(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
const char *e = getenv("HOS_OS_YIELD");
|
||||
on = e && *e ? atoi(e) != 0 : 1;
|
||||
}
|
||||
return on;
|
||||
}
|
||||
HLE(_ZN2nn2os11YieldThreadEv) {
|
||||
hos_vcore_yield();
|
||||
SwitchToThread();
|
||||
if (os_yield() || !hos_vcores_on()) SwitchToThread();
|
||||
}
|
||||
/* HOS_SLEEP_LOG=1 (diagnostic): guest sleeps by caller (LR) every 5 s: count, asked, slept */
|
||||
void hos_sleep_logged(int64_t ns, uint64_t lr) {
|
||||
@@ -1020,9 +1250,9 @@ void hos_sleep_logged(int64_t ns, uint64_t lr) {
|
||||
if (poll_cap && ns > poll_cap && ns <= 100000) ns = poll_cap;
|
||||
static int on = -1;
|
||||
if (on < 0) on = getenv("HOS_SLEEP_LOG") != NULL;
|
||||
if (!on) { hos_sleep_ns(ns); return; }
|
||||
if (!on) { sleep_away(ns, 1); return; } /* ((Triforce, 11-21, OS-31) the game's own sleep: short_sleep.h) */
|
||||
const int64_t t0 = hos_now_ns();
|
||||
hos_sleep_ns(ns);
|
||||
sleep_away(ns, 1);
|
||||
const int64_t took = hos_now_ns() - t0;
|
||||
static SRWLOCK lk = SRWLOCK_INIT;
|
||||
static struct { uint64_t lr; int64_t n, asked, slept; } tab[64];
|
||||
@@ -1044,6 +1274,11 @@ void hos_sleep_logged(int64_t ns, uint64_t lr) {
|
||||
hos_log("sleep: lr %#llx: %lld calls, asked %.2f ms, slept %.2f ms (%.1f us each)",
|
||||
(unsigned long long)tab[k].lr, (long long)tab[k].n, tab[k].asked / 1e6, tab[k].slept / 1e6,
|
||||
tab[k].slept / 1e3 / tab[k].n);
|
||||
/* ((Triforce, OS-33) the short sleeps that took the timer over the cap, HOS_SHORT_SLEEP_POLLERS) */
|
||||
const int64_t capped = __atomic_exchange_n(&short_capped, 0, __ATOMIC_RELAXED);
|
||||
if (capped)
|
||||
hos_log("sleep: %lld short sleeps took the timer, the cap's worth of game threads yielding already "
|
||||
"(HOS_SHORT_SLEEP_POLLERS)", (long long)capped);
|
||||
memset(tab, 0, sizeof tab);
|
||||
}
|
||||
ReleaseSRWLockExclusive(&lk);
|
||||
@@ -1342,6 +1577,10 @@ static struct {
|
||||
sleep (NX2navi's "core grant to run", runtime/kernel.c grant_at) */
|
||||
int64_t spun, slept, spin_ns, spin_capped, run_spin_ns, run_spin_n, run_sleep_ns, run_sleep_n, run_max_ns;
|
||||
} vc_st;
|
||||
/* ((Triforce, 11-22, the performance audit's OS-51) the waits since the last present, beside vc_st's 5 s: their time summed
|
||||
over the threads, how many, the longest. Added to with vc_lock held, taken by the present's look without it
|
||||
(hos_vcore_waits_take; host/stall_host.h has the line) */
|
||||
static struct { int64_t ns, n, longest; } vc_gap;
|
||||
|
||||
int hos_vcores_on(void) {
|
||||
static int on = -1;
|
||||
@@ -1364,7 +1603,17 @@ static unsigned vc_allowed(HosThread *t) {
|
||||
static int any = -1;
|
||||
if (any < 0) any = getenv("HOS_VCORE_ANY") && atoi(getenv("HOS_VCORE_ANY")) != 0;
|
||||
if (any) return all;
|
||||
/* (Triforce, 11-22, the performance audit's GAME-22, after Source B's) HOS_VCORE_WIDE=1: a thread whose mask names two
|
||||
or more cores takes any free core. Source B's reason: the game's two main threads may run on two of three cores
|
||||
only, where per-core job workers of better priority preempt them while the other cores stand idle a third of the
|
||||
time. A one-core mask, which is what guards the game's lock-free per-core data, stays as it is, and so does a
|
||||
thread with no mask. Off unless set: a widened thread runs under core numbers the console never gives it (the
|
||||
risk HOS_VCORE_ANY has for every thread), Source B never measured it, and neither was it here. Whether any thread
|
||||
has such a mask is in the log (core_mask_note). Read once */
|
||||
static int wide = -1;
|
||||
if (wide < 0) wide = getenv("HOS_VCORE_WIDE") && atoi(getenv("HOS_VCORE_WIDE")) != 0;
|
||||
unsigned m = (unsigned)(t->core_mask & all);
|
||||
if (wide && __builtin_popcount(m) > 1) return all;
|
||||
if (!m) m = 1u << hos_thread_core(t);
|
||||
return m;
|
||||
}
|
||||
@@ -1449,7 +1698,8 @@ static int vc_spin_max(void) {
|
||||
const char *e = getenv("HOS_VCORE_SPIN_MAX");
|
||||
SYSTEM_INFO si;
|
||||
GetSystemInfo(&si);
|
||||
const int n = e ? atoi(e) : vc_spin_most((int)si.dwNumberOfProcessors);
|
||||
/* ((Triforce, 11-22, OS-49) by the processors HOS_SIM_CPU leaves the process, when it holds it) */
|
||||
const int n = e ? atoi(e) : vc_spin_most(sim_cpu_mask ? hos_sim_cpu_count(sim_cpu_mask) : (int)si.dwNumberOfProcessors);
|
||||
most = n < 0 ? 0 : n;
|
||||
}
|
||||
return most;
|
||||
@@ -1568,6 +1818,8 @@ static void vc_acquire(HosThread *t) {
|
||||
const int64_t now = hos_now_ns(), waited = now - t0;
|
||||
vc_st.wait_ns += waited;
|
||||
if (waited > vc_st.max_ns) vc_st.max_ns = waited;
|
||||
__atomic_add_fetch(&vc_gap.ns, waited, __ATOMIC_RELAXED), __atomic_add_fetch(&vc_gap.n, 1, __ATOMIC_RELAXED); /* ((Triforce, OS-51)) */
|
||||
if (waited > __atomic_load_n(&vc_gap.longest, __ATOMIC_RELAXED)) __atomic_store_n(&vc_gap.longest, waited, __ATOMIC_RELAXED);
|
||||
if (ended) vc_st.spun++; /* ((Triforce) every wait ends in the spin or after a sleep) */
|
||||
else if (slept) vc_st.slept++;
|
||||
if (t->vc_grant_at) { /* ((Triforce) granted by a release: from the grant until it runs) */
|
||||
@@ -1577,6 +1829,7 @@ static void vc_acquire(HosThread *t) {
|
||||
if (run > vc_st.run_max_ns) vc_st.run_max_ns = run;
|
||||
t->vc_grant_at = 0;
|
||||
}
|
||||
t->cc_vcw++, t->cc_vcw_ns += waited; /* (HOS_CORE_COUNT) */
|
||||
vc_stats_locked(now);
|
||||
ReleaseSRWLockExclusive(&vc_lock);
|
||||
}
|
||||
@@ -1623,6 +1876,13 @@ void hos_vcore_back(void) {
|
||||
HosThread *t = cur;
|
||||
if (hos_vcores_on() && t && !t->vc_held) vc_acquire(t);
|
||||
}
|
||||
/* ((Triforce, 11-22, the performance audit's OS-51) the waits for a core since the last take: their summed time, how many,
|
||||
the longest; all left at zero. No lock: a wait that ends meanwhile is in this take or the next) */
|
||||
void hos_vcore_waits_take(int64_t *ns, int64_t *count, int64_t *longest) {
|
||||
*ns = __atomic_exchange_n(&vc_gap.ns, 0, __ATOMIC_RELAXED);
|
||||
*count = __atomic_exchange_n(&vc_gap.n, 0, __ATOMIC_RELAXED);
|
||||
*longest = __atomic_exchange_n(&vc_gap.longest, 0, __ATOMIC_RELAXED);
|
||||
}
|
||||
/* Lifted code calls this from its interrupt polls with the loop's guest registers still in host registers (a64_poll_cur,
|
||||
mkw_glue.c: the poll hands over no context, so the compiler need not spill them). It, and everything it calls, must never
|
||||
read or write the thread's guest context (Ctx) or run guest code: what it would see there may be older than the loop. A
|
||||
@@ -1662,6 +1922,15 @@ void hos_vcore_yield(void) {
|
||||
HosThread *t = cur;
|
||||
if (hos_vcores_on() && t) vc_yield(t, 0);
|
||||
}
|
||||
/* ((Triforce, 11-21) a waiter asked the calling thread for its core (vc_kick): nn_os_sync.c's spins before a sleep stop at once
|
||||
then, where they spun on with the core held for up to HOS_SPIN_US (20 us) while the waiter could not run. HOS_SPIN_YIELD=0:
|
||||
the spins never look, as before */
|
||||
int hos_vcore_asked(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = !(getenv("HOS_SPIN_YIELD") && atoi(getenv("HOS_SPIN_YIELD")) == 0);
|
||||
HosThread *t = cur;
|
||||
return on && t && t->vc_held && t->vc_kick;
|
||||
}
|
||||
/* SleepConditionVariableSRW for guest threads: the core given up for the wait; after it the lock is let go while a
|
||||
core is taken again (waiting for a core while holding it could stall the thread that will wake the next waiter),
|
||||
so callers see a spurious wake-up and look at their condition again, as they do anyway */
|
||||
@@ -1679,6 +1948,7 @@ int hos_cv_sleep(void *cv, void *lk, unsigned ms) {
|
||||
int hos_vcores_on(void) { return 0; }
|
||||
int hos_vcore_away(void) { return 0; }
|
||||
void hos_vcore_back(void) {}
|
||||
void hos_vcore_waits_take(int64_t *ns, int64_t *count, int64_t *longest) { *ns = *count = *longest = 0; }
|
||||
void hos_vcore_checkpoint(void) {}
|
||||
void hos_vcore_yield(void) {}
|
||||
int hos_cv_sleep(void *cv, void *lk, unsigned ms) {
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
none of them crosses dies. Windows' own placement of games on that die (Game Mode with AMD's chipset driver) depends
|
||||
on its taking the program for a game.
|
||||
|
||||
(Triforce, 11-22, the performance audit's THREAD-49, after Source A's) The replay's core is kept on the die only where
|
||||
the die's cores have more than one logical processor each (SMT on). With SMT off a kept core left the game 7 of the
|
||||
die's 8 cores: the replay ran faster and the frame slower (hos_x3d_keep_replay_core below). HOS_RESERVE_CORE=1 keeps
|
||||
one there too, as before.
|
||||
|
||||
Nothing is done:
|
||||
- with every L3 cache the same size: one die (a 7800X3D), two dies alike (a 7950X, a part with the cache on both), an
|
||||
Intel processor of two kinds of core, whose cores differ in their L2 caches and share one L3;
|
||||
@@ -96,4 +101,24 @@ static inline uint64_t hos_x3d_thread_mask(uint64_t process, uint64_t die, uint6
|
||||
if (on & ~reserved) return reserved || on != process ? on & ~reserved : 0;
|
||||
return on != process ? on : 0;
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's THREAD-49, after Source A's) Whether a core of the die is kept for the replay.
|
||||
die: hos_x3d_pick's die, 0 for none; cores: the cores in it; asked: HOS_RESERVE_CORE, -1 when it is not set, else 0 or
|
||||
1; own_thread: the replay has a thread of its own (RX_VK_ASYNC=1), which is pinned to the kept core.
|
||||
Source A measured a 9950X3D with SMT off: with one of the die's 8 cores kept the replay took 9.8-10.0 ms a frame and
|
||||
the game ran at 56.9-57.4 fps; with none kept, 10.1-10.5 ms and 59.8-59.9 fps. The game runs six cores' worth of
|
||||
threads beside the replay's, and the eighth core was worth more to them than to the replay alone. With SMT on (16
|
||||
logical processors on the die's 8 cores) it was never measured, so the core stays kept there, as before; so it does
|
||||
with no such die, where this rule changes nothing. The switch, when set, wins either way */
|
||||
static inline int hos_x3d_keep_replay_core(uint64_t die, int cores, int asked, int own_thread) {
|
||||
if (asked >= 0) return asked != 0;
|
||||
if (!die || cores <= 0 || own_thread) return 1;
|
||||
return __builtin_popcountll(die) > cores;
|
||||
}
|
||||
|
||||
/* The core kept for the replay (hos_reserved_cpus): fast, the last core of the fastest kind among the die's cores when
|
||||
there is a die and among all otherwise (pick_reserved_core's, 0 for none), unless the rule above keeps none */
|
||||
static inline uint64_t hos_x3d_reserved(uint64_t fast, uint64_t die, int cores, int asked, int own_thread) {
|
||||
return hos_x3d_keep_replay_core(die, cores, asked, own_thread) ? fast : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -3,12 +3,16 @@
|
||||
calls the lifted code makes at the head of those functions (nx2/oot_patches.py patch_host_zstd).
|
||||
|
||||
HOS_HOST_ZSTD, read once at the first call:
|
||||
unset or 0 the game's own decoder, exactly as before (the calls answer 0 and its code runs; after the first call
|
||||
unset or 1 the host decodes what host_zstd.h takes, the game's code the rest
|
||||
0 the game's own decoder, exactly as before (the calls answer 0 and its code runs; after the first call
|
||||
the lifted code no longer makes them)
|
||||
1 the host decodes what host_zstd.h takes, the game's code the rest
|
||||
2 check: the game's code decodes everything and its results stand; the host decodes beside it and every
|
||||
difference is logged ("zstd check: ... MISMATCH"). The log's "zstd:" lines count what was compared
|
||||
Off by default: nobody has run it in Triforce's game. A wrong answer here damages every file the game loads. */
|
||||
difference is logged ("zstd check: ... MISMATCH", "wrote past the end"). The log's "zstd:" lines count
|
||||
what was compared
|
||||
((Triforce, 11-21, the performance audit's IO-01, after Source A's) On by default since the host is given the
|
||||
content's exact size to write into and check mode ran clean in the game: host_zstd.h hz_room. A wrong answer here
|
||||
damages every file the game loads, so 0 stays, and 2 is the way to look. HOS_HOST_ZSTD_AWAY (0, 1, 2): which decodes
|
||||
give their virtual core up, host_zstd.h hz_away_mode.) */
|
||||
#include "hos.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
@@ -37,8 +41,11 @@ static int mode(void) {
|
||||
LONG v = m;
|
||||
if (v < 0) {
|
||||
const char *e = getenv("HOS_HOST_ZSTD");
|
||||
v = e && *e ? atoi(e) : 0;
|
||||
v = e && *e ? atoi(e) : 1; /* ((Triforce, 11-21, IO-01) not set: on */
|
||||
v = v == 1 || v == 2 ? v : 0;
|
||||
const char *a = getenv("HOS_HOST_ZSTD_AWAY");
|
||||
const int away = a && *a ? atoi(a) : 1;
|
||||
hz_away_mode = away == 0 || away == 2 ? away : 1;
|
||||
/* (only the build the addresses are for: nx2/oot_patches.py puts the calls into no other, and this says so twice) */
|
||||
if (v && strncmp(hos_main_build_id, "e0f3ccfa", 8)) {
|
||||
hos_log("zstd: HOS_HOST_ZSTD is for game build e0f3ccfa, this is %.8s: the game's own decoder", hos_main_build_id);
|
||||
@@ -50,8 +57,11 @@ static int mode(void) {
|
||||
}
|
||||
if (InterlockedCompareExchange(&m, v, -1) == -1 && v)
|
||||
hos_log(v == 2 ? "zstd: check mode (HOS_HOST_ZSTD=2): the game's decoder decodes, the host's beside it, differences are logged"
|
||||
: "zstd: the game's files are decoded by the host's zstd %s (HOS_HOST_ZSTD=1; =2 checks it against the game's)",
|
||||
ZSTD_versionString());
|
||||
: "zstd: the game's files are decoded by the host's zstd %s (HOS_HOST_ZSTD=0: the game's own decoder; =2 "
|
||||
"checks the host's against it); whole buffers %s (HOS_HOST_ZSTD_AWAY)",
|
||||
v == 2 ? "" : ZSTD_versionString(),
|
||||
hz_away_mode == 1 ? "of 128 KB give their virtual core up, streams keep it"
|
||||
: hz_away_mode == 2 ? "and stream feeds of 64 KB give their virtual core up" : "and streams keep their virtual core");
|
||||
v = m;
|
||||
hos_host_zstd = (int)v;
|
||||
}
|
||||
|
||||
48
host/host_alloc.h
Normal file
48
host/host_alloc.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/* (Triforce, 11-22, the performance audit's OS-04, after Source A's) The wait at Windows' commit limit.
|
||||
|
||||
When RAM and the page file are full Windows refuses a commit (error 1455), and a refusal often ends in moments: a
|
||||
page file set to grow grows, another program lets memory go, the renderer gives its textures' commit back. The
|
||||
game's own commits already wait for that (mkw_glue.c commit_wait, since the heap is committed as it is touched). The
|
||||
host's own tables did not: a malloc that failed there ended the game at once. Now those allocations wait the same
|
||||
way, tried again every 50 ms for up to 5 s, and only then end the game as before, after the out-of-memory notice.
|
||||
The renderer's failed `new` waits too (gpu/vk/new_wait.h).
|
||||
|
||||
HOS_COMMIT_WAIT_MS=<ms>: how long all three wait, 5000 unless set, 60000 at most; 0: none, a refusal fails at once.
|
||||
|
||||
The numbers and the loop alone, no system calls: tests/test_commit_limit.py compiles them. */
|
||||
#ifndef HOST_ALLOC_H
|
||||
#define HOST_ALLOC_H
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum { HOST_ALLOC_STEP_MS = 50, HOST_ALLOC_WAIT_MS = 5000, HOST_ALLOC_WAIT_MOST_MS = 60000 };
|
||||
|
||||
/* HOS_COMMIT_WAIT_MS's value in ms: not set (or empty) 5000; a number that many, 0 and less none, 60000 at most */
|
||||
static inline int host_alloc_wait_ms(const char *env) {
|
||||
if (!env || !*env) return HOST_ALLOC_WAIT_MS;
|
||||
const long v = atol(env);
|
||||
return v <= 0 ? 0 : v > HOST_ALLOC_WAIT_MOST_MS ? HOST_ALLOC_WAIT_MOST_MS : (int)v;
|
||||
}
|
||||
/* How often a refusal is tried again: once a step for the wait (5000 ms: 100 times; under a step: never) */
|
||||
static inline int host_alloc_tries(int wait_ms) { return wait_ms > 0 ? wait_ms / HOST_ALLOC_STEP_MS : 0; }
|
||||
|
||||
/* After a first refusal: a step slept and try_again(ctx) called, up to `tries` times, until it gives something. What it
|
||||
gave (NULL: still refused), and in *waited_ms how long that took */
|
||||
static inline void *host_alloc_retry(void *(*try_again)(void *ctx), void *ctx, int tries, void (*sleep_ms)(int ms), int *waited_ms) {
|
||||
void *p = NULL;
|
||||
*waited_ms = 0;
|
||||
for (int i = 0; i < tries && !p; i++) {
|
||||
sleep_ms(HOST_ALLOC_STEP_MS);
|
||||
*waited_ms += HOST_ALLOC_STEP_MS;
|
||||
p = try_again(ctx);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/* malloc, calloc and realloc that wait at the limit before they answer NULL (mkw_glue.c); a size of 0 is answered as the
|
||||
C library answers it. The caller still ends the game on NULL, as before */
|
||||
void *hos_xmalloc(size_t n);
|
||||
void *hos_xcalloc(size_t count, size_t size);
|
||||
void *hos_xrealloc(void *p, size_t n);
|
||||
int hos_commit_wait_ms(void); /* (HOS_COMMIT_WAIT_MS, read once) */
|
||||
#endif
|
||||
102
host/host_zstd.h
102
host/host_zstd.h
@@ -66,7 +66,23 @@
|
||||
#include "../third_party/zstd/lib/zstd.h"
|
||||
#include "../third_party/zstd/lib/zstd_errors.h"
|
||||
|
||||
#define HZ_AWAY_BYTES (64u << 10) /* inputs from this size give the virtual core up while they decode */
|
||||
#define HZ_AWAY_BYTES (64u << 10) /* inputs from this size give the virtual core up while they decode (hz_away_mode 2) */
|
||||
/* ((Triforce, 11-21, the performance audit's IO-01, after Source A's) Who gives the game's virtual core up for a decode:
|
||||
HOS_HOST_ZSTD_AWAY, set by the includer before the first call.
|
||||
1 (the default) a whole buffer whose input or output (the content's size where the frame says it, else the
|
||||
caller's room) is HZ_AWAY_WHOLE or more; a stream's feed never. A feed is a
|
||||
chunk the loading thread just read, a millisecond or two of decoding, and taking a core again
|
||||
after each one cost more than it gave: Source A's loads froze in 7 of 10 runs on a 22-thread
|
||||
laptop with the core given up there, in none of 3 with it kept;
|
||||
0 never;
|
||||
2 as before this: whole buffers and feeds alike from HZ_AWAY_BYTES of input */
|
||||
#define HZ_AWAY_WHOLE (128u << 10)
|
||||
static int hz_away_mode = 1;
|
||||
static inline int hz_away_whole(uint64_t size, uint64_t cap) {
|
||||
return hz_away_mode == 2 ? size >= HZ_AWAY_BYTES : hz_away_mode == 1 ? size >= HZ_AWAY_WHOLE || cap >= HZ_AWAY_WHOLE : 0;
|
||||
}
|
||||
static inline int hz_away_feed(uint64_t size) { return hz_away_mode == 2 && size >= HZ_AWAY_BYTES; }
|
||||
#define HZ_CANARY 64 /* (check mode: bytes after the host's own output that must stay as they were set) */
|
||||
#define HZ_MAX_DICT (64u << 20)
|
||||
#define HZ_NDICT 32
|
||||
#define HZ_NSTREAM 16
|
||||
@@ -90,6 +106,7 @@ static _Thread_local uint64_t hz_caller;
|
||||
static struct {
|
||||
uint64_t calls, in, out, ns, game, inplace, away, checked, differ; /* one-shot */
|
||||
uint64_t streams, feeds, s_in, s_out, s_ns, s_game, s_checked, s_differ;
|
||||
uint64_t tail; /* ((Triforce, IO-01) check mode: decodes that wrote past their content's end) */
|
||||
} hz_stats;
|
||||
|
||||
static inline uint32_t hz_ld32(uint64_t a) { uint32_t v; memcpy(&v, HZ_PTR(a), 4); return v; }
|
||||
@@ -210,7 +227,7 @@ static size_t hz_decode(void *out, uint64_t cap, uint64_t src, uint64_t size, ui
|
||||
int slot;
|
||||
ZSTD_DCtx *d = hz_dctx_take(&slot);
|
||||
if (!d) return 0;
|
||||
const int away = size >= HZ_AWAY_BYTES ? HZ_AWAY() : 0;
|
||||
const int away = hz_away_whole(size, cap) ? HZ_AWAY() : 0;
|
||||
if (away) hz_add(&hz_stats.away, 1);
|
||||
*ok = 1;
|
||||
const size_t r = dd ? ZSTD_decompress_usingDDict(d, out, (size_t)cap, HZ_PTR(src), (size_t)size, dd)
|
||||
@@ -223,12 +240,12 @@ static size_t hz_decode(void *out, uint64_t cap, uint64_t src, uint64_t size, ui
|
||||
static void hz_count(uint64_t in, uint64_t out, uint64_t ns) {
|
||||
const uint64_t n = __atomic_add_fetch(&hz_stats.calls, 1, __ATOMIC_RELAXED);
|
||||
hz_add(&hz_stats.in, in), hz_add(&hz_stats.out, out), hz_add(&hz_stats.ns, ns);
|
||||
if (n == 1 || !(n & 0x3fff))
|
||||
if (!(n & (n - 1)) || !(n & 0x3fff)) /* ((Triforce, IO-01) the 1st, 2nd, 4th, ... and every 16384th) */
|
||||
HZ_LOG("zstd: %llu calls decoded on the host, %.1f MB in, %.1f MB out, %.0f ms; %llu left to the game's decoder (%llu of "
|
||||
"them in place), %llu with the core given up; check: %llu compared, %llu differ", (unsigned long long)n,
|
||||
hz_stats.in / 1048576.0, hz_stats.out / 1048576.0, hz_stats.ns / 1e6, (unsigned long long)hz_stats.game,
|
||||
(unsigned long long)hz_stats.inplace, (unsigned long long)hz_stats.away, (unsigned long long)hz_stats.checked,
|
||||
(unsigned long long)hz_stats.differ);
|
||||
"them in place), %llu with the core given up; check: %llu compared, %llu differ, %llu wrote past the end",
|
||||
(unsigned long long)n, hz_stats.in / 1048576.0, hz_stats.out / 1048576.0, hz_stats.ns / 1e6,
|
||||
(unsigned long long)hz_stats.game, (unsigned long long)hz_stats.inplace, (unsigned long long)hz_stats.away,
|
||||
(unsigned long long)hz_stats.checked, (unsigned long long)hz_stats.differ, (unsigned long long)hz_stats.tail);
|
||||
}
|
||||
|
||||
/* (the input inside the output: the result depends on how the decoder's writes overtake its reads, which libzstd does
|
||||
@@ -238,6 +255,21 @@ static int hz_in_place(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size)
|
||||
return cap && src < dst + cap && dst < src + size;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-21, the performance audit's IO-01, after Source A's) The room libzstd is given for a whole buffer: the
|
||||
content's size when the frames say it and it fits, not the caller's whole room. libzstd may park a block's literals in
|
||||
spare room past where the block ends (zstd_decompress_block.c, the "split literals" buffer); the game's decoder, zstd
|
||||
1.4, never writes past the content, and what lies in the game's memory after it is not the decoder's to touch. *exact:
|
||||
the room is the content's. Else (a frame without its content size, or a size that does not fit: libzstd then refuses
|
||||
and the game's decoder decides) the caller's room, all of which may have been written */
|
||||
static uint64_t hz_room(uint64_t cap, uint64_t src, uint64_t size, int *exact) {
|
||||
*exact = 0;
|
||||
if (size < 5 || !src || !HZ_OK(src, size)) return cap;
|
||||
const unsigned long long n = ZSTD_findDecompressedSize(HZ_PTR(src), (size_t)size);
|
||||
if (n == ZSTD_CONTENTSIZE_UNKNOWN || n == ZSTD_CONTENTSIZE_ERROR || n > cap) return cap;
|
||||
*exact = 1;
|
||||
return (uint64_t)n;
|
||||
}
|
||||
|
||||
/* A call of the game's ZSTD_decompressMultiFrame. 1: decoded here, *ret its result (the decoded size). 0: the game's
|
||||
own function is to run, with its arguments as they are */
|
||||
static int hz_oneshot(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, uint64_t dict, uint64_t dict_size,
|
||||
@@ -246,9 +278,10 @@ static int hz_oneshot(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, u
|
||||
hz_add(&hz_stats.inplace, 1), hz_add(&hz_stats.game, 1);
|
||||
return 0;
|
||||
}
|
||||
int ok = 0;
|
||||
int ok = 0, exact = 0;
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS();
|
||||
const size_t r = (cap && !dst) || !HZ_OK(dst, cap) ? 0 : hz_decode(HZ_PTR(dst), cap, src, size, dict, dict_size, ddict, &ok);
|
||||
const uint64_t room = hz_room(cap, src, size, &exact); /* ((Triforce, IO-01) the content's size, when known) */
|
||||
const size_t r = (cap && !dst) || !HZ_OK(dst, cap) ? 0 : hz_decode(HZ_PTR(dst), room, src, size, dict, dict_size, ddict, &ok);
|
||||
if (!ok || ZSTD_isError(r)) { /* (the game's decoder decides, and gives its own error code) */
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 8)
|
||||
@@ -257,7 +290,8 @@ static int hz_oneshot(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, u
|
||||
hz_add(&hz_stats.game, 1);
|
||||
return 0;
|
||||
}
|
||||
if (r) HZ_WRITTEN(dst, r);
|
||||
/* ((Triforce, IO-01) a room that was not the content's own: all of it may have been written, and is told so) */
|
||||
if (exact ? r != 0 : cap != 0) HZ_WRITTEN(dst, exact ? r : cap);
|
||||
*ret = r;
|
||||
hz_count(size, r, (uint64_t)HZ_NOW_NS() - t0);
|
||||
return 1;
|
||||
@@ -271,10 +305,14 @@ static uint64_t hz_oneshot_check(uint64_t dst, uint64_t cap, uint64_t src, uint6
|
||||
hz_add(&hz_stats.inplace, 1), hz_add(&hz_stats.game, 1);
|
||||
return game(user);
|
||||
}
|
||||
uint8_t *mine = cap ? (uint8_t *)malloc((size_t)cap) : NULL;
|
||||
int ok = 0;
|
||||
/* ((Triforce, IO-01) the host's output with the room hz_oneshot would give it, every byte set first: what is not the
|
||||
content must come back as it was, or the decode wrote past its content's end (counted, "wrote past the end") */
|
||||
uint8_t *mine = cap ? (uint8_t *)malloc((size_t)cap + HZ_CANARY) : NULL;
|
||||
int ok = 0, exact = 0;
|
||||
const uint64_t room = hz_room(cap, src, size, &exact);
|
||||
if (mine) memset(mine, 0xA5, (size_t)cap + HZ_CANARY);
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS();
|
||||
const size_t r = cap && !mine ? 0 : hz_decode(mine, cap, src, size, dict, dict_size, ddict, &ok);
|
||||
const size_t r = cap && !mine ? 0 : hz_decode(mine, room, src, size, dict, dict_size, ddict, &ok);
|
||||
const uint64_t ns = (uint64_t)HZ_NOW_NS() - t0;
|
||||
const uint64_t g = game(user);
|
||||
const int host_ok = ok && !ZSTD_isError(r);
|
||||
@@ -287,6 +325,17 @@ static uint64_t hz_oneshot_check(uint64_t dst, uint64_t cap, uint64_t src, uint6
|
||||
!ok ? "inputs not taken" : ZSTD_getErrorName(r));
|
||||
} else {
|
||||
hz_add(&hz_stats.checked, 1);
|
||||
for (uint64_t i = r; mine && i < cap + HZ_CANARY; i++)
|
||||
if (mine[i] != 0xA5) {
|
||||
hz_add(&hz_stats.tail, 1);
|
||||
static int told;
|
||||
if (__atomic_fetch_add(&told, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd check: the host wrote past the end: %llu bytes of content, byte %llu of a room of %llu changed "
|
||||
"(%s; called from %#llx)", (unsigned long long)r, (unsigned long long)i, (unsigned long long)cap,
|
||||
exact ? "the room was the content's: libzstd wrote past its capacity" : "no content size in the frame",
|
||||
(unsigned long long)hz_caller);
|
||||
break;
|
||||
}
|
||||
if (g != r || (r && (!HZ_OK(dst, r) || memcmp(mine, HZ_PTR(dst), r)))) {
|
||||
hz_add(&hz_stats.differ, 1);
|
||||
static int logged;
|
||||
@@ -394,7 +443,7 @@ static ZSTD_DCtx *hz_stream_dctx(void) {
|
||||
/* Feeds in to the stream's context until the input is used up, the frame ends (0), an error, or nothing moves. With
|
||||
the virtual core given up for large chunks, as hz_decode */
|
||||
static size_t hz_stream_run(HzStream *st, ZSTD_outBuffer *out, ZSTD_inBuffer *in) {
|
||||
const int away = in->size >= HZ_AWAY_BYTES ? HZ_AWAY() : 0;
|
||||
const int away = hz_away_feed(in->size) ? HZ_AWAY() : 0; /* ((Triforce, IO-01) a feed keeps its core: hz_away_mode) */
|
||||
if (away) hz_add(&hz_stats.away, 1);
|
||||
size_t r = 1;
|
||||
while (in->pos < in->size) {
|
||||
@@ -409,11 +458,12 @@ static size_t hz_stream_run(HzStream *st, ZSTD_outBuffer *out, ZSTD_inBuffer *in
|
||||
static void hz_stream_count(const HzStream *st, uint64_t out) {
|
||||
const uint64_t n = __atomic_add_fetch(&hz_stats.streams, 1, __ATOMIC_RELAXED);
|
||||
hz_add(&hz_stats.s_in, st->in), hz_add(&hz_stats.s_out, out);
|
||||
if (n == 1 || !(n & 0x3ff))
|
||||
if (!(n & (n - 1)) || !(n & 0x3ff)) /* ((Triforce, IO-01) the 1st, 2nd, 4th, ... and every 1024th) */
|
||||
HZ_LOG("zstd: %llu streams decoded on the host in %llu feeds, %.1f MB in, %.1f MB out, %.0f ms; %llu streams left to the "
|
||||
"game's decoder; check: %llu compared, %llu differ", (unsigned long long)n, (unsigned long long)hz_stats.feeds,
|
||||
hz_stats.s_in / 1048576.0, hz_stats.s_out / 1048576.0, hz_stats.s_ns / 1e6, (unsigned long long)hz_stats.s_game,
|
||||
(unsigned long long)hz_stats.s_checked, (unsigned long long)hz_stats.s_differ);
|
||||
"game's decoder; check: %llu compared, %llu differ, %llu wrote past the end", (unsigned long long)n,
|
||||
(unsigned long long)hz_stats.feeds, hz_stats.s_in / 1048576.0, hz_stats.s_out / 1048576.0, hz_stats.s_ns / 1e6,
|
||||
(unsigned long long)hz_stats.s_game, (unsigned long long)hz_stats.s_checked, (unsigned long long)hz_stats.s_differ,
|
||||
(unsigned long long)hz_stats.tail);
|
||||
}
|
||||
|
||||
/* A stream at its begin (phase 1): its slot, the host context ready for the frame, or NULL when the game's feed is to
|
||||
@@ -441,10 +491,11 @@ static HzStream *hz_stream_begin(uint64_t s, uint64_t src, int32_t size, uint64_
|
||||
if ((!st->dctx && !(st->dctx = hz_stream_dctx())) ||
|
||||
ZSTD_isError(ZSTD_DCtx_reset(st->dctx, ZSTD_reset_session_only)) ||
|
||||
ZSTD_isError(ZSTD_DCtx_refDDict(st->dctx, dd)) ||
|
||||
(check && !(st->shadow = (uint8_t *)malloc((size_t)st->content)))) {
|
||||
(check && !(st->shadow = (uint8_t *)malloc((size_t)st->content + HZ_CANARY)))) {
|
||||
hz_stream_drop(st);
|
||||
return NULL;
|
||||
}
|
||||
if (check) memset(st->shadow + st->content, 0xA5, HZ_CANARY); /* ((Triforce, IO-01) what must stay: hz_stream_check) */
|
||||
if (!check) { /* what the game's begin leaves in its state */
|
||||
hz_st32(s + 0x14, (uint32_t)st->content);
|
||||
hz_st64(s + 0x30, ws);
|
||||
@@ -474,7 +525,9 @@ static uint64_t hz_stream_feed(HzStream *st, uint64_t s, uint64_t src, int32_t s
|
||||
return 0xffffffffu;
|
||||
}
|
||||
ZSTD_inBuffer in = {HZ_PTR(src), (size_t)size, 0};
|
||||
ZSTD_outBuffer out = {HZ_PTR(st->dst0), (size_t)st->cap0, (size_t)st->pos};
|
||||
/* ((Triforce, IO-01) the content's own size, not the game's whole room: libzstd may park literals in room past a
|
||||
block's end, where the game's decoder never writes. The begin took the stream only with room for the content) */
|
||||
ZSTD_outBuffer out = {HZ_PTR(st->dst0), (size_t)st->content, (size_t)st->pos};
|
||||
const size_t r = hz_stream_run(st, &out, &in);
|
||||
const uint32_t wrote = (uint32_t)(out.pos - st->pos);
|
||||
st->pos = out.pos, st->in += in.pos;
|
||||
@@ -545,6 +598,15 @@ static uint64_t hz_stream_check(HzStream *st, uint64_t s, uint64_t src, int32_t
|
||||
const int out_all = ph == 2 && !g; /* the job feeds no more, with or without the frame's last bytes */
|
||||
if (ph != 3 && ph != 4 && !out_all) return g;
|
||||
hz_add(&hz_stats.s_checked, 1);
|
||||
for (int i = 0; i < HZ_CANARY; i++) /* ((Triforce, IO-01) the bytes after the host's own output: as they were set) */
|
||||
if (st->shadow[st->content + i] != 0xA5) {
|
||||
hz_add(&hz_stats.tail, 1);
|
||||
static int told;
|
||||
if (__atomic_fetch_add(&told, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd check: a host stream wrote past the end of its %llu bytes of content (called from %#llx)",
|
||||
(unsigned long long)st->content, (unsigned long long)hz_caller);
|
||||
break;
|
||||
}
|
||||
const int same = ph == 3 ? st->bad || st->done != 1 /* the game failed: the host must not have a clean frame */
|
||||
: !st->bad && !st->moved && st->done != 2 && (ph != 4 || st->done == 1) &&
|
||||
st->pos == st->content && HZ_OK(st->dst0, st->content) &&
|
||||
|
||||
@@ -154,14 +154,33 @@ static float *to_mix_rate(float *pcm, uint32_t *frames, uint32_t rate) {
|
||||
return out;
|
||||
}
|
||||
|
||||
float *hos_menu_sound_decode(const uint8_t *b, uint64_t size, uint32_t *frames_out) {
|
||||
/* ((Triforce, 11-20) the samples of a channel in the whole DSP-ADPCM frames the file holds, `frames` at most. A tune's last
|
||||
frame may be cut short at the file's end: the shop theme's second channel ends 4 bytes into its last frame of 8 (the 5
|
||||
samples it still counts are there, the frame is not), which decode_channel refuses, as it must for an interface sound.
|
||||
The tune is cut to the whole frames: 5 samples of a minute) */
|
||||
static uint32_t frames_held(const uint8_t *info, uint64_t size, uint32_t frames) {
|
||||
const uint16_t codec = u16(info);
|
||||
const uint32_t off = u32(info + 0x30);
|
||||
if (off >= size) return 0;
|
||||
const uint64_t have = size - off;
|
||||
uint64_t n = 0;
|
||||
if (codec == 0) n = have / 2;
|
||||
else if (codec == 1) n = have / 8 * 14;
|
||||
return n < frames ? (uint32_t)n : frames;
|
||||
}
|
||||
|
||||
/* (the decoder, for a file of at most `seconds`: the interface sounds' ten, the Mods screen's tune's more) */
|
||||
static float *decode_most(const uint8_t *b, uint64_t size, uint32_t *frames_out, uint32_t seconds) {
|
||||
if (size < 0x10 || memcmp(b, "BWAV", 4)) return NULL;
|
||||
const uint16_t nch = u16(b + 0x0e);
|
||||
if (!nch || nch > 2 || 0x10 + (uint64_t)nch * 0x4c > size) return NULL;
|
||||
const uint32_t rate = u32(b + 0x10 + 4);
|
||||
uint32_t frames = u32(b + 0x10 + 8);
|
||||
/* (the interface sounds are 48 kHz and short: at most ten seconds of a rate a sound file has) */
|
||||
if (rate < 8000 || rate > 192000 || !frames || frames > rate * 10) return NULL;
|
||||
if (rate < 8000 || rate > 192000 || !frames || frames > (uint64_t)rate * seconds) return NULL;
|
||||
if (seconds > 10) /* (a tune: as long as the samples its channels hold, the last frame's cut-off ones left out) */
|
||||
for (uint16_t c = 0; c < nch; c++) frames = frames_held(b + 0x10 + c * 0x4c, size, frames);
|
||||
if (!frames) return NULL;
|
||||
float *pcm = (float *)calloc((size_t)frames * 2, sizeof *pcm);
|
||||
int ok = pcm != NULL;
|
||||
for (uint16_t c = 0; ok && c < nch; c++) ok = decode_channel(b, size, b + 0x10 + c * 0x4c, pcm, frames);
|
||||
@@ -176,6 +195,12 @@ float *hos_menu_sound_decode(const uint8_t *b, uint64_t size, uint32_t *frames_o
|
||||
return pcm;
|
||||
}
|
||||
|
||||
float *hos_menu_sound_decode(const uint8_t *b, uint64_t size, uint32_t *frames_out) { return decode_most(b, size, frames_out, 10); }
|
||||
/* ((Triforce, 11-20) a tune: the Mods screen's music, host/mods_music.c) */
|
||||
float *hos_menu_sound_decode_long(const uint8_t *b, uint64_t size, uint32_t *frames_out, uint32_t seconds) {
|
||||
return decode_most(b, size, frames_out, seconds);
|
||||
}
|
||||
|
||||
static void load_all(void) { /* (once: hos_menu_sound_frame's store of 1 into loaded starts it) */
|
||||
int n = 0;
|
||||
for (int id = 1; id < SOUNDS; id++) {
|
||||
|
||||
@@ -9,6 +9,12 @@ extern "C" {
|
||||
/* A BWAV file's samples as interleaved stereo floats at 48 kHz, *frames of them (freed by the caller); NULL when the
|
||||
bytes are not a file this plays. Nothing outside [file, file + size) is read */
|
||||
float *hos_menu_sound_decode(const uint8_t *file, uint64_t size, uint32_t *frames);
|
||||
/* ((Triforce, 11-20) the same for a file of up to `seconds`: a tune (host/mods_music.c, the Mods screen's music) */
|
||||
float *hos_menu_sound_decode_long(const uint8_t *file, uint64_t size, uint32_t *frames, uint32_t seconds);
|
||||
/* The Mods screen's music (host/mods_music.c), at each block the sound thread mixes, after the game's own mix and its
|
||||
limiter and before the options' sounds: while the Mods screen is open from the title the game's nch channels are
|
||||
turned down and the game's shop theme is added to the front pair, crossing over in 0.6 s each way */
|
||||
void hos_mods_music_frame(float *const *channels, int nch, uint32_t frames);
|
||||
/* The Menu sounds row's percent as a gain: 0 at 0 and under, 1 at 100 and over */
|
||||
float hos_menu_sound_gain(int percent);
|
||||
/* Starts the sound of a HOS_MENU_SOUND_* (gpu/vk/vk_api.h) on a free voice, else on the oldest. No lock is taken: it
|
||||
|
||||
216
host/mkw_glue.c
216
host/mkw_glue.c
@@ -2,6 +2,9 @@
|
||||
commit (TOTK commits its whole 4 GB window up front; MKW's 512 GB window is only reserved), and stand-ins for the
|
||||
hooks of files not taken into host/ (the renderer and display, the profiler, mods, TOTK's own patches). */
|
||||
#include "hos.h"
|
||||
#include "heap_commit.h" /* ((Triforce, 11-21, MEM-09) the heap committed as it is touched: its rules) */
|
||||
#include "stall_host.h" /* ((Triforce, 11-22, OS-51) the host's part of a long frame, counted where it is spent) */
|
||||
#include "host_alloc.h" /* ((Triforce, 11-22, OS-04) the wait at Windows' commit limit: its length, and the host's own allocations) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -15,6 +18,83 @@
|
||||
The runtime commits what it hands out (image, system region, heap, address regions: hos_commit), because host
|
||||
APIs given guest buffers (ReadFile into a game buffer) fail on reserved pages without raising an exception. A
|
||||
guest or host access to a page nothing committed is committed on first touch, 64 KB at a time, by the handler. */
|
||||
/* ((Triforce, 11-21, the performance audit's MEM-09, after Source A's) A commit refused at Windows' commit limit (1455: RAM
|
||||
and page file full) is tried again for up to 5 s, 100 times 50 ms apart: the renderer gives its textures' commit back
|
||||
when the system's runs low (vk_texture.cpp, RX_VK_TEX_COMMIT_FREE_MB), and a page file set to grow does. Before, the
|
||||
commit failed at once and the game's next write into those pages ended it. With the heap committed as it is touched
|
||||
(heap_commit.h) a refusal can come at any first touch. 1: committed after waiting; 0: not */
|
||||
/* ((Triforce, 11-22, the performance audit's OS-04) HOS_COMMIT_WAIT_MS, read once: how long a refusal at the commit limit
|
||||
is waited out, here and in hos_xmalloc below: 5000 ms unless set, 0 for no wait at all (host_alloc.h) */
|
||||
int hos_commit_wait_ms(void) {
|
||||
static int ms = -1;
|
||||
if (ms < 0) ms = host_alloc_wait_ms(getenv("HOS_COMMIT_WAIT_MS"));
|
||||
return ms;
|
||||
}
|
||||
static int commit_wait(void *p, SIZE_T size) {
|
||||
static volatile LONG made, given_up;
|
||||
const int tries = host_alloc_tries(hos_commit_wait_ms()); /* ((Triforce, OS-04) 100 for the 5 s; 0: the refusal stands at once) */
|
||||
for (int i = 0; i < tries; i++) {
|
||||
Sleep(50);
|
||||
if (VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE)) {
|
||||
const LONG n = InterlockedIncrement(&made);
|
||||
if (n <= 8 || (n & 255) == 0)
|
||||
hos_log("mem: commit of %#llx bytes refused at the commit limit, made after %d ms (%ld so far)",
|
||||
(unsigned long long)size, (i + 1) * 50, (long)n);
|
||||
return 1;
|
||||
}
|
||||
if (GetLastError() != ERROR_COMMITMENT_LIMIT) return 0;
|
||||
}
|
||||
if (tries && InterlockedIncrement(&given_up) <= 8)
|
||||
hos_log("mem: commit of %#llx bytes still refused after %d ms at the commit limit", (unsigned long long)size,
|
||||
tries * HOST_ALLOC_STEP_MS);
|
||||
return 0;
|
||||
}
|
||||
/* ((Triforce, 11-22, the performance audit's OS-04, after Source A's) The host's own tables at the commit limit: malloc,
|
||||
calloc and realloc that are tried again every 50 ms for up to HOS_COMMIT_WAIT_MS before they answer NULL, on which
|
||||
their callers end the game as before (host_alloc.h has why). The thread keeps its virtual core while it waits: some of
|
||||
the callers hold a lock of the host's, and a thread that queued for its core again under one could wait on a thread
|
||||
that holds a core and waits for that lock. The first 8 rescues and the first 8 refusals are logged, and a refusal
|
||||
that stands brings the out-of-memory notice, once */
|
||||
typedef struct { int kind; void *p; size_t count, n; } HostAllocAsk;
|
||||
static void *host_alloc_try(void *ctx) {
|
||||
const HostAllocAsk *a = (const HostAllocAsk *)ctx;
|
||||
return a->kind == 0 ? malloc(a->n) : a->kind == 1 ? calloc(a->count, a->n) : realloc(a->p, a->n);
|
||||
}
|
||||
static void host_alloc_sleep(int ms) { Sleep((DWORD)ms); }
|
||||
DWORD WINAPI hos_out_of_memory_box(LPVOID);
|
||||
static void *host_alloc(HostAllocAsk *a) {
|
||||
void *p = host_alloc_try(a);
|
||||
if (p || !a->n || (a->kind == 1 && !a->count)) return p; /* (a size of 0: the C library's own answer) */
|
||||
static volatile LONG made, given_up, said;
|
||||
int waited = 0;
|
||||
p = host_alloc_retry(host_alloc_try, a, host_alloc_tries(hos_commit_wait_ms()), host_alloc_sleep, &waited);
|
||||
const unsigned long long bytes = (unsigned long long)a->n * (a->kind == 1 ? a->count : 1);
|
||||
if (p) {
|
||||
const LONG n = InterlockedIncrement(&made);
|
||||
if (n <= 8 || (n & 255) == 0)
|
||||
hos_log("mem: a host allocation of %llu bytes refused, made after %d ms (%ld so far)", bytes, waited, (long)n);
|
||||
return p;
|
||||
}
|
||||
if (InterlockedIncrement(&given_up) <= 8)
|
||||
hos_log("mem: a host allocation of %llu bytes still refused after %d ms", bytes, waited);
|
||||
if (InterlockedExchange(&said, 1) == 0) {
|
||||
hos_log("mem: Windows is out of memory (RAM and page file): close other programs, or let Windows manage the page file size");
|
||||
CloseHandle(CreateThread(NULL, 0, hos_out_of_memory_box, NULL, 0, NULL));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
void *hos_xmalloc(size_t n) {
|
||||
HostAllocAsk a = {0, NULL, 1, n};
|
||||
return host_alloc(&a);
|
||||
}
|
||||
void *hos_xcalloc(size_t count, size_t size) {
|
||||
HostAllocAsk a = {1, NULL, count, size};
|
||||
return host_alloc(&a);
|
||||
}
|
||||
void *hos_xrealloc(void *p, size_t n) {
|
||||
HostAllocAsk a = {2, p, 1, n};
|
||||
return host_alloc(&a);
|
||||
}
|
||||
void *hos_mem_commit_begin(uint64_t addr, uint64_t size); /* ((Triforce) hos_mem.c: the census's notes around the commit) */
|
||||
void hos_mem_commit_end(void *had, uint64_t addr, uint64_t size, int committed);
|
||||
int hos_commit(uint64_t addr, uint64_t size) {
|
||||
@@ -40,8 +120,10 @@ int hos_commit(uint64_t addr, uint64_t size) {
|
||||
void *const census_had = hos_mem_commit_begin(lo, hi - lo);
|
||||
/* (Triforce: a commit of committed pages resets their protection, the lazy write-back's too: rx_backend.c) */
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo);
|
||||
const int committed = VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) != NULL;
|
||||
const DWORD commit_err = committed ? 0 : GetLastError();
|
||||
int committed = VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) != NULL;
|
||||
DWORD commit_err = committed ? 0 : GetLastError();
|
||||
/* ((Triforce, 11-21, MEM-09) refused at the commit limit: tried again for up to 5 s, commit_wait above) */
|
||||
if (!committed && commit_err == ERROR_COMMITMENT_LIMIT && commit_wait(GP(lo), (SIZE_T)(hi - lo))) committed = 1, commit_err = 0;
|
||||
hos_guest_io_end(io);
|
||||
hos_mem_commit_end(census_had, lo, hi - lo, committed);
|
||||
if (!committed) {
|
||||
@@ -61,6 +143,20 @@ int hos_commit(uint64_t addr, uint64_t size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-21, MEM-09) Guest memory about to go to the kernel (a file read into it, a write from it): with the heap
|
||||
committed as it is touched (hos_heap_on_touch, hos_mem.c), each page of [addr, addr + size) is read once first, so that
|
||||
one still only reserved is committed by the handler below, as the CPU's own first touch of it would. The kernel does
|
||||
not fault a page in: ReadFile into heap pages nothing touched yet failed with no exception. A byte read a 4 KB page,
|
||||
nothing written: 30 microseconds for a 100 MB buffer in use. Not VirtualQuery for which pages are reserved: it walks
|
||||
every page of the committed region it is asked in, GBs of them, and the title ran 105 to 141 fps for 240 with it in
|
||||
each read. With the heap committed whole (HOS_HEAP_COMMIT=1, large pages, Linux) nothing is done, as before */
|
||||
int hos_heap_on_touch;
|
||||
void hos_guest_touch(uint64_t addr, uint64_t size) {
|
||||
if (!hos_heap_on_touch || !size || !hos_guest_range(addr, size)) return;
|
||||
const uint64_t end = addr + size;
|
||||
for (uint64_t a = addr; a < end; a = (a | 0xFFF) + 1) (void)*(volatile const uint8_t *)GP(a);
|
||||
}
|
||||
|
||||
DWORD WINAPI hos_out_of_memory_box(LPVOID unused) {
|
||||
(void)unused;
|
||||
MessageBoxA(NULL,
|
||||
@@ -80,12 +176,38 @@ static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
|
||||
const uint64_t guest = (uint64_t)(p - A64_MEM);
|
||||
if (guest < 0x100000) return EXCEPTION_CONTINUE_SEARCH; /* (a null pointer: the game's own fault) */
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
if (!VirtualQuery(p, &mbi, sizeof mbi) || mbi.State != MEM_RESERVE) return EXCEPTION_CONTINUE_SEARCH;
|
||||
static volatile LONG touched;
|
||||
if (InterlockedIncrement(&touched) <= 16)
|
||||
if (!VirtualQuery(p, &mbi, sizeof mbi)) return EXCEPTION_CONTINUE_SEARCH;
|
||||
#ifdef _WIN32
|
||||
/* ((Triforce, 11-21, MEM-09) another thread committed this 64 KB between the fault and the look: the access is tried
|
||||
again. With the heap committed as it is touched, two threads touching one new chunk at once is common, and the second
|
||||
went on to the crash report. Read-write only: the lazy write-back's pages and the watches' are their own handlers'.
|
||||
Windows only: Linux's VirtualQuery stand-in calls every guest page committed and read-write, and every fault in the
|
||||
window would be tried again for ever) */
|
||||
if (heap_touch_again(mbi.State == MEM_COMMIT, mbi.Protect == PAGE_READWRITE, rec->ExceptionInformation[0]))
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
#endif
|
||||
if (mbi.State != MEM_RESERVE) return EXCEPTION_CONTINUE_SEARCH;
|
||||
/* ((Triforce, 11-21, MEM-09) the heap's own first touches are how it is committed now (hos_mem.c SetMemoryHeapSize):
|
||||
counted apart, and not among the 16 lines for pages nothing handed out) */
|
||||
const int heap = guest >= HOS_HEAP_BASE && guest - HOS_HEAP_BASE < hos_heap_size;
|
||||
static volatile LONG touched, heap_touched;
|
||||
if (!heap && InterlockedIncrement(&touched) <= 16)
|
||||
hos_log("mem: %#llx committed on first touch (nothing handed it out)", (unsigned long long)guest);
|
||||
if (!VirtualAlloc(GP(guest & ~0xFFFFULL), 0x10000, MEM_COMMIT, PAGE_READWRITE)) return EXCEPTION_CONTINUE_SEARCH;
|
||||
hos_mem_note(HOS_MEM_TOUCHED, guest & ~0xFFFFULL, 0x10000); /* ((Triforce) the census: atomics only, no lock) */
|
||||
/* (only the reserved pages of its 64 KB: a commit over committed ones resets their protection, heap_commit.h) */
|
||||
const uint64_t run = (uint64_t)((const uint8_t *)mbi.BaseAddress - A64_MEM);
|
||||
uint64_t lo, hi;
|
||||
heap_touch_range(guest, run, run + mbi.RegionSize, &lo, &hi);
|
||||
if (!VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) &&
|
||||
!(GetLastError() == ERROR_COMMITMENT_LIMIT && commit_wait(GP(lo), (SIZE_T)(hi - lo)))) /* (at the limit: waited for) */
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
hos_mem_note(HOS_MEM_TOUCHED, lo, hi - lo); /* ((Triforce) the census: atomics only, no lock) */
|
||||
if (heap) {
|
||||
const LONG n = InterlockedIncrement(&heap_touched);
|
||||
if (heap_touch_said(n))
|
||||
hos_log("mem: the heap is committed as it is touched: %ld first touches so far, 64 KB each at most (%ld MB of its "
|
||||
"%llu; HOS_HEAP_COMMIT=1: all of it at the start, as before)", (long)n, (long)(n >> 4),
|
||||
(unsigned long long)(hos_heap_size >> 20));
|
||||
}
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
|
||||
@@ -785,23 +907,81 @@ void hos_host_thread_register_self(const char *name) { /* (hos_prof.c: names f
|
||||
wchar_t w[64];
|
||||
if (MultiByteToWideChar(CP_UTF8, 0, name, -1, w, 64)) SetThreadDescription(GetCurrentThread(), w);
|
||||
}
|
||||
void hos_process_memory(uint64_t *private_bytes, uint64_t *working_set) {
|
||||
typedef struct {
|
||||
DWORD cb, PageFaultCount;
|
||||
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage,
|
||||
QuotaPeakNonPagedPoolUsage, QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
|
||||
} Counters;
|
||||
typedef BOOL(WINAPI * Fn)(HANDLE, Counters *, DWORD);
|
||||
typedef struct {
|
||||
DWORD cb, PageFaultCount;
|
||||
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage,
|
||||
QuotaPeakNonPagedPoolUsage, QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
|
||||
} ProcessCounters;
|
||||
/* (the process's memory counters as Windows keeps them; all zero when they cannot be read) */
|
||||
static void process_counters(ProcessCounters *m) {
|
||||
typedef BOOL(WINAPI * Fn)(HANDLE, ProcessCounters *, DWORD);
|
||||
static Fn fn;
|
||||
if (!fn) fn = (Fn)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "K32GetProcessMemoryInfo");
|
||||
Counters m;
|
||||
memset(&m, 0, sizeof m);
|
||||
m.cb = sizeof m;
|
||||
if (!fn || !fn(GetCurrentProcess(), &m, sizeof m)) memset(&m, 0, sizeof m);
|
||||
memset(m, 0, sizeof *m);
|
||||
m->cb = sizeof *m;
|
||||
if (!fn || !fn(GetCurrentProcess(), m, sizeof *m)) memset(m, 0, sizeof *m);
|
||||
}
|
||||
void hos_process_memory(uint64_t *private_bytes, uint64_t *working_set) {
|
||||
ProcessCounters m;
|
||||
process_counters(&m);
|
||||
*private_bytes = m.PrivateUsage;
|
||||
*working_set = m.WorkingSetSize;
|
||||
}
|
||||
|
||||
/* ---- (Triforce, 11-22, the performance audit's OS-51, after Source B's) the host's part of a long frame --------------------
|
||||
host/stall_host.h has what is counted and the line. The counters are here, and the look the present makes
|
||||
(nvn_native.c nvnQueuePresentTexture, on the presenting thread): every present takes what was counted since the last
|
||||
one, with the process's page faults and CPU time (one K32GetProcessMemoryInfo and one GetProcessTimes), and a present
|
||||
that comes the stall line's threshold or more after the last one logs it. HOS_STALL_HOST=0, or RX_VK_STALL_LOG=0: none
|
||||
of it, and neither system call */
|
||||
StallHostCause hos_stall_host[STALL_HOST_CAUSES];
|
||||
int64_t hos_stall_host_us = -1;
|
||||
/* (the process's page faults so far, soft and hard together, and its CPU time in the kernel and in its own code, in ns;
|
||||
zero where they cannot be read) */
|
||||
static void process_work(uint64_t *page_faults, int64_t *kernel_ns, int64_t *user_ns) {
|
||||
ProcessCounters m;
|
||||
process_counters(&m);
|
||||
*page_faults = m.PageFaultCount;
|
||||
*kernel_ns = *user_ns = 0;
|
||||
#if defined(_WIN32)
|
||||
FILETIME made, ended, kernel, user;
|
||||
if (GetProcessTimes(GetCurrentProcess(), &made, &ended, &kernel, &user)) {
|
||||
*kernel_ns = (int64_t)(((uint64_t)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime) * 100;
|
||||
*user_ns = (int64_t)(((uint64_t)user.dwHighDateTime << 32) | user.dwLowDateTime) * 100;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void hos_stall_host_presented(void) {
|
||||
void hos_vcore_waits_take(int64_t *ns, int64_t *count, int64_t *longest);
|
||||
extern volatile LONG64 hos_present_count; /* display.c */
|
||||
static int64_t last_ns, last_kernel, last_user;
|
||||
static uint64_t last_faults;
|
||||
static StallHostBound bound = STALL_HOST_BOUND;
|
||||
const int64_t threshold_us = stall_host_us();
|
||||
if (!threshold_us) return;
|
||||
const int64_t now = hos_now_ns();
|
||||
StallHostFrame f;
|
||||
uint64_t faults;
|
||||
int64_t kernel, user;
|
||||
process_work(&faults, &kernel, &user);
|
||||
for (int i = 0; i < STALL_HOST_CAUSES; i++) f.cause[i] = stall_host_take(&hos_stall_host[i]);
|
||||
{ /* (the waits for a virtual core are counted where they end, with the virtual cores' lock held: hos_thread.c) */
|
||||
StallHostCause *w = &f.cause[STALL_HOST_CORE_WAIT];
|
||||
hos_vcore_waits_take(&w->ns, &w->count, &w->longest);
|
||||
w->bytes = 0;
|
||||
}
|
||||
if (last_ns && now - last_ns >= threshold_us * 1000 && stall_host_bound_take(&bound, now)) {
|
||||
char line[1024];
|
||||
f.frame = (int64_t)hos_present_count, f.gap_ns = now - last_ns;
|
||||
f.page_faults = (uint32_t)(faults - last_faults); /* (Windows' count is 32 bits and wraps) */
|
||||
f.kernel_ns = kernel - last_kernel, f.user_ns = user - last_user;
|
||||
f.held = bound.held, bound.held = 0;
|
||||
stall_host_line(&f, line, sizeof line);
|
||||
hos_log("%s", line);
|
||||
}
|
||||
last_ns = now, last_faults = faults, last_kernel = kernel, last_user = user;
|
||||
}
|
||||
|
||||
/* ---- (Triforce) the guest memory census's thread (host/guest_census.h has why) ---------------------------------------------
|
||||
HOS_MEM_CENSUS=<seconds> (5 by default; 0: none): every that many seconds a "mem: census" line: what the runtime handed out
|
||||
of the guest window by kind (the program and the system region, the heap, the address regions and the game's pages in them,
|
||||
|
||||
111
host/mods_music.c
Normal file
111
host/mods_music.c
Normal file
@@ -0,0 +1,111 @@
|
||||
/* (Triforce, 11-20) The Mods screen's music: the game's shop theme under the Mods screen opened from the title, the
|
||||
title's own music faded out meanwhile (mods_music.h has the rule and the owner's words). Here are the file and the
|
||||
sound thread's side.
|
||||
|
||||
Which tune: the Happy Mask Shop's, where the Salesman of the Mods screen keeps his masks, else the Market's shop
|
||||
(the game's shops share the tune): Sound/Resource/Stream/BGM_Shop_Market_Mask.bwav of the player's game files, read
|
||||
through nn_fs.c (a mod's where a mod replaces it), decoded by menu_sound.c's decoder (two channels of DSP-ADPCM at
|
||||
48 kHz, a minute long, looping from 5.6 s to its end as its file says). It is read once, on a thread of its own,
|
||||
when the title's list first shows with its Mods entry, so that it is there when the screen opens; about 23 MB of
|
||||
samples are kept from then on. Nothing is shipped: without the file the title's music plays on as it is.
|
||||
|
||||
When: the options say (gpu/vk/vk_menu.cpp menu_mods_music, HOS_OPT_MODS_MUSIC), asked at each block the sound thread
|
||||
mixes, 5 ms. How loud: MODS_MUSIC_GAIN under the Audio tab's Menu sounds row, and the Master volume over it as over
|
||||
everything; with that row at 0 there is no tune and the title's music stays. */
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "hos.h"
|
||||
#include "menu_sound.h"
|
||||
#include "mods_music.h"
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_OPT_MODS_MUSIC, HOS_OPT_MENU_SOUND_VOLUME) */
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
int rx_option(int id, int otherwise); /* rx_backend.c */
|
||||
uint8_t *hos_fs_rom_file(const char *rel, uint64_t most, uint64_t *size); /* nn_fs.c */
|
||||
|
||||
#define FILE_MOST (32u << 20) /* (the shop theme is 3.3 MB) */
|
||||
#define SECONDS_MOST 600
|
||||
|
||||
static const char *const files[] = {
|
||||
"Sound/Resource/Stream/BGM_Shop_Market_Mask.bwav",
|
||||
"Sound/Resource/Stream/BGM_Shop_Market.bwav",
|
||||
};
|
||||
|
||||
static ModsMusic music; /* (the sound thread's) */
|
||||
/* (what the reading thread made: written before its release store of 2 into loaded, taken by the sound thread after) */
|
||||
static struct { float *pcm; uint32_t frames, loop_start, loop_end; } read_tune;
|
||||
static atomic_int loaded; /* 0 not yet, 1 being read, 2 ready, 3 none in the game's files */
|
||||
|
||||
static uint32_t u32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
|
||||
|
||||
static DWORD WINAPI load_main(void *arg) {
|
||||
(void)arg;
|
||||
for (size_t i = 0; i < sizeof files / sizeof *files; i++) {
|
||||
uint64_t size = 0;
|
||||
uint8_t *b = hos_fs_rom_file(files[i], FILE_MOST, &size);
|
||||
if (!b) continue;
|
||||
uint32_t frames = 0;
|
||||
float *pcm = hos_menu_sound_decode_long(b, size, &frames, SECONDS_MOST);
|
||||
/* (the first channel's: its rate, and the loop's end and start in the file's own samples, 0xffffffff for none) */
|
||||
uint32_t rate = 0, loop_end = 0, loop_start = 0;
|
||||
if (pcm && size >= 0x10 + 0x4c) rate = u32(b + 0x14), loop_end = u32(b + 0x10 + 0x3c), loop_start = u32(b + 0x10 + 0x40);
|
||||
free(b);
|
||||
if (!pcm) {
|
||||
hos_log("mods music: %s is not a BWAV file the Mods screen plays (PCM16 or DSP-ADPCM, one or two channels, at most "
|
||||
"%d s): left out", files[i], SECONDS_MOST);
|
||||
continue;
|
||||
}
|
||||
if (loop_end == 0xffffffffu || !rate) loop_end = loop_start = 0;
|
||||
else { /* (at the mixer's rate, as the samples are) */
|
||||
loop_end = (uint32_t)((uint64_t)loop_end * MODS_MUSIC_RATE / rate);
|
||||
loop_start = (uint32_t)((uint64_t)loop_start * MODS_MUSIC_RATE / rate);
|
||||
}
|
||||
read_tune.pcm = pcm, read_tune.frames = frames, read_tune.loop_start = loop_start, read_tune.loop_end = loop_end;
|
||||
hos_log("mods music: the Mods screen's tune from the game's files: %s, %.1f s%s", files[i],
|
||||
(double)frames / MODS_MUSIC_RATE, loop_end > loop_start ? ", looping" : ", played once");
|
||||
atomic_store_explicit(&loaded, 2, memory_order_release);
|
||||
return 0;
|
||||
}
|
||||
hos_log("mods music: the game's files have no shop theme (%s): the Mods screen has no music, the title's plays on", files[0]);
|
||||
atomic_store_explicit(&loaded, 3, memory_order_release);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hos_mods_music_frame(float *const *channels, int nch, uint32_t frames) {
|
||||
if (nch < 1) return;
|
||||
const int mode = rx_option(HOS_OPT_MODS_MUSIC, 0);
|
||||
int state = atomic_load_explicit(&loaded, memory_order_acquire);
|
||||
if (!mode && state != 2) return; /* (nothing asked and nothing playing: no other option is read) */
|
||||
const int volume = rx_option(HOS_OPT_MENU_SOUND_VOLUME, -1);
|
||||
if (state == 0 && mode >= 1 && volume > 0) {
|
||||
int none = 0;
|
||||
if (atomic_compare_exchange_strong(&loaded, &none, 1)) { /* (read off the sound thread: a disk may take its time) */
|
||||
const HANDLE t = CreateThread(NULL, 0, load_main, NULL, 0, NULL);
|
||||
if (t) CloseHandle(t);
|
||||
else {
|
||||
hos_log("mods music: no thread to read the Mods screen's tune: no music there");
|
||||
atomic_store_explicit(&loaded, 3, memory_order_release);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (state != 2) return;
|
||||
if (!music.pcm) { /* (the tune was read: the sound thread's from here) */
|
||||
music.pcm = read_tune.pcm, music.frames = read_tune.frames;
|
||||
mods_music_loop(&music, read_tune.loop_start, read_tune.loop_end);
|
||||
}
|
||||
const int want = mode == 2 && volume > 0;
|
||||
static int wanted;
|
||||
if (want != wanted) {
|
||||
wanted = want;
|
||||
hos_log(want ? "mods music: the Mods screen opened from the title: the shop theme comes, the title's music goes (0.6 s)"
|
||||
: "mods music: the Mods screen shut: the shop theme goes, the title's music comes back (0.6 s)");
|
||||
}
|
||||
/* (the tune into the front pair; a single channel takes both of its sides) */
|
||||
mods_music_block(&music, want, channels, nch, channels[0], channels[nch > 1 ? 1 : 0], frames,
|
||||
MODS_MUSIC_GAIN * hos_menu_sound_gain(volume) * (nch > 1 ? 1.0f : 0.5f));
|
||||
}
|
||||
66
host/mods_music.h
Normal file
66
host/mods_music.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* (Triforce, 11-20) The Mods screen's music. The owner: "have the in game mods menu in the title screen play the shop
|
||||
theme quietly in the background ... it mutes the title screen music's audio while in the mod menu and fades in/out".
|
||||
While the Mods screen is open from the title's list, the game's own shop theme (the Happy Mask Shop's, the Salesman's:
|
||||
Sound/Resource/Stream/BGM_Shop_Market_Mask.bwav of the player's game files, a mod's where a mod replaces it) plays
|
||||
under the screen, quietly, and the game's own sound (the title's music) is turned down to nothing as it comes and
|
||||
back up as it goes. Nothing is shipped: game files without the tune leave the title's music as it is.
|
||||
|
||||
This is the rule alone, no files and no threads (tests/test_mods_music.py compiles it): one level, 0 to 1, moves a
|
||||
sample at a time toward 1 while the screen wants the music and toward 0 while it does not, over MODS_MUSIC_FADE
|
||||
samples each way. The game's channels are multiplied by 1 - level and the tune is added times level, so the two
|
||||
cross over with no step and no click. The tune loops between the points its file names (the shop theme: from 5.6 s
|
||||
to its end); once the level is back at 0 it is at its top again for the next time the screen opens. */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MODS_MUSIC_RATE 48000 /* (nn_audio.c RATE, menu_sound.c MIX_RATE: the output's) */
|
||||
#define MODS_MUSIC_FADE (MODS_MUSIC_RATE * 6 / 10) /* the cross-over's length each way: 0.6 s */
|
||||
#define MODS_MUSIC_GAIN 0.6f /* "quietly": the file's own level is low (peak 0.25); 4 dB under it */
|
||||
|
||||
typedef struct ModsMusic {
|
||||
const float *pcm; /* interleaved stereo at 48 kHz; NULL: no tune (not read, or not in the files) */
|
||||
uint32_t frames, loop_start, loop_end; /* loop_end > loop_start: it goes round between them; else once, then silence */
|
||||
uint32_t pos; /* the next frame of the tune */
|
||||
float level; /* how much of the tune is heard, 0 to 1; the game's own has 1 - level */
|
||||
} ModsMusic;
|
||||
|
||||
/* The loop a file names, as the tune keeps it: [start, end) inside its frames, or none (0, 0) */
|
||||
static inline void mods_music_loop(ModsMusic *m, uint32_t start, uint32_t end) {
|
||||
if (end > m->frames) end = m->frames;
|
||||
if (end > start + MODS_MUSIC_RATE / 10) m->loop_start = start, m->loop_end = end; /* (a loop under 0.1 s is no tune's) */
|
||||
else m->loop_start = m->loop_end = 0;
|
||||
}
|
||||
|
||||
/* One block of the output. want: the Mods screen is open from the title and its sounds are not off. game: the nch
|
||||
channels the game mixed, frames samples each, turned down by the level sample by sample; the tune is added to left
|
||||
and right (the game's front pair) times gain. With no tune nothing changes, whatever want is. Returns the level. */
|
||||
static inline float mods_music_block(ModsMusic *m, int want, float *const *game, int nch, float *left, float *right,
|
||||
uint32_t frames, float gain) {
|
||||
if (!m->pcm || !m->frames) {
|
||||
m->level = 0.0f;
|
||||
return 0.0f;
|
||||
}
|
||||
const float step = 1.0f / (float)MODS_MUSIC_FADE;
|
||||
if (!want && m->level <= 0.0f) return 0.0f; /* (shut and silent: the block is the game's as it is) */
|
||||
for (uint32_t i = 0; i < frames; i++) {
|
||||
float level = m->level + (want ? step : -step);
|
||||
level = level < 0.0f ? 0.0f : level > 1.0f ? 1.0f : level;
|
||||
m->level = level;
|
||||
const float game_gain = 1.0f - level;
|
||||
for (int ch = 0; ch < nch; ch++) game[ch][i] *= game_gain;
|
||||
if (level <= 0.0f) { /* (gone: at its top for the next time; the rest of the block is the game's) */
|
||||
m->pos = 0;
|
||||
if (!want) break;
|
||||
continue;
|
||||
}
|
||||
if (m->pos < m->frames) {
|
||||
const float g = level * gain;
|
||||
left[i] += m->pcm[2 * (size_t)m->pos] * g;
|
||||
right[i] += m->pcm[2 * (size_t)m->pos + 1] * g;
|
||||
m->pos++;
|
||||
}
|
||||
if (m->loop_end > m->loop_start && m->pos >= m->loop_end) m->pos = m->loop_start;
|
||||
}
|
||||
return m->level;
|
||||
}
|
||||
54
host/movie_pace.h
Normal file
54
host/movie_pace.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/* (Triforce, 11-22, the performance audit's PRESENT-30, after Source A's) The frames paced at 240 while a movie plays at
|
||||
the 60 cap (nn_vi.c movie_pace_decide, pace_fps, realtime_step).
|
||||
|
||||
The game's movie player takes one picture at each of its updates at most, and its updates follow the frame loop. At
|
||||
a cap of 60 the loop runs 60 times a second at best, a movie has 59.94 pictures a second, and every frame that comes
|
||||
late is a picture the player never takes: the decoder skips it (nn_video.c's catch-up). A PC that misses 60 in a
|
||||
movie skips a picture for every frame it misses. While a movie plays, at a cap of exactly 60, the frames are now
|
||||
paced by 240, with the game's own limiter and its time step following as they do at a cap of 240: after a late
|
||||
frame the next comes at once, and the player takes the picture that is due. Game time stays real time. When no
|
||||
movie player has updated for a quarter second the cap is 60 again.
|
||||
|
||||
Not at any other cap (above 60 the loop is fast enough already; under 60 the player asked for it), and not with the
|
||||
Steady frame rate row on, which paces by a period of its own. HOS_MOVIE_PACE=0: the cap kept through movies, as
|
||||
before.
|
||||
|
||||
The rules alone, no game and no clock: tests/test_movie_pace.py compiles them. */
|
||||
#pragma once
|
||||
|
||||
#define MOVIE_PACE_FPS 240 /* what the frames are paced by while a movie plays at the 60 cap */
|
||||
|
||||
/* 1 when this present is paced at MOVIE_PACE_FPS. on: HOS_MOVIE_PACE is not 0; cap: the frame rate cap; steady: the
|
||||
Steady frame rate row is on; movie: a movie player updated in the last quarter second */
|
||||
static inline int movie_pace_raise(int on, int cap, int steady, int movie) { return on && cap == 60 && !steady && movie; }
|
||||
|
||||
/* the rate the pacing, the game's limiter and its time step go by */
|
||||
static inline int movie_pace_fps(int raise, int cap) { return raise ? MOVIE_PACE_FPS : cap; }
|
||||
|
||||
/* The game's own frame limiter (its rate, 0 or less: none) under the rate the frames are paced by. Above 60 it is
|
||||
raised to that rate where it is lower, as it always was for a cap above 60; what it was before is kept, and put back
|
||||
once the rate is 60 or less again, unless the game has written another value over the raised one meanwhile */
|
||||
typedef struct { int before, raised; } MovieLimit;
|
||||
static inline int movie_pace_limit(MovieLimit *m, int limit, int pace) {
|
||||
if (pace > 60) {
|
||||
if (limit > 0 && limit < pace) {
|
||||
if (!m->before) m->before = limit;
|
||||
m->raised = pace;
|
||||
return pace;
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
if (m->before) {
|
||||
const int back = limit == m->raised ? m->before : limit;
|
||||
m->before = m->raised = 0;
|
||||
return back;
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
|
||||
/* The game's frame rate slot when the rate the frames are paced by comes down to 60 or less from `before`: a rate above
|
||||
60 that the time step left in it (only without the frame-time hook, where the slot follows the pace) goes back to 60,
|
||||
which the step then moves as before. Without this the step would find a rate it does not own and leave it alone */
|
||||
static inline int movie_pace_slot(int slot, int before, int pace) {
|
||||
return before > 60 && pace <= 60 && slot > 60 && slot <= before ? 60 : slot;
|
||||
}
|
||||
@@ -152,6 +152,11 @@ uint32_t hos_output_size(int height) {
|
||||
much as the pool (nx2/oot_patches.py): a null pointer (a player's log, v0.3.7). With the 900 MB, 11-15 on an RTX 4070 SUPER: that
|
||||
output with RenoDX HDR boots and played in the world, 17,160 presents at about 50 fps, RenoDX's four shaders in place, no
|
||||
error from the pool (one short session, not every area; its scene target and each copy are 118 MB there). */
|
||||
/* ((Triforce, 11-21, the performance audit's TEX-85) Whether the render targets' heap is held to a size, with no room to
|
||||
give: HOS_RT_POOL_MB set, or the 5120-wide outputs' 900 MB. -1 until hos_rt_pool_size has run. host/shadow_slot.h reserves
|
||||
no stepped storage there unless asked to) */
|
||||
static int rt_pool_pinned = -1;
|
||||
int hos_rt_pool_pinned(void) { return __atomic_load_n(&rt_pool_pinned, __ATOMIC_RELAXED); }
|
||||
uint64_t hos_rt_pool_size(uint64_t game) {
|
||||
const char *e = getenv("HOS_RT_POOL_MB");
|
||||
extern volatile uint64_t hos_wide_out;
|
||||
@@ -169,6 +174,7 @@ uint64_t hos_rt_pool_size(uint64_t game) {
|
||||
const uint64_t asked = (e ? strtoull(e, NULL, 0) << 20 : scaled < minimum ? minimum : is_five_k && scaled > five_k ? five_k : scaled) + (e ? 0 : rgba16f);
|
||||
/* (the 5120 x 2880 output: 900 MB for the whole pool, see above; an explicit HOS_RT_POOL_MB still wins) */
|
||||
const uint64_t v = !e && is_five_k && asked > five_k ? five_k : asked;
|
||||
__atomic_store_n(&rt_pool_pinned, e != NULL || v != asked || (is_five_k && scaled > five_k), __ATOMIC_RELAXED); /* ((Triforce, 11-21, TEX-85)) */
|
||||
static int logged;
|
||||
if (!logged++) {
|
||||
hos_log("applet: render target pool %llu MB (the game's %llu MB)", (unsigned long long)(v >> 20), (unsigned long long)(game >> 20));
|
||||
|
||||
511
host/nn_audio.c
511
host/nn_audio.c
@@ -8,8 +8,10 @@
|
||||
-> sub mixes in dependency order (sub mix volume, then to their destination)
|
||||
-> the final mix (volume) -> the device sink (its channel map: 5.1 as it is to a 5.1 device, else down to stereo)
|
||||
-> waveOut
|
||||
and signals the renderer event the game's audio thread waits on. Effects are accepted and left out (dry signal);
|
||||
aux effects pass through. Played sample counts and released wave buffers go back to the game as on hardware.
|
||||
and signals the renderer event the game's audio thread waits on. A mix's effects (I3DL2 and plain reverbs, delays,
|
||||
biquad filters) run on its buffers in the order the game added them, and splitter destinations have their biquad
|
||||
filters (see "mix effects"); aux effects pass through. Played sample counts and released wave buffers go back to
|
||||
the game as on hardware.
|
||||
The game's parameter changes take effect together at RequestUpdateAudioRenderer, and volume changes ramp over a
|
||||
frame, as the console's renderer does them (see "parameter updates").
|
||||
HOS_AUDIO_DUMP=<file.wav>: also write what is played (checking without speakers); HOS_AUDIO=0: silence;
|
||||
@@ -51,7 +53,7 @@ void hos_system_event_signal(uint64_t key);
|
||||
#define MAX_WB 32 /* queued wave buffers per voice */
|
||||
#define MAX_REL 128
|
||||
|
||||
enum { K_VOICE = 1, K_MIX, K_SPLITTER, K_SINK, K_POOL, K_EFFECT, K_CONFIG };
|
||||
enum { K_VOICE = 1, K_MIX, K_SPLITTER, K_SINK, K_POOL, K_EFFECT, K_CONFIG, K_BIQUAD, K_DELAY };
|
||||
enum { FMT_INVALID, FMT_PCM8, FMT_PCM16, FMT_PCM24, FMT_PCM32, FMT_FLOAT, FMT_ADPCM };
|
||||
enum { PLAY_STARTED, PLAY_STOPPED, PLAY_PAUSED };
|
||||
enum { DEST_NONE, DEST_MIX, DEST_SPLITTER };
|
||||
@@ -71,6 +73,8 @@ typedef struct {
|
||||
int dest_mix[MAX_SPLIT_DESTS];
|
||||
float vol[MAX_SPLIT_DESTS][MAX_CH][MAX_BUF];
|
||||
float ramp[MAX_SPLIT_DESTS][MAX_CH][MAX_BUF]; /* the volumes the last frame ended with */
|
||||
int bq_on[MAX_SPLIT_DESTS][2]; /* each destination's two biquad filters (SetSplitterBiquadFilterParameter) */
|
||||
float bq_b[MAX_SPLIT_DESTS][2][3], bq_a[MAX_SPLIT_DESTS][2][2], bq_z[MAX_SPLIT_DESTS][2][MAX_CH][2];
|
||||
} Splitter;
|
||||
|
||||
typedef struct {
|
||||
@@ -299,12 +303,61 @@ typedef struct {
|
||||
float line[4][REV_LINE];
|
||||
int pos[4], len[4];
|
||||
float lp[4]; /* damping filter states */
|
||||
int seq; /* (the order effects were added in: effects_frame) */
|
||||
/* a plain reverb's (AddReverb) parameters: gains 0 to 1, times as nn::TimeSpan; plain_reverb_params turns them
|
||||
into the fields above each frame */
|
||||
int plain, early_mode, late_mode;
|
||||
float p_early, p_late, p_reverb, p_out, p_dry, p_hf, p_color;
|
||||
int64_t p_decay_ns, p_pre_ns;
|
||||
uint64_t buffer; /* (the game's work buffer, for RemoveReverb) */
|
||||
} Reverb;
|
||||
static Reverb reverbs[MAX_EFFECTS];
|
||||
static int fx_seq;
|
||||
/* HOS_AUDIO_EFFECTS=0: the plain reverb, delay and biquad filter effects and the splitter biquad filters accepted and
|
||||
left out, as before they were emulated (the I3DL2 reverb still runs) */
|
||||
static int effects_on(void) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = !(getenv("HOS_AUDIO_EFFECTS") && atoi(getenv("HOS_AUDIO_EFFECTS")) == 0);
|
||||
return on;
|
||||
}
|
||||
/* an effect left disabled still copies its inputs to its outputs where they differ, as the renderer's do */
|
||||
static void fx_bypass(const int8_t *in, const int8_t *out, int n, Mix *m) {
|
||||
for (int c = 0; c < n; c++)
|
||||
if (in[c] != out[c] && in[c] >= 0 && in[c] < m->buffers && out[c] >= 0 && out[c] < m->buffers)
|
||||
memcpy(m->buf[out[c]], m->buf[in[c]], sizeof m->buf[0]);
|
||||
}
|
||||
static float clamp01(float f) { return f > 0.0f ? (f < 1.0f ? f : 1.0f) : 0.0f; }
|
||||
static float gain_mb(float g) { return g > 0.00001f ? 2000.0f * log10f(g) : -10000.0f; }
|
||||
/* The plain reverb on the same delay network: EarlyMode SmallRoom 0, LargeRoom 1, Hall 2, Cavern 3,
|
||||
NoEarlyReflection 4; LateMode Room 0, Hall 1, MetallicCorridor 2, Cavern 3, MaximumDelay 4. Reverb gain feeds
|
||||
both parts, early and late gain each part, out gain the wet sum; coloration is less diffusion. The DSP's own early
|
||||
and late tables are not ours: the modes set the line lengths and the late part's delay. */
|
||||
static void plain_reverb_params(Reverb *rv) {
|
||||
static const float density[5] = {50, 100, 25, 100, 100}, late_ms[5] = {5, 10, 4, 20, 30};
|
||||
const int em = rv->early_mode >= 0 && rv->early_mode < 5 ? rv->early_mode : 2;
|
||||
const int lm = rv->late_mode >= 0 && rv->late_mode < 5 ? rv->late_mode : 1;
|
||||
const float wet = clamp01(rv->p_out) * clamp01(rv->p_reverb);
|
||||
rv->room = 0;
|
||||
rv->refl = em == 4 ? -10000.0f : gain_mb(wet * clamp01(rv->p_early));
|
||||
rv->reverb = gain_mb(wet * clamp01(rv->p_late));
|
||||
rv->dry = clamp01(rv->p_dry);
|
||||
rv->hf_ratio = rv->p_hf < 0.1f ? 0.1f : rv->p_hf > 1.0f ? 1.0f : rv->p_hf;
|
||||
rv->hf_ref = 5000;
|
||||
rv->diffusion = 100.0f * (1.0f - 0.7f * clamp01(rv->p_color));
|
||||
rv->density = density[lm];
|
||||
rv->decay_ns = rv->p_decay_ns > 0 ? rv->p_decay_ns : 1000000000ll;
|
||||
rv->refl_delay_ns = rv->p_pre_ns > 0 ? rv->p_pre_ns : 0;
|
||||
rv->late_delay_ns = (int64_t)(late_ms[lm] * 1e6f);
|
||||
}
|
||||
static float mb_gain(float mb) { return powf(10.0f, mb / 2000.0f); }
|
||||
static void reverb_frame(Reverb *rv, Mix *m) {
|
||||
static int off = -1;
|
||||
if (off < 0) off = getenv("HOS_AUDIO_NO_REVERB") != NULL;
|
||||
if (rv->plain) {
|
||||
if (!effects_on()) return;
|
||||
if (!rv->enabled) { fx_bypass(rv->in, rv->out, rv->n, m); return; }
|
||||
plain_reverb_params(rv);
|
||||
}
|
||||
if (off || !rv->enabled || rv->n <= 0) return;
|
||||
static const int base_len[4] = {1559, 1617, 1491, 1422};
|
||||
const float density = rv->density > 0 ? rv->density / 100.0f : 1.0f;
|
||||
@@ -370,9 +423,148 @@ static void reverb_frame(Reverb *rv, Mix *m) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- mix effects: biquad filter and delay ----------------------------------------------------------------------
|
||||
As the console's renderer runs them on a mix's buffers, each channel its input buffer to its output buffer
|
||||
(SetXInputOutput; the same buffer in place). Biquad filter: the game's coefficients (TBiquadFilterParameter<float>,
|
||||
the denominator added, as the voices' filters) in transposed direct form II. Delay: a line per channel of the
|
||||
delay time, fed in gain x input plus the lines' outputs through the feedback gain (x 0.98) and the channel spread
|
||||
(to the neighbouring channels), through a one-pole low pass (0.95 x the low pass amount); out = dry gain x input
|
||||
+ the line's output. Both allocate nothing here: the delay's lines are taken by AddDelay. */
|
||||
#define MAX_FX 32
|
||||
#define DELAY_CH 6 /* (the renderer's most) */
|
||||
#define DELAY_MAX (RATE * 5) /* a delay line: 5 s at most */
|
||||
typedef struct {
|
||||
int used, mix, enabled, on, n, seq; /* (on: the parameter's own enable) */
|
||||
int8_t in[MAX_CH], out[MAX_CH];
|
||||
float b[3], a[2], z[MAX_CH][2];
|
||||
} Biquad;
|
||||
typedef struct {
|
||||
int used, mix, enabled, n, seq, cap, len, pos;
|
||||
int8_t in[DELAY_CH], out[DELAY_CH];
|
||||
int64_t time_ns, max_ns;
|
||||
float in_gain, feedback, dry, spread, lowpass;
|
||||
float lp[DELAY_CH];
|
||||
float *line; /* cap frames a channel, one channel after another */
|
||||
uint64_t buffer; /* (the game's work buffer, for RemoveDelay) */
|
||||
} Delay;
|
||||
static Biquad biquads[MAX_FX];
|
||||
static Delay delays[MAX_FX];
|
||||
static void biquad_run(const float *b, const float *a, float *z, const float *in, float *out) { /* (in may be out) */
|
||||
float z0 = z[0], z1 = z[1];
|
||||
for (int i = 0; i < FRAME; i++) {
|
||||
const float x = in[i], y = b[0] * x + z0;
|
||||
z0 = b[1] * x + a[0] * y + z1;
|
||||
z1 = b[2] * x + a[1] * y;
|
||||
out[i] = y;
|
||||
}
|
||||
if (!isfinite(z0) || !isfinite(z1)) { /* (blown up: silence and start over rather than a full-scale burst) */
|
||||
z0 = z1 = 0;
|
||||
memset(out, 0, FRAME * sizeof *out);
|
||||
}
|
||||
z[0] = z0;
|
||||
z[1] = z1;
|
||||
}
|
||||
static void biquad_frame(Biquad *f, Mix *m) {
|
||||
if (!f->enabled || !f->on) { fx_bypass(f->in, f->out, f->n, m); return; }
|
||||
for (int c = 0; c < f->n; c++) {
|
||||
const int bi = f->in[c], bo = f->out[c];
|
||||
if (bi >= 0 && bi < m->buffers && bo >= 0 && bo < m->buffers) biquad_run(f->b, f->a, f->z[c], m->buf[bi], m->buf[bo]);
|
||||
}
|
||||
}
|
||||
static void delay_frame(Delay *dl, Mix *m) {
|
||||
if (!dl->enabled || !dl->line || dl->n <= 0) { fx_bypass(dl->in, dl->out, dl->n, m); return; }
|
||||
const int n = dl->n, cap = dl->cap;
|
||||
const double want = (double)dl->time_ns * RATE / 1e9;
|
||||
const int len = want < 1 ? 1 : want < cap ? (int)want : cap;
|
||||
if (len != dl->len) { dl->len = len; if (dl->pos >= len) dl->pos = 0; }
|
||||
/* the feedback matrix: stereo each line to the other, 4 channels to the two beside it, 5.1 front to centre and
|
||||
the rear on its side, centre to both fronts, LFE to itself alone */
|
||||
const float fb = clamp01(dl->feedback) * 0.98f, spread = clamp01(dl->spread), base = (1.0f - spread) * fb;
|
||||
const float cross = (n == 4 || n == 6 ? 0.5f : 1.0f) * spread * fb;
|
||||
float mx[DELAY_CH][DELAY_CH] = {{0}};
|
||||
for (int c = 0; c < n; c++) mx[c][c] = n == 2 || n == 4 || n == 6 ? base : fb;
|
||||
if (n == 2) mx[0][1] = mx[1][0] = cross;
|
||||
if (n == 4)
|
||||
for (int c = 0; c < 4; c++) mx[c][(c + 1) % 4] = mx[c][(c + 3) % 4] = cross;
|
||||
if (n == 6) {
|
||||
mx[0][2] = mx[0][4] = mx[1][2] = mx[1][5] = mx[2][0] = mx[2][1] = cross;
|
||||
mx[4][0] = mx[4][5] = mx[5][1] = mx[5][4] = cross;
|
||||
mx[3][3] = fb;
|
||||
}
|
||||
const float lp_fb = 0.95f * clamp01(dl->lowpass), lp_in = 1.0f - lp_fb, in_gain = dl->in_gain, dry = dl->dry;
|
||||
const float *src[DELAY_CH];
|
||||
float *dst[DELAY_CH];
|
||||
for (int c = 0; c < n; c++) {
|
||||
src[c] = dl->in[c] >= 0 && dl->in[c] < m->buffers ? m->buf[dl->in[c]] : NULL;
|
||||
dst[c] = dl->out[c] >= 0 && dl->out[c] < m->buffers ? m->buf[dl->out[c]] : NULL;
|
||||
}
|
||||
int pos = dl->pos;
|
||||
for (int i = 0; i < FRAME; i++) {
|
||||
float x[DELAY_CH], d[DELAY_CH];
|
||||
for (int c = 0; c < n; c++) {
|
||||
x[c] = src[c] ? src[c][i] : 0.0f;
|
||||
d[c] = dl->line[c * cap + pos];
|
||||
}
|
||||
for (int c = 0; c < n; c++) {
|
||||
float t = in_gain * x[c];
|
||||
for (int k = 0; k < n; k++) t += mx[c][k] * d[k];
|
||||
dl->lp[c] = lp_fb * dl->lp[c] + lp_in * t;
|
||||
dl->line[c * cap + pos] = dl->lp[c];
|
||||
}
|
||||
for (int c = 0; c < n; c++)
|
||||
if (dst[c]) dst[c][i] = dry * x[c] + d[c];
|
||||
if (++pos >= len) pos = 0;
|
||||
}
|
||||
dl->pos = pos;
|
||||
for (int c = 0; c < n; c++)
|
||||
if (!isfinite(dl->lp[c])) { /* (a NaN would stay in the line for good) */
|
||||
memset(dl->line, 0, (size_t)cap * n * sizeof *dl->line);
|
||||
memset(dl->lp, 0, sizeof dl->lp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* A mix's effects in the order the game added them; HOS_AUDIO_EFFECTS=0: the I3DL2 reverb alone, as before */
|
||||
static void effects_frame(int mix) {
|
||||
struct { int seq, kind, i; } run[MAX_EFFECTS + 2 * MAX_FX];
|
||||
int n = 0;
|
||||
const int all = effects_on();
|
||||
for (int e = 0; e < MAX_EFFECTS; e++)
|
||||
if (reverbs[e].used && reverbs[e].mix == mix) reverb_frame(&reverbs[e], &mixes[mix]);
|
||||
if (reverbs[e].used && reverbs[e].mix == mix && (all || !reverbs[e].plain)) run[n].seq = reverbs[e].seq, run[n].kind = 0, run[n++].i = e;
|
||||
for (int e = 0; all && e < MAX_FX; e++) {
|
||||
if (biquads[e].used && biquads[e].mix == mix) run[n].seq = biquads[e].seq, run[n].kind = 1, run[n++].i = e;
|
||||
if (delays[e].used && delays[e].mix == mix) run[n].seq = delays[e].seq, run[n].kind = 2, run[n++].i = e;
|
||||
}
|
||||
for (int a = 1; a < n; a++)
|
||||
for (int b = a; b > 0 && run[b].seq < run[b - 1].seq; b--) {
|
||||
const int s = run[b].seq, k = run[b].kind, i = run[b].i;
|
||||
run[b] = run[b - 1];
|
||||
run[b - 1].seq = s, run[b - 1].kind = k, run[b - 1].i = i;
|
||||
}
|
||||
for (int k = 0; k < n; k++) {
|
||||
if (run[k].kind == 0) reverb_frame(&reverbs[run[k].i], &mixes[mix]);
|
||||
else if (run[k].kind == 1) biquad_frame(&biquads[run[k].i], &mixes[mix]);
|
||||
else delay_frame(&delays[run[k].i], &mixes[mix]);
|
||||
}
|
||||
}
|
||||
static void effects_drop_mix(int mix) { /* (a mix slot taken again: the effects of the one released there go; the
|
||||
I3DL2 reverb's as before, kept) */
|
||||
for (int e = 0; e < MAX_EFFECTS; e++)
|
||||
if (reverbs[e].plain && reverbs[e].mix == mix) reverbs[e].used = 0;
|
||||
for (int e = 0; e < MAX_FX; e++) {
|
||||
if (biquads[e].mix == mix) biquads[e].used = 0;
|
||||
if (delays[e].mix == mix) delays[e].used = 0; /* (its lines stay for the next AddDelay to free) */
|
||||
}
|
||||
}
|
||||
/* A splitter destination's two biquad filters on one channel of a voice: the filtered samples (in tmp), or in */
|
||||
static const float *splitter_filtered(Splitter *s, int d, int ch, const float *in, float *tmp) {
|
||||
const float *src = in;
|
||||
for (int q = 0; q < 2; q++) {
|
||||
if (!s->bq_on[d][q]) continue;
|
||||
biquad_run(s->bq_b[d][q], s->bq_a[d][q], s->bq_z[d][q][ch], src, tmp);
|
||||
src = tmp;
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
/* ---- the mixer ------------------------------------------------------------------------------------------------- */
|
||||
@@ -435,17 +627,22 @@ static void render_voice(Voice *v) {
|
||||
}
|
||||
} else if (v->dest_kind == DEST_SPLITTER && splitters[v->dest].used) {
|
||||
Splitter *s = &splitters[v->dest];
|
||||
const int fx = effects_on(); /* (the destinations' biquad filters: HOS_AUDIO_EFFECTS=0 leaves them out) */
|
||||
for (int d = 0; d < s->dests; d++) {
|
||||
const int mi = s->dest_mix[d];
|
||||
if (mi < 0 || !mixes[mi].used) continue;
|
||||
Mix *m = &mixes[mi];
|
||||
for (int ch = 0; ch < v->channels && ch < MAX_CH; ch++)
|
||||
const int filtered = fx && (s->bq_on[d][0] || s->bq_on[d][1]);
|
||||
for (int ch = 0; ch < v->channels && ch < MAX_CH; ch++) {
|
||||
float tmp[FRAME];
|
||||
const float *src = filtered ? splitter_filtered(s, d, ch, out[ch], tmp) : out[ch];
|
||||
for (int b = 0; b < m->buffers; b++) {
|
||||
const float g0 = s->ramp[d][ch][b], g1 = s->vol[d][ch][b];
|
||||
if (g0 == 0.0f && g1 == 0.0f) continue;
|
||||
const float dg = (g1 - g0) / FRAME;
|
||||
for (int i = 0; i < FRAME; i++) m->buf[b][i] += out[ch][i] * (g0 + dg * (float)(i + 1));
|
||||
for (int i = 0; i < FRAME; i++) m->buf[b][i] += src[i] * (g0 + dg * (float)(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
memcpy(v->ramp_vol, v->vol, sizeof v->ramp_vol); /* (splitters catch up in render_frame) */
|
||||
@@ -590,6 +787,13 @@ static void render_frame(int16_t *pcm, int channels) { /* 240 frames of the de
|
||||
lim_env = want < lim_env ? want : lim_env + (want - lim_env) * 0.0004f;
|
||||
for (int ch = 0; ch < nch; ch++) out[ch][i] *= lim_in * lim_env * lim_out;
|
||||
}
|
||||
{ /* ((Triforce, 11-20) the Mods screen's music (mods_music.c): while that screen is open from the title, the game's
|
||||
own mix (the title's music) fades to nothing and the game's shop theme plays quietly in its place; after the
|
||||
limiter, the game's mix's own, and before the options' sounds, which stay as loud as they are) */
|
||||
float *channels[6];
|
||||
for (int ch = 0; ch < nch && ch < 6; ch++) channels[ch] = out[ch];
|
||||
hos_mods_music_frame(channels, nch < 6 ? nch : 6, FRAME);
|
||||
}
|
||||
/* (Triforce) the options' own sounds, the game's interface sounds (menu_sound.c), into the front left and right:
|
||||
after the limiter, which is the game's mix's own, and before the Master volume, which is over them too */
|
||||
hos_menu_sound_frame(out[0], out[1], FRAME);
|
||||
@@ -1011,7 +1215,7 @@ static void output_frame(const int16_t *pcm) {
|
||||
HOS_AUDIO_IMMEDIATE=1: every change applied as it comes (as before). */
|
||||
enum { OP_VOICE_RELEASE, OP_VOICE_DEST, OP_VOICE_MIX_VOLUME, OP_VOICE_VOLUME, OP_VOICE_PITCH, OP_VOICE_BIQUAD,
|
||||
OP_VOICE_STATE, OP_VOICE_APPEND, OP_MIX_RELEASE, OP_MIX_VOLUME, OP_MIX_DEST, OP_MIX_MIX_VOLUME, OP_SPLIT_RELEASE,
|
||||
OP_SPLIT_DEST, OP_SPLIT_VOLUME };
|
||||
OP_SPLIT_DEST, OP_SPLIT_VOLUME, OP_SPLIT_BIQUAD };
|
||||
typedef struct {
|
||||
int op, idx, tgt; /* our voice / mix / splitter; a destination mix or splitter (-1: none) */
|
||||
int i0, i1, i2; /* (op-specific: a kind, an index, channels, a state) */
|
||||
@@ -1097,6 +1301,12 @@ static void apply(const Cmd *k) { /* (with the lock held) */
|
||||
s->vol[k->i0][k->i1][k->i2] = k->f;
|
||||
if (s->dest_mix[k->i0] < 0) s->dest_mix[k->i0] = tgt_mix;
|
||||
break;
|
||||
case OP_SPLIT_BIQUAD: /* (destination i0, filter i1, on i2) */
|
||||
if (k->i2 && !s->bq_on[k->i0][k->i1]) memset(s->bq_z[k->i0][k->i1], 0, sizeof s->bq_z[k->i0][k->i1]);
|
||||
s->bq_on[k->i0][k->i1] = k->i2;
|
||||
memcpy(s->bq_b[k->i0][k->i1], k->b, sizeof k->b);
|
||||
memcpy(s->bq_a[k->i0][k->i1], k->a, sizeof k->a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
static void submit(const Cmd *k) { /* (with the lock held) */
|
||||
@@ -1278,6 +1488,7 @@ static int new_mix(uint64_t obj, int final_mix, int buffers) {
|
||||
while (i < MAX_MIXES && mixes[i].used) i++;
|
||||
if (i == MAX_MIXES) { au_unlock(); return 0; }
|
||||
memset(&mixes[i], 0, sizeof mixes[i]);
|
||||
effects_drop_mix(i);
|
||||
mixes[i].used = 1;
|
||||
mixes[i].final_mix = final_mix;
|
||||
mixes[i].buffers = buffers < 1 ? 1 : buffers > MAX_BUF ? MAX_BUF : buffers;
|
||||
@@ -1397,6 +1608,28 @@ static void set_split_volume(Ctx *c) {
|
||||
}
|
||||
HLE(_ZN2nn5audio20SetSplitterMixVolumeEPNS0_12SplitterTypeEiPNS0_10SubMixTypeEfii) { set_split_volume(c); }
|
||||
HLE(_ZN2nn5audio20SetSplitterMixVolumeEPNS0_12SplitterTypeEiPNS0_12FinalMixTypeEfii) { set_split_volume(c); }
|
||||
/* SetSplitterBiquadFilterParameter(SplitterType*, int destinationIndex, mix, int filterIndex,
|
||||
const TBiquadFilterParameter<float>&): bool @0, numerator[3] @4, additive-feedback denominator[2] @16, as the
|
||||
voices'. The game sets both of a destination's filters (off) as it sets the destination, then its own. */
|
||||
static void set_split_biquad(Ctx *c) {
|
||||
au_lock();
|
||||
const int d = (int)ARG(1), f = (int)ARG(3);
|
||||
const uint64_t p = ARG(4);
|
||||
if (splitter_of(ARG(0)) && d >= 0 && d < MAX_SPLIT_DESTS && f >= 0 && f < 2 && p) {
|
||||
Cmd k = {.op = OP_SPLIT_BIQUAD, .idx = splitter_index(ARG(0)), .tgt = mix_index(ARG(2)), .i0 = d, .i1 = f,
|
||||
.i2 = *(uint8_t *)GP(p) != 0};
|
||||
memcpy(k.b, GP(p + 4), sizeof k.b);
|
||||
memcpy(k.a, GP(p + 16), sizeof k.a);
|
||||
submit(&k);
|
||||
static int logged;
|
||||
if (k.i2 && logged++ < 8)
|
||||
AUT("audio: splitter %d destination %d biquad %d: b %.3f %.3f %.3f a %.3f %.3f", k.idx, d, f, k.b[0], k.b[1],
|
||||
k.b[2], k.a[0], k.a[1]);
|
||||
}
|
||||
au_unlock();
|
||||
}
|
||||
HLE(_ZN2nn5audio32SetSplitterBiquadFilterParameterEPNS0_12SplitterTypeEiPNS0_10SubMixTypeEiRKNS0_22TBiquadFilterParameterIfEE) { set_split_biquad(c); }
|
||||
HLE(_ZN2nn5audio32SetSplitterBiquadFilterParameterEPNS0_12SplitterTypeEiPNS0_12FinalMixTypeEiRKNS0_22TBiquadFilterParameterIfEE) { set_split_biquad(c); }
|
||||
|
||||
/* ---- the device sink: AddDeviceSink(config, DeviceSinkType*, FinalMixType*, const int8* input, int count, name) - */
|
||||
HLE(_ZN2nn5audio13AddDeviceSinkEPNS0_19AudioRendererConfigEPNS0_14DeviceSinkTypeEPNS0_12FinalMixTypeEPKaiPKc) {
|
||||
@@ -1570,6 +1803,7 @@ HLE(_ZN2nn5audio16AppendWaveBufferEPNS0_9VoiceTypeEPKNS0_10WaveBufferE) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f) {
|
||||
fwrite(v->coefs, 1, 32, f);
|
||||
hos_guest_touch(w.buffer, (uint64_t)w.size); /* ((Triforce, 11-21, MEM-09) fwrite hands it to the kernel) */
|
||||
const int io = hos_guest_io_begin(GP(w.buffer), (uint64_t)w.size); /* (Triforce: rx_backend.c) */
|
||||
fwrite(GP(w.buffer), 1, (size_t)w.size, f);
|
||||
hos_guest_io_end(io);
|
||||
@@ -1619,9 +1853,13 @@ HLE(_ZN2nn5audio25GetVoicePlayedSampleCountEPKNS0_9VoiceTypeE) {
|
||||
}
|
||||
HLE(_ZN2nn5audio20IsVoiceDroppedFlagOnEPKNS0_9VoiceTypeE) { RET(0); }
|
||||
|
||||
/* ---- effects: accepted, not processed (the dry signal goes through) -------------------------------------------- */
|
||||
/* ---- aux effects: accepted, passed through ----------------------------------------------------------------------
|
||||
The game reads the send buffer and writes the return buffer on its own threads (its CPU effects); with nothing to
|
||||
read, it writes nothing back, and the mix goes through as it came. */
|
||||
HLE(_ZN2nn5audio6AddAuxEPNS0_19AudioRendererConfigEPNS0_7AuxTypeEPNS0_10SubMixTypeEPvS7_m) { RET(0); }
|
||||
HLE(_ZN2nn5audio6AddAuxEPNS0_19AudioRendererConfigEPNS0_7AuxTypeEPNS0_12FinalMixTypeEPvS7_m) { RET(0); }
|
||||
HLE(_ZN2nn5audio13SetAuxEnabledEPNS0_7AuxTypeEb) {}
|
||||
HLE(_ZN2nn5audio17SetAuxInputOutputEPNS0_7AuxTypeEPKaS4_i) {}
|
||||
HLE(_ZN2nn5audio12IsAuxEnabledEPKNS0_7AuxTypeE) { RET(1); }
|
||||
HLE(_ZN2nn5audio14IsAuxRemovableEPKNS0_7AuxTypeE) { RET(1); }
|
||||
HLE(_ZN2nn5audio17ReadAuxSendBufferEPNS0_7AuxTypeEPii) { RET(0); }
|
||||
@@ -1645,14 +1883,14 @@ static Reverb *reverb_of(uint64_t obj) {
|
||||
const int i = get_handle(obj, K_EFFECT);
|
||||
return i >= 0 && i < MAX_EFFECTS && reverbs[i].used ? &reverbs[i] : NULL;
|
||||
}
|
||||
static void add_reverb(Ctx *c) {
|
||||
static void add_reverb(Ctx *c, int plain) {
|
||||
au_lock();
|
||||
int e = 0;
|
||||
while (e < MAX_EFFECTS && reverbs[e].used) e++;
|
||||
const int m = mix_index(ARG(4));
|
||||
if (e == MAX_EFFECTS || m < 0) {
|
||||
au_unlock();
|
||||
AUT("audio: i3dl2 reverb refused (mix %d)", m);
|
||||
AUT("audio: %s reverb refused (mix %d)", plain ? "plain" : "i3dl2", m);
|
||||
RET(0x1A9 << 9 | 153); /* (out of effects: an audio result) */
|
||||
return;
|
||||
}
|
||||
@@ -1666,13 +1904,22 @@ static void add_reverb(Ctx *c) {
|
||||
rv->room = -1000; rv->room_hf = -100; rv->refl = -2602; rv->reverb = 200; rv->hf_ratio = 0.83f;
|
||||
rv->diffusion = 100; rv->density = 100; rv->hf_ref = 5000; rv->dry = 1.0f;
|
||||
rv->decay_ns = 1490000000ll; rv->refl_delay_ns = 7000000; rv->late_delay_ns = 11000000;
|
||||
rv->seq = ++fx_seq;
|
||||
rv->buffer = ARG(2);
|
||||
if (plain) { /* (until the game sets its own: a hall, half early and late) */
|
||||
rv->plain = 1;
|
||||
rv->early_mode = 2;
|
||||
rv->late_mode = 1;
|
||||
rv->p_early = rv->p_late = 0.5f; rv->p_reverb = rv->p_out = rv->p_dry = 1.0f; rv->p_hf = 0.5f; rv->p_color = 0.5f;
|
||||
rv->p_decay_ns = 1500000000ll; rv->p_pre_ns = 10000000;
|
||||
}
|
||||
put_handle(ARG(1), K_EFFECT, e);
|
||||
au_unlock();
|
||||
AUT("audio: i3dl2 reverb %d on mix %d, %d channels", e, m, rv->n);
|
||||
AUT("audio: %s reverb %d on mix %d, %d channels", plain ? "plain" : "i3dl2", e, m, rv->n);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn5audio14AddI3dl2ReverbEPNS0_19AudioRendererConfigEPNS0_15I3dl2ReverbTypeEPvmPNS0_10SubMixTypeEi) { add_reverb(c); }
|
||||
HLE(_ZN2nn5audio14AddI3dl2ReverbEPNS0_19AudioRendererConfigEPNS0_15I3dl2ReverbTypeEPvmPNS0_12FinalMixTypeEi) { add_reverb(c); }
|
||||
HLE(_ZN2nn5audio14AddI3dl2ReverbEPNS0_19AudioRendererConfigEPNS0_15I3dl2ReverbTypeEPvmPNS0_10SubMixTypeEi) { add_reverb(c, 0); }
|
||||
HLE(_ZN2nn5audio14AddI3dl2ReverbEPNS0_19AudioRendererConfigEPNS0_15I3dl2ReverbTypeEPvmPNS0_12FinalMixTypeEi) { add_reverb(c, 0); }
|
||||
static void ret_float(Ctx *c, float f) {
|
||||
uint32_t bits;
|
||||
memcpy(&bits, &f, 4);
|
||||
@@ -1737,3 +1984,241 @@ HLE(_ZN2nn5audio25GetI3dl2ReverbInputOutputEPKNS0_15I3dl2ReverbTypeEPaS4_Pii) {
|
||||
}
|
||||
if (ARG(3)) G32(ARG(3)) = (uint32_t)n;
|
||||
}
|
||||
|
||||
/* ---- plain reverb: AddReverb(config, ReverbType*, buffer, size, SubMixType* / FinalMixType*, channels) ---------- */
|
||||
static Reverb *plain_of(uint64_t obj) {
|
||||
Reverb *rv = reverb_of(obj);
|
||||
return rv && rv->plain ? rv : NULL;
|
||||
}
|
||||
HLE(_ZN2nn5audio9AddReverbEPNS0_19AudioRendererConfigEPNS0_10ReverbTypeEPvmPNS0_10SubMixTypeEi) { add_reverb(c, 1); }
|
||||
HLE(_ZN2nn5audio9AddReverbEPNS0_19AudioRendererConfigEPNS0_10ReverbTypeEPvmPNS0_12FinalMixTypeEi) { add_reverb(c, 1); }
|
||||
static void remove_reverb(Ctx *c) { /* (RemoveReverb(config, ReverbType*, mix): the work buffer back) */
|
||||
au_lock();
|
||||
Reverb *rv = plain_of(ARG(1));
|
||||
uint64_t buffer = 0;
|
||||
if (rv) rv->used = 0, buffer = rv->buffer;
|
||||
au_unlock();
|
||||
if (rv) G64(ARG(1)) = 0;
|
||||
RET(buffer);
|
||||
}
|
||||
HLE(_ZN2nn5audio12RemoveReverbEPNS0_19AudioRendererConfigEPNS0_10ReverbTypeEPNS0_10SubMixTypeE) { remove_reverb(c); }
|
||||
HLE(_ZN2nn5audio12RemoveReverbEPNS0_19AudioRendererConfigEPNS0_10ReverbTypeEPNS0_12FinalMixTypeE) { remove_reverb(c); }
|
||||
HLE(_ZN2nn5audio18SetReverbEarlyModeEPNS0_10ReverbTypeENS1_9EarlyModeE) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->early_mode = (int)ARG(1); }
|
||||
HLE(_ZN2nn5audio17SetReverbLateModeEPNS0_10ReverbTypeENS1_8LateModeE) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->late_mode = (int)ARG(1); }
|
||||
HLE(_ZN2nn5audio18SetReverbEarlyGainEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_early = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio17SetReverbLateGainEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_late = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio19SetReverbReverbGainEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_reverb = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio16SetReverbOutGainEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_out = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio16SetReverbDryGainEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_dry = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio32SetReverbHighFrequencyDecayRatioEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_hf = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio19SetReverbColorationEPNS0_10ReverbTypeEf) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_color = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio18SetReverbDecayTimeEPNS0_10ReverbTypeENS_8TimeSpanE) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_decay_ns = (int64_t)ARG(1); }
|
||||
HLE(_ZN2nn5audio21SetReverbPredelayTimeEPNS0_10ReverbTypeENS_8TimeSpanE) { Reverb *rv = plain_of(ARG(0)); if (rv) rv->p_pre_ns = (int64_t)ARG(1); }
|
||||
HLE(_ZN2nn5audio16SetReverbEnabledEPNS0_10ReverbTypeEb) {
|
||||
Reverb *rv = plain_of(ARG(0));
|
||||
if (rv) rv->enabled = (int)(ARG(1) & 1);
|
||||
AUT("audio: plain reverb %s", ARG(1) & 1 ? "on" : "off");
|
||||
}
|
||||
HLE(_ZN2nn5audio15IsReverbEnabledEPKNS0_10ReverbTypeE) { Reverb *rv = plain_of(ARG(0)); RET(rv ? rv->enabled : 0); }
|
||||
HLE(_ZN2nn5audio20SetReverbInputOutputEPNS0_10ReverbTypeEPKaS4_i) {
|
||||
Reverb *rv = plain_of(ARG(0));
|
||||
if (!rv) return;
|
||||
const int n = (int)ARG(3) > MAX_CH ? MAX_CH : (int)ARG(3) < 0 ? 0 : (int)ARG(3);
|
||||
for (int k = 0; k < n; k++) {
|
||||
rv->in[k] = *(int8_t *)GP(ARG(1) + k);
|
||||
rv->out[k] = *(int8_t *)GP(ARG(2) + k);
|
||||
}
|
||||
rv->n = n;
|
||||
}
|
||||
HLE(_ZN2nn5audio20GetReverbInputOutputEPKNS0_10ReverbTypeEPaS4_Pii) {
|
||||
Reverb *rv = plain_of(ARG(0));
|
||||
const int n = rv ? rv->n : 0;
|
||||
for (int k = 0; k < n && k < (int)ARG(4); k++) {
|
||||
*(int8_t *)GP(ARG(1) + k) = rv->in[k];
|
||||
*(int8_t *)GP(ARG(2) + k) = rv->out[k];
|
||||
}
|
||||
if (ARG(3)) G32(ARG(3)) = (uint32_t)n;
|
||||
}
|
||||
|
||||
/* ---- biquad filter: AddBiquadFilter(config, BiquadFilterType*, SubMixType* / FinalMixType*) -------------------- */
|
||||
static Biquad *biquad_of(uint64_t obj) {
|
||||
const int i = get_handle(obj, K_BIQUAD);
|
||||
return i >= 0 && i < MAX_FX && biquads[i].used ? &biquads[i] : NULL;
|
||||
}
|
||||
static void add_biquad(Ctx *c) {
|
||||
au_lock();
|
||||
int e = 0;
|
||||
while (e < MAX_FX && biquads[e].used) e++;
|
||||
const int m = mix_index(ARG(2));
|
||||
if (e == MAX_FX || m < 0) {
|
||||
au_unlock();
|
||||
AUT("audio: biquad filter refused (mix %d)", m);
|
||||
RET(0x1A9 << 9 | 153);
|
||||
return;
|
||||
}
|
||||
Biquad *f = &biquads[e];
|
||||
memset(f, 0, sizeof *f);
|
||||
f->used = 1;
|
||||
f->mix = m;
|
||||
f->seq = ++fx_seq;
|
||||
f->n = mixes[m].buffers < MAX_CH ? mixes[m].buffers : MAX_CH; /* (until SetBiquadFilterInputOutput) */
|
||||
for (int k = 0; k < MAX_CH; k++) f->in[k] = f->out[k] = (int8_t)k;
|
||||
f->b[0] = 1.0f;
|
||||
put_handle(ARG(1), K_BIQUAD, e);
|
||||
au_unlock();
|
||||
AUT("audio: biquad filter %d on mix %d", e, m);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn5audio15AddBiquadFilterEPNS0_19AudioRendererConfigEPNS0_16BiquadFilterTypeEPNS0_10SubMixTypeE) { add_biquad(c); }
|
||||
HLE(_ZN2nn5audio15AddBiquadFilterEPNS0_19AudioRendererConfigEPNS0_16BiquadFilterTypeEPNS0_12FinalMixTypeE) { add_biquad(c); }
|
||||
static void remove_biquad(Ctx *c) {
|
||||
au_lock();
|
||||
Biquad *f = biquad_of(ARG(1));
|
||||
if (f) f->used = 0;
|
||||
au_unlock();
|
||||
if (f) G64(ARG(1)) = 0;
|
||||
}
|
||||
HLE(_ZN2nn5audio18RemoveBiquadFilterEPNS0_19AudioRendererConfigEPNS0_16BiquadFilterTypeEPNS0_10SubMixTypeE) { remove_biquad(c); }
|
||||
HLE(_ZN2nn5audio18RemoveBiquadFilterEPNS0_19AudioRendererConfigEPNS0_16BiquadFilterTypeEPNS0_12FinalMixTypeE) { remove_biquad(c); }
|
||||
HLE(_ZN2nn5audio26SetBiquadFilterInputOutputEPNS0_16BiquadFilterTypeEPKaS4_i) {
|
||||
au_lock();
|
||||
Biquad *f = biquad_of(ARG(0));
|
||||
if (f) {
|
||||
const int n = (int)ARG(3) > MAX_CH ? MAX_CH : (int)ARG(3) < 0 ? 0 : (int)ARG(3);
|
||||
for (int k = 0; k < n; k++) {
|
||||
f->in[k] = *(int8_t *)GP(ARG(1) + k);
|
||||
f->out[k] = *(int8_t *)GP(ARG(2) + k);
|
||||
}
|
||||
f->n = n;
|
||||
AUT("audio: biquad filter %d channels: %d", (int)(f - biquads), n);
|
||||
}
|
||||
au_unlock();
|
||||
}
|
||||
/* TBiquadFilterParameter<float>: bool @0, numerator[3] @4, additive-feedback denominator[2] @16 (as the voices').
|
||||
The filter runs while both it (SetBiquadFilterEnabled) and its parameter are on. */
|
||||
HLE(_ZN2nn5audio24SetBiquadFilterParameterEPNS0_16BiquadFilterTypeERKNS0_22TBiquadFilterParameterIfEE) {
|
||||
au_lock();
|
||||
Biquad *f = biquad_of(ARG(0));
|
||||
const uint64_t p = ARG(1);
|
||||
if (f && p) {
|
||||
const int on = *(uint8_t *)GP(p) != 0;
|
||||
if (on && !f->on) memset(f->z, 0, sizeof f->z);
|
||||
f->on = on;
|
||||
memcpy(f->b, GP(p + 4), sizeof f->b);
|
||||
memcpy(f->a, GP(p + 16), sizeof f->a);
|
||||
static int logged;
|
||||
if (on && logged++ < 8)
|
||||
AUT("audio: biquad filter %d: b %.3f %.3f %.3f a %.3f %.3f", (int)(f - biquads), f->b[0], f->b[1], f->b[2],
|
||||
f->a[0], f->a[1]);
|
||||
}
|
||||
au_unlock();
|
||||
}
|
||||
HLE(_ZN2nn5audio22SetBiquadFilterEnabledEPNS0_16BiquadFilterTypeEb) {
|
||||
au_lock();
|
||||
Biquad *f = biquad_of(ARG(0));
|
||||
if (f) {
|
||||
if ((ARG(1) & 1) && !f->enabled) memset(f->z, 0, sizeof f->z);
|
||||
f->enabled = (int)(ARG(1) & 1);
|
||||
}
|
||||
au_unlock();
|
||||
}
|
||||
HLE(_ZN2nn5audio21IsBiquadFilterEnabledEPKNS0_16BiquadFilterTypeE) { Biquad *f = biquad_of(ARG(0)); RET(f ? f->enabled : 0); }
|
||||
|
||||
/* ---- delay: AddDelay(config, DelayType*, buffer, size, SubMixType* / FinalMixType*, TimeSpan delayTimeMax,
|
||||
channels). The lines are ours, taken here (the game's buffer is not used); until the game sets its gains the
|
||||
delay passes the dry signal alone. There is no out gain to set: the line's output is added at 1. */
|
||||
static Delay *delay_of(uint64_t obj) {
|
||||
const int i = get_handle(obj, K_DELAY);
|
||||
return i >= 0 && i < MAX_FX && delays[i].used ? &delays[i] : NULL;
|
||||
}
|
||||
static void add_delay(Ctx *c) {
|
||||
const int64_t max_ns = (int64_t)ARG(5);
|
||||
const int n = (int)ARG(6) < 1 ? 1 : (int)ARG(6) > DELAY_CH ? DELAY_CH : (int)ARG(6);
|
||||
const double frames = (double)max_ns * RATE / 1e9;
|
||||
const int cap = frames < 1 ? 1 : frames < DELAY_MAX ? (int)frames + 1 : DELAY_MAX;
|
||||
float *line = (float *)calloc((size_t)cap * n, sizeof *line); /* (on the game's thread, before the lock) */
|
||||
au_lock();
|
||||
int e = 0;
|
||||
while (e < MAX_FX && delays[e].used) e++;
|
||||
const int m = mix_index(ARG(4));
|
||||
if (e == MAX_FX || m < 0) {
|
||||
au_unlock();
|
||||
free(line);
|
||||
AUT("audio: delay refused (mix %d)", m);
|
||||
RET(0x1A9 << 9 | 153);
|
||||
return;
|
||||
}
|
||||
Delay *dl = &delays[e];
|
||||
float *old = dl->line;
|
||||
memset(dl, 0, sizeof *dl);
|
||||
dl->used = 1;
|
||||
dl->mix = m;
|
||||
dl->seq = ++fx_seq;
|
||||
dl->n = n;
|
||||
for (int k = 0; k < DELAY_CH; k++) dl->in[k] = dl->out[k] = (int8_t)k;
|
||||
dl->line = line;
|
||||
dl->cap = line ? cap : 0;
|
||||
dl->max_ns = dl->time_ns = max_ns;
|
||||
dl->dry = 1.0f;
|
||||
dl->buffer = ARG(2);
|
||||
put_handle(ARG(1), K_DELAY, e);
|
||||
au_unlock();
|
||||
free(old);
|
||||
AUT("audio: delay %d on mix %d, %d channels, %.0f ms at most", e, m, n, (double)max_ns / 1e6);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn5audio8AddDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPvmPNS0_10SubMixTypeENS_8TimeSpanEi) { add_delay(c); }
|
||||
HLE(_ZN2nn5audio8AddDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPvmPNS0_12FinalMixTypeENS_8TimeSpanEi) { add_delay(c); }
|
||||
static void remove_delay(Ctx *c) { /* (RemoveDelay(config, DelayType*, mix): the work buffer back) */
|
||||
au_lock();
|
||||
Delay *dl = delay_of(ARG(1));
|
||||
uint64_t buffer = 0;
|
||||
float *line = NULL;
|
||||
if (dl) dl->used = 0, buffer = dl->buffer, line = dl->line, dl->line = NULL, dl->cap = 0;
|
||||
au_unlock();
|
||||
free(line);
|
||||
if (dl) G64(ARG(1)) = 0;
|
||||
RET(buffer);
|
||||
}
|
||||
HLE(_ZN2nn5audio11RemoveDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPNS0_10SubMixTypeE) { remove_delay(c); }
|
||||
HLE(_ZN2nn5audio11RemoveDelayEPNS0_19AudioRendererConfigEPNS0_9DelayTypeEPNS0_12FinalMixTypeE) { remove_delay(c); }
|
||||
HLE(_ZN2nn5audio16IsDelayRemovableEPKNS0_9DelayTypeE) { RET(1); }
|
||||
HLE(_ZN2nn5audio15SetDelayEnabledEPNS0_9DelayTypeEb) {
|
||||
au_lock();
|
||||
Delay *dl = delay_of(ARG(0));
|
||||
if (dl) {
|
||||
if ((ARG(1) & 1) && !dl->enabled && dl->line) { /* (switched on: the lines start empty) */
|
||||
memset(dl->line, 0, (size_t)dl->cap * dl->n * sizeof *dl->line);
|
||||
memset(dl->lp, 0, sizeof dl->lp);
|
||||
dl->pos = 0;
|
||||
}
|
||||
dl->enabled = (int)(ARG(1) & 1);
|
||||
}
|
||||
au_unlock();
|
||||
AUT("audio: delay %s", ARG(1) & 1 ? "on" : "off");
|
||||
}
|
||||
HLE(_ZN2nn5audio14IsDelayEnabledEPKNS0_9DelayTypeE) { Delay *dl = delay_of(ARG(0)); RET(dl ? dl->enabled : 0); }
|
||||
HLE(_ZN2nn5audio12SetDelayTimeEPNS0_9DelayTypeENS_8TimeSpanE) {
|
||||
Delay *dl = delay_of(ARG(0));
|
||||
if (dl) dl->time_ns = (int64_t)ARG(1) < 0 ? 0 : (int64_t)ARG(1) > dl->max_ns ? dl->max_ns : (int64_t)ARG(1);
|
||||
}
|
||||
HLE(_ZN2nn5audio12GetDelayTimeEPKNS0_9DelayTypeE) { Delay *dl = delay_of(ARG(0)); RET(dl ? (uint64_t)dl->time_ns : 0); }
|
||||
HLE(_ZN2nn5audio14SetDelayInGainEPNS0_9DelayTypeEf) { Delay *dl = delay_of(ARG(0)); if (dl) dl->in_gain = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio20SetDelayFeedbackGainEPNS0_9DelayTypeEf) { Delay *dl = delay_of(ARG(0)); if (dl) dl->feedback = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio15SetDelayDryGainEPNS0_9DelayTypeEf) { Delay *dl = delay_of(ARG(0)); if (dl) dl->dry = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio21SetDelayChannelSpreadEPNS0_9DelayTypeEf) { Delay *dl = delay_of(ARG(0)); if (dl) dl->spread = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio21SetDelayLowPassAmountEPNS0_9DelayTypeEf) { Delay *dl = delay_of(ARG(0)); if (dl) dl->lowpass = farg(c, 0); }
|
||||
HLE(_ZN2nn5audio19SetDelayInputOutputEPNS0_9DelayTypeEPKaS4_i) {
|
||||
au_lock();
|
||||
Delay *dl = delay_of(ARG(0));
|
||||
if (dl) { /* (no more channels than AddDelay took lines for) */
|
||||
const int n = (int)ARG(3) > dl->n ? dl->n : (int)ARG(3) < 0 ? 0 : (int)ARG(3);
|
||||
for (int k = 0; k < n; k++) {
|
||||
dl->in[k] = *(int8_t *)GP(ARG(1) + k);
|
||||
dl->out[k] = *(int8_t *)GP(ARG(2) + k);
|
||||
}
|
||||
dl->n = n;
|
||||
AUT("audio: delay %d channels: %d", (int)(dl - delays), n);
|
||||
}
|
||||
au_unlock();
|
||||
}
|
||||
|
||||
49
host/nn_fs.c
49
host/nn_fs.c
@@ -7,6 +7,7 @@
|
||||
hos_romfs_dir and hos_user_dir are set by hos_main.c. Reads are positional (ReadFile with an offset), so any number
|
||||
of guest threads can read one file at once. Results: switchbrew "Error codes" (module 2 = fs). */
|
||||
#include "hos.h"
|
||||
#include "stall_host.h" /* ((Triforce, 11-22, OS-51) the host's part of a long frame, counted where it is spent) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -532,8 +533,10 @@ HLE(_ZN2nn2fs6CommitEPKc) {
|
||||
if (i < ntx) {
|
||||
const int away = hos_vcore_away();
|
||||
const ULONGLONG t0 = GetTickCount64();
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
|
||||
tx_wait_locked(); /* (the Commit before this one: its staging folder is this one's) */
|
||||
const int staged = save_tx_stage(&tx_mounts[i].p);
|
||||
stall_host_end(STALL_HOST_COMMIT, stall0, 0);
|
||||
if (staged == 1) {
|
||||
tx_finish_paths = tx_mounts[i].p;
|
||||
tx_thread = CreateThread(NULL, 0, tx_finish_main, NULL, 0, NULL);
|
||||
@@ -562,9 +565,27 @@ HLE(_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci) {
|
||||
char path[HOS_FS_PATH];
|
||||
int writable = 0;
|
||||
const char *g = gstr(ARG(1));
|
||||
if (!host_path(g, path, sizeof path, &writable)) { hos_log("fs: OpenFile(%s): unknown mount", g); RET(R_PATH_NOT_FOUND); return; }
|
||||
/* ((Triforce, 11-21, the performance audit's IO-24, after Source A's) The virtual core is given up from here, not only
|
||||
around CreateFileA below: host_path reaches the button prompts' builder (host/prompts.c hos_prompts_romfs), which
|
||||
builds a file at its first open, a quarter of a second and more for the font or a texture, while the other openers
|
||||
wait for it in Sleep(1); none of them holds a core now. Only host code runs from here to the open: the guest's path
|
||||
is read above, and every return takes the core back before it answers the game. HOS_OPEN_KEEP_CORE=1: given up
|
||||
around CreateFileA alone, as before */
|
||||
static int open_keep_core = -1;
|
||||
if (open_keep_core < 0) open_keep_core = getenv("HOS_OPEN_KEEP_CORE") != NULL;
|
||||
int away = open_keep_core ? 0 : hos_vcore_away();
|
||||
if (!host_path(g, path, sizeof path, &writable)) {
|
||||
hos_log("fs: OpenFile(%s): unknown mount", g);
|
||||
if (away) hos_vcore_back();
|
||||
RET(R_PATH_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
int want_write = (ARG(2) & 6) != 0;
|
||||
if (want_write && !writable) { RET(R_PATH_NOT_FOUND); return; }
|
||||
if (want_write && !writable) {
|
||||
if (away) hos_vcore_back();
|
||||
RET(R_PATH_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
{ void hos_bench_file_opened(const char *guest); hos_bench_file_opened(g); } /* ((Triforce) benchmark mode: which save file loads */
|
||||
/* (mkw-recomp) HOS_NO_MOVIES=1: MKW's WebM movies (cup openings, billboards, emblems) are not found, and the
|
||||
game goes on without them (decoded by nn_video.c otherwise) */
|
||||
@@ -573,6 +594,7 @@ HLE(_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci) {
|
||||
if (n > 5 && !_stricmp(g + n - 5, ".webm")) {
|
||||
static int logged;
|
||||
if (!logged++) hos_log("fs: OpenFile(%s): movies are left out (HOS_NO_MOVIES)", g);
|
||||
if (away) hos_vcore_back();
|
||||
RET(R_PATH_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
@@ -580,7 +602,11 @@ HLE(_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci) {
|
||||
{ int hos_ultrawide_redirect(const char *guest, char *out, size_t cap); /* (totk_ultrawide_ui.c: the HUD) */
|
||||
char alt[HOS_FS_PATH];
|
||||
if (!want_write && hos_ultrawide_redirect(g, alt, sizeof alt)) snprintf(path, sizeof path, "%s", alt); }
|
||||
if (!want_write && hos_blank_cached_icon(g, path)) { RET(R_PATH_NOT_FOUND); return; }
|
||||
if (!want_write && hos_blank_cached_icon(g, path)) {
|
||||
if (away) hos_vcore_back();
|
||||
RET(R_PATH_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
{ /* HOS_FS_OVERLAY=<folder> (a test): a file of the game's data found there is read in place of the game's own */
|
||||
const char *ov = getenv("HOS_FS_OVERLAY");
|
||||
const char *rel = !want_write && ov ? strstr(g, ":/") : NULL;
|
||||
@@ -595,7 +621,7 @@ HLE(_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci) {
|
||||
}
|
||||
/* (mkw-recomp 16bc3bc: the host's file calls block, so the virtual core goes to another thread meanwhile, as
|
||||
Horizon would run another while this one waits for the file system. OoT's ARes loaders held it up to 250 ms) */
|
||||
const int away = hos_vcore_away();
|
||||
if (open_keep_core) away = hos_vcore_away(); /* ((Triforce, 11-21, IO-24) otherwise given up already, above) */
|
||||
HANDLE h = CreateFileA(path, GENERIC_READ | (want_write ? GENERIC_WRITE : 0), FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (away) hos_vcore_back();
|
||||
@@ -629,6 +655,10 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
|
||||
hos_sleep_ns(delay_ns);
|
||||
}
|
||||
const int away = hos_vcore_away(); /* (as OpenFile: no core held through the read) */
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the read itself, not the wait for the core after it) */
|
||||
/* ((Triforce, 11-21, MEM-09) the kernel's ReadFile does not fault pages in: heap pages nothing touched yet (committed
|
||||
as they are touched now, heap_commit.h) are touched for it first) */
|
||||
hos_guest_touch(buf, size);
|
||||
const int io = hos_guest_io_begin(GP(buf), size); /* (Triforce: the kernel writes there: rx_backend.c) */
|
||||
while (done < size) {
|
||||
OVERLAPPED ov = {0};
|
||||
@@ -640,6 +670,7 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
|
||||
done += got;
|
||||
}
|
||||
hos_guest_io_end(io);
|
||||
stall_host_end(STALL_HOST_READ, stall0, done);
|
||||
if (away) hos_vcore_back();
|
||||
HOS_WRITTEN(GP(buf), (size_t)done); /* (into guest memory: the GPU may read it) */
|
||||
{ /* HOS_FS_WATCH=<lo hex>:<hi hex> (debugging): reads into that guest range, with the file */
|
||||
@@ -683,9 +714,12 @@ HLE(_ZN2nn2fs9WriteFileENS0_10FileHandleElPKvmRKNS0_11WriteOptionE) {
|
||||
DWORD put = 0;
|
||||
/* (hookshot) from guest memory only, and the whole size: a DWORD cut wrote less than asked and said so */
|
||||
if (ARG(3) <= 0xFFFFFFFFu && hos_guest_range(ARG(2), ARG(3))) {
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
|
||||
hos_guest_touch(ARG(2), ARG(3)); /* ((Triforce, 11-21, MEM-09) as the read: touched before the kernel reads them) */
|
||||
const int io = hos_guest_io_begin(GP(ARG(2)), ARG(3)); /* (Triforce: the kernel reads there: rx_backend.c) */
|
||||
WriteFile(fh->h, GP(ARG(2)), (DWORD)ARG(3), &put, &ov);
|
||||
hos_guest_io_end(io);
|
||||
stall_host_end(STALL_HOST_WRITE, stall0, put);
|
||||
}
|
||||
RET(put == ARG(3) ? 0 : 0x2EE202);
|
||||
}
|
||||
@@ -697,7 +731,12 @@ HLE(_ZN2nn2fs11SetFileSizeENS0_10FileHandleEl) {
|
||||
SetEndOfFile(fh->h);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn2fs9FlushFileENS0_10FileHandleE) { FlushFileBuffers(get_handle(c, ARG(0))->h); RET(0); }
|
||||
HLE(_ZN2nn2fs9FlushFileENS0_10FileHandleE) {
|
||||
const int64_t stall0 = stall_host_begin(); /* ((Triforce, OS-51) the host's part of a long frame: stall_host.h) */
|
||||
FlushFileBuffers(get_handle(c, ARG(0))->h);
|
||||
stall_host_end(STALL_HOST_FLUSH, stall0, 0);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn2fs9CloseFileENS0_10FileHandleE) {
|
||||
Handle *fh = get_handle(c, ARG(0));
|
||||
CloseHandle(fh->h);
|
||||
|
||||
@@ -963,7 +963,6 @@ static int flight_jump_shortcut(uint64_t *buttons, int mode, int menu_open, int
|
||||
static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
*buttons = 0;
|
||||
memset(stick, 0, 4 * sizeof *stick);
|
||||
void hos_mouse_native_stick(int held);
|
||||
scripted_input(buttons, stick);
|
||||
/* ((Triforce, 11-19) benchmark mode (host/hos_bench.c) drives the game itself: its presses and stick alone, with the window
|
||||
in front or behind other windows; the player's devices do not count while it runs (a mouse or a pad would move the camera
|
||||
@@ -1090,9 +1089,6 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
controller's stick and the scripted presses are as they are) */
|
||||
if (walk_vk && key(walk_vk)) walk_keys(keys, rx_option(HOS_OPT_WALK_SCALE, 51));
|
||||
stick_max(stick, keys);
|
||||
/* (hookshot, mouse_native.c) the keys and the pads are the right stick too: while they turn the camera, its angles are
|
||||
the native mouse's new start, so the two do not undo each other */
|
||||
hos_mouse_native_stick(stick[2] != 0 || stick[3] != 0);
|
||||
}
|
||||
|
||||
static void vibration_send(uint64_t handle, uint64_t value);
|
||||
@@ -1212,6 +1208,13 @@ static void npad_states(Ctx *c) {
|
||||
}
|
||||
if (connected) {
|
||||
read_input(&buttons, stick);
|
||||
/* (mouse_native.c) the keys, the pads and the scripted presses are the right stick too: while they turn the camera,
|
||||
its angles are the native mouse's new start, so the two do not undo each other. ((Triforce, 11-22) Told after
|
||||
every read, not at the end of the window-in-front one alone: with the window behind another and Controller in
|
||||
background on, the pad's turns were put back each frame and the camera moved a frame's worth (#bug-tracker,
|
||||
11-21); the benchmark's camera turn too) */
|
||||
void hos_mouse_native_stick(int held);
|
||||
hos_mouse_native_stick(stick[2] != 0 || stick[3] != 0);
|
||||
mods_input(&buttons, stick); /* (Triforce) the Mods entry's: below */
|
||||
{ /* (Triforce) the sprint button's press is its own while Link plays (host/oot_sprint.c) */
|
||||
void hos_oot_sprint_pad(uint64_t *buttons);
|
||||
|
||||
@@ -56,11 +56,17 @@ static SRWLOCK table_lock = SRWLOCK_INIT;
|
||||
static Obj **table;
|
||||
static uint64_t *table_keys; /* each slot's object's key, beside it: a probe compares keys without visiting objects */
|
||||
static uint64_t cap, used;
|
||||
typedef struct Table { /* (the slots, then the keys: cap of each) */
|
||||
uint64_t cap;
|
||||
/* TICK-062 A4/B7, HOS_OBJ_PACK=1: the slot pointer and its key side by side (stride 2: {Obj *, key} pairs, 16 bytes) instead of
|
||||
the pointer array and then the key array (stride 1): a probe touches one cache line, not two, before the object itself.
|
||||
The same values at the same slot numbers, so every lookup answers as before. */
|
||||
static uint64_t tstride = 1;
|
||||
typedef struct Table { /* (stride 1: the slots, then the keys, cap of each; stride 2: cap pairs) */
|
||||
uint64_t cap, stride;
|
||||
Obj *slot[];
|
||||
} Table;
|
||||
static uint64_t *table_keys_of(const Table *t) { return (uint64_t *)(void *)(t->slot + t->cap); }
|
||||
static uint64_t *table_keys_of(const Table *t) {
|
||||
return t->stride == 2 ? (uint64_t *)(void *)t->slot + 1 : (uint64_t *)(void *)(t->slot + t->cap);
|
||||
}
|
||||
static Table *live;
|
||||
|
||||
static uint64_t slot_of(uint64_t key, uint64_t c) { return (key * 0x9E3779B97F4A7C15ULL >> 20) & (c - 1); }
|
||||
@@ -68,8 +74,8 @@ static uint64_t slot_of(uint64_t key, uint64_t c) { return (key * 0x9E3779B97F4A
|
||||
static Obj *find_locked(uint64_t key) {
|
||||
if (!table) return NULL;
|
||||
for (uint64_t i = slot_of(key, cap);; i = (i + 1) & (cap - 1)) {
|
||||
if (!table[i]) return NULL;
|
||||
if (table_keys[i] == key && table[i]->kind) return table[i];
|
||||
if (!table[i * tstride]) return NULL;
|
||||
if (table_keys[i * tstride] == key && table[i * tstride]->kind) return table[i * tstride];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,40 +141,43 @@ static void bury_locked(Obj *o) {
|
||||
static Obj *find_dead_locked(uint64_t key) {
|
||||
if (!table) return NULL;
|
||||
for (uint64_t i = slot_of(key, cap);; i = (i + 1) & (cap - 1)) {
|
||||
if (!table[i]) return NULL;
|
||||
if (table_keys[i] == key && !table[i]->kind) return table[i];
|
||||
if (!table[i * tstride]) return NULL;
|
||||
if (table_keys[i * tstride] == key && !table[i * tstride]->kind) return table[i * tstride];
|
||||
}
|
||||
}
|
||||
|
||||
static void insert_locked(Obj *o) {
|
||||
if (!table || used * 2 >= cap) {
|
||||
uint64_t ncap = cap ? cap * 2 : 1 << 16;
|
||||
if (!cap) tstride = getenv("HOS_OBJ_PACK") ? 2 : 1;
|
||||
const uint64_t ns = tstride;
|
||||
Table *t = (Table *)calloc(1, sizeof(Table) + ncap * (sizeof(Obj *) + sizeof(uint64_t)));
|
||||
Obj **nt = t->slot;
|
||||
t->cap = ncap;
|
||||
t->stride = ns;
|
||||
uint64_t *nk = table_keys_of(t);
|
||||
for (uint64_t i = 0; i < cap; i++)
|
||||
if (table[i] && table[i]->kind) {
|
||||
uint64_t j = slot_of(table[i]->key, ncap);
|
||||
while (nt[j]) j = (j + 1) & (ncap - 1);
|
||||
nt[j] = table[i];
|
||||
nk[j] = table[i]->key;
|
||||
} else if (table[i]) {
|
||||
bury_locked(table[i]);
|
||||
if (table[i * tstride] && table[i * tstride]->kind) {
|
||||
uint64_t j = slot_of(table[i * tstride]->key, ncap);
|
||||
while (nt[j * ns]) j = (j + 1) & (ncap - 1);
|
||||
nt[j * ns] = table[i * tstride];
|
||||
nk[j * ns] = table[i * tstride]->key;
|
||||
} else if (table[i * tstride]) {
|
||||
bury_locked(table[i * tstride]);
|
||||
}
|
||||
table = nt;
|
||||
table_keys = nk;
|
||||
cap = ncap;
|
||||
used = 0;
|
||||
for (uint64_t i = 0; i < cap; i++) used += table[i] != NULL;
|
||||
for (uint64_t i = 0; i < cap; i++) used += table[i * tstride] != NULL;
|
||||
__atomic_store_n(&live, t, __ATOMIC_RELEASE);
|
||||
}
|
||||
uint64_t i = slot_of(o->key, cap);
|
||||
while (table[i] && table[i]->kind) i = (i + 1) & (cap - 1);
|
||||
if (!table[i]) used++;
|
||||
else bury_locked(table[i]); /* (a tombstone of another address) */
|
||||
__atomic_store_n(&table_keys[i], o->key, __ATOMIC_RELAXED); /* (before the object: see find_unlocked) */
|
||||
__atomic_store_n(&table[i], o, __ATOMIC_RELEASE);
|
||||
while (table[i * tstride] && table[i * tstride]->kind) i = (i + 1) & (cap - 1);
|
||||
if (!table[i * tstride]) used++;
|
||||
else bury_locked(table[i * tstride]); /* (a tombstone of another address) */
|
||||
__atomic_store_n(&table_keys[i * tstride], o->key, __ATOMIC_RELAXED); /* (before the object: see find_unlocked) */
|
||||
__atomic_store_n(&table[i * tstride], o, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
static Obj *find_unlocked(uint64_t key, int kind) {
|
||||
@@ -176,10 +185,10 @@ static Obj *find_unlocked(uint64_t key, int kind) {
|
||||
if (!t) return NULL;
|
||||
const uint64_t *keys = table_keys_of(t);
|
||||
for (uint64_t i = slot_of(key, t->cap), n = 0; n < t->cap; i = (i + 1) & (t->cap - 1), n++) {
|
||||
Obj *o = __atomic_load_n(&t->slot[i], __ATOMIC_ACQUIRE);
|
||||
Obj *o = __atomic_load_n(&t->slot[i * t->stride], __ATOMIC_ACQUIRE);
|
||||
if (!o) return NULL;
|
||||
/* (the key beside the slot first; a slot changing meanwhile gives a miss, and the locked lookup answers) */
|
||||
if (__atomic_load_n(&keys[i], __ATOMIC_RELAXED) == key && o->key == key &&
|
||||
if (__atomic_load_n(&keys[i * t->stride], __ATOMIC_RELAXED) == key && o->key == key &&
|
||||
__atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind)
|
||||
return o;
|
||||
}
|
||||
@@ -190,10 +199,109 @@ static Obj *obj_lookup(uint64_t key, int kind);
|
||||
/* Every nn::os call looks its object up by the guest address (~1.6% of the game threads' time at the castle: a probe
|
||||
of the shared table and a cache miss on a scattered object). Game threads use a few objects over and over: a small
|
||||
direct-mapped cache per thread answers those. An entry holds while its object is still that address's and of the
|
||||
kind asked for (objects are reused, never freed: spares). HOS_NO_OBJ_CACHE=1: off. */
|
||||
kind asked for (objects are reused, never freed: spares). HOS_NO_OBJ_CACHE=1: off.
|
||||
|
||||
(Triforce) The direct-mapped cache still missed at every step of a walk over many objects at a fixed stride: the game's
|
||||
record pass under the graphics front end (f_80364080) locks and unlocks some 4,000 per-slot mutexes 0x80 apart, and the
|
||||
multiply-shift index folds such keys onto few entries (3,541 of 4,000 missed a pass with 4,096 entries). So by default
|
||||
the cache is 16,384 entries, four ways to a 64-byte set, the set picked by murmur3's finalizer, the oldest way replaced
|
||||
(a stale way of the same key in place); a hit is checked by key and kind as before. The table is allocated at a
|
||||
thread's first lookup (256 KB, kept for the thread's life); without memory, the shared table answers. Each thread counts
|
||||
its lookups and misses; after 65,536 lookups a miss asks obj (never obj_nockpt, which runs under host locks) to add them
|
||||
up, and every 10 s the log says "sync: object cache". HOS_OBJ_CACHE=<n>: n entries (256 to 65,536, a power of two);
|
||||
0: the direct-mapped 4,096 entries, as before. */
|
||||
typedef struct { uint64_t key; Obj *o; } ObjWay;
|
||||
enum { OBJ_CACHE_WAYS = 4, OBJ_CACHE_DEFAULT = 16384, OBJ_CACHE_DUE = 65536 };
|
||||
static _Thread_local uint32_t obj_cache_lookups, obj_cache_missed;
|
||||
static _Thread_local int obj_cache_due;
|
||||
/* HOS_OBJ_CACHE: unset, empty or not a number 16,384; 0 the direct-mapped cache; else 256 to 65,536, down to a power of two */
|
||||
static int obj_cache_entries(void) {
|
||||
static int n = -1;
|
||||
if (n < 0) {
|
||||
const char *e = getenv("HOS_OBJ_CACHE");
|
||||
long v = -1;
|
||||
if (e && *e) {
|
||||
v = 0;
|
||||
for (const char *p = e; *p; p++) {
|
||||
if (*p < '0' || *p > '9') { v = -1; break; }
|
||||
if (v < 1000000) v = v * 10 + (*p - '0');
|
||||
}
|
||||
}
|
||||
int r = OBJ_CACHE_DEFAULT;
|
||||
if (v == 0) r = 0;
|
||||
else if (v > 0) {
|
||||
if (v > 65536) v = 65536;
|
||||
r = 256;
|
||||
while ((long)r * 2 <= v) r *= 2;
|
||||
}
|
||||
n = r;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/* The four-way cache (above); entries: obj_cache_entries's, not 0 */
|
||||
static Obj *obj_lookup_cached(uint64_t key, int kind, int entries) {
|
||||
static _Thread_local ObjWay *ways;
|
||||
static _Thread_local int no_memory;
|
||||
if (!ways) {
|
||||
if (no_memory) return obj_lookup(key, kind);
|
||||
char *raw = (char *)calloc((size_t)entries + OBJ_CACHE_WAYS, sizeof(ObjWay)); /* (one set more: aligned to 64 bytes) */
|
||||
if (!raw) {
|
||||
no_memory = 1;
|
||||
return obj_lookup(key, kind);
|
||||
}
|
||||
ways = (ObjWay *)(((uintptr_t)raw + 63) & ~(uintptr_t)63);
|
||||
}
|
||||
uint64_t h = key; /* (murmur3's 64-bit finalizer) */
|
||||
h ^= h >> 33;
|
||||
h *= 0xff51afd7ed558ccdULL;
|
||||
h ^= h >> 33;
|
||||
h *= 0xc4ceb9fe1a85ec53ULL;
|
||||
h ^= h >> 33;
|
||||
ObjWay *set = ways + (h & (uint64_t)(entries / OBJ_CACHE_WAYS - 1)) * OBJ_CACHE_WAYS;
|
||||
obj_cache_lookups++;
|
||||
for (int w = 0; w < OBJ_CACHE_WAYS; w++) {
|
||||
Obj *o = set[w].o;
|
||||
if (set[w].key == key && o && __atomic_load_n(&o->key, __ATOMIC_RELAXED) == key &&
|
||||
__atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind)
|
||||
return o;
|
||||
}
|
||||
obj_cache_missed++;
|
||||
if (obj_cache_lookups >= OBJ_CACHE_DUE) obj_cache_due = 1;
|
||||
Obj *o = obj_lookup(key, kind);
|
||||
int w = 0;
|
||||
while (w < OBJ_CACHE_WAYS && set[w].key != key) w++;
|
||||
if (w == OBJ_CACHE_WAYS) { /* (the oldest way, the last, goes; the newest is the first) */
|
||||
memmove(set + 1, set, (OBJ_CACHE_WAYS - 1) * sizeof *set);
|
||||
w = 0;
|
||||
}
|
||||
set[w].key = key, set[w].o = o;
|
||||
return o;
|
||||
}
|
||||
/* (obj, outside every host lock) this thread's counts into the process's; every 10 s the log line */
|
||||
static void obj_cache_flush(void) {
|
||||
static uint64_t lookups, missed;
|
||||
static int64_t said;
|
||||
obj_cache_due = 0;
|
||||
__atomic_fetch_add(&lookups, obj_cache_lookups, __ATOMIC_RELAXED);
|
||||
__atomic_fetch_add(&missed, obj_cache_missed, __ATOMIC_RELAXED);
|
||||
obj_cache_lookups = obj_cache_missed = 0;
|
||||
const int64_t now = hos_now_ns();
|
||||
int64_t was = __atomic_load_n(&said, __ATOMIC_RELAXED);
|
||||
if (!was) {
|
||||
__atomic_compare_exchange_n(&said, &was, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
|
||||
return;
|
||||
}
|
||||
if (now - was < 10000000000LL || !__atomic_compare_exchange_n(&said, &was, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
return;
|
||||
const uint64_t l = __atomic_exchange_n(&lookups, 0, __ATOMIC_RELAXED), m = __atomic_exchange_n(&missed, 0, __ATOMIC_RELAXED);
|
||||
hos_log("sync: object cache (%d entries, %d ways a set; HOS_OBJ_CACHE): %llu lookups, %llu missed (%.2f %%) in %.0f s",
|
||||
obj_cache_entries(), OBJ_CACHE_WAYS, (unsigned long long)l, (unsigned long long)m, l ? 100.0 * m / l : 0.0,
|
||||
(now - was) / 1e9);
|
||||
}
|
||||
static Obj *obj_nockpt(uint64_t key, int kind);
|
||||
static Obj *obj(uint64_t key, int kind) {
|
||||
hos_vcore_checkpoint(); /* (virtual cores: every nn::os call, where a better waiter takes the core) */
|
||||
if (obj_cache_due) obj_cache_flush();
|
||||
return obj_nockpt(key, kind);
|
||||
}
|
||||
/* obj without the checkpoint, for lookups under a host lock: a checkpoint there can give the core up and wait for one
|
||||
@@ -202,11 +310,13 @@ static Obj *obj(uint64_t key, int kind) {
|
||||
static Obj *obj_nockpt(uint64_t key, int kind) {
|
||||
/* (hookshot: 128 still missed: obj_lookup was the busiest leaf in dense Kokiri, 3.2% of all samples, 4.3% of the instants when one
|
||||
thread alone ran; each actor has its own mutexes) */
|
||||
static _Thread_local struct { uint64_t key; Obj *o; } cache[1024];
|
||||
static _Thread_local struct { uint64_t key; Obj *o; } cache[4096]; /* ((Triforce, 11-21) 4096, was 1024: obj_lookup still 1.9% of the game threads */
|
||||
static int off = -1;
|
||||
if (off < 0) off = getenv("HOS_NO_OBJ_CACHE") != NULL;
|
||||
if (!off) {
|
||||
const unsigned i = (unsigned)((key * 0x9E3779B97F4A7C15ULL) >> 54);
|
||||
const int entries = obj_cache_entries();
|
||||
if (entries) return obj_lookup_cached(key, kind, entries);
|
||||
const unsigned i = (unsigned)((key * 0x9E3779B97F4A7C15ULL) >> 52); /* (12 bits: 4096 entries) */
|
||||
Obj *o = cache[i].o;
|
||||
if (cache[i].key == key && o && __atomic_load_n(&o->key, __ATOMIC_RELAXED) == key &&
|
||||
__atomic_load_n(&o->kind, __ATOMIC_ACQUIRE) == kind)
|
||||
@@ -336,7 +446,8 @@ static int64_t spin_ns(void) {
|
||||
static int64_t ns = -1;
|
||||
if (ns < 0) {
|
||||
const char *e = getenv("HOS_SPIN_US");
|
||||
ns = (e ? atoll(e) : 20) * 1000;
|
||||
ns = (e ? atoll(e) : 40) * 1000; /* ((Triforce, 11-21) 40 us, was 20: a spin now gives its core to a waiter that asks
|
||||
(hos_vcore_asked), so a longer one blocks nobody. Lab A/B 79.7 -> 80.4, every round) */
|
||||
}
|
||||
return ns;
|
||||
}
|
||||
@@ -367,6 +478,7 @@ static void spin_until_set(Obj *o, volatile int *flag) {
|
||||
spin_end(o, misses, 0);
|
||||
return;
|
||||
}
|
||||
if (hos_vcore_asked()) return; /* ((Triforce, 11-21) a waiter wants this core: to the sleep, which gives it up */
|
||||
}
|
||||
}
|
||||
static void spin_until_free(Obj *o) {
|
||||
@@ -385,6 +497,7 @@ static void spin_until_free(Obj *o) {
|
||||
spin_end(o, misses, 0);
|
||||
return;
|
||||
}
|
||||
if (hos_vcore_asked()) return; /* ((Triforce, 11-21) a waiter wants this core: to the sleep, which gives it up */
|
||||
}
|
||||
}
|
||||
#else
|
||||
@@ -448,12 +561,12 @@ static void cv_enter(Obj *cv, Obj *m) {
|
||||
if (cv->cv_waiting && cv->cv_mutex != m) cv->cv_mixed = 1;
|
||||
if (!cv->cv_waiting) cv->cv_mixed = 0;
|
||||
cv->cv_mutex = m;
|
||||
cv->cv_waiting++;
|
||||
__atomic_add_fetch(&cv->cv_waiting, 1, __ATOMIC_SEQ_CST);
|
||||
}
|
||||
/* a waiter out (cv->lk held): whether a signal reserved its mutex m for it. A reservation made on another mutex (a
|
||||
waiter with a different one came in after the signal) is given back: never left owed to nobody */
|
||||
static int cv_leave(Obj *cv, Obj *m) {
|
||||
cv->cv_waiting--;
|
||||
__atomic_sub_fetch(&cv->cv_waiting, 1, __ATOMIC_SEQ_CST);
|
||||
if (cv->cv_owed <= 0) return 0;
|
||||
Obj *r = cv->cv_owed_mutex[--cv->cv_owed];
|
||||
if (r == m) return 1;
|
||||
@@ -578,6 +691,18 @@ HLE(_ZN2nn2os25FinalizeConditionVariableEPNS0_21ConditionVariableTypeE) { drop(A
|
||||
HLE(_ZN2nn2os23SignalConditionVariableEPNS0_21ConditionVariableTypeE) {
|
||||
Obj *cv = obj(ARG(0), K_COND);
|
||||
if (watched_cv(ARG(0))) watch_log("signal", watch_mutex(), ARG(0), c, NULL);
|
||||
/* TICK-062 B1, HOS_CV_SIGNAL_FAST=1: a signal with no waiter changes nothing a later wait can see, so it returns before
|
||||
taking cv->lk. No wake is lost: (1) a waiter registers (cv_waiting++, seq_cst) under cv->lk BEFORE it releases the guest
|
||||
mutex (mutex_release_all) and before it sleeps, and reads cv->gen before registering. (2) A signal the guest orders
|
||||
after a wait's registration (it took the mutex the waiter released, or any release/acquire chain from it) reads
|
||||
cv_waiting with a seq_cst load after that, so it sees >= 1 and takes the slow path below, exactly as before. (3) A
|
||||
signal not ordered with the registration (no mutex, truly concurrent) may read 0: that is the signal ordered before the
|
||||
wait, which the slow path also loses (it bumps gen before the waiter reads gen, so the waiter sleeps on). (4) With no
|
||||
waiter the slow path only bumps gen (compared only as a delta by waiters that registered later) and cv_reserve (a no-op
|
||||
with cv_waiting <= cv_owed, owed being 0 with no waiter) and wakes nobody. The wake order of waiters is untouched. */
|
||||
static int fast = -1;
|
||||
if (fast < 0) fast = !(getenv("HOS_CV_SIGNAL_FAST") && atoi(getenv("HOS_CV_SIGNAL_FAST")) == 0); /* ((Triforce) on by default since 11-21, lab +5.8%; =0: off */
|
||||
if (fast && !__atomic_load_n(&cv->cv_waiting, __ATOMIC_SEQ_CST)) return;
|
||||
AcquireSRWLockExclusive(&cv->lk);
|
||||
cv_reserve(cv);
|
||||
cv->gen++;
|
||||
@@ -862,6 +987,12 @@ HLE(_ZN2nn2os19TimedWaitLightEventEPNS0_14LightEventTypeENS_8TimeSpanE) {
|
||||
/* System events are created by other HLE APIs (vsync, audio device changes ...) with hos_system_event_init. */
|
||||
void hos_system_event_init(uint64_t key, int autoclear) { event_init(key, 0, autoclear); }
|
||||
void hos_system_event_signal(uint64_t key) { event_signal(obj(key, K_EVENT)); }
|
||||
/* ((Triforce, 11-22, the performance audit's IO-34, after Source A's) A system event cleared, and set, by an HLE that
|
||||
holds a host lock of its own while it does (nn_video.c: the movie decoder's lock, so the event's state and the
|
||||
decoder's count change together). Looked up without the virtual-core checkpoint: one there can give the core up and
|
||||
wait for another with that lock held, while a thread that holds a core waits for the lock (see obj_nockpt) */
|
||||
void hos_system_event_clear(uint64_t key) { event_clear(obj_nockpt(key, K_EVENT)); }
|
||||
void hos_system_event_signal_held(uint64_t key) { event_signal(obj_nockpt(key, K_EVENT)); }
|
||||
HLE(_ZN2nn2os18DestroySystemEventEPNS0_15SystemEventTypeE) { drop(ARG(0)); }
|
||||
HLE(_ZN2nn2os16ClearSystemEventEPNS0_15SystemEventTypeE) { event_clear(obj(ARG(0), K_EVENT)); }
|
||||
HLE(_ZN2nn2os15WaitSystemEventEPNS0_15SystemEventTypeE) { logged_wait(c, obj(ARG(0), K_EVENT), 0, 0, "system"); }
|
||||
|
||||
60
host/nn_vi.c
60
host/nn_vi.c
@@ -5,6 +5,7 @@
|
||||
#include "pace_wait.h" /* ((Triforce) the acquire's wait to the frame's time: pace_sleep) */
|
||||
#include "step_steady.h" /* ((Triforce) HOS_STEP_STEADY=1: realtime_step) */
|
||||
#include "pace_steady.h" /* ((Triforce) the Steady frame rate row: steady_period) */
|
||||
#include "movie_pace.h" /* ((Triforce, 11-22, PRESENT-30) paced at 240 while a movie plays at the 60 cap) */
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_OPT_HDR_*: the Insert menu's HDR, answered by the renderer) */
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
@@ -286,6 +287,32 @@ void hos_vi_native_acquire(int active) {
|
||||
const int64_t now = hos_now_ns();
|
||||
if (until > now) pace_sleep(until, now);
|
||||
}
|
||||
/* ((Triforce, 11-22, the performance audit's PRESENT-30, after Source A's) While a movie plays at a cap of exactly 60 the
|
||||
frames are paced by 240, and the game's limiter and its time step follow as at a cap of 240 (movie_pace.h): the movie
|
||||
player takes one picture an update at most, so at 60 every late frame was a picture the decoder then skipped. Decided
|
||||
once a present (movie_pace_decide, from hos_vi_native_presented); pace_fps is what the pacing and realtime_step go by,
|
||||
fps_max stays the cap itself, which the Steady row, the present interval and the other limiters' hand-off read.
|
||||
Not with the Steady frame rate row on. HOS_MOVIE_PACE=0: the cap kept through movies, as before */
|
||||
static int movie_raise;
|
||||
static int pace_fps(void) {
|
||||
return movie_pace_fps(movie_raise, fps_max());
|
||||
}
|
||||
static void movie_pace_decide(void) {
|
||||
static int on = -1, steady = -1;
|
||||
static long long raised;
|
||||
if (on < 0) on = !(getenv("HOS_MOVIE_PACE") && *getenv("HOS_MOVIE_PACE") && atoi(getenv("HOS_MOVIE_PACE")) == 0);
|
||||
if (steady < 0) { /* (the Steady frame rate row, read as steady_period reads it) */
|
||||
const char *e = getenv("HOS_FPS_STEADY");
|
||||
steady = e && *e ? atoi(e) : hos_ini_option("steady_frame_rate", PACE_STEADY_OFF);
|
||||
if (steady < PACE_STEADY_OFF || steady > PACE_STEADY_REFRESH) steady = PACE_STEADY_OFF;
|
||||
}
|
||||
const int raise = movie_pace_raise(on, fps_max(), steady != PACE_STEADY_OFF, movie_plays());
|
||||
if (raise && !movie_raise)
|
||||
hos_log("vi: a movie plays at the 60 cap: frames paced at %d while it does (HOS_MOVIE_PACE=0: the cap kept)", MOVIE_PACE_FPS);
|
||||
if (!raise && movie_raise) hos_log("vi: the movie is over: frames paced at the 60 cap again, after %lld at %d", raised, MOVIE_PACE_FPS), raised = 0;
|
||||
if (raise) raised++;
|
||||
movie_raise = raise;
|
||||
}
|
||||
/* (mkw-recomp) MKW's frame pacer (0x806bcbb0, x19 when it sets the interval: nvn_native.c): its measured frame time
|
||||
(+0x128, ticks), its running excess (+0x218) and interval (+0x214), logged with the frame rate */
|
||||
volatile uint64_t hos_mkw_pacer;
|
||||
@@ -510,6 +537,16 @@ static void realtime_step(int64_t now) {
|
||||
}
|
||||
const uint64_t mod = hos_gfx_module;
|
||||
if (!mod) return;
|
||||
/* ((Triforce, 11-22, the performance audit's PRESENT-30) what the frames are paced by: the cap, or 240 while a movie
|
||||
plays at the 60 cap (pace_fps). The limiter, the step and the game's rate below follow it, as they followed the cap */
|
||||
const int pace = pace_fps();
|
||||
{ /* (when that comes back down to 60: a rate above 60 left in the game's slot goes back with it, movie_pace.h) */
|
||||
static int before;
|
||||
int *slot = (int *)GP(mod + 0x2c8);
|
||||
const int was = *slot, back = movie_pace_slot(was, before, pace);
|
||||
if (back != was) *slot = back;
|
||||
before = pace;
|
||||
}
|
||||
{ /* (hookshot) The frame limiter (f_8042cd20 sleeps to 1 / +0xfc). The engine's adaptive controller writes +0xfc when
|
||||
variable refresh is on (+0x231, f_804fc0f0), any rate from 40 up. HOS_FPS_TARGET=<fps>: that rate kept, 0: left alone;
|
||||
HOS_FPS_LOG=1: what the game asks for, every 300 presents */
|
||||
@@ -534,12 +571,16 @@ static void realtime_step(int64_t now) {
|
||||
clock_before = game_clock, at_before = now;
|
||||
}
|
||||
if (target && *limit > 0 && *limit < target) *limit = target;
|
||||
if (fps_max() > 60 && *limit > 0 && *limit < fps_max()) *limit = fps_max();
|
||||
/* ((Triforce, 11-22, PRESENT-30) raised to the pace above 60 as before, and now put back where it was once the
|
||||
pace is 60 again: it was raised once and never lowered, which a cap that never changed did not need) */
|
||||
static MovieLimit raised;
|
||||
const int was = *limit, to = movie_pace_limit(&raised, was, pace);
|
||||
if (to != was) *limit = to;
|
||||
}
|
||||
/* (the Insert menu's Game speed "per frame": a frame is a 60th of a second of game time. Not past 60 fps: the game would run
|
||||
faster than real time, so a cap above 60 keeps the real-time step whatever that row says) */
|
||||
/* (nor under 60: a cap of 30 would be the game at half speed for good) */
|
||||
if (!rx_option(0, 1) && fps_max() == 60) {
|
||||
if (!rx_option(0, 1) && pace == 60) {
|
||||
int *slot = (int *)GP(mod + 0x2c8);
|
||||
if (*slot >= 15 && *slot <= 60) *slot = 60;
|
||||
return;
|
||||
@@ -549,6 +590,12 @@ static void realtime_step(int64_t now) {
|
||||
static unsigned count, next;
|
||||
static long long steps;
|
||||
static StepSteady steady; /* ((Triforce) HOS_STEP_STEADY=1, below) */
|
||||
{ /* ((Triforce, 11-22, PRESENT-30) a change of the pace, a movie's start or its end: the step's average starts
|
||||
again, as after a load, since the intervals before it were paced by another period) */
|
||||
static int paced;
|
||||
if (paced && paced != pace) { count = next = 0; carry = 0; step_steady_reset(&steady); }
|
||||
paced = pace;
|
||||
}
|
||||
const double dt = (now - last) / 1e9;
|
||||
last = now;
|
||||
if (dt <= 0 || dt > 0.25) { count = next = 0; carry = 0; step_steady_reset(&steady); return; } /* (a load: the average starts again) */
|
||||
@@ -569,8 +616,8 @@ static void realtime_step(int64_t now) {
|
||||
HOS_FPS_STEP=0, or a lift without the hook (hos_step_probe unset): the rate up to the cap, as before */
|
||||
static int timed_step = -1;
|
||||
if (timed_step < 0) timed_step = !(getenv("HOS_FPS_STEP") && !atoi(getenv("HOS_FPS_STEP")));
|
||||
const int timed = fps_max() > 60 && timed_step && hos_step_probe;
|
||||
const int top = timed ? 60 : fps_max();
|
||||
const int timed = pace > 60 && timed_step && hos_step_probe;
|
||||
const int top = timed ? 60 : pace;
|
||||
/* (Triforce, after NX2navi's steady step: host/step_steady.h) HOS_STEP_STEADY (on unless =0): the step is not the mean of the last five
|
||||
intervals but that of the last 12 without their longest and shortest, the paced period where it is within 3% of it, and
|
||||
what the display showed and the game was not given repaid at a 64th a frame (10% of the step at most), within the bounds
|
||||
@@ -598,7 +645,7 @@ static void realtime_step(int64_t now) {
|
||||
}
|
||||
const double ratio = secs_avg * 60.0;
|
||||
int *slot = (int *)GP(mod + 0x2c8);
|
||||
if (*slot < 15 || *slot > (fps_max() > 60 ? fps_max() : 60)) { /* (under a cap below 60 it starts at the game's 60) */
|
||||
if (*slot < 15 || *slot > (pace > 60 ? pace : 60)) { /* (under a cap below 60 it starts at the game's 60) */
|
||||
static int logged;
|
||||
if (!logged++) hos_log("vi: the graphics module's frame rate reads %d: no real-time step", *slot);
|
||||
return;
|
||||
@@ -778,7 +825,8 @@ void hos_vi_native_presented(void) {
|
||||
const int64_t now = hos_now_ns();
|
||||
{ void hos_bench_present(int64_t now); hos_bench_present(now); } /* ((Triforce) benchmark mode: every present's interval) */
|
||||
/* ((Triforce) the Steady frame rate row: a period this PC holds in the scene, never shorter than the cap's) */
|
||||
const int64_t period = steady_period(1000000000LL / fps_max() * interval, now);
|
||||
movie_pace_decide(); /* ((Triforce, 11-22, PRESENT-30) once a present: 240 while a movie plays at the 60 cap) */
|
||||
const int64_t period = steady_period(1000000000LL / pace_fps() * interval, now);
|
||||
pace_period_ns = period; /* (Triforce: what another limiter is given to hold in this file's place, pace_handed) */
|
||||
realtime_step(now);
|
||||
/* (behind by more than a frame: the next one is paced from now, with no burst to catch up) */
|
||||
|
||||
210
host/nn_video.c
210
host/nn_video.c
@@ -12,6 +12,12 @@
|
||||
free registered buffer (host memory: the pool's guest memory is not used). The video service itself (nn::gsv) is
|
||||
not emulated: nn::codec::video::Initialize does nothing. */
|
||||
#include "hos.h"
|
||||
#include "host_alloc.h" /* ((Triforce, 11-22, OS-04) allocations that wait at Windows' commit limit before they answer NULL) */
|
||||
#include "video_av1.h" /* ((Triforce, 11-22, IO-32) which AV1 decoder goes first on Windows) */
|
||||
#include "video_movie.h" /* ((Triforce, 11-22, IO-32) what a movie cost to decode, and its line) */
|
||||
#include "video_nv12.h" /* ((Triforce, 11-22, IO-36) a decoded picture kept as NV12 alone) */
|
||||
#include "video_catchup.h" /* ((Triforce, 11-22, THREAD-82) how far dec_acquire decodes ahead to catch up) */
|
||||
#include "video_input.h" /* ((Triforce, 11-22, IO-34) how many submissions the decoder holds before it refuses one) */
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -38,6 +44,8 @@ static int video_log(void) { /* HOS_VIDEO_LOG=1: submissions and acquisitions
|
||||
|
||||
void hos_system_event_init(uint64_t key, int autoclear); /* nn_os_sync.c */
|
||||
void hos_system_event_signal(uint64_t key);
|
||||
void hos_system_event_clear(uint64_t key); /* ((Triforce, 11-22, IO-34) these two under a host lock: no checkpoint) */
|
||||
void hos_system_event_signal_held(uint64_t key);
|
||||
|
||||
/* an image pool: its pictures, w x h, as RGBA and as NV12 (both kept: the reader asks for either) */
|
||||
typedef struct {
|
||||
@@ -71,7 +79,12 @@ static DWORD WINAPI av1_notice(LPVOID unused) {
|
||||
(Triforce) It needs Microsoft's AV1 Video Extension, which not every PC has: without it the movies are decoded here, by
|
||||
dav1d, when the FFmpeg the build links has it (the Tools' FFmpeg with AV1; nx2/build.py takes whichever is there), as
|
||||
NX2navi decodes them always. No notice and no black movies then. HOS_AV1=dav1d: dav1d first, for tests;
|
||||
HOS_AV1_MISSING=1: as if the extension were missing. */
|
||||
HOS_AV1_MISSING=1: as if the extension were missing.
|
||||
(Triforce, 11-22, the performance audit's IO-32, after Source A's) The order is the other way round now: dav1d first
|
||||
where that FFmpeg has it, Media Foundation when it has not or when libdav1d does not open (video_av1.h). Media
|
||||
Foundation's path here decodes in software on every GPU (mf_av1.c takes the synchronous decoder, with no D3D
|
||||
manager), and its decode time was never seen: each movie now says in the log what it took (video_movie.h).
|
||||
HOS_AV1=mf: Media Foundation first, as before. */
|
||||
typedef void (*mf_av1_picture_fn)(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts);
|
||||
void *mf_av1_open(int w, int h, int tell);
|
||||
void mf_av1_decode(void *m, const uint8_t *data, size_t n, int64_t pts, mf_av1_picture_fn fn, void *user);
|
||||
@@ -92,12 +105,21 @@ typedef struct {
|
||||
Picture queue[MAX_QUEUE];
|
||||
int qhead, qn;
|
||||
int eos; /* the last submission is in and decoded: an empty queue is the end */
|
||||
int stopped; /* a Stop or Flush with nothing submitted since: no picture will come (dec_acquire) */
|
||||
Pending *phead, *ptail; /* submissions not decoded yet, oldest first */
|
||||
int64_t submits, acquires;
|
||||
int clock_set; /* (hookshot) the movie's clock: the picture at clock_pts was due at clock_ns */
|
||||
int64_t clock_ns, clock_pts;
|
||||
int64_t last_pts; /* the pts of the last picture handed out */
|
||||
int64_t log_submits; /* (HOS_VIDEO_LOG) submissions still to log, after a flush or the end */
|
||||
VideoMovie movie; /* ((Triforce, 11-22, IO-32) this movie's pictures and decode time, for its line) */
|
||||
VideoPace pace; /* ((Triforce, 11-22, THREAD-82) the gap between its pictures and what a decode takes
|
||||
lately: the catch-up's bound (dec_acquire, video_catchup.h)) */
|
||||
int npending; /* ((Triforce, 11-22, IO-34) submissions in phead's list: with qn, what the input limit
|
||||
counts (video_input.h)) */
|
||||
uint64_t ready; /* ((Triforce, 11-22, IO-34) its "ready for a submission" event, a guest address: set,
|
||||
and clear from a refused submission until there is room again) */
|
||||
int ready_clear; /* (... whether it is clear now; changed only under the lock, with the event) */
|
||||
SRWLOCK lock;
|
||||
} Dec;
|
||||
|
||||
@@ -127,7 +149,7 @@ static void set_host(uint64_t obj, uint32_t magic, void *p) {
|
||||
if (i == nhost_objs) {
|
||||
if (nhost_objs == cap_host_objs) {
|
||||
cap_host_objs = cap_host_objs ? cap_host_objs * 2 : 16;
|
||||
host_objs = (HostObj *)realloc(host_objs, cap_host_objs * sizeof *host_objs);
|
||||
host_objs = (HostObj *)hos_xrealloc(host_objs, cap_host_objs * sizeof *host_objs); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!host_objs) hos_fatal(NULL, "video: out of memory");
|
||||
}
|
||||
nhost_objs++;
|
||||
@@ -164,7 +186,7 @@ HLE(_ZN2nn5codec31CalculateDecoderImagePoolBufferEPmS1_RKNS0_27VideoDecoderImage
|
||||
/* InitializeDecoderImagePool(pool, void* memory, size, Preference const&, u32 count) */
|
||||
HLE(_ZN2nn5codec26InitializeDecoderImagePoolEPNS0_25VideoDecoderImagePoolTypeEPvmRKNS0_27VideoDecoderImagePreferenceEj) {
|
||||
const Pref *p = (const Pref *)GP(ARG(3));
|
||||
Pool *o = (Pool *)calloc(1, sizeof *o);
|
||||
Pool *o = (Pool *)hos_xcalloc(1, sizeof *o); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!o) hos_fatal(NULL, "video: out of memory for an image pool");
|
||||
o->w = p->magic == PREF_MAGIC && p->w ? p->w : 1280, o->h = p->magic == PREF_MAGIC && p->h ? p->h : 720;
|
||||
o->format = p->magic == PREF_MAGIC ? (int)p->format : 2;
|
||||
@@ -280,6 +302,40 @@ HLE(_ZN2nn5codec26ReadDecoderOutputImageNv12EPKNS0_25VideoDecoderImagePoolTypeEj
|
||||
/* ---- decoders ---- */
|
||||
static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, int h);
|
||||
static void dec_init(Ctx *c, enum AVCodecID id, const char *what) { dec_init_sized(c, id, what, 0, 0); }
|
||||
#ifdef _WIN32
|
||||
static const char *av1_first(void) { /* ((Triforce, 11-22, IO-32) HOS_AV1, read once: "mf" puts Media Foundation first) */
|
||||
static const char *value;
|
||||
static int read;
|
||||
if (!read) value = getenv("HOS_AV1"), read = 1;
|
||||
return value;
|
||||
}
|
||||
#endif
|
||||
/* ((Triforce, 11-22, the performance audit's IO-34, after Source A's) The input limit (video_input.h): the decoder
|
||||
refuses a submission while this many wait, not decoded yet or decoded and not taken. Every submission was taken
|
||||
before, and the game submits a whole movie at once on the thread that also takes the pictures: the pictures that
|
||||
came due while it submitted were skipped by the catch-up, a stutter at each movie's start.
|
||||
HOS_VIDEO_INPUT_LIMIT=<n>, read once: that many instead of 16; 0: none, as before) */
|
||||
static int input_limit(void) {
|
||||
static int limit = -1;
|
||||
if (limit < 0) limit = video_input_limit(getenv("HOS_VIDEO_INPUT_LIMIT"));
|
||||
return limit;
|
||||
}
|
||||
static int input_full(const Dec *d) { return video_input_full(input_limit(), d->npending, d->qn); }
|
||||
/* (under d->lock, wherever the count went down) room again: the event a refused submission cleared is set */
|
||||
static void input_room(Dec *d) {
|
||||
if (d->ready_clear && !input_full(d)) {
|
||||
d->ready_clear = 0;
|
||||
hos_system_event_signal_held(d->ready);
|
||||
}
|
||||
}
|
||||
/* a decoder's own ready event, set: one for all decoders could not be clear for the full one alone. Never freed, as
|
||||
the one event before it (64 bytes of the system region a decoder) */
|
||||
static uint64_t ready_new(void) {
|
||||
const uint64_t event = hos_sys_alloc(0x40, 16);
|
||||
hos_system_event_init(event, 0);
|
||||
hos_system_event_signal(event);
|
||||
return event;
|
||||
}
|
||||
static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, int h) {
|
||||
const AVCodec *codec = NULL;
|
||||
#ifndef _WIN32 /* (linux) FFmpeg's own AV1 decoder needs a hardware context, and there is no Media Foundation to fall
|
||||
@@ -289,11 +345,12 @@ static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, i
|
||||
#endif
|
||||
Dec *d = (Dec *)calloc(1, sizeof *d);
|
||||
InitializeSRWLock(&d->lock);
|
||||
#ifdef _WIN32 /* (Triforce) AV1: Windows' decoder, else dav1d; never FFmpeg's own, which needs a hardware context */
|
||||
#ifdef _WIN32 /* (Triforce) AV1: dav1d, else Windows' decoder; never FFmpeg's own, which needs a hardware context.
|
||||
((Triforce, 11-22, IO-32) It was Windows' decoder first: video_av1.h has the rule and HOS_AV1=mf the old
|
||||
order. Media Foundation is still opened below when libdav1d is there and does not open) */
|
||||
if (id == AV_CODEC_ID_AV1) {
|
||||
const char *first = getenv("HOS_AV1");
|
||||
codec = avcodec_find_decoder_by_name("libdav1d");
|
||||
if (!(codec && first && !strcmp(first, "dav1d"))) d->mf = mf_av1_open(w, h, 0);
|
||||
if (video_av1_mf_first(av1_first(), codec != NULL)) d->mf = mf_av1_open(w, h, 0);
|
||||
if (d->mf) codec = NULL;
|
||||
} else
|
||||
#endif
|
||||
@@ -316,6 +373,7 @@ static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, i
|
||||
#endif
|
||||
d->pkt = av_packet_alloc();
|
||||
d->frame = av_frame_alloc();
|
||||
d->ready = ready_new(); /* ((Triforce, 11-22, IO-34) before the game can ask for it or submit) */
|
||||
set_host(ARG(0), DEC_MAGIC, d);
|
||||
static int logged;
|
||||
if (logged++ < 4)
|
||||
@@ -335,10 +393,25 @@ static void pending_free(Dec *d) {
|
||||
free(n);
|
||||
}
|
||||
d->ptail = NULL;
|
||||
d->npending = 0;
|
||||
}
|
||||
/* ((Triforce, 11-22, the performance audit's IO-32, after Source A's) The movie's line, once, and its counts from zero
|
||||
for the next movie: at a Stop, a Flush and the decoder's end (video_movie.h) */
|
||||
static void movie_end(Dec *d) {
|
||||
char line[280];
|
||||
if (video_movie_line(&d->movie, d->ctx ? d->ctx->codec->name : "Media Foundation", video_pace_spacing(&d->pace), line,
|
||||
sizeof line))
|
||||
hos_log("video: %s", line);
|
||||
memset(&d->movie, 0, sizeof d->movie);
|
||||
memset(&d->pace, 0, sizeof d->pace); /* ((Triforce, 11-22, THREAD-82) the next movie's gap and decode time are its own) */
|
||||
}
|
||||
static void dec_finalize(Ctx *c) {
|
||||
Dec *d = (Dec *)host_of(ARG(0), DEC_MAGIC);
|
||||
if (!d) return;
|
||||
movie_end(d);
|
||||
/* ((Triforce, 11-22, IO-34) its ready event is left set: a thread that still waits for it is not left waiting. No
|
||||
lock: nothing else has this decoder any more) */
|
||||
if (d->ready_clear) hos_system_event_signal(d->ready);
|
||||
pending_free(d);
|
||||
for (int i = 0; i < d->qn; i++) picture_free(&d->queue[(d->qhead + i) % MAX_QUEUE]);
|
||||
if (d->sws) sws_freeContext(d->sws);
|
||||
@@ -349,11 +422,24 @@ static void dec_finalize(Ctx *c) {
|
||||
free(d);
|
||||
set_host(ARG(0), DEC_MAGIC, NULL);
|
||||
}
|
||||
/* a decoded frame into the queue, as RGBA and NV12 (the oldest dropped when the reader falls behind) */
|
||||
static int rgba_eager(void) { /* ((Triforce, 11-22, IO-36) HOS_VIDEO_RGBA=1, read once: planar pictures made RGBA at once) */
|
||||
static int on = -1;
|
||||
if (on < 0) on = getenv("HOS_VIDEO_RGBA") != NULL && atoi(getenv("HOS_VIDEO_RGBA")) != 0;
|
||||
return on;
|
||||
}
|
||||
/* a decoded frame into the queue, as RGBA and NV12 (the oldest dropped when the reader falls behind).
|
||||
((Triforce, 11-22, the performance audit's IO-36, after Source A's) As NV12 alone for libdav1d's planar 4:2:0 too, as
|
||||
Media Foundation's NV12 already was: no swscale context, no RGBA buffer and no conversion, the pool making the RGBA
|
||||
only if it is read (pool_rgba; dec_acquire copies no RGBA rows for such a picture). Each 1080p picture was converted
|
||||
on the movie thread for a game that reads only the NV12. The NV12 buffer is zeroed only where a copy leaves bytes
|
||||
out, and U and V are interleaved 16 pairs a step (video_nv12.h). HOS_VIDEO_RGBA=1: the RGBA of every planar picture,
|
||||
as before) */
|
||||
static void queue_frame(Dec *d, const AVFrame *f) {
|
||||
const int w = f->width, h = f->height;
|
||||
if (w <= 0 || h <= 0) return;
|
||||
if (f->format != AV_PIX_FMT_NV12 && (!d->sws || d->sws_w != w || d->sws_h != h || d->sws_fmt != f->format)) {
|
||||
const int planar = f->format == AV_PIX_FMT_YUV420P || f->format == AV_PIX_FMT_YUVA420P;
|
||||
const int lazy = video_rgba_lazy(rgba_eager(), f->format == AV_PIX_FMT_NV12, f->format == AV_PIX_FMT_YUV420P);
|
||||
if (!lazy && (!d->sws || d->sws_w != w || d->sws_h != h || d->sws_fmt != f->format)) {
|
||||
if (d->sws) sws_freeContext(d->sws);
|
||||
d->sws = sws_getContext(w, h, (enum AVPixelFormat)f->format, w, h, AV_PIX_FMT_RGBA, SWS_BILINEAR, NULL, NULL, NULL);
|
||||
d->sws_w = w, d->sws_h = h, d->sws_fmt = f->format;
|
||||
@@ -361,14 +447,15 @@ static void queue_frame(Dec *d, const AVFrame *f) {
|
||||
Picture p = {0};
|
||||
p.w = w, p.h = h, p.pts = f->pts;
|
||||
const int nv12 = f->format == AV_PIX_FMT_NV12; /* (hookshot: Media Foundation's pictures, kept as NV12 alone) */
|
||||
p.rgba = nv12 ? NULL : (uint8_t *)malloc((size_t)w * h * 4);
|
||||
p.nv12 = nv12 ? (uint8_t *)malloc((size_t)w * h * 3 / 2) : (uint8_t *)calloc(1, (size_t)w * h * 3 / 2);
|
||||
if (!p.nv12 || (!nv12 && !p.rgba)) { /* (out of memory: this frame is dropped, not written through NULL; issue 342) */
|
||||
p.rgba = lazy ? NULL : (uint8_t *)malloc((size_t)w * h * 4);
|
||||
p.nv12 = (uint8_t *)malloc((size_t)w * h * 3 / 2);
|
||||
if (!p.nv12 || (!lazy && !p.rgba)) { /* (out of memory: this frame is dropped, not written through NULL; issue 342) */
|
||||
static volatile LONG told;
|
||||
if (InterlockedIncrement(&told) <= 4) hos_log("video: no memory for a %dx%d frame: dropped", w, h);
|
||||
free(p.rgba), free(p.nv12);
|
||||
return;
|
||||
}
|
||||
if (video_nv12_zero(w, h, nv12 || planar)) memset(p.nv12, 0, (size_t)w * h * 3 / 2);
|
||||
uint8_t *dst[4] = {p.rgba, NULL, NULL, NULL};
|
||||
int dst_stride[4] = {w * 4, 0, 0, 0};
|
||||
if (d->sws && p.rgba) sws_scale(d->sws, (const uint8_t *const *)f->data, f->linesize, 0, h, dst, dst_stride);
|
||||
@@ -376,14 +463,12 @@ static void queue_frame(Dec *d, const AVFrame *f) {
|
||||
for (int y = 0; y < h; y++) memcpy(p.nv12 + (size_t)y * w, f->data[0] + (size_t)y * f->linesize[0], (size_t)w);
|
||||
for (int y = 0; y < h / 2; y++)
|
||||
memcpy(p.nv12 + (size_t)w * h + (size_t)y * w, f->data[1] + (size_t)y * f->linesize[1], (size_t)w);
|
||||
} else if (f->format == AV_PIX_FMT_YUV420P || f->format == AV_PIX_FMT_YUVA420P) { /* (planar 4:2:0 -> NV12) */
|
||||
} else if (planar) { /* (planar 4:2:0 -> NV12) */
|
||||
for (int y = 0; y < h; y++) memcpy(p.nv12 + (size_t)y * w, f->data[0] + (size_t)y * f->linesize[0], (size_t)w);
|
||||
uint8_t *uv = p.nv12 + (size_t)w * h;
|
||||
for (int y = 0; y < h / 2; y++)
|
||||
for (int x = 0; x < w / 2; x++) {
|
||||
uv[(size_t)y * w + 2 * x] = f->data[1][(size_t)y * f->linesize[1] + x];
|
||||
uv[(size_t)y * w + 2 * x + 1] = f->data[2][(size_t)y * f->linesize[2] + x];
|
||||
}
|
||||
video_uv_interleave(uv + (size_t)y * w, f->data[1] + (size_t)y * f->linesize[1],
|
||||
f->data[2] + (size_t)y * f->linesize[2], w / 2);
|
||||
}
|
||||
if (d->qn == MAX_QUEUE) {
|
||||
picture_free(&d->queue[d->qhead]);
|
||||
@@ -391,6 +476,8 @@ static void queue_frame(Dec *d, const AVFrame *f) {
|
||||
}
|
||||
d->queue[(d->qhead + d->qn) % MAX_QUEUE] = p;
|
||||
d->qn++;
|
||||
d->movie.pictures++;
|
||||
video_pace_picture(&d->pace, f->pts); /* ((Triforce, 11-22, THREAD-82) the gap between pictures, from their own times) */
|
||||
}
|
||||
static void mf_picture(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts) {
|
||||
AVFrame f;
|
||||
@@ -417,9 +504,12 @@ static void decode_pending(Dec *d) {
|
||||
/* one submission decoded (its pictures into the queue, if any) */
|
||||
static void decode_next(Dec *d) {
|
||||
if (d->phead) {
|
||||
const int64_t began = hos_now_ns(); /* ((Triforce, 11-22, IO-32) each submission timed, for the movie's line) */
|
||||
const int64_t pictures = d->movie.pictures;
|
||||
Pending *n = d->phead;
|
||||
d->phead = n->next;
|
||||
if (!d->phead) d->ptail = NULL;
|
||||
d->npending--;
|
||||
if (d->mf) { /* (hookshot) */
|
||||
if (n->pkt) {
|
||||
d->eos = 0;
|
||||
@@ -448,6 +538,10 @@ static void decode_next(Dec *d) {
|
||||
avcodec_flush_buffers(d->ctx); /* (a stream after it starts again) */
|
||||
}
|
||||
free(n);
|
||||
const int64_t took = hos_now_ns() - began;
|
||||
video_movie_decoded(&d->movie, took);
|
||||
/* ((Triforce, 11-22, THREAD-82) and what a decode that gives a picture takes lately, for the catch-up's bound) */
|
||||
if (d->movie.pictures != pictures) video_pace_decoded(&d->pace, took);
|
||||
}
|
||||
}
|
||||
static void pending_push(Dec *d, AVPacket *pkt) {
|
||||
@@ -456,6 +550,7 @@ static void pending_push(Dec *d, AVPacket *pkt) {
|
||||
if (d->ptail) d->ptail->next = n;
|
||||
else d->phead = n;
|
||||
d->ptail = n;
|
||||
d->npending++;
|
||||
}
|
||||
/* SubmitXDecoderInputBuffer(u64* id, decoder, data, size, TimeSpan pts, bool last): kept, decoded when a picture is
|
||||
acquired (the library submits a whole movie at once: its pictures decoded then would not fit any queue) */
|
||||
@@ -526,6 +621,24 @@ static uint64_t submitted(void) { /* a submission: every handle given out befo
|
||||
|
||||
static void dec_submit(Ctx *c) {
|
||||
Dec *d = (Dec *)host_of(ARG(1), DEC_MAGIC);
|
||||
/* ((Triforce, 11-22, the performance audit's IO-34, after Source A's) Refused at once, with the console's result for
|
||||
it, while the limit's worth of submissions wait (video_input.h): the game submits it again later. Never the
|
||||
stream's last, and only by a decoder that decodes: one without would never make room. Before submitted(): a
|
||||
refusal is no submission, and must not move the count the idle buffers' sync handles are compared with. The
|
||||
event is cleared under the lock, with the flag input_room reads, so no room made on another thread is missed) */
|
||||
if (d && (d->ctx || d->mf) && input_limit()) {
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
const int refuse = video_input_refuse(input_limit(), d->npending, d->qn, (int)(ARG(5) & 1));
|
||||
if (refuse) {
|
||||
if (!d->ready_clear) d->ready_clear = 1, hos_system_event_clear(d->ready);
|
||||
if (video_log() && d->movie.refused < 4)
|
||||
hos_log("video: a submission refused (pts %lld): %d wait to be decoded and %d to be taken", (long long)ARG(4),
|
||||
d->npending, d->qn);
|
||||
d->movie.refused++;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
if (refuse) { RET(VIDEO_RESULT_INPUT_FULL); return; }
|
||||
}
|
||||
const uint64_t id = submitted();
|
||||
if (ARG(0)) G64(ARG(0)) = id;
|
||||
if (!d || (!d->ctx && !d->mf)) { RET(0); return; }
|
||||
@@ -546,12 +659,14 @@ static void dec_submit(Ctx *c) {
|
||||
}
|
||||
}
|
||||
d->submits++;
|
||||
d->stopped = 0;
|
||||
if (ARG(5) & 1) pending_push(d, NULL);
|
||||
const int logged_one = d->log_submits > 0;
|
||||
if (logged_one) d->log_submits--;
|
||||
if (video_log() && (d->submits <= 8 || (ARG(5) & 1) || logged_one))
|
||||
hos_log("video: submit %lld (%llu bytes, pts %lld%s)", (long long)d->submits, (unsigned long long)ARG(3),
|
||||
(long long)ARG(4), (ARG(5) & 1) ? ", last" : "");
|
||||
input_room(d); /* ((Triforce, 11-22, IO-34) as at every unlock that ends a call: the event never stays clear with room) */
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
RET(0);
|
||||
}
|
||||
@@ -564,14 +679,17 @@ static void dec_flush(Ctx *c, const char *what) {
|
||||
(long long)d->acquires, d->qn, d->eos ? "at the end" : d->phead ? "submissions left" : "none left",
|
||||
(unsigned long long)c->x[30]);
|
||||
d->log_submits = 8;
|
||||
movie_end(d); /* ((Triforce, 11-22, IO-32) what the movie cost to decode, once) */
|
||||
if (d->ctx) avcodec_flush_buffers(d->ctx);
|
||||
else mf_av1_flush(d->mf);
|
||||
pending_free(d);
|
||||
for (int i = 0; i < d->qn; i++) picture_free(&d->queue[(d->qhead + i) % MAX_QUEUE]);
|
||||
d->qhead = d->qn = 0;
|
||||
d->eos = 0;
|
||||
d->stopped = 1;
|
||||
d->clock_set = 0;
|
||||
for (int i = 0; i < MAX_SLOTS; i++) d->slot_last[i] = 0;
|
||||
input_room(d); /* ((Triforce, 11-22, IO-34) nothing waits any more: ready for submissions) */
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
}
|
||||
RET(0);
|
||||
@@ -592,11 +710,22 @@ static void dec_acquire(Ctx *c) {
|
||||
Dec *d = (Dec *)host_of(ARG(2), DEC_MAGIC);
|
||||
if (ARG(1)) G64(ARG(1)) = 0;
|
||||
if (!d) { RET(RESULT_NO_OUTPUT); return; }
|
||||
/* (the library aborts on any failure here: the hardware call waits for a picture. Up to ~100 ms for one decoded
|
||||
by another thread's submission; then a free buffer as it was, so the caller is never refused) */
|
||||
/* (the library aborts on any failure here: the hardware call waits for a picture. Up to 100 ms by the clock for one
|
||||
decoded by another thread's submission, none after a Stop or Flush with nothing submitted since; then a free
|
||||
buffer as it was, so the caller is never refused. The wait was 50 sleeps of 2 ms, each 4-5 ms long on Windows:
|
||||
~250 ms after a movie's Stop, while the game's main thread joined the movie thread, a frame that long at every
|
||||
load from the title. HOS_VIDEO_ACQUIRE_TRIES=1: the 50 sleeps as before.
|
||||
The decode runs without the thread's virtual core: other game threads run on it meanwhile. It is taken back only
|
||||
after the lock is let go, never under it (dec_submit holds a core while it waits for the lock).
|
||||
HOS_VIDEO_KEEP_CORE=1: the core held throughout, as before) */
|
||||
static int by_tries = -1, keep_core = -1;
|
||||
if (by_tries < 0) by_tries = getenv("HOS_VIDEO_ACQUIRE_TRIES") && atoi(getenv("HOS_VIDEO_ACQUIRE_TRIES")) == 1;
|
||||
if (keep_core < 0) keep_core = getenv("HOS_VIDEO_KEEP_CORE") && atoi(getenv("HOS_VIDEO_KEEP_CORE")) == 1;
|
||||
const int away = keep_core ? 0 : hos_vcore_away();
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
decode_pending(d);
|
||||
for (int tries = 0; !d->qn && !d->eos && tries < 50; tries++) {
|
||||
const int64_t wait_until = hos_now_ns() + 100000000;
|
||||
for (int tries = 0; !d->qn && !d->eos && (by_tries ? tries < 50 : !d->stopped && hos_now_ns() < wait_until); tries++) {
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
hos_sleep_ns(2000000);
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
@@ -606,7 +735,9 @@ static void dec_acquire(Ctx *c) {
|
||||
for (int i = 0; i < MAX_SLOTS && slot < 0; i++)
|
||||
if (d->slots[i] && !d->busy[i] && host_of(d->slots[i], POOL_MAGIC)) slot = i;
|
||||
if (slot < 0) {
|
||||
input_room(d); /* ((Triforce, 11-22, IO-34) here and at the two exits below: what was decoded or taken made room) */
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
if (away) hos_vcore_back();
|
||||
RET(RESULT_NO_OUTPUT);
|
||||
return;
|
||||
}
|
||||
@@ -621,7 +752,9 @@ static void dec_acquire(Ctx *c) {
|
||||
d->eos ? " (the end)" : idle ? " (idle: its sync not reached)" : "", slot);
|
||||
else if (d->acquires == 1 && !d->eos) /* (the start: the frame shows the buffer as it was, black since the pool's fill) */
|
||||
hos_log("video: the first buffer went out before the first picture was decoded (shown black)");
|
||||
input_room(d);
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
if (away) hos_vcore_back();
|
||||
if (ARG(0)) G32(ARG(0)) = (uint32_t)slot;
|
||||
RET(0);
|
||||
return;
|
||||
@@ -629,8 +762,16 @@ static void dec_acquire(Ctx *c) {
|
||||
{ /* (hookshot) The game takes one picture a frame, and the sound plays on: under 60 fps the movie fell behind its
|
||||
sound (the intro "slow motion, audio ahead", issues 146 and 179). Pictures behind the movie's clock are skipped:
|
||||
the next ones decoded, as the whole movie is submitted at once. HOS_VIDEO_NO_CATCHUP=1: as before */
|
||||
static int off = -1;
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-82, after Source A's) Decoding ahead to skip had no bound but
|
||||
the guard below. With a decoder slower than the movie every picture decoded to catch up puts the clock
|
||||
further ahead, so an acquire decoded hundreds of pictures with the lock held, threw them away, and the next
|
||||
one started further behind. It now stops when a decode takes longer than the gap between two pictures, or
|
||||
once this acquire has decoded for one gap, and shows the late picture it has (video_catchup.h): such a movie
|
||||
falls behind its sound instead, and its line says so. HOS_VIDEO_UNBOUNDED_CATCHUP=1: no bound, as before) */
|
||||
static int off = -1, unbounded = -1;
|
||||
if (off < 0) off = getenv("HOS_VIDEO_NO_CATCHUP") != NULL;
|
||||
if (unbounded < 0)
|
||||
unbounded = getenv("HOS_VIDEO_UNBOUNDED_CATCHUP") != NULL && atoi(getenv("HOS_VIDEO_UNBOUNDED_CATCHUP")) != 0;
|
||||
const int64_t now = hos_now_ns(), late = 25000000; /* (a frame and a half at 60 fps) */
|
||||
Picture *h = &d->queue[d->qhead];
|
||||
if (!d->clock_set || h->pts < d->clock_pts) d->clock_set = 1, d->clock_ns = now, d->clock_pts = h->pts; /* (a start, a loop) */
|
||||
@@ -640,6 +781,14 @@ static void dec_acquire(Ctx *c) {
|
||||
if (d->queue[d->qhead].pts + late >= due) break;
|
||||
if (d->qn < 2) {
|
||||
if (!d->phead) break;
|
||||
if (video_catchup_stop(unbounded, video_pace_spacing(&d->pace), d->pace.recent_ns, hos_now_ns() - now)) {
|
||||
if (video_log())
|
||||
hos_log("video: the catch-up stops %.2f s behind: a decode takes %.1f ms lately, a picture is due "
|
||||
"every %.1f ms, and this acquire has decoded for %.1f ms", (double)(due - d->queue[d->qhead].pts) / 1e9,
|
||||
(double)d->pace.recent_ns / 1e6, (double)video_pace_spacing(&d->pace) / 1e6,
|
||||
(double)(hos_now_ns() - now) / 1e6);
|
||||
break;
|
||||
}
|
||||
decode_next(d);
|
||||
continue;
|
||||
}
|
||||
@@ -648,6 +797,7 @@ static void dec_acquire(Ctx *c) {
|
||||
skipped++;
|
||||
}
|
||||
if (skipped && video_log()) hos_log("video: %d pictures skipped to catch up with the movie's clock", skipped);
|
||||
d->movie.skipped += skipped; /* ((Triforce, 11-22, IO-32) and in the movie's line) */
|
||||
}
|
||||
Picture *p = &d->queue[d->qhead];
|
||||
Pool *o = (Pool *)host_of(d->slots[slot], POOL_MAGIC);
|
||||
@@ -703,7 +853,9 @@ static void dec_acquire(Ctx *c) {
|
||||
}
|
||||
picture_free(p);
|
||||
d->qhead = (d->qhead + 1) % MAX_QUEUE, d->qn--;
|
||||
input_room(d);
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
if (away) hos_vcore_back();
|
||||
if (ARG(0)) G32(ARG(0)) = (uint32_t)slot;
|
||||
RET(0);
|
||||
}
|
||||
@@ -734,13 +886,13 @@ static void dec_properties(Ctx *c) {
|
||||
}
|
||||
RET(0);
|
||||
}
|
||||
static uint64_t ready_event(void) {
|
||||
/* GetXDecoderSubmissionReadyEvent(decoder). ((Triforce, 11-22, IO-34) The decoder's own event, clear while its input
|
||||
is full: it was one event for every decoder, always set. That one is still what an unknown decoder gets) */
|
||||
static uint64_t ready_event(Ctx *c) {
|
||||
static uint64_t event;
|
||||
if (!event) {
|
||||
event = hos_sys_alloc(0x40, 16);
|
||||
hos_system_event_init(event, 0);
|
||||
hos_system_event_signal(event);
|
||||
}
|
||||
const Dec *d = (const Dec *)host_of(ARG(0), DEC_MAGIC);
|
||||
if (d && d->ready) return d->ready;
|
||||
if (!event) event = ready_new();
|
||||
return event;
|
||||
}
|
||||
|
||||
@@ -753,7 +905,7 @@ HLE(_ZN2nn5codec26UnregisterVp9DecoderBufferEPNS0_14Vp9DecoderTypeEj) { dec_unre
|
||||
HLE(_ZN2nn5codec15StartVp9DecoderEPNS0_14Vp9DecoderTypeE) { RET(0); }
|
||||
HLE(_ZN2nn5codec14StopVp9DecoderEPNS0_14Vp9DecoderTypeE) { dec_flush(c, "Stop"); }
|
||||
HLE(_ZN2nn5codec15FlushVp9DecoderEPNS0_14Vp9DecoderTypeE) { dec_flush(c, "Flush"); }
|
||||
HLE(_ZN2nn5codec33GetVp9DecoderSubmissionReadyEventEPNS0_14Vp9DecoderTypeE) { RET(ready_event()); }
|
||||
HLE(_ZN2nn5codec33GetVp9DecoderSubmissionReadyEventEPNS0_14Vp9DecoderTypeE) { RET(ready_event(c)); }
|
||||
HLE(_ZN2nn5codec27SubmitVp9DecoderInputBufferEPmPNS0_14Vp9DecoderTypeEPKvmNS_8TimeSpanEb) { dec_submit(c); }
|
||||
HLE(_ZN2nn5codec36SubmitEncryptedVp9DecoderInputBufferEPmPNS0_14Vp9DecoderTypeEPKNS0_18EncryptionPropertyEPKvmNS_8TimeSpanEb) {
|
||||
const uint64_t id = submitted();
|
||||
@@ -778,7 +930,7 @@ HLE(_ZN2nn5codec24RegisterAv1DecoderBufferEPNS0_14Av1DecoderTypeEjPNS0_25VideoDe
|
||||
HLE(_ZN2nn5codec15StartAv1DecoderEPNS0_14Av1DecoderTypeE) { RET(0); }
|
||||
HLE(_ZN2nn5codec14StopAv1DecoderEPNS0_14Av1DecoderTypeE) { dec_flush(c, "Stop"); }
|
||||
HLE(_ZN2nn5codec15FlushAv1DecoderEPNS0_14Av1DecoderTypeE) { dec_flush(c, "Flush"); }
|
||||
HLE(_ZN2nn5codec33GetAv1DecoderSubmissionReadyEventEPNS0_14Av1DecoderTypeE) { RET(ready_event()); }
|
||||
HLE(_ZN2nn5codec33GetAv1DecoderSubmissionReadyEventEPNS0_14Av1DecoderTypeE) { RET(ready_event(c)); }
|
||||
HLE(_ZN2nn5codec27SubmitAv1DecoderInputBufferEPmPNS0_14Av1DecoderTypeEPKvmNS_8TimeSpanEb) { dec_submit(c); }
|
||||
HLE(_ZN2nn5codec29AcquireAv1DecoderOutputBufferEPjPNS_2gs16NativeSyncHandleEPNS0_14Av1DecoderTypeE) { dec_acquire(c); }
|
||||
HLE(_ZN2nn5codec29ReleaseAv1DecoderOutputBufferEPNS0_14Av1DecoderTypeEjNS_2gs16NativeSyncHandleE) { dec_release(c); }
|
||||
@@ -793,7 +945,7 @@ HLE(_ZN2nn5codec26UnregisterVp8DecoderBufferEPNS0_14Vp8DecoderTypeEj) { dec_unre
|
||||
HLE(_ZN2nn5codec15StartVp8DecoderEPNS0_14Vp8DecoderTypeE) { RET(0); }
|
||||
HLE(_ZN2nn5codec14StopVp8DecoderEPNS0_14Vp8DecoderTypeE) { dec_flush(c, "Stop"); }
|
||||
HLE(_ZN2nn5codec15FlushVp8DecoderEPNS0_14Vp8DecoderTypeE) { dec_flush(c, "Flush"); }
|
||||
HLE(_ZN2nn5codec33GetVp8DecoderSubmissionReadyEventEPNS0_14Vp8DecoderTypeE) { RET(ready_event()); }
|
||||
HLE(_ZN2nn5codec33GetVp8DecoderSubmissionReadyEventEPNS0_14Vp8DecoderTypeE) { RET(ready_event(c)); }
|
||||
HLE(_ZN2nn5codec27SubmitVp8DecoderInputBufferEPmPNS0_14Vp8DecoderTypeEPKvmNS_8TimeSpanEb) { dec_submit(c); }
|
||||
HLE(_ZN2nn5codec29AcquireVp8DecoderOutputBufferEPjPNS_2gs16NativeSyncHandleEPNS0_14Vp8DecoderTypeE) { dec_acquire(c); }
|
||||
HLE(_ZN2nn5codec29ReleaseVp8DecoderOutputBufferEPNS0_14Vp8DecoderTypeEjNS_2gs16NativeSyncHandleE) { dec_release(c); }
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
own procs (program builders, native images, sparse tile layouts, recording modes). The SDK's exports reach the
|
||||
same functions as the proc table (nvn2_dispatch, from gen/nvn2_exports.c). */
|
||||
#include "hos.h"
|
||||
#include "host_alloc.h" /* ((Triforce, 11-22, OS-04) allocations that wait at Windows' commit limit before they answer NULL) */
|
||||
#include "shadow_slot.h" /* ((Triforce, 11-21, TEX-85) a character's shadow map's storage in steps) */
|
||||
#include "guest_bounds.h" /* ((Triforce) hos_span_ok: the game's first and count checked without an int overflow) */
|
||||
#include "../gpu/render/rx_nvn.h"
|
||||
#include <stdio.h>
|
||||
@@ -673,17 +675,53 @@ static int no_builder(Ctx *c, const char *what) {
|
||||
if (told++ < 4) hos_log("nvn: %s of no texture builder (lr %#llx): 0", what, (unsigned long long)c->x[30]);
|
||||
return 1;
|
||||
}
|
||||
/* ((Triforce, 11-21, the performance audit's TEX-85, after Source A's) A character's shadow map reserves the storage of the
|
||||
next step of sides up (shadow_slot.h), so that the render targets the game makes after it keep their addresses while its
|
||||
side changes from frame to frame. HOS_SHADOW_BUCKET=<step> (384); 0: every texture its own size, as before; not set, 0
|
||||
where the target heap is pinned (nn_applet.c hos_rt_pool_pinned). The layout of the side it reserves in *out, or 0 when it
|
||||
reserves its own. The texture's own layout (nvn_tex_layout of its builder) is not changed anywhere */
|
||||
static int shadow_slot_layout(const nvn_texdesc *d, nvn_texlayout *out) {
|
||||
extern int hos_rt_pool_pinned(void);
|
||||
static int step = -1;
|
||||
static volatile long stepped;
|
||||
int now = step;
|
||||
if (now < 0) {
|
||||
const char *e = getenv("HOS_SHADOW_BUCKET");
|
||||
const int pinned = hos_rt_pool_pinned(); /* (-1: the heap not sized yet: asked again at the next map) */
|
||||
now = (int)hos_shadow_slot_step(e, pinned > 0);
|
||||
if (e || pinned >= 0) {
|
||||
step = now;
|
||||
hos_log("nvn: a character's shadow map reserves the storage of the next multiple of %d up (HOS_SHADOW_BUCKET%s; 0: "
|
||||
"its own size, as before)", now, e ? "" : pinned > 0 ? ": the target heap is pinned, so 0" : ", 384 when not set");
|
||||
}
|
||||
}
|
||||
const uint32_t side = hos_shadow_slot_side(d->target == NVN_TEXTURE_TARGET_2D, d->format, d->levels, d->depth, d->width,
|
||||
d->height, (uint32_t)now);
|
||||
if (!side || side == d->width) return 0;
|
||||
nvn_texdesc slot = *d;
|
||||
slot.width = slot.height = side;
|
||||
nvn_tex_layout(&slot, out);
|
||||
const long n = __atomic_add_fetch(&stepped, 1, __ATOMIC_RELAXED); /* (as it doubles: how often the game asks) */
|
||||
if (n >= 1024 && !(n & (n - 1)))
|
||||
hos_log("nvn: shadow maps answered a stepped storage: %ld so far (the last %ux%u as %ux%u)", n, d->width, d->height, side, side);
|
||||
return 1;
|
||||
}
|
||||
NV(nvnTextureBuilderGetStorageSize) {
|
||||
if (no_builder(c, "storage size")) { RET(0); return; }
|
||||
nvn_texlayout L;
|
||||
nvn_texlayout L, slot;
|
||||
nvn_tex_layout(&CTB(P(0))->d, &L);
|
||||
if (!L.bpb) hos_log("nvn: texture format %#x unknown: storage size 0", CTB(P(0))->d.format);
|
||||
/* ((Triforce, 11-21, TEX-85) the larger of its own and its slot's) */
|
||||
if (L.bpb && shadow_slot_layout(&CTB(P(0))->d, &slot) && slot.size > L.size) L.size = slot.size;
|
||||
RET(L.size);
|
||||
}
|
||||
NV(nvnTextureBuilderGetStorageAlignment) {
|
||||
if (no_builder(c, "storage alignment")) { RET(512); return; }
|
||||
nvn_texlayout L;
|
||||
nvn_texlayout L, slot;
|
||||
nvn_tex_layout(&CTB(P(0))->d, &L);
|
||||
/* ((Triforce, 11-21, TEX-85) the slot's where it is the larger: one alignment for every side within a step, and the
|
||||
texture's own still met) */
|
||||
if (L.bpb && shadow_slot_layout(&CTB(P(0))->d, &slot) && slot.align > L.align) L.align = slot.align;
|
||||
RET(L.align);
|
||||
}
|
||||
NV(nvnTextureBuilderGetZCullStorageSize) { RET(0); } /* no ZCull: nothing to store */
|
||||
@@ -1368,7 +1406,7 @@ static unsigned pool_class(size_t n) {
|
||||
static void *pool_get(size_t n, unsigned *cls) {
|
||||
const unsigned c = pool_off() ? POOL_MAX + 1 : pool_class(n);
|
||||
*cls = c;
|
||||
if (c > POOL_MAX) return malloc(n);
|
||||
if (c > POOL_MAX) return hos_xmalloc(n); /* ((Triforce, OS-04) waits at the commit limit before it answers NULL: host_alloc.h) */
|
||||
AcquireSRWLockExclusive(&pool_lock);
|
||||
void *b = pool_head[c];
|
||||
if (b) {
|
||||
@@ -1376,7 +1414,7 @@ static void *pool_get(size_t n, unsigned *cls) {
|
||||
pool_bytes[c] -= (uint64_t)1 << c;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&pool_lock);
|
||||
return b ? b : malloc((size_t)1 << c);
|
||||
return b ? b : hos_xmalloc((size_t)1 << c);
|
||||
}
|
||||
static void pool_put(void *b, unsigned cls) {
|
||||
if (!b) return;
|
||||
@@ -1417,7 +1455,7 @@ static cmd_rec *cmd_adopt(nvn_cmd *o) {
|
||||
const uint32_t s = ncmd_free ? cmd_free[--ncmd_free] : cmd_next++;
|
||||
if ((s >> CMD_CHUNK_BITS) >= CMD_CHUNKS) die("too many command buffers");
|
||||
if (!cmd_chunks[s >> CMD_CHUNK_BITS]) {
|
||||
cmd_rec *k = (cmd_rec *)calloc((size_t)1 << CMD_CHUNK_BITS, sizeof *k);
|
||||
cmd_rec *k = (cmd_rec *)hos_xcalloc((size_t)1 << CMD_CHUNK_BITS, sizeof *k); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!k) die("command buffer table");
|
||||
cmd_chunks[s >> CMD_CHUNK_BITS] = k;
|
||||
}
|
||||
@@ -1452,7 +1490,7 @@ static void cmd_release(nvn_cmd *o) { /* the object's slot and recording buffe
|
||||
memset(r, 0, sizeof *r);
|
||||
if (ncmd_free == cap_cmd_free) {
|
||||
cap_cmd_free = cap_cmd_free ? cap_cmd_free * 2 : 256;
|
||||
cmd_free = (uint32_t *)realloc(cmd_free, cap_cmd_free * sizeof *cmd_free);
|
||||
cmd_free = (uint32_t *)hos_xrealloc(cmd_free, cap_cmd_free * sizeof *cmd_free); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!cmd_free) die("command buffer table");
|
||||
}
|
||||
cmd_free[ncmd_free++] = o->slot;
|
||||
@@ -1560,8 +1598,8 @@ static size_t live_at(uint64_t h, size_t cap) { return (size_t)((h * 0x9E3779B97
|
||||
static void live_handle_add(uint64_t h) { /* (copy_lock held exclusively) */
|
||||
if ((nlive + 1) * 2 > live_cap) {
|
||||
const size_t cap = live_cap ? live_cap * 2 : 65536;
|
||||
uint64_t *t = (uint64_t *)calloc(cap, sizeof *t);
|
||||
live_table *pub = (live_table *)malloc(sizeof *pub);
|
||||
uint64_t *t = (uint64_t *)hos_xcalloc(cap, sizeof *t); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
live_table *pub = (live_table *)hos_xmalloc(sizeof *pub);
|
||||
if (!t || !pub) die("recording handle set");
|
||||
for (size_t i = 0; i < live_cap; i++) {
|
||||
if (!live_handles[i]) continue;
|
||||
@@ -1635,7 +1673,7 @@ static void free_dead_copies(void) { /* (copy_lock held) */
|
||||
static void dead_copy_add(copy_prefix *p, uint64_t after) { /* (copy_lock held) */
|
||||
if (ndead == cap_dead) {
|
||||
cap_dead = cap_dead ? cap_dead * 2 : 1024;
|
||||
dead = (dead_copy *)realloc(dead, cap_dead * sizeof *dead);
|
||||
dead = (dead_copy *)hos_xrealloc(dead, cap_dead * sizeof *dead); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!dead) die("recording copies let go");
|
||||
}
|
||||
dead[ndead].p = p;
|
||||
@@ -1682,10 +1720,13 @@ static void release_copies(uint64_t lo, uint64_t hi) {
|
||||
or calling a recording waited behind it */
|
||||
#define SWEEP_EVERY 1024u
|
||||
#define SWEEP_SLICE 2688u /* (65536 / 2688 * 1024: every bucket once in ~25,000 recordings) */
|
||||
static void sweep_overwritten_copies(void) { /* (copy_lock held) */
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-56, after Source A's) it returns how many it let go and logs nothing:
|
||||
its line was written with copy_lock held exclusively, every thread recording or calling a recording waiting behind the
|
||||
write. nvnCommandBufferEndRecording says it once the lock is released) */
|
||||
static size_t sweep_overwritten_copies(void) { /* (copy_lock held) */
|
||||
static int off = -1;
|
||||
if (off < 0) off = getenv("HOS_NVN_NO_SWEEP") != NULL;
|
||||
if (off || !rec_buckets) return;
|
||||
if (off || !rec_buckets) return 0;
|
||||
free_dead_copies();
|
||||
const uint64_t after = rx_nvn_pushed() + held_count();
|
||||
size_t swept = 0;
|
||||
@@ -1721,8 +1762,7 @@ static void sweep_overwritten_copies(void) { /* (copy_lock held) */
|
||||
swept++;
|
||||
}
|
||||
}
|
||||
static int logged;
|
||||
if (swept && logged++ < 12) hos_log("nvn: %zu copies of recordings whose control memory was reused let go (%zu live)", swept, ncopies);
|
||||
return swept;
|
||||
}
|
||||
NV(nvnCommandBufferAddControlMemory) {
|
||||
nvn_cmd *o = CB(P(0));
|
||||
@@ -1815,12 +1855,27 @@ NV(nvnCommandBufferEndRecording) {
|
||||
ncopies++;
|
||||
live_handle_add((uint64_t)(uintptr_t)copy);
|
||||
static uint64_t made;
|
||||
if (++made % SWEEP_EVERY == 0) sweep_overwritten_copies();
|
||||
if (made % 50000 == 0)
|
||||
hos_log("nvn: %llu recordings made, %zu live copies (%.1f MB), %zu texture copies; %lld command buffers' segments "
|
||||
"(%.1f MB)", (unsigned long long)made, ncopies, copy_bytes / 1048576.0, npins, (long long)seg_count,
|
||||
seg_bytes / 1048576.0);
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-56, after Source A's) what the sweep's line and the 50,000-recording
|
||||
line say is taken under the lock; the lines are written after it is released, with the virtual core given up for
|
||||
when this thread makes the write itself (HOS_LOG_SYNC=1, or the log's queue full): a write that blocks then holds
|
||||
neither the recordings' lock nor a core another game thread waits for) */
|
||||
static int sweeps_said;
|
||||
const size_t swept = ++made % SWEEP_EVERY == 0 ? sweep_overwritten_copies() : 0;
|
||||
const int say_swept = swept && sweeps_said < 12 ? ++sweeps_said : 0;
|
||||
const int say_made = made % 50000 == 0;
|
||||
const uint64_t made_now = made;
|
||||
const size_t live_now = ncopies, pins_now = npins;
|
||||
ReleaseSRWLockExclusive(©_lock);
|
||||
if (say_swept || say_made) {
|
||||
const int away = hos_vcore_away();
|
||||
if (say_swept)
|
||||
hos_log("nvn: %zu copies of recordings whose control memory was reused let go (%zu live)", swept, live_now);
|
||||
if (say_made)
|
||||
hos_log("nvn: %llu recordings made, %zu live copies (%.1f MB), %zu texture copies; %lld command buffers' segments "
|
||||
"(%.1f MB)", (unsigned long long)made_now, live_now, copy_bytes / 1048576.0, pins_now, (long long)seg_count,
|
||||
seg_bytes / 1048576.0);
|
||||
if (away) hos_vcore_back();
|
||||
}
|
||||
if (watch_draw_cmd && watch_draw_cmd == (void *)o && nwatch_handles < 16) {
|
||||
watch_handles[nwatch_handles++] = (uint64_t)(uintptr_t)copy;
|
||||
watch_draw_cmd = NULL;
|
||||
@@ -2315,7 +2370,7 @@ static int pin_same(const nvn_tex *a, const nvn_tex *b) {
|
||||
}
|
||||
static void pins_grow(void) {
|
||||
const size_t n = pin_slots ? pin_slots * 2 : (1u << 16);
|
||||
nvn_tex **t = (nvn_tex **)calloc(n, sizeof *t);
|
||||
nvn_tex **t = (nvn_tex **)hos_xcalloc(n, sizeof *t); /* ((Triforce, OS-04) host_alloc.h) */
|
||||
if (!t) die("texture copies table (%zu slots)", n);
|
||||
for (size_t i = 0; i < pin_slots; i++)
|
||||
if (pins[i]) {
|
||||
@@ -2734,8 +2789,12 @@ NV(nvnQueuePresentTexture) {
|
||||
extern volatile LONG64 hos_present_count; /* display.c */
|
||||
InterlockedIncrement64(&hos_present_count);
|
||||
hos_vi_native_presented();
|
||||
/* ((Triforce, 11-22, the performance audit's OS-51) the host's part of a frame as long as the stall line's: mkw_glue.c) */
|
||||
{ void hos_stall_host_presented(void); hos_stall_host_presented(); }
|
||||
void hos_mod_cheats_frame(void); /* (Triforce) mod_cheats.c: the mods' cheat files, once a game frame; nothing without one */
|
||||
hos_mod_cheats_frame();
|
||||
void hos_achievements_frame(void);
|
||||
hos_achievements_frame(); /* ((Triforce) host/achievements.c: its lists read and named to the menu; nothing while it is off) */
|
||||
}
|
||||
|
||||
/* ---- NVN2 (mkw-recomp): what the Switch 2's NVN adds, as MKW uses it ---- */
|
||||
|
||||
180
host/oot_menu.c
180
host/oot_menu.c
@@ -28,14 +28,13 @@
|
||||
#define PAUSE_SCREEN_VTABLE 0x84c0baa8ULL
|
||||
#define OPTIONS_FROM_PAUSE_NS 500000000LL /* (the pause menu's last update, at most this long before the Options' first) */
|
||||
void rx_option_set(int id, int value); /* rx_backend.c */
|
||||
#if defined(_WIN32) && !defined(HOS_OOT_MENU_NO_MOUSE) /* (the macro: tests/test_oot_menu_mouse.py builds the other half on Windows) */
|
||||
#include <windows.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
/* ((Triforce, 11-22) Both halves': the guest reads, a screen's list of entries and its buttons' boxes, and benchmark mode's
|
||||
presses through the start's screens (bench_menu). Without them the Linux half pressed nothing, and the benchmark stood
|
||||
on the title screen (#bug-tracker, 11-21). The mouse, Quit and Mods stay the Windows half's */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_MOUSE_*) */
|
||||
#include "hos_bench.h" /* ((Triforce, 11-19) benchmark mode) */
|
||||
void hos_hid_tap(const char *press); /* (nn_hid.c) */
|
||||
|
||||
/* A guest read, of the pointers the game's objects hold: inside the guest's space (below 1 MB: a null pointer's), it is
|
||||
read as the lifted code reads it; a page there not committed yet is committed on the touch (mkw_glue.c
|
||||
@@ -50,6 +49,77 @@ static uint64_t peek64(uint64_t a) {
|
||||
uint64_t v = 0;
|
||||
return peek(a, &v, 8) ? v : 0;
|
||||
}
|
||||
|
||||
/* the screen's list of entries: fn(entry, button) for each, at most max */
|
||||
static int each_entry(uint64_t screen, int max, void (*fn)(int i, uint64_t entry, uint64_t button, void *arg), void *arg) {
|
||||
const uint64_t head = screen + 136;
|
||||
int32_t off = 0;
|
||||
peek(screen + 156, &off, 4);
|
||||
uint64_t node = peek64(screen + 144);
|
||||
int i = 0;
|
||||
for (; node && node != head && i < max; i++) {
|
||||
const uint64_t entry = node - (uint64_t)(int64_t)off;
|
||||
if (fn) fn(i, entry, peek64(entry + 24), arg);
|
||||
node = peek64(node + 8);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
typedef struct { float x0, y0, x1, y1; } Rect;
|
||||
/* The button's boxes (button+0x58, 0xb0 apart). The first is its hit box: its global matrix at +0x28 (3 x 4, by column:
|
||||
the translation at +0x4c, +0x50; the layout's units, moving with a page as it slides; all 0 while its page is not shown)
|
||||
and its rectangle at +0x84 (x, y, w, h, about the box's origin). The second is centred on the slot's picture (its
|
||||
translation at +0xfc, +0x100; its own rectangle a fixed 700 x 700). A few slots' hit boxes cover only a side of the
|
||||
picture (Pa_ItemIcon_05: 100 wide on the right of a slot 260 wide, its centre 80 to the right of the picture's): such
|
||||
a box is mirrored about the picture's centre and both taken */
|
||||
static int button_rect(uint64_t button, Rect *r) {
|
||||
float m[12], b[4], c[2];
|
||||
const uint64_t box = peek64(button + 0x58);
|
||||
if (!peek(box + 0x28, m, sizeof m) || !peek(box + 0x84, b, sizeof b)) return 0;
|
||||
const float sx = m[0], sy = m[4], tx = m[9], ty = m[10];
|
||||
if (!(sx > 1e-3f) || !(sy > 1e-3f) || !(b[2] > 0) || !(b[3] > 0)) return 0;
|
||||
r->x0 = tx + sx * b[0], r->x1 = tx + sx * (b[0] + b[2]);
|
||||
r->y0 = ty + sy * b[1], r->y1 = ty + sy * (b[1] + b[3]);
|
||||
float s2;
|
||||
if (peek(box + 0xd8, &s2, 4) && s2 > 1e-3f && peek(box + 0xfc, c, sizeof c)) {
|
||||
const float w = r->x1 - r->x0, h = r->y1 - r->y0, cx = (r->x0 + r->x1) / 2, cy = (r->y0 + r->y1) / 2;
|
||||
if (c[0] - cx < w && cx - c[0] < w && c[1] - cy < h && cy - c[1] < h) { /* (the picture's centre near the box) */
|
||||
const float x0 = 2 * c[0] - r->x1, x1 = 2 * c[0] - r->x0, y0 = 2 * c[1] - r->y1, y1 = 2 * c[1] - r->y0;
|
||||
if (x0 < r->x0) r->x0 = x0;
|
||||
if (x1 > r->x1) r->x1 = x1;
|
||||
if (y0 < r->y0) r->y0 = y0;
|
||||
if (y1 > r->y1) r->y1 = y1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-19) benchmark mode, host/hos_bench.c: the start's screens pressed through as each gets a focused button. The
|
||||
file select is left on the file it opens on, the newest (its entries are a carousel's places, not files: focusing one by
|
||||
its place loaded another file in a lab run) */
|
||||
typedef struct { int index, state; } BenchLook;
|
||||
static void bench_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)entry;
|
||||
BenchLook *l = (BenchLook *)arg;
|
||||
uint8_t state = 0;
|
||||
if (peek(button + 56, &state, 1) && state > l->state) l->index = i, l->state = state;
|
||||
}
|
||||
static void bench_menu(Ctx *c, uint64_t screen, uint64_t vt) {
|
||||
(void)c;
|
||||
BenchLook l = {-1, 0};
|
||||
const int n = each_entry(screen, 64, bench_entry, &l);
|
||||
int entry = -1;
|
||||
if (hos_bench_menu(screen, vt, 1, l.index, n, &entry) == HOS_BENCH_MENU_PRESS_A) hos_hid_tap("0:A/120");
|
||||
}
|
||||
|
||||
#if defined(_WIN32) && !defined(HOS_OOT_MENU_NO_MOUSE) /* (the macro: tests/test_oot_menu_mouse.py builds the other half on Windows) */
|
||||
#include <windows.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_MOUSE_*) */
|
||||
|
||||
/* a printable C string at a (else "-") */
|
||||
static const char *text_at(uint64_t a, char *buf, size_t cap) {
|
||||
if (!peek(a, buf, cap - 1)) return "-";
|
||||
@@ -72,21 +142,6 @@ static uint64_t focused_button; /* (the last pane f_802ed660 was handed) */
|
||||
static uint64_t asked; /* (the button the mouse asked the focus for, until it has it) */
|
||||
static int page_now = -1; /* (the shown page: 0 Collection, 1 Equipment, 2 Items, 3 System; L and R step through them) */
|
||||
|
||||
/* the screen's list of entries: fn(entry, button) for each, at most max */
|
||||
static int each_entry(uint64_t screen, int max, void (*fn)(int i, uint64_t entry, uint64_t button, void *arg), void *arg) {
|
||||
const uint64_t head = screen + 136;
|
||||
int32_t off = 0;
|
||||
peek(screen + 156, &off, 4);
|
||||
uint64_t node = peek64(screen + 144);
|
||||
int i = 0;
|
||||
for (; node && node != head && i < max; i++) {
|
||||
const uint64_t entry = node - (uint64_t)(int64_t)off;
|
||||
if (fn) fn(i, entry, peek64(entry + 24), arg);
|
||||
node = peek64(node + 8);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/* the game's focus setter on a button (on the game's thread, at a screen's update) */
|
||||
static int set_focus(Ctx *c, uint64_t screen, uint64_t button) {
|
||||
A64Fn fn = a64_lookup(0x8088ffc0ULL);
|
||||
@@ -270,7 +325,6 @@ static int map_floors(int *cur, int *top, int *bottom) {
|
||||
return *cur >= *bottom && *cur <= *top;
|
||||
}
|
||||
|
||||
typedef struct { float x0, y0, x1, y1; } Rect;
|
||||
/* (Key Items' items, in their grid's order: item_found puts the page's own positions of them in) */
|
||||
static const char *const item_keys[9] = {"Item_Medal_Shadow", "Item_Medal_Light", "Item_Medal_Forest", "Item_Stone_Forest",
|
||||
"Item_Stone_Fire", "Item_Stone_Water", "Item_Medal_Spirit", "Item_Medal_Water",
|
||||
@@ -285,9 +339,6 @@ static void item_found(const char *key, float x, float y, void *arg) {
|
||||
*a->found |= 1 << i, a->x[i] = x, a->y[i] = y;
|
||||
}
|
||||
}
|
||||
/* a button's rectangle in the layout's units (0 when it has none or is hidden) */
|
||||
static int button_rect(uint64_t button, Rect *r);
|
||||
|
||||
typedef struct { float x, y; uint64_t hit; float area; int n; uint64_t button[128]; Rect rect[128]; } Hit;
|
||||
static void shown_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)i, (void)entry;
|
||||
@@ -810,34 +861,6 @@ static void mouse_tick_locked(Ctx *c, uint64_t screen) {
|
||||
if (pressed & HOS_MOUSE_RIGHT) hos_hid_tap("0:B/80");
|
||||
}
|
||||
|
||||
/* The button's boxes (button+0x58, 0xb0 apart). The first is its hit box: its global matrix at +0x28 (3 x 4, by column:
|
||||
the translation at +0x4c, +0x50; the layout's units, moving with a page as it slides; all 0 while its page is not shown)
|
||||
and its rectangle at +0x84 (x, y, w, h, about the box's origin). The second is centred on the slot's picture (its
|
||||
translation at +0xfc, +0x100; its own rectangle a fixed 700 x 700). A few slots' hit boxes cover only a side of the
|
||||
picture (Pa_ItemIcon_05: 100 wide on the right of a slot 260 wide, its centre 80 to the right of the picture's): such
|
||||
a box is mirrored about the picture's centre and both taken */
|
||||
static int button_rect(uint64_t button, Rect *r) {
|
||||
float m[12], b[4], c[2];
|
||||
const uint64_t box = peek64(button + 0x58);
|
||||
if (!peek(box + 0x28, m, sizeof m) || !peek(box + 0x84, b, sizeof b)) return 0;
|
||||
const float sx = m[0], sy = m[4], tx = m[9], ty = m[10];
|
||||
if (!(sx > 1e-3f) || !(sy > 1e-3f) || !(b[2] > 0) || !(b[3] > 0)) return 0;
|
||||
r->x0 = tx + sx * b[0], r->x1 = tx + sx * (b[0] + b[2]);
|
||||
r->y0 = ty + sy * b[1], r->y1 = ty + sy * (b[1] + b[3]);
|
||||
float s2;
|
||||
if (peek(box + 0xd8, &s2, 4) && s2 > 1e-3f && peek(box + 0xfc, c, sizeof c)) {
|
||||
const float w = r->x1 - r->x0, h = r->y1 - r->y0, cx = (r->x0 + r->x1) / 2, cy = (r->y0 + r->y1) / 2;
|
||||
if (c[0] - cx < w && cx - c[0] < w && c[1] - cy < h && cy - c[1] < h) { /* (the picture's centre near the box) */
|
||||
const float x0 = 2 * c[0] - r->x1, x1 = 2 * c[0] - r->x0, y0 = 2 * c[1] - r->y1, y1 = 2 * c[1] - r->y0;
|
||||
if (x0 < r->x0) r->x0 = x0;
|
||||
if (x1 > r->x1) r->x1 = x1;
|
||||
if (y0 < r->y0) r->y0 = y0;
|
||||
if (y1 > r->y1) r->y1 = y1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- (Triforce) The Mods entry under a game list's last button: the pause menu's System page (Save, Load, Options,
|
||||
Special Actions, Title Screen: its buttons left of the Basic Controls panel, x under 0) and the title's list (by its
|
||||
screen's vtable). Each frame such a list runs the renderer is told its last button's box and whether the game's cursor
|
||||
@@ -934,6 +957,15 @@ static void mods_list_tick(uint64_t screen, int title, ModsCall *call) {
|
||||
const Rect r = l.r[last];
|
||||
const float pitch = (l.r[above].y0 + l.r[above].y1) / 2 - (r.y0 + r.y1) / 2;
|
||||
if (!(pitch > 20 && pitch < 600)) return;
|
||||
/* ((Triforce, 11-20) the owner: "it should be synced with the timing of the other button ui so it seamlessly moves and
|
||||
disappears with them". How much of the button the game shows is in its box beside its matrix (+0x78, a float, 0 to
|
||||
1: the lab's probe of the title's list read 0 for the 1.5 s the list runs before it shows, a rise over a second
|
||||
as the title opens, 1 to 0 in one frame as Options is chosen, and the 15-frame rise as the list comes back). The
|
||||
renderer drew the entry by a clock of its own from the first frame the list ran: a second and a half before the
|
||||
game's buttons, and 150 ms and a fade after they had gone. It is told the game's own amount now, each frame.
|
||||
Anything but a number from 0 to 1 there: all of it, as before) */
|
||||
float alpha = 1.0f;
|
||||
if (!peek(peek64(l.b[last] + 0x58) + 0x78, &alpha, 4) || !(alpha >= 0.0f && alpha <= 1.0f)) alpha = 1.0f;
|
||||
/* The button's state byte (+56) is its focus animation's: 0 none, 1 coming, 3 focused, 2 going. While Mods has the
|
||||
cursor the game gets no input (nn_hid.c) and its last button is let go (mods_held, above) */
|
||||
uint8_t state = 0;
|
||||
@@ -964,7 +996,14 @@ static void mods_list_tick(uint64_t screen, int title, ModsCall *call) {
|
||||
on it) The entry only while the game's cursor is on this list (its focus controller's entry is this screen's), or
|
||||
Mods has the cursor */
|
||||
int pend = 0;
|
||||
if (!mods_cursor_button(screen, &pend) && !mods_held.button) return;
|
||||
if (!mods_cursor_button(screen, &pend) && !mods_held.button) {
|
||||
/* ((Triforce, 11-20) the game's buttons are hidden too (Options or the Load list opened over the title: their
|
||||
alpha goes to 0 on that frame): said, not left to the renderer's 150 ms without a report, so the entry
|
||||
goes on the frame they do. With the buttons still showing (the cursor between two of them for a frame) it
|
||||
is not said, and the entry stays as before) */
|
||||
if (alpha <= 0.0f) rx_option_set(HOS_OPT_MODS_LIST, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* (coming or there; 2 is its focus going to another button. Let go for Mods, it is still the game's cursor's place) */
|
||||
const int focused = state == 1 || state == 3 || (mods_held.button && mods_held.button == l.b[last]);
|
||||
@@ -979,7 +1018,8 @@ static void mods_list_tick(uint64_t screen, int title, ModsCall *call) {
|
||||
mods_entry = (Rect){mx - 550, my - pitch / 2, mx + 550, my + pitch / 2};
|
||||
}
|
||||
quit_entry = title ? (Rect){0, 0, 0, 0} : (Rect){r.x0, r.y0 - 2 * pitch, r.x1, r.y1 - 2 * pitch};
|
||||
InterlockedExchange64(&mods_entry_ns, (LONG64)now);
|
||||
if (alpha >= 0.5f) InterlockedExchange64(&mods_entry_ns, (LONG64)now); /* (the mouse's: an entry not shown yet is not there to click) */
|
||||
rx_option_set(HOS_OPT_MODS_ALPHA, (int)(alpha * 1000.0f + 0.5f));
|
||||
rx_option_set(HOS_OPT_MODS_X0, (int)r.x0), rx_option_set(HOS_OPT_MODS_X1, (int)r.x1);
|
||||
rx_option_set(HOS_OPT_MODS_Y0, (int)r.y0), rx_option_set(HOS_OPT_MODS_Y1, (int)r.y1);
|
||||
rx_option_set(HOS_OPT_MODS_PITCH, (int)pitch);
|
||||
@@ -1050,24 +1090,6 @@ static Seen *screen_seen(uint64_t screen, int64_t now) {
|
||||
return free_one;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-19) benchmark mode, host/hos_bench.c: the start's screens pressed through as each gets a focused button. The
|
||||
file select is left on the file it opens on, the newest (its entries are a carousel's places, not files: focusing one by
|
||||
its place loaded another file in a lab run) */
|
||||
typedef struct { int index, state; } BenchLook;
|
||||
static void bench_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)entry;
|
||||
BenchLook *l = (BenchLook *)arg;
|
||||
uint8_t state = 0;
|
||||
if (peek(button + 56, &state, 1) && state > l->state) l->index = i, l->state = state;
|
||||
}
|
||||
static void bench_menu(Ctx *c, uint64_t screen, uint64_t vt) {
|
||||
(void)c;
|
||||
BenchLook l = {-1, 0};
|
||||
const int n = each_entry(screen, 64, bench_entry, &l);
|
||||
int entry = -1;
|
||||
if (hos_bench_menu(screen, vt, 1, l.index, n, &entry) == HOS_BENCH_MENU_PRESS_A) hos_hid_tap("0:A/120");
|
||||
}
|
||||
|
||||
void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
if (!screen) return;
|
||||
if (trace < 0) trace = getenv("HOS_MENU_TRACE") != NULL;
|
||||
@@ -1157,11 +1179,23 @@ void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_OPT_GAME_OPTIONS) */
|
||||
static int64_t options_seen_ns; /* (the screens' updates run on several of the game's threads: read and written whole) */
|
||||
void hos_oot_menu_focus(Ctx *c, uint64_t screen, uint64_t pane, uint64_t lr) { (void)c, (void)screen, (void)pane, (void)lr; }
|
||||
/* ((Triforce, 11-22) benchmark mode: a screen's focused button as the Windows half's focus_entry finds it (its state set and its
|
||||
box shown, the screen's focus manager there), then bench_menu as there */
|
||||
static void bench_focus_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)i, (void)entry;
|
||||
uint8_t state = 0;
|
||||
Rect r;
|
||||
if (!*(int *)arg && peek(button + 56, &state, 1) && state && button_rect(button, &r)) *(int *)arg = 1;
|
||||
}
|
||||
void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
(void)c;
|
||||
uint64_t vt = 0;
|
||||
if (screen < 0x100000 || screen >= HOS_GUEST_SPACE - 8) return;
|
||||
memcpy(&vt, GP(screen), 8);
|
||||
if (hos_bench_on()) {
|
||||
int focus = 0;
|
||||
if (peek64(peek64(screen + 32) + 96)) each_entry(screen, 400, bench_focus_entry, &focus);
|
||||
if (focus) bench_menu(c, screen, vt);
|
||||
}
|
||||
static int64_t pause_ran_ns; /* ((Triforce, 11-20) the pause menu's last update: where the Options were chosen from) */
|
||||
static int options_from_pause;
|
||||
const int64_t now = hos_now_ns();
|
||||
|
||||
25
host/oot_shadow_step.c
Normal file
25
host/oot_shadow_step.c
Normal file
@@ -0,0 +1,25 @@
|
||||
/* (Triforce, 11-21, the performance audit's GAME-05, after Source B's) The side of a character's or an object's shadow
|
||||
map rounded up to a step: the lifted code's read of the side goes through hos_oot_shadow_side (nx2/oot_patches.py
|
||||
patch_shadow_step). host/oot_shadow_step.h has why and the rule.
|
||||
HOS_OOT_SHADOW_STEP=<n>: the step (64 when not set); 0 or 1: the game's own side, as before. */
|
||||
#include "hos.h"
|
||||
#include "oot_shadow_step.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
uint32_t hos_oot_shadow_side(uint32_t side) {
|
||||
static int step = -1; /* (read once: the same value whichever thread is first) */
|
||||
static volatile long rounded; /* (the log's count) */
|
||||
int s = __atomic_load_n(&step, __ATOMIC_RELAXED);
|
||||
if (s < 0) {
|
||||
s = (int)oot_shadow_step(getenv("HOS_OOT_SHADOW_STEP"));
|
||||
int unset = -1;
|
||||
if (__atomic_compare_exchange_n(&step, &unset, s, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
hos_log("oot: shadow maps' sides rounded up to a multiple of %d (HOS_OOT_SHADOW_STEP; 0: the game's own, as before)", s <= 1 ? 0 : s);
|
||||
}
|
||||
const uint32_t out = oot_shadow_side(side, (uint32_t)s);
|
||||
if (out != side) {
|
||||
const long n = __atomic_add_fetch(&rounded, 1, __ATOMIC_RELAXED);
|
||||
if (n >= 4096 && !(n & (n - 1))) hos_log("oot: shadow maps' sides rounded: %ld so far (the last %u as %u)", n, side, out);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
41
host/oot_shadow_step.h
Normal file
41
host/oot_shadow_step.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/* (Triforce, 11-21, the performance audit's GAME-05, after Source B's) The side of a character's or an object's shadow
|
||||
map, in steps. nx2/oot_patches.py patch_shadow_step hooks the place; host/oot_shadow_step.c reads the switch.
|
||||
|
||||
The game gives each nearby character or object a square depth shadow map of its own, sized by its size on screen
|
||||
(384 to 1,920 a side), so while anything moves the side changes every frame, and each frame asks for depth textures
|
||||
of sizes not seen before: an image, its memory and its views made, and the old ones destroyed. The render targets'
|
||||
pool gives an image back only to a target of exactly its shape. Rounded up to a multiple of 64 the sides are 25
|
||||
sizes, which come round again, and the pool serves them. Source B measured, strafing along a wall in Castle Town's
|
||||
pot room, 4,000 to 10,000 new depth images in 600 frames against 30 to 90.
|
||||
|
||||
The one read of the side, before the texture is asked for, is rounded; the function's later uses read the same
|
||||
register, so the viewport and the texture stay in step. The projection is unchanged: a map has at most 63 more
|
||||
texels a side. 768 is 12 x 64, so no map crosses the renderer's rule for maps of 768 and less.
|
||||
|
||||
HOS_OOT_SHADOW_STEP=<n>: the step, a power of two from 2 to 256; not set, 64; 0 or 1: the game's own side, as before.
|
||||
|
||||
The rule alone, no game: tests/test_oot_shadow_step.py compiles it. */
|
||||
#ifndef OOT_SHADOW_STEP_H
|
||||
#define OOT_SHADOW_STEP_H
|
||||
#include <stdint.h>
|
||||
|
||||
enum { OOT_SHADOW_STEP_DEFAULT = 64, OOT_SHADOW_STEP_MOST = 256 };
|
||||
|
||||
/* The switch's value as a step: not set, the default; 0 or 1, off (1); a power of two up to OOT_SHADOW_STEP_MOST, itself;
|
||||
anything else (not a number, not a power of two, more), the default */
|
||||
static inline uint32_t oot_shadow_step(const char *asked) {
|
||||
if (!asked || !*asked) return OOT_SHADOW_STEP_DEFAULT;
|
||||
uint32_t v = 0;
|
||||
int digits = 0;
|
||||
const char *p = asked;
|
||||
for (; *p >= '0' && *p <= '9' && v < 100000; p++) v = v * 10 + (uint32_t)(*p - '0'), digits++;
|
||||
if (!digits || *p) return OOT_SHADOW_STEP_DEFAULT;
|
||||
if (v <= 1) return 1;
|
||||
return v <= OOT_SHADOW_STEP_MOST && !(v & (v - 1)) ? v : OOT_SHADOW_STEP_DEFAULT;
|
||||
}
|
||||
|
||||
/* The side the game asks its shadow map with: the next multiple of step from side (step a power of two; 1: side itself) */
|
||||
static inline uint32_t oot_shadow_side(uint32_t side, uint32_t step) {
|
||||
return step <= 1 ? side : (side + step - 1) & ~(step - 1);
|
||||
}
|
||||
#endif
|
||||
@@ -19,10 +19,16 @@
|
||||
are not touched. Nothing is held unless the switch is set: whether the game can do with fewer cycles (its heap is the
|
||||
one that fills, host/oot_heap_retry.c) is for the lab to say first.
|
||||
|
||||
HOS_OOT_TEX_COMPACT_MS=<n>: at most one cycle started every n ms. -1 (a test): none started at all. Unset or 0: as shipped. */
|
||||
HOS_OOT_TEX_COMPACT_MS=<n>: at most one cycle started every n ms. -1 (a test): none started at all. Unset or 0: as shipped.
|
||||
|
||||
(Triforce) And before that, the ask: the manager's step (f_80797240) asks for a round at most once a second
|
||||
(hos_oot_tex_compact_due, nx2/oot_patches.py patch_tex_ask; the rule and HOS_OOT_TEX_ASK_MS in oot_tex_compact.h). Asking
|
||||
every frame against never, standing at the Market: 11.0% and 9.8% of frames of 16.7 ms or more against 3.0% and 1.2%. */
|
||||
#include "hos.h"
|
||||
#include "oot_tex_compact.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <windows.h>
|
||||
|
||||
/* (the supported build's) the compaction's record at the manager's +7992, its state at the record's +8: 3 idle (the next
|
||||
step plans), 0 to 2 a cycle's steps */
|
||||
@@ -78,3 +84,32 @@ void hos_oot_tex_compact(Ctx *c, A64Fn game) {
|
||||
said = now, started = held = steps = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* (Triforce) Whether the manager's step may ask for a round now (patch_tex_ask: called where it would ask, with no round asked
|
||||
for and the ask's bit set). The asks let through and held are logged every 10 s while there are any */
|
||||
int hos_oot_tex_compact_due(void) {
|
||||
static SRWLOCK lock = SRWLOCK_INIT;
|
||||
static int ms = -2;
|
||||
static int64_t last = -1, said;
|
||||
static long let, held;
|
||||
AcquireSRWLockExclusive(&lock);
|
||||
if (ms == -2) {
|
||||
const char *e = getenv("HOS_OOT_TEX_ASK_MS");
|
||||
ms = hos_oot_tex_ask_ms(e);
|
||||
if (ms > 0) hos_log("oot: texture compaction: at most one ask every %d ms (HOS_OOT_TEX_ASK_MS; 0: every ask, as shipped)", ms);
|
||||
else hos_log("oot: texture compaction: %s (HOS_OOT_TEX_ASK_MS=%s)", ms ? "no ask let through" : "every ask, as shipped", e);
|
||||
}
|
||||
const int64_t now = hos_now_ns();
|
||||
const int due = hos_oot_tex_ask_due(ms, now, &last);
|
||||
if (due) let++;
|
||||
else held++;
|
||||
if (!said) said = now;
|
||||
if (now - said >= 10000000000LL) {
|
||||
if (let || held)
|
||||
hos_log("oot: texture compaction asks in %.0f s: %ld let through, %ld held (HOS_OOT_TEX_ASK_MS=%d)",
|
||||
(now - said) / 1e9, let, held, ms);
|
||||
said = now, let = held = 0;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&lock);
|
||||
return due;
|
||||
}
|
||||
|
||||
39
host/oot_tex_compact.h
Normal file
39
host/oot_tex_compact.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* (Triforce) When the game's texture data manager may ask its compaction thread for a round (host/oot_tex_compact.c
|
||||
hos_oot_tex_compact_due, nx2/oot_patches.py patch_tex_ask), apart from the game so that tests/test_oot_tex_compact.py can
|
||||
compile it alone.
|
||||
|
||||
The manager steps every frame (f_80797240): with no round asked for (its +776) and bit 1 of its +960 set, it asks for one
|
||||
and sends the thread the message, so a round could start every frame. Each round moves texture data in the game's heap,
|
||||
and each texture moved is a new address to the renderer: captured, compared and hashed again. With a gap, an ask is let
|
||||
through only once that gap has passed since the last one let through; a round started always finishes, and the game asks
|
||||
again at its next step.
|
||||
|
||||
HOS_OOT_TEX_ASK_MS=<n>: at most one ask every n ms. Unset, empty or not a number: 1000. 0: every ask, as shipped. off (or a
|
||||
negative number): none. */
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum { HOS_OOT_TEX_ASK_DEFAULT_MS = 1000 };
|
||||
|
||||
/* The switch's value: the gap in ms, 0 every ask, -1 none */
|
||||
static inline int hos_oot_tex_ask_ms(const char *e) {
|
||||
if (!e || !*e) return HOS_OOT_TEX_ASK_DEFAULT_MS;
|
||||
if (e[0] == 'o' && e[1] == 'f' && e[2] == 'f' && !e[3]) return -1;
|
||||
char *end;
|
||||
const long v = strtol(e, &end, 10);
|
||||
if (end == e || *end) return HOS_OOT_TEX_ASK_DEFAULT_MS;
|
||||
if (v < 0) return -1;
|
||||
return v > 3600000 ? 3600000 : (int)v;
|
||||
}
|
||||
|
||||
/* Whether an ask goes through now. ms: the switch's value; now: the clock (ns); last: the time of the last ask let through,
|
||||
negative before the first, set here when this one goes through */
|
||||
static inline int hos_oot_tex_ask_due(int ms, int64_t now, int64_t *last) {
|
||||
if (ms == 0) return 1;
|
||||
if (ms < 0) return 0;
|
||||
if (*last >= 0 && now - *last < (int64_t)ms * 1000000) return 0;
|
||||
*last = now;
|
||||
return 1;
|
||||
}
|
||||
@@ -83,7 +83,12 @@ static DWORD WINAPI pgo_thread(void *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-21, the performance audit's LIFT-10) the renderer's copy (triforce_vk, nx2/build.py RX_VK_PGO=gen) is
|
||||
compiled with PGO_ENV "RX_VK_PGO_FILE": its counters are the DLL's own sections, written to its own file) */
|
||||
#ifndef PGO_ENV
|
||||
#define PGO_ENV "HOS_PGO"
|
||||
#endif
|
||||
__attribute__((constructor)) static void pgo_start(void) {
|
||||
const char *e = getenv("HOS_PGO");
|
||||
const char *e = getenv(PGO_ENV);
|
||||
if (e) CreateThread(NULL, 0, pgo_thread, _strdup(e), 0, NULL);
|
||||
}
|
||||
|
||||
@@ -833,7 +833,13 @@ int hos_prompts_romfs(const char *key, char *out, size_t cap);
|
||||
finds that one first) */
|
||||
int hos_prompts_font_file(const char *key, char *out, size_t cap) {
|
||||
if (strcmp(key, FONT_PATH)) return 0;
|
||||
return hos_prompts_romfs(FONT_PATH, out, cap);
|
||||
/* ((Triforce, 11-21, the performance audit's IO-24) from the lifted code's size hook (host/mods.c), a game thread: the
|
||||
core given up around the builder, which builds the font at its first call and holds its lock meanwhile; never inside
|
||||
hos_prompts_romfs, where that lock is held) */
|
||||
const int away = hos_vcore_away();
|
||||
const int found = hos_prompts_romfs(FONT_PATH, out, cap);
|
||||
if (away) hos_vcore_back();
|
||||
return found;
|
||||
}
|
||||
|
||||
/* (Triforce) The title screen's third entry, "Options / ZELDA NOTES Setting" in US English, reads "Options / Triforce":
|
||||
|
||||
@@ -400,6 +400,8 @@ void hos_guest_io_end(int held) {
|
||||
has nothing left, or there is none. Both back end calls are found by name, as the lazy write-back's; without them nothing is
|
||||
told and the wait returns at once */
|
||||
static void (*volatile submit_waits_fn)(const uint64_t *, int); /* (the back end's mkwvk_submit_waits, once it runs) */
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-25) the back end's mkwvk_invalidate_quick, once it runs: rx_nvn_invalidate) */
|
||||
static int (*volatile invalidate_quick_fn)(uint64_t, uint64_t);
|
||||
static int (*volatile replay_idle_wait_fn)(uint64_t);
|
||||
int rx_async_replay(void) {
|
||||
static volatile int on = -1;
|
||||
@@ -455,6 +457,8 @@ int rx_start(void *hwnd) {
|
||||
/* (Triforce: the replay thread's two calls, rx_async_replay above; a back end without them has none) */
|
||||
submit_waits_fn = (void (*)(const uint64_t *, int))(void *)GetProcAddress(vk_module, "mkwvk_submit_waits");
|
||||
replay_idle_wait_fn = (int (*)(uint64_t))(void *)GetProcAddress(vk_module, "mkwvk_replay_idle_wait");
|
||||
/* ((Triforce, THREAD-25) an invalidate with nothing to wait for; a back end without it is asked nothing) */
|
||||
invalidate_quick_fn = (int (*)(uint64_t, uint64_t))(void *)GetProcAddress(vk_module, "mkwvk_invalidate_quick");
|
||||
hos_log("rx: renderer: %s (%s)", eden ? "Eden's" : "Vulkan", dll);
|
||||
flush_early();
|
||||
return 1;
|
||||
@@ -596,9 +600,24 @@ void rx_nvn_texture_created(const nvn_tex *t) {
|
||||
if (vk && vk->nvn_texture_created) vk->nvn_texture_created(t);
|
||||
}
|
||||
void rx_nvn_program_released(const nvn_prog *p) { vk ? vk->nvn_program_released(p) : wgr_nvn_program_released(p); }
|
||||
/* ((Triforce, 11-21, the performance audit's THREAD-20, after Source A's) ReadTexels and WriteTexels reach the back end, which
|
||||
waits for all the GPU work submitted (its finish) and then for the replay's lock: a GPU drain of 6 ms and more, at an
|
||||
area's arrival and at boot, where the game calls them from its loading threads. The thread kept its virtual core through
|
||||
that wait, and the game threads allowed only on that core waited with it, up to the 250 ms escape. No guest code runs in
|
||||
the call, so the core is given up for it, as around rx_nvn_invalidate below. HOS_TEXELS_KEEP_CORE=1: kept, as before */
|
||||
static int texels_keep_core(void) {
|
||||
static int keep = -1;
|
||||
if (keep < 0) {
|
||||
const char *e = getenv("HOS_TEXELS_KEEP_CORE");
|
||||
keep = e && atoi(e) != 0;
|
||||
}
|
||||
return keep;
|
||||
}
|
||||
void rx_nvn_read_texels(const nvn_tex *t, unsigned level, unsigned layer0, const NVNcopyRegion *r, uint8_t *dst,
|
||||
size_t row, size_t img) {
|
||||
const int away = texels_keep_core() ? 0 : hos_vcore_away();
|
||||
vk ? vk->nvn_read_texels(t, level, layer0, r, dst, row, img) : wgr_nvn_read_texels(t, level, layer0, r, dst, row, img);
|
||||
if (away) hos_vcore_back();
|
||||
}
|
||||
void rx_nvn_write_texels(const nvn_tex *t, unsigned level, unsigned layer0, const NVNcopyRegion *r, const uint8_t *src,
|
||||
size_t row, size_t img) {
|
||||
@@ -620,10 +639,18 @@ void rx_nvn_write_texels(const nvn_tex *t, unsigned level, unsigned layer0, cons
|
||||
}
|
||||
}
|
||||
}
|
||||
/* ((Triforce, 11-21, THREAD-20) as the read: no core held through the back end's wait. The early path above never waits) */
|
||||
const int away = texels_keep_core() ? 0 : hos_vcore_away();
|
||||
vk ? vk->nvn_write_texels(t, level, layer0, r, src, row, img)
|
||||
: wgr_nvn_write_texels(t, level, layer0, r, src, row, img);
|
||||
if (away) hos_vcore_back();
|
||||
}
|
||||
void rx_nvn_invalidate(uint64_t gpu, uint64_t size) {
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-25, after Source A's) The game invalidates each shape's 64-byte
|
||||
occlusion slot, per view, and nearly none of those calls has anything to wait for; each gave the core up and queued
|
||||
for it again. The back end is asked first, without a lock and without a wait (gpu/vk/page_marks.h has what it
|
||||
looks at): 1, and the call is left out with the core kept. It answers 0 unless RX_VK_INVALIDATE_QUICK=1 */
|
||||
if (vk && invalidate_quick_fn && invalidate_quick_fn(gpu, size)) return;
|
||||
/* The callback can wait for replay or GPU write-back: let another guest thread use this core meanwhile. */
|
||||
const int away = hos_vcore_away();
|
||||
int hos_ft_on(void);
|
||||
|
||||
52
host/shadow_slot.h
Normal file
52
host/shadow_slot.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* (Triforce, 11-21, the performance audit's TEX-85, after Source A's) The storage a character's shadow map reserves
|
||||
(host/nvn_native.c nvnTextureBuilderGetStorageSize and nvnTextureBuilderGetStorageAlignment).
|
||||
|
||||
The game gives each shadow-casting character near the camera a square depth map (NVN format 0x34, one level) whose
|
||||
side it works out anew every frame from the camera's distance: 384 from afar, more from near, a few pixels different
|
||||
each frame while either moves. It makes a frame's render targets one after another, first fit, in its target heap,
|
||||
asking the size and alignment of each here. So a map a few pixels larger pushes every target made after it to another
|
||||
address, and each of those is a new texture to the game and, keyed by its address, a new image to the renderer:
|
||||
Source A counted 15 to 16 textures made a frame in the Market with the camera turning, and 6 with this.
|
||||
|
||||
Such a map is answered the storage size and alignment of the next multiple of a step up (384: sides 768, 1152, 1536,
|
||||
1920), so the targets after it stay where they are until the map crosses a step. Only the bytes the game sets aside
|
||||
change, by less than one step's worth a map (1.8 MB at the first step, 5.3 MB at the worst); the texture keeps its own
|
||||
size and layout, and nothing is drawn or sampled differently.
|
||||
|
||||
HOS_SHADOW_BUCKET=<step>: the step, 384 when not set; 0: every map its own size, as before. Not set, the step is 0
|
||||
where the target heap is pinned (HOS_RT_POOL_MB, or the 5120-wide outputs' 900 MB), where there is no room to give.
|
||||
|
||||
The rule alone, no NVN: tests/test_shadow_slot.py compiles it. */
|
||||
#ifndef HOS_SHADOW_SLOT_H
|
||||
#define HOS_SHADOW_SLOT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
enum { HOS_SHADOW_SLOT_STEP = 384, HOS_SHADOW_SLOT_FORMAT = 0x34, HOS_SHADOW_SLOT_LEAST = 384, HOS_SHADOW_SLOT_MOST = 1920 };
|
||||
|
||||
/* The side whose storage a texture of this description reserves: the next multiple of step from its own, for a square
|
||||
two-dimensional one-level depth map wider than 384 and up to 1920, and never more than 1920. 0: its own (not such a
|
||||
map, or step 0) */
|
||||
static inline uint32_t hos_shadow_slot_side(int is_2d, int format, uint32_t levels, uint32_t depth, uint32_t width, uint32_t height,
|
||||
uint32_t step) {
|
||||
if (!step || !is_2d || format != HOS_SHADOW_SLOT_FORMAT || levels != 1 || depth > 1 || width != height ||
|
||||
width <= HOS_SHADOW_SLOT_LEAST || width > HOS_SHADOW_SLOT_MOST)
|
||||
return 0;
|
||||
const uint32_t side = (width + step - 1) / step * step;
|
||||
return side > HOS_SHADOW_SLOT_MOST ? HOS_SHADOW_SLOT_MOST : side;
|
||||
}
|
||||
|
||||
/* The step: asked, HOS_SHADOW_BUCKET's value (NULL: not set); pinned: the target heap is held to a size (no room to give).
|
||||
Set: its value, 0 for off or a negative number. Not set: 0 on a pinned heap, else HOS_SHADOW_SLOT_STEP */
|
||||
static inline uint32_t hos_shadow_slot_step(const char *asked, int pinned) {
|
||||
if (asked) {
|
||||
long v = 0;
|
||||
int digits = 0;
|
||||
const char *p = asked;
|
||||
for (; *p >= '0' && *p <= '9' && v < 100000; p++) v = v * 10 + (*p - '0'), digits++;
|
||||
return digits && !*p ? (uint32_t)v : *asked ? 0 : HOS_SHADOW_SLOT_STEP; /* (not a number: off; empty: as not set) */
|
||||
}
|
||||
return pinned ? 0 : HOS_SHADOW_SLOT_STEP;
|
||||
}
|
||||
|
||||
#endif
|
||||
115
host/short_sleep.h
Normal file
115
host/short_sleep.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/* (Triforce, 11-21, the performance audit's OS-31, after Source A's) How sleeps under a millisecond wait.
|
||||
|
||||
The game's idle job workers, and its main thread at its barriers, sleep 20 us and look again, about 7,600 times a
|
||||
second. A sleep under a millisecond is yielded away until its time (the timer wakes about 0.5 ms late, which made
|
||||
those waits 25 times longer on the frame's critical path), so each sleeper keeps a processor busy. On a PC with few
|
||||
processors that is time the frame's own threads need: Source A measured, with the game held to 4 processors, a tenth
|
||||
more frames, no frame over 250 ms where the yielding runs had them, and on 6 the same frame rate for a tenth less CPU;
|
||||
from 8 on, no gain. So with 6 or fewer logical processors in the process's mask the game's own short sleeps wait on
|
||||
the high-resolution timer, and the host's own (the frame limiter, the audio renderer) keep yielding. Above 6 nothing
|
||||
changes.
|
||||
|
||||
HOS_SHORT_SLEEP_TIMER: not set, by the processors; 1 (any value that is not off): every short sleep on the timer; 0,
|
||||
off, no, false: every short sleep yields.
|
||||
|
||||
(Triforce, 11-22, the performance audit's OS-32, after Source A's) Off Windows the processors are not counted, so
|
||||
every short sleep yielded there whatever the PC, the Steam Deck's 8 logical processors included. Source A, off
|
||||
Windows and with the switch not set, puts the game's own short sleeps on the timer at any processor count (the
|
||||
timer there is the POSIX shim's clock_nanosleep, which wakes tens of microseconds late, not half a millisecond),
|
||||
so idle workers stop holding processors the frame needs. That mode was never measured in the game by itself (with
|
||||
every short sleep on the timer a 24-thread Linux PC lost 2.9% of its frame rate for a fifth less CPU), and it
|
||||
cannot be run on the PC this was written on: it is built, and is not the default. HOS_SHORT_SLEEP_TIMER=game asks
|
||||
for it on any system; SHORT_SLEEP_ELSEWHERE below is what a build that is not Windows' does with the switch not
|
||||
set: SHORT_SLEEP_YIELD, as before, until a Steam Deck A/B says SHORT_SLEEP_GUEST_TIMER (Source A's default).
|
||||
|
||||
The rule alone, no threads and no clock: tests/test_short_sleep_timer.py compiles it. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { SHORT_SLEEP_TIMER_THREADS = 6 }; /* (no more than the game's six cores) */
|
||||
enum { SHORT_SLEEP_YIELD = 0, SHORT_SLEEP_GUEST_TIMER = 1, SHORT_SLEEP_ALL_TIMER = 2 };
|
||||
|
||||
/* A switch's value that turns it off: 0, off, no, false (and nothing at all) */
|
||||
static inline int short_sleep_value_off(const char *v) {
|
||||
return !v || !*v || !strcmp(v, "0") || !strcmp(v, "off") || !strcmp(v, "no") || !strcmp(v, "false");
|
||||
}
|
||||
|
||||
/* How short sleeps wait. asked: HOS_SHORT_SLEEP_TIMER's value, NULL when it is not set. threads: the logical processors
|
||||
the process may run on, 0 when not known (more than one processor group, or not Windows) */
|
||||
static inline int short_sleep_timer(const char *asked, int threads) {
|
||||
if (asked) return short_sleep_value_off(asked) ? SHORT_SLEEP_YIELD : SHORT_SLEEP_ALL_TIMER;
|
||||
return threads > 0 && threads <= SHORT_SLEEP_TIMER_THREADS ? SHORT_SLEEP_GUEST_TIMER : SHORT_SLEEP_YIELD;
|
||||
}
|
||||
|
||||
/* ((Triforce, 11-22, the performance audit's OS-32) what a build that is not Windows' does with the switch not set */
|
||||
#define SHORT_SLEEP_ELSEWHERE SHORT_SLEEP_YIELD
|
||||
|
||||
/* The same with the system: windows, a Windows build (the processors count there); elsewhere, what any other build does
|
||||
with the switch not set (SHORT_SLEEP_ELSEWHERE). "game": the game's own short sleeps on the timer, the host's
|
||||
yield, at any processor count on any system */
|
||||
static inline int short_sleep_timer_os(const char *asked, int threads, int windows, int elsewhere) {
|
||||
if (asked && !strcmp(asked, "game")) return SHORT_SLEEP_GUEST_TIMER;
|
||||
if (!asked && !windows) return elsewhere;
|
||||
return short_sleep_timer(asked, threads);
|
||||
}
|
||||
|
||||
/* Whether a sleep of ns is yielded away until its time. guest: the game's own sleep (SleepThread), not the host's */
|
||||
static inline int short_sleep_yields(int64_t ns, int how, int guest) {
|
||||
return ns < 1000000 && how < (guest ? SHORT_SLEEP_GUEST_TIMER : SHORT_SLEEP_ALL_TIMER);
|
||||
}
|
||||
|
||||
/* (Triforce, 11-22, the performance audit's OS-33, after Source A's) A cap on the game threads that yield through a short
|
||||
sleep at once. Where the game's short sleeps yield (more than 6 logical processors), every idle job worker keeps a
|
||||
processor busy. Source A lets at most a quarter of the processors' worth of game threads do so at a time (2 with 8, 4
|
||||
with 16, 6 with 24); the next sleeper waits on the timer, about 0.5 ms late. Its numbers are provisional: with 16
|
||||
processors 2.7% less CPU a frame for the same frame rate; a 24-thread run held to 2 yielding threads lost 1.7% of its
|
||||
frame rate. The game's MainThread and Presentation Thread, which the frame waits on, and the host's own sleeps are
|
||||
never counted and always yield.
|
||||
|
||||
Off unless asked for, as it changes how the game's threads wait and was not measured here:
|
||||
HOS_SHORT_SLEEP_POLLERS not set, or 0, off, no, false: no limit, as before; a number: at most that many (1024 at
|
||||
most); auto (any other word): a quarter of the process's logical processors when they are more than 6, else no limit.
|
||||
Any HOS_SHORT_SLEEP_TIMER value turns the cap off (the switch then says how every short sleep waits), and so does a
|
||||
choice other than "all yield". */
|
||||
enum { SHORT_SLEEP_POLLERS_MOST = 1024 };
|
||||
|
||||
/* The cap: 0 for no limit. asked: HOS_SHORT_SLEEP_POLLERS's value, NULL when not set; timer_asked: HOS_SHORT_SLEEP_TIMER's;
|
||||
how: short_sleep_timer's choice; threads: the process's logical processors, 0 when not known */
|
||||
static inline int short_sleep_pollers(const char *asked, const char *timer_asked, int how, int threads) {
|
||||
if (timer_asked || how != SHORT_SLEEP_YIELD || short_sleep_value_off(asked)) return 0;
|
||||
if (asked[0] >= '0' && asked[0] <= '9') {
|
||||
int n = 0;
|
||||
for (const char *p = asked; *p >= '0' && *p <= '9' && n <= SHORT_SLEEP_POLLERS_MOST; p++) n = n * 10 + (*p - '0');
|
||||
return n > SHORT_SLEEP_POLLERS_MOST ? SHORT_SLEEP_POLLERS_MOST : n;
|
||||
}
|
||||
return threads > SHORT_SLEEP_TIMER_THREADS ? threads / 4 : 0;
|
||||
}
|
||||
|
||||
/* The choice kept in one word, made once: how, and the cap above it */
|
||||
static inline int short_sleep_word(int how, int cap) { return how | cap << 2; }
|
||||
static inline int short_sleep_how(int word) { return word & 3; }
|
||||
static inline int short_sleep_cap(int word) { return word >> 2; }
|
||||
|
||||
/* A thread the frame waits on, never counted: the game's MainThread (by any longer name too) and its Presentation Thread */
|
||||
static inline int short_sleep_serial_name(const char *name) {
|
||||
return name && (strstr(name, "MainThread") != NULL || !strcmp(name, "Presentation Thread"));
|
||||
}
|
||||
|
||||
/* The cap's gate, around the yielding. 1: this sleep yields; 0: it waits on the timer. pollers: the counted threads
|
||||
yielding now. cap 0, a host sleep or a serial thread: not counted, yields. A counted one yields while it is one of at
|
||||
most `cap`; one over the cap gives its count back here. *took: the count is held and must be given back with
|
||||
short_sleep_poll_leave once the yielding is over: one decrement on every path */
|
||||
static inline int short_sleep_poll_enter(int *pollers, int cap, int guest, int serial, int *took) {
|
||||
*took = 0;
|
||||
if (cap <= 0 || !guest || serial) return 1;
|
||||
if (__atomic_add_fetch(pollers, 1, __ATOMIC_RELAXED) <= cap) {
|
||||
*took = 1;
|
||||
return 1;
|
||||
}
|
||||
__atomic_sub_fetch(pollers, 1, __ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
static inline void short_sleep_poll_leave(int *pollers, int took) {
|
||||
if (took) __atomic_sub_fetch(pollers, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
143
host/stall_host.h
Normal file
143
host/stall_host.h
Normal file
@@ -0,0 +1,143 @@
|
||||
/* (Triforce, 11-22, the performance audit's OS-51, after Source B's) The host's part of a long frame.
|
||||
|
||||
The renderer's stall line (gpu/vk/stall_watch.h) splits a frame of 50 ms or more on the replay's side and calls the
|
||||
rest "outside the replay and the present". This is that rest, counted where the host spends it: the game's file
|
||||
reads, writes, flushes and save commits, the discards, zero-fills and decommits of memory the game lets go, the
|
||||
give-back's passes, and the waits for a virtual core; with the process's page faults and its CPU time in the kernel
|
||||
and in its own code over the same frame. Each cause is added to with relaxed atomics where it happens (a clock read
|
||||
before and after: nothing else), and at each present all are taken: a present that comes the stall line's threshold
|
||||
or more after the last one logs them as one line beside the stall line, at most 3 a second and 1500 in a session:
|
||||
|
||||
vi: stall, the host's part: frame 4812, 143.2 ms: file reads 3 (12.0 MB, longest 41.0 ms); discards 2 (180.0 MB,
|
||||
longest 6.1 ms); core waits 64.0 ms (212, longest 31.0); 41000 page faults; kernel 38.0 ms, user 120.0
|
||||
|
||||
Source B's found a read pacing of its own that way, sleeps of 36 to 151 ms at boot and area loads. Triforce has no
|
||||
such pacing: what is ported is the log. No frame gets shorter by it; it names, or rules out, the host's causes.
|
||||
|
||||
On whenever the stall line is (RX_VK_STALL_LOG, its threshold; 0 turns both off). HOS_STALL_HOST=0: off, nothing
|
||||
counted and no system call at a present.
|
||||
|
||||
Plain C, no system calls: tests/test_stall_host.py compiles it. */
|
||||
#ifndef STALL_HOST_H
|
||||
#define STALL_HOST_H
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum {
|
||||
STALL_HOST_READ, /* nn::fs ReadFile: the kernel's read into the game's memory (the virtual core given up) */
|
||||
STALL_HOST_WRITE, /* nn::fs WriteFile (the core held) */
|
||||
STALL_HOST_FLUSH, /* nn::fs FlushFile (the core held) */
|
||||
STALL_HOST_COMMIT, /* nn::fs Commit: the save staged (the core given up) */
|
||||
STALL_HOST_DISCARD, /* a freed region's or freed pages' discard */
|
||||
STALL_HOST_ZERO, /* freed pages a GPU import holds, zeroed where they are */
|
||||
STALL_HOST_DECOMMIT, /* FreeMemoryPages' decommit above 4 GB */
|
||||
STALL_HOST_GIVE_BACK, /* a pass of the give-back of pages let go */
|
||||
STALL_HOST_CORE_WAIT, /* waits for a virtual core, summed over the game's threads */
|
||||
STALL_HOST_CAUSES
|
||||
};
|
||||
|
||||
/* One cause since the last present: the time in it, how often, the bytes it was about, its longest single time */
|
||||
typedef struct { int64_t ns, count, bytes, longest; } StallHostCause;
|
||||
|
||||
static inline void stall_host_add(StallHostCause *c, int64_t ns, int64_t bytes) {
|
||||
if (ns < 0) ns = 0;
|
||||
__atomic_add_fetch(&c->ns, ns, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&c->count, 1, __ATOMIC_RELAXED);
|
||||
if (bytes) __atomic_add_fetch(&c->bytes, bytes, __ATOMIC_RELAXED);
|
||||
int64_t was = __atomic_load_n(&c->longest, __ATOMIC_RELAXED);
|
||||
while (ns > was && !__atomic_compare_exchange_n(&c->longest, &was, ns, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {}
|
||||
}
|
||||
/* What was counted, the counters left at zero (an add in the middle of a take lands in this frame or the next) */
|
||||
static inline StallHostCause stall_host_take(StallHostCause *c) {
|
||||
StallHostCause t;
|
||||
t.ns = __atomic_exchange_n(&c->ns, 0, __ATOMIC_RELAXED);
|
||||
t.count = __atomic_exchange_n(&c->count, 0, __ATOMIC_RELAXED);
|
||||
t.bytes = __atomic_exchange_n(&c->bytes, 0, __ATOMIC_RELAXED);
|
||||
t.longest = __atomic_exchange_n(&c->longest, 0, __ATOMIC_RELAXED);
|
||||
return t;
|
||||
}
|
||||
|
||||
/* The threshold in microseconds, 0 for off. stall_log: RX_VK_STALL_LOG's value (NULL: not set, the stall line's 50 ms; a
|
||||
number: that many ms; 0 or no number: no stall line, and none of this); host: HOS_STALL_HOST's (0: off) */
|
||||
static inline int64_t stall_host_threshold_us(const char *stall_log, const char *host) {
|
||||
if (host && atoi(host) == 0) return 0;
|
||||
const double ms = stall_log ? atof(stall_log) : 50.0;
|
||||
return ms > 0 ? (int64_t)(ms * 1000.0 + 0.5) : 0;
|
||||
}
|
||||
|
||||
/* At most per_second lines a second (a burst of that many after a quiet stretch) and total in a session: the stall
|
||||
line's own bound (stall_watch.h Bound) */
|
||||
typedef struct { double per_second, tokens; int64_t last_ns, total, told, held; } StallHostBound;
|
||||
#define STALL_HOST_BOUND {3.0, 3.0, 0, 1500, 0, 0}
|
||||
static inline int stall_host_bound_take(StallHostBound *b, int64_t now_ns) {
|
||||
if (b->last_ns) {
|
||||
b->tokens += (double)(now_ns - b->last_ns) / 1e9 * b->per_second;
|
||||
if (b->tokens > b->per_second) b->tokens = b->per_second;
|
||||
}
|
||||
b->last_ns = now_ns;
|
||||
if (b->told >= b->total || b->tokens < 1.0) {
|
||||
b->held++;
|
||||
return 0;
|
||||
}
|
||||
b->tokens -= 1.0;
|
||||
b->told++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* One long frame's account */
|
||||
typedef struct {
|
||||
int64_t frame, gap_ns; /* the present's number, the time since the one before */
|
||||
StallHostCause cause[STALL_HOST_CAUSES];
|
||||
uint64_t page_faults; /* soft and hard together, as Windows counts them */
|
||||
int64_t kernel_ns, user_ns; /* the process's CPU time over the frame, all threads */
|
||||
int64_t held; /* lines the bound left out since the last one */
|
||||
} StallHostFrame;
|
||||
|
||||
/* The line. Causes that did not happen are left out; with none, it says so (the frame's time was not the host's) */
|
||||
static inline int stall_host_line(const StallHostFrame *f, char *out, size_t cap) {
|
||||
static const char *const name[STALL_HOST_CAUSES] = {"file reads", "file writes", "flushes", "save commits", "discards",
|
||||
"zero-fills", "decommits", "give-backs", "core waits"};
|
||||
int n = snprintf(out, cap, "vi: stall, the host's part: frame %lld, %.1f ms:", (long long)f->frame, f->gap_ns / 1e6);
|
||||
int any = 0;
|
||||
for (int i = 0; i < STALL_HOST_CAUSES && n > 0 && (size_t)n < cap; i++) {
|
||||
const StallHostCause *c = &f->cause[i];
|
||||
if (!c->count) continue;
|
||||
const char *sep = any ? (i == STALL_HOST_DISCARD || i == STALL_HOST_CORE_WAIT ? ";" : ",") : "";
|
||||
any = 1;
|
||||
if (i == STALL_HOST_CORE_WAIT) /* (summed over threads: more than the frame's own time is usual) */
|
||||
n += snprintf(out + n, cap - n, "%s core waits %.1f ms (%lld, longest %.1f)", sep, c->ns / 1e6, (long long)c->count,
|
||||
c->longest / 1e6);
|
||||
else if (c->bytes)
|
||||
n += snprintf(out + n, cap - n, "%s %s %lld (%.1f MB, %.1f ms, longest %.1f)", sep, name[i], (long long)c->count,
|
||||
c->bytes / 1048576.0, c->ns / 1e6, c->longest / 1e6);
|
||||
else
|
||||
n += snprintf(out + n, cap - n, "%s %s %lld (%.1f ms, longest %.1f)", sep, name[i], (long long)c->count, c->ns / 1e6,
|
||||
c->longest / 1e6);
|
||||
}
|
||||
if (n > 0 && (size_t)n < cap)
|
||||
n += snprintf(out + n, cap - n, "%s %llu page faults; kernel %.1f ms, user %.1f", any ? ";" : " none of the host's causes;",
|
||||
(unsigned long long)f->page_faults, f->kernel_ns / 1e6, f->user_ns / 1e6);
|
||||
if (f->held && n > 0 && (size_t)n < cap) n += snprintf(out + n, cap - n, " (%lld such lines left out before this one)", (long long)f->held);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* ---- the counters, and the two calls a counted place makes (host/mkw_glue.c has the counters and the present's look) ---- */
|
||||
extern StallHostCause hos_stall_host[STALL_HOST_CAUSES];
|
||||
extern int64_t hos_stall_host_us; /* (the threshold; -1: the switches not read yet) */
|
||||
int64_t hos_now_ns(void);
|
||||
|
||||
static inline int64_t stall_host_us(void) {
|
||||
int64_t us = __atomic_load_n(&hos_stall_host_us, __ATOMIC_RELAXED);
|
||||
if (us < 0) {
|
||||
us = stall_host_threshold_us(getenv("RX_VK_STALL_LOG"), getenv("HOS_STALL_HOST"));
|
||||
__atomic_store_n(&hos_stall_host_us, us, __ATOMIC_RELAXED);
|
||||
}
|
||||
return us;
|
||||
}
|
||||
/* The clock before a counted call, 0 when nothing is counted; and the call counted after it */
|
||||
static inline int64_t stall_host_begin(void) { return stall_host_us() ? hos_now_ns() : 0; }
|
||||
static inline void stall_host_end(int cause, int64_t began, uint64_t bytes) {
|
||||
if (began) stall_host_add(&hos_stall_host[cause], hos_now_ns() - began, (int64_t)bytes);
|
||||
}
|
||||
#endif
|
||||
19
host/video_av1.h
Normal file
19
host/video_av1.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/* (Triforce, 11-22, the performance audit's IO-32, after Source A's) Which AV1 decoder a movie gets first on Windows
|
||||
(nn_video.c dec_init_sized).
|
||||
|
||||
The game's movies are 1080p AV1. Windows decoded them with Media Foundation first (the AV1 Video Extension, where the PC
|
||||
has it), and that path takes the extension's synchronous decoder with no D3D manager: software decoding on every GPU.
|
||||
dav1d, through the FFmpeg the build links, was the fallback for a PC without the extension, and what Linux always
|
||||
runs. It goes first now where that FFmpeg has it; Media Foundation stays the decoder of an FFmpeg without libdav1d,
|
||||
and the fallback when libdav1d does not open. HOS_AV1=mf: Media Foundation first, as before.
|
||||
|
||||
The rule alone, no decoder: tests/test_video_av1_order.py compiles it. */
|
||||
#pragma once
|
||||
#include <string.h>
|
||||
|
||||
/* 1 when Media Foundation is tried before libdav1d. hos_av1: HOS_AV1's value, NULL when unset; have_dav1d: the FFmpeg
|
||||
this build links has libdav1d. (HOS_AV1=dav1d, the old way to ask for dav1d first, is the default now) */
|
||||
static inline int video_av1_mf_first(const char *hos_av1, int have_dav1d) {
|
||||
if (!have_dav1d) return 1;
|
||||
return hos_av1 != NULL && !strcmp(hos_av1, "mf");
|
||||
}
|
||||
46
host/video_catchup.h
Normal file
46
host/video_catchup.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/* (Triforce, 11-22, the performance audit's THREAD-82, after Source A's) How far the movie decoder's acquire decodes
|
||||
ahead to catch up with the movie's clock (nn_video.c dec_acquire).
|
||||
|
||||
The game takes one picture a frame and the sound plays on, so the acquire skips the pictures that are behind the
|
||||
movie's clock, decoding the next ones to skip them too. Nothing bounded that but a guard of 600 steps. With a
|
||||
decoder that takes longer than the gap between two pictures (19.5 ms a picture against 16.7), every picture decoded
|
||||
to catch up puts the clock further ahead: the acquire decoded up to about 300 pictures, near 6 s with its lock and
|
||||
the movie thread held, threw nearly all of them away, and the next acquire started further behind.
|
||||
|
||||
Now each decode that gives a picture is timed, about the last 8 of them kept as one number, and the acquire stops
|
||||
decoding to catch up when that number is more than the gap between pictures, or once it has decoded for one gap:
|
||||
the late picture is shown. A decoder faster than the movie catches up as before, a few pictures an acquire.
|
||||
HOS_VIDEO_UNBOUNDED_CATCHUP=1: no bound, as before.
|
||||
|
||||
The rule alone, no decoder and no clock: tests/test_video_catchup.py compiles it. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
int64_t n; /* pictures decoded since first_pts */
|
||||
int64_t first_pts, end_pts; /* the first of them and the newest, by the movie's own time (ns) */
|
||||
int64_t recent_ns; /* what a decode that gives a picture takes: about the last 8 */
|
||||
} VideoPace;
|
||||
|
||||
/* a picture came out of the decoder, with its time in the movie. One whose time is before the last one's is a movie
|
||||
that started again (a loop): the gap is counted from it */
|
||||
static inline void video_pace_picture(VideoPace *p, int64_t pts) {
|
||||
if (!p->n || pts < p->end_pts) p->n = 0, p->first_pts = pts;
|
||||
p->n++;
|
||||
p->end_pts = pts;
|
||||
}
|
||||
/* the submission that gave it took ns to decode: seven parts of the old number and one of this */
|
||||
static inline void video_pace_decoded(VideoPace *p, int64_t ns) {
|
||||
if (ns < 0) ns = 0;
|
||||
p->recent_ns = (p->recent_ns * 7 + ns) / 8;
|
||||
}
|
||||
/* the gap between two pictures as the movie plays, in ns; 0 while fewer than two came out */
|
||||
static inline int64_t video_pace_spacing(const VideoPace *p) {
|
||||
return p->n > 1 && p->end_pts > p->first_pts ? (p->end_pts - p->first_pts) / (p->n - 1) : 0;
|
||||
}
|
||||
/* 1 when the acquire stops decoding to catch up and shows the late picture it has. spent_ns: how long this acquire's
|
||||
catch-up has run. Never with the switch, and never before the gap is known */
|
||||
static inline int video_catchup_stop(int unbounded, int64_t spacing_ns, int64_t recent_ns, int64_t spent_ns) {
|
||||
if (unbounded || spacing_ns <= 0) return 0;
|
||||
return recent_ns > spacing_ns || spent_ns >= spacing_ns;
|
||||
}
|
||||
33
host/video_input.h
Normal file
33
host/video_input.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/* (Triforce, 11-22, the performance audit's IO-34, after Source A's) How many submissions the movie decoder holds before
|
||||
it refuses one (nn_video.c dec_submit).
|
||||
|
||||
The game's movie library submits a whole movie at once, on the thread that also takes the pictures, and every
|
||||
submission was taken. While it submits it takes a picture only now and then, so at a movie's start the pictures
|
||||
that came due meanwhile were skipped by the catch-up: a stutter at each movie's start. The console's decoder holds
|
||||
only so much input and refuses a submission with a result of its own while it is full; the library submits it again
|
||||
later. So does this one now: while 16 submissions wait, not decoded yet or decoded and not taken, a submission is
|
||||
refused at once with that result. The stream's last submission is never refused: nothing would mark the end. The
|
||||
decoder's "ready for a submission" event is clear while it is full and set again when there is room.
|
||||
HOS_VIDEO_INPUT_LIMIT=<n>: that many instead of 16; 0: no limit, every submission taken, as before.
|
||||
|
||||
The rule alone, no decoder: tests/test_video_input_limit.py compiles it. */
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
|
||||
#define VIDEO_INPUT_LIMIT 16 /* submissions waiting, pending or decoded, before one is refused */
|
||||
#define VIDEO_RESULT_INPUT_FULL 0x1c266fu /* the console's result for a submission its decoder has no room for */
|
||||
|
||||
/* The limit from HOS_VIDEO_INPUT_LIMIT's value (NULL or empty: unset): 0 is no limit, and so is anything not a
|
||||
positive number */
|
||||
static inline int video_input_limit(const char *value) {
|
||||
if (!value || !*value) return VIDEO_INPUT_LIMIT;
|
||||
const int n = atoi(value);
|
||||
return n > 0 ? n : 0;
|
||||
}
|
||||
/* 1 while the decoder holds as much as the limit. pending: submissions not decoded yet; queued: pictures decoded and
|
||||
not taken */
|
||||
static inline int video_input_full(int limit, int pending, int queued) { return limit > 0 && pending + queued >= limit; }
|
||||
/* 1 when this submission is refused. last: it is the stream's last */
|
||||
static inline int video_input_refuse(int limit, int pending, int queued, int last) {
|
||||
return !last && video_input_full(limit, pending, queued);
|
||||
}
|
||||
45
host/video_movie.h
Normal file
45
host/video_movie.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/* (Triforce, 11-22, the performance audit's IO-32, after Source A's) What one movie cost to decode: a count kept by the
|
||||
decoder (nn_video.c decode_next, queue_frame, the catch-up in dec_acquire) and one line in the log when the movie is
|
||||
stopped, flushed or its decoder finalized. Nothing was timed before: whether a decoder keeps up with a movie (a
|
||||
picture is due every 16.7 ms) could only be guessed from the pictures skipped.
|
||||
|
||||
(Triforce, 11-22, THREAD-82) The line also has the gap between two pictures as the movie plays, and says when the
|
||||
decoder is slower than that: such a movie falls behind its sound (video_catchup.h).
|
||||
|
||||
The counts and the line alone, no decoder and no clock: tests/test_video_av1_order.py compiles them. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
int64_t pictures; /* pictures decoded since the movie's start */
|
||||
int64_t decode_ns; /* the time its submissions took to decode, pictures made ready for the game included */
|
||||
int64_t worst_ns; /* the longest of them */
|
||||
int64_t skipped; /* pictures dropped to catch up with the movie's clock */
|
||||
int64_t refused; /* ((Triforce, 11-22, IO-34) submissions refused while the decoder's input was full: video_input.h) */
|
||||
} VideoMovie;
|
||||
|
||||
/* one submission decoded, in ns by the clock */
|
||||
static inline void video_movie_decoded(VideoMovie *m, int64_t ns) {
|
||||
if (ns < 0) ns = 0;
|
||||
m->decode_ns += ns;
|
||||
if (ns > m->worst_ns) m->worst_ns = ns;
|
||||
}
|
||||
|
||||
/* The movie's line into out (without the log's "video: "): 0 and nothing written when no picture was decoded, so a
|
||||
Stop followed by a Flush, or a decoder finalized after its Stop, says it once. spacing_ns: the gap between two
|
||||
pictures as the movie plays, 0 when it is not known (video_catchup.h) */
|
||||
static inline int video_movie_line(const VideoMovie *m, const char *decoder, int64_t spacing_ns, char *out, size_t size) {
|
||||
if (m->pictures <= 0) return 0;
|
||||
char shown[80] = "";
|
||||
if (spacing_ns > 0)
|
||||
snprintf(shown, sizeof shown, ", one shown every %.2f ms%s", (double)spacing_ns / 1e6,
|
||||
m->decode_ns / m->pictures > spacing_ns ? " (slower than it plays)" : "");
|
||||
char refused[80] = "";
|
||||
if (m->refused > 0)
|
||||
snprintf(refused, sizeof refused, ", %lld submissions refused while its input was full", (long long)m->refused);
|
||||
snprintf(out, size, "a movie of %lld pictures on %s: %.2f ms to decode each (worst %.2f)%s; %lld skipped to catch up%s",
|
||||
(long long)m->pictures, decoder, (double)m->decode_ns / 1e6 / (double)m->pictures, (double)m->worst_ns / 1e6,
|
||||
shown, (long long)m->skipped, refused);
|
||||
return 1;
|
||||
}
|
||||
42
host/video_nv12.h
Normal file
42
host/video_nv12.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/* (Triforce, 11-22, the performance audit's IO-36, after Source A's) A decoded movie picture kept as NV12 alone
|
||||
(nn_video.c queue_frame).
|
||||
|
||||
Media Foundation's pictures come as NV12 and were kept as that: the pool makes RGBA from it only if the game reads
|
||||
RGBA (pool_rgba), which this game's movie library does not. libdav1d's come as planar 4:2:0 (Y, U and V planes), and
|
||||
each one was converted to RGBA on the movie thread all the same: a full-size swscale pass, an 8 MB allocation and
|
||||
an 8 MB row copy for a 1080p picture, with its NV12 made beside it in a zeroed buffer, one byte at a time. Planar
|
||||
4:2:0 is now kept as NV12 alone too. HOS_VIDEO_RGBA=1: its RGBA made for every picture, as before.
|
||||
|
||||
The rules and the U/V interleave alone, no decoder: tests/test_video_lazy_rgba.py compiles them. */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#if (defined(__x86_64__) || defined(_M_X64)) && !defined(__EMSCRIPTEN__)
|
||||
#include <emmintrin.h>
|
||||
#define VIDEO_NV12_SSE2 1
|
||||
#endif
|
||||
|
||||
/* 1 when a picture is kept as NV12 alone, its RGBA left to the pool that is read. nv12: the decoder gave NV12 (always
|
||||
lazy, as before); planar420: it gave planar 4:2:0 without alpha (with alpha the RGBA is made at once: one made later
|
||||
from NV12 would have lost it); eager: HOS_VIDEO_RGBA=1 */
|
||||
static inline int video_rgba_lazy(int eager, int nv12, int planar420) { return nv12 || (planar420 && !eager); }
|
||||
|
||||
/* 1 when the picture's NV12 buffer (w * h * 3 / 2 bytes) is zeroed before it is filled. written: the format is one
|
||||
queue_frame copies into it (NV12, planar 4:2:0 with or without alpha); any other leaves it as it is, so it is zeroed,
|
||||
as the buffer always was. An odd width or height leaves bytes no copy reaches: a chroma row's last byte, and the
|
||||
rows past h / 2 */
|
||||
static inline int video_nv12_zero(int w, int h, int written) { return ((w | h) & 1) != 0 || !written; }
|
||||
|
||||
/* One row of NV12's chroma from a row of U and a row of V: n pairs, uv[2x] = u[x] and uv[2x + 1] = v[x]. 16 pairs a
|
||||
step with SSE2 on x86-64, then one at a time */
|
||||
static inline void video_uv_interleave(uint8_t *uv, const uint8_t *u, const uint8_t *v, int n) {
|
||||
int x = 0;
|
||||
#ifdef VIDEO_NV12_SSE2
|
||||
for (; x + 16 <= n; x += 16) {
|
||||
const __m128i us = _mm_loadu_si128((const __m128i *)(u + x)), vs = _mm_loadu_si128((const __m128i *)(v + x));
|
||||
_mm_storeu_si128((__m128i *)(uv + 2 * (size_t)x), _mm_unpacklo_epi8(us, vs));
|
||||
_mm_storeu_si128((__m128i *)(uv + 2 * (size_t)x + 16), _mm_unpackhi_epi8(us, vs));
|
||||
}
|
||||
#endif
|
||||
for (; x < n; x++) uv[2 * (size_t)x] = u[x], uv[2 * (size_t)x + 1] = v[x];
|
||||
}
|
||||
Reference in New Issue
Block a user