Repository navigation
Expand file tree
/
Copy pathpresent_cadence.hpp
More file actions
106 lines (99 loc) · 4.29 KB
/
Copy pathpresent_cadence.hpp
File metadata and controls
106 lines (99 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
namespace renodx::dlss5 {
// NRPresentFgSource: declared DLSS-G count with a stable timing fallback,
// stable timing only, or the pre-hardening timing rule for compatibility.
enum class PresentFgSource : uint32_t { kAuto = 0, kTiming = 1, kLegacyTiming = 2 };
// Presentation cadence is evidence about a stream, not an instantaneous
// frame-generation setting. A one-second window can straddle FG activation,
// a CPU/GPU hitch, or queued presents; publishing its nearest integer once
// produced 3x -> 4x -> 3x -> 4x on a declared 4x field session (2026-09-25).
// This state is API-free so those scheduling cases can be checked without
// depending on the speed or load of the GPU running the qualification lane.
struct PresentCadence {
static constexpr int64_t kWindowNs = 1'000'000'000;
static constexpr int64_t kStallNs = 250'000'000;
int64_t window_start_ns = 0;
int64_t last_present_ns = 0;
uint64_t window_presents = 0;
uint64_t window_captures = 0;
float cadence = 1.f;
uint32_t multiplier = 1;
uint32_t candidate = 0;
uint32_t candidate_windows = 0;
uint32_t low_windows = 0;
PresentFgSource source = PresentFgSource::kAuto;
bool declared = false;
// `declaration` is 0 when absent/ambiguous, otherwise the observed
// DLSSG.MultiFrameCount + 1. The caller owns its stream association and
// lifetime. A declaration wins immediately; losing one keeps the current
// multiplier until the fallback has actual evidence for another value.
bool Observe(int64_t now_ns, uint64_t captures, bool fresh,
uint32_t declaration, PresentFgSource mode) {
const uint32_t previous = multiplier;
const bool legacy = mode == PresentFgSource::kLegacyTiming;
const bool use_declaration = mode == PresentFgSource::kAuto
&& declaration >= 1 && declaration <= 8;
const bool restart = window_start_ns == 0 || mode != source
|| declared != use_declaration
|| captures < window_captures
|| (!legacy && (now_ns - last_present_ns > kStallNs || !fresh));
source = mode;
declared = use_declaration;
last_present_ns = now_ns;
if (use_declaration) multiplier = declaration;
if (restart) {
window_start_ns = now_ns;
window_presents = 0;
window_captures = captures;
candidate = candidate_windows = low_windows = 0;
return multiplier != previous;
}
if (fresh) ++window_presents;
if (now_ns - window_start_ns < kWindowNs) return multiplier != previous;
const uint64_t count = captures - window_captures;
// More complete game frames bound the phase error at the window edges.
// Slow but continuous streams accumulate them across multiple seconds.
if (!legacy && count < 16) return multiplier != previous;
if (count >= 8) {
cadence = static_cast<float>(window_presents) / static_cast<float>(count);
if (!use_declaration && legacy) {
low_windows = cadence < 1.25f ? low_windows + 1 : 0;
const auto nearest = static_cast<uint32_t>(std::clamp(std::lround(cadence), 2l, 8l));
if (multiplier == 1) {
if (cadence >= 1.5f) multiplier = nearest;
} else if (low_windows >= 2) {
multiplier = 1;
} else if (std::abs(cadence - static_cast<float>(multiplier)) > 0.75f) {
multiplier = nearest;
}
} else if (!use_declaration) {
const auto nearest = static_cast<uint32_t>(std::clamp(std::lround(cadence), 1l, 8l));
const uint32_t measured = cadence < 1.25f ? 1u
: cadence >= 1.5f && std::abs(cadence - static_cast<float>(nearest)) <= 0.25f
? nearest : 0u;
if (measured == 0 || measured == multiplier) {
candidate = candidate_windows = 0;
} else {
candidate_windows = candidate == measured ? candidate_windows + 1 : 1;
candidate = measured;
if (candidate_windows >= 2) {
multiplier = measured;
candidate = candidate_windows = 0;
}
}
}
}
window_start_ns = now_ns;
window_presents = 0;
window_captures = captures;
return multiplier != previous;
}
};
} // namespace renodx::dlss5