mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
Triforce v0.3.3
This commit is contained in:
@@ -601,6 +601,27 @@ static void au_device_name(unsigned device, char *out, size_t size) {
|
||||
#endif
|
||||
}
|
||||
static uint64_t au_ms(void) { return (uint64_t)GetTickCount64(); }
|
||||
/* (hookshot) Which output Windows has as its default now (the waveOut mapper's preferred device, its number and name):
|
||||
a key that changes when the default does; 0 when unknown. A device opened as the default stays on that device when
|
||||
Windows moves its default (a Bluetooth headset gone and back), so the output section asks every second */
|
||||
#ifndef DRVM_MAPPER_PREFERRED_GET
|
||||
#define DRVM_MAPPER_PREFERRED_GET (0x2000 + 21) /* (mmddk.h: DRVM_MAPPER + 21) */
|
||||
#endif
|
||||
static uint32_t au_default_key(void) {
|
||||
#ifdef _WIN32
|
||||
DWORD id = (DWORD)-1, flags = 0;
|
||||
if (waveOutMessage((HWAVEOUT)(UINT_PTR)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&id, (DWORD_PTR)&flags) !=
|
||||
MMSYSERR_NOERROR || id == (DWORD)-1)
|
||||
return 0;
|
||||
char name[64];
|
||||
au_device_name(id, name, sizeof name);
|
||||
uint32_t h = 2166136261u ^ id;
|
||||
for (const char *p = name; *p; p++) h = (h ^ (uint8_t)*p) * 16777619u;
|
||||
return h ? h : 1;
|
||||
#else
|
||||
return 0; /* (linux: SDL follows the default device itself) */
|
||||
#endif
|
||||
}
|
||||
/* HOS_AUDIO_MUTE=1 (test runs): the device opened and fed as for a player, at volume 0 */
|
||||
static void au_opened(HWAVEOUT w) {
|
||||
#ifdef _WIN32
|
||||
@@ -735,6 +756,15 @@ static void output_error(const char *operation, MMRESULT result) {
|
||||
static int out_next; /* the next candidate: 0 = HOS_AUDIO_DEVICE, 1 = the default, 2.. = device n-2 */
|
||||
static uint64_t out_opened, out_stall_since;
|
||||
static unsigned out_played, out_peak, out_reported;
|
||||
/* (hookshot) A device gone (a Bluetooth headset off, the audio service restarted: "no driver", "invalid handle" from the
|
||||
next call) is closed and the default opened again; none opening, the default is tried every second until one does.
|
||||
Opened as the default, the output follows Windows': its key (au_default_key) looked at every second, a change reopens
|
||||
there. Before, a call that failed this way was only logged, the same block tried again for good, and the game had no
|
||||
sound for the rest of the run, on any device (RX 5500M laptop, a WH-CH720N headset) */
|
||||
static int out_retry, out_reopens;
|
||||
static uint64_t out_retry_at, out_default_seen;
|
||||
static uint32_t out_default_key; /* (0: not opened as the default, or Windows did not say which it is) */
|
||||
static int au_device_gone(MMRESULT r) { return r == 2 || r == 5 || r == 6; } /* BADDEVICEID, INVALHANDLE, NODRIVER */
|
||||
static int output_try(int candidate, const WAVEFORMATEX *f) {
|
||||
const char *want = getenv("HOS_AUDIO_DEVICE");
|
||||
unsigned device;
|
||||
@@ -749,6 +779,8 @@ static int output_try(int candidate, const WAVEFORMATEX *f) {
|
||||
return 0;
|
||||
}
|
||||
au_opened(wout);
|
||||
out_default_key = device == WAVE_MAPPER ? au_default_key() : 0;
|
||||
out_default_seen = au_ms();
|
||||
char name[64];
|
||||
if (device == WAVE_MAPPER) snprintf(name, sizeof name, "the default device");
|
||||
else au_device_name(device, name, sizeof name);
|
||||
@@ -790,6 +822,7 @@ static void output_open(void) {
|
||||
channels = 2;
|
||||
}
|
||||
out_channels = channels;
|
||||
out_retry = !wout, out_retry_at = au_ms(); /* (none opened: tried again in a second, output_frame) */
|
||||
if (!wout) return;
|
||||
for (int i = 0; i < NBLOCKS; i++) {
|
||||
memset(&hdr[i], 0, sizeof hdr[i]);
|
||||
@@ -802,18 +835,30 @@ static void output_open(void) {
|
||||
out_stall_since = 0;
|
||||
out_played = out_peak = out_reported = 0;
|
||||
}
|
||||
static void output_reopen(void) {
|
||||
static int reopens;
|
||||
out_stall_since = 0;
|
||||
if (++reopens > 6) return;
|
||||
hos_log("audio: the output device took no sound for 1.5 s: trying the next one");
|
||||
static void output_close(void) { /* (what fails here is not looked at: a device gone takes none of these calls) */
|
||||
waveOutReset(wout);
|
||||
for (int i = 0; i < NBLOCKS; i++)
|
||||
if (hdr[i].dwFlags & WHDR_PREPARED) waveOutUnprepareHeader(wout, &hdr[i], sizeof hdr[i]);
|
||||
waveOutClose(wout);
|
||||
wout = NULL;
|
||||
}
|
||||
static void output_reopen(void) { /* (a device that takes nothing: the next candidate, 6 times; again after 30 s of sound) */
|
||||
out_stall_since = 0;
|
||||
if (++out_reopens > 6) return;
|
||||
hos_log("audio: the output device took no sound for 1.5 s: trying the next one");
|
||||
output_close();
|
||||
output_open();
|
||||
}
|
||||
static void output_open_default(void) { /* (HOS_AUDIO_DEVICE first when it is set, as at the start) */
|
||||
const char *want = getenv("HOS_AUDIO_DEVICE");
|
||||
out_next = want && *want ? 0 : 1;
|
||||
output_open();
|
||||
}
|
||||
static void output_move(const char *why) {
|
||||
hos_log("audio: %s: the output opened again on the default device", why);
|
||||
output_close();
|
||||
output_open_default();
|
||||
}
|
||||
static void dump_finish(void) {
|
||||
if (!dump) return;
|
||||
uint8_t h[44];
|
||||
@@ -846,7 +891,19 @@ static void output_frame(const int16_t *pcm) {
|
||||
fseek(dump, at, SEEK_SET);
|
||||
}
|
||||
}
|
||||
if (!wout) return;
|
||||
if (!wout) {
|
||||
if (!out_retry || au_ms() - out_retry_at < 1000) return;
|
||||
output_open_default(); /* (the service back, a device plugged in) */
|
||||
if (!wout) return;
|
||||
}
|
||||
if (out_default_key && au_ms() - out_default_seen >= 1000) {
|
||||
out_default_seen = au_ms();
|
||||
const uint32_t now = au_default_key();
|
||||
if (now && now != out_default_key) {
|
||||
output_move("Windows' default output changed");
|
||||
if (!wout) return;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < FRAME * out_channels; i++) {
|
||||
const unsigned a = (unsigned)(pcm[i] < 0 ? -pcm[i] : pcm[i]);
|
||||
if (a > out_peak) out_peak = a;
|
||||
@@ -869,6 +926,7 @@ static void output_frame(const int16_t *pcm) {
|
||||
const MMRESULT result = waveOutUnprepareHeader(wout, h, sizeof *h);
|
||||
if (result != MMSYSERR_NOERROR) {
|
||||
output_error("waveOutUnprepareHeader", result);
|
||||
if (au_device_gone(result)) output_move("the output device went away");
|
||||
return; /* the driver has not released the header: do not change its samples or flags */
|
||||
}
|
||||
}
|
||||
@@ -881,10 +939,17 @@ static void output_frame(const int16_t *pcm) {
|
||||
if (result == MMSYSERR_NOERROR) {
|
||||
operation = "waveOutWrite";
|
||||
result = waveOutWrite(wout, h, sizeof *h);
|
||||
if (result == MMSYSERR_NOERROR) out_played++;
|
||||
if (result == MMSYSERR_NOERROR) {
|
||||
out_played++;
|
||||
if (out_reopens && au_ms() - out_opened > 30000) out_reopens = 0; /* (sound again: the stall rule's 6 anew) */
|
||||
}
|
||||
}
|
||||
if (result != MMSYSERR_NOERROR) {
|
||||
output_error(operation, result);
|
||||
if (au_device_gone(result)) {
|
||||
output_move("the output device went away");
|
||||
return;
|
||||
}
|
||||
/* A failed submission has no completion to set DONE. Reuse only a host-owned block; a prepared header
|
||||
is unprepared on its next turn. Without this, returning to this slot drops every later audio frame. */
|
||||
if (!(h->dwFlags & WHDR_INQUEUE)) h->dwFlags |= WHDR_DONE;
|
||||
|
||||
@@ -592,8 +592,12 @@ HLE(_ZN2nn2fs13OpenDirectoryEPNS0_15DirectoryHandleEPKci) {
|
||||
G64(ARG(0)) = new_handle(dh);
|
||||
RET(0);
|
||||
}
|
||||
/* (hookshot) A directory's listing is read, counted and closed under one lock: a CloseDirectory on another guest thread freed
|
||||
the handle (and its find handle) while a ReadDirectory or GetDirectoryEntryCount still walked it */
|
||||
static SRWLOCK dir_lock = SRWLOCK_INIT;
|
||||
/* ReadDirectory(long* count, DirectoryEntry* entries, DirectoryHandle, long max) */
|
||||
HLE(_ZN2nn2fs13ReadDirectoryEPlPNS0_14DirectoryEntryENS0_15DirectoryHandleEl) {
|
||||
AcquireSRWLockExclusive(&dir_lock);
|
||||
Handle *dh = get_handle(c, ARG(2));
|
||||
int64_t n = 0, max = (int64_t)ARG(3);
|
||||
while (n < max && !dh->find_done) {
|
||||
@@ -620,11 +624,13 @@ HLE(_ZN2nn2fs13ReadDirectoryEPlPNS0_14DirectoryEntryENS0_15DirectoryHandleEl) {
|
||||
G64(e + 0x308) = x->size;
|
||||
n++;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&dir_lock);
|
||||
G64(ARG(0)) = (uint64_t)n;
|
||||
RET(0);
|
||||
}
|
||||
/* (hookshot) GetDirectoryEntryCount(long* count, DirectoryHandle): every entry the open mode lists, read or not */
|
||||
HLE(_ZN2nn2fs22GetDirectoryEntryCountEPlNS0_15DirectoryHandleE) {
|
||||
AcquireSRWLockExclusive(&dir_lock);
|
||||
Handle *dh = get_handle(c, ARG(1));
|
||||
int64_t n = 0;
|
||||
WIN32_FIND_DATAA fd;
|
||||
@@ -640,13 +646,16 @@ HLE(_ZN2nn2fs22GetDirectoryEntryCountEPlNS0_15DirectoryHandleE) {
|
||||
}
|
||||
for (int i = 0; i < dh->nextras; i++)
|
||||
n += (dh->extras[i].is_dir && (dh->is_dir & 1)) || (!dh->extras[i].is_dir && (dh->is_dir & 2));
|
||||
ReleaseSRWLockExclusive(&dir_lock);
|
||||
G64(ARG(0)) = (uint64_t)n;
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn2fs14CloseDirectoryENS0_15DirectoryHandleE) {
|
||||
AcquireSRWLockExclusive(&dir_lock);
|
||||
Handle *dh = get_handle(c, ARG(0));
|
||||
if (dh->find != INVALID_HANDLE_VALUE) FindClose(dh->find);
|
||||
free_handle(ARG(0));
|
||||
ReleaseSRWLockExclusive(&dir_lock);
|
||||
free(dh->extras);
|
||||
free(dh);
|
||||
}
|
||||
|
||||
27
host/nn_vi.c
27
host/nn_vi.c
@@ -278,6 +278,21 @@ uint32_t hos_frame_whole(uint64_t at, uint32_t amount_bits) {
|
||||
return whole;
|
||||
}
|
||||
volatile uint64_t hos_res_module = 0; /* (f_831f3200's object, whose +0xe5c is the 3D's scale: nx2/oot_patches.py) */
|
||||
/* Quantize the time step to the engine's integer nominal rate. Keep debt from a short frame even when the minimum
|
||||
step is 1/60: discarding it made alternating short/long frames advance more game time than elapsed wall time.
|
||||
Keep at most four nominal frames of debt so unpaced fast presents cannot leave a long recovery. Steps beyond
|
||||
1/15 s are still dropped, rather than caught up after a long stall. */
|
||||
static int realtime_rate(double ratio, int maximum, double *carry) {
|
||||
ratio += *carry;
|
||||
if (ratio > 4.0) ratio = 4.0;
|
||||
const double least = 60.0 / maximum;
|
||||
const double bounded = ratio < least ? least : ratio;
|
||||
int fps = (int)(60.0 / bounded + 0.5);
|
||||
fps = fps < 15 ? 15 : fps > maximum ? maximum : fps;
|
||||
*carry = ratio - 60.0 / fps;
|
||||
if (*carry < -4.0) *carry = -4.0;
|
||||
return fps;
|
||||
}
|
||||
static void realtime_step(int64_t now) {
|
||||
int rx_option(int id, int otherwise);
|
||||
/* (Resolution 100% from the start once came out black (the dynamic block's fields unset), so the dynamic path ran 30
|
||||
@@ -415,19 +430,17 @@ static void realtime_step(int64_t now) {
|
||||
__atomic_store_n(&hos_frame_seconds_bits, 0, __ATOMIC_RELAXED);
|
||||
}
|
||||
const int top = timed ? 60 : fps_max();
|
||||
double ratio = sum / count * 60.0 + carry;
|
||||
const double least = 60.0 / top;
|
||||
ratio = ratio < least ? least : ratio > 4.0 ? 4.0 : ratio;
|
||||
int fps = (int)(60.0 / ratio + 0.5);
|
||||
fps = fps < 15 ? 15 : fps > top ? top : fps;
|
||||
const double ratio = sum / count * 60.0;
|
||||
int *slot = (int *)GP(mod + 0x2c8);
|
||||
if (*slot < 15 || *slot > fps_max()) {
|
||||
static int logged;
|
||||
if (!logged++) hos_log("vi: the graphics module's frame rate reads %d: no real-time step", *slot);
|
||||
return;
|
||||
}
|
||||
carry = ratio - 60.0 / fps;
|
||||
carry = carry > 0.5 ? 0.5 : carry < -0.5 ? -0.5 : carry;
|
||||
/* The timed hook already supplies the actual frame time. Its nominal rate tracks this frame without debt. */
|
||||
double frame_carry = timed ? 0 : carry;
|
||||
const int fps = realtime_rate(ratio, top, &frame_carry);
|
||||
carry = timed ? 0 : frame_carry;
|
||||
*slot = fps;
|
||||
if (!(steps++ % 3000)) hos_log("vi: real-time step on: ratio %.3f, game's frame rate %d", ratio, fps);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user