Repository navigation
Expand file tree
/
Copy pathnr_edit_fg_observation.hpp
More file actions
325 lines (305 loc) · 11.1 KB
/
Copy pathnr_edit_fg_observation.hpp
File metadata and controls
325 lines (305 loc) · 11.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
#pragma once
#if defined(NR_NEXUS_DEV_CONTROL)
#include <windows.h>
#include <array>
#include <chrono>
#include <cstdint>
#include <mutex>
#include <shared_mutex>
#include "../../utils/directx.hpp"
#include "submission_tracker.hpp"
namespace renodx::addons::dlss5::edit_fg_observation {
enum class State : uint8_t { DISABLED, ARMED, CAPTURING, STOPPED };
enum class Kind : uint8_t {
SR_COPYBACK, NATIVE_FG_ENTRY, NATIVE_FG_RETURN,
XEFG_TAG_ENTRY, XEFG_TAG_RETURN, XEFG_PRESENT_ENTRY, XEFG_PRESENT_RETURN,
};
struct ResourceIdentity {
uintptr_t raw = 0;
uintptr_t canonical = 0;
};
struct RecordingIdentity {
uintptr_t raw = 0;
uint64_t list_id = 0;
uint64_t generation = 0;
};
struct SrCopyback {
uintptr_t source_handle = 0;
uint64_t source_generation = 0;
uint64_t sr_sequence = 0;
bool reused = false;
ResourceIdentity output;
uint32_t width = 0;
uint32_t height = 0;
uint32_t format = 0;
RecordingIdentity recording;
uint64_t environment_provider = 0;
uintptr_t environment_context = 0;
uint64_t environment_generation = 0;
};
struct NativeFgObservation {
uintptr_t handle = 0;
// Existing native FG environment revision, not a viewport or pixel binding.
uint64_t generation = 0;
uint32_t slot = 0;
bool c_export = false;
RecordingIdentity recording;
ResourceIdentity backbuffer;
ResourceIdentity hudless;
ResourceIdentity ui;
ResourceIdentity ui_alpha;
uint32_t ui_mode = 0;
bool has_ui_mode = false;
uint64_t frame_id = 0;
// Success of the documented uint64 getter only; not verified frame semantics.
bool has_frame_id = false;
};
struct XefgTagObservation {
uint32_t sdk_slot = 0;
uintptr_t sdk_context = 0;
uintptr_t context_identity = 0;
uint64_t epoch = 0;
uint64_t generation = 0;
uint32_t present_id = 0;
int32_t type = -1;
int32_t validity = -1;
ResourceIdentity resource;
// validity ONLY_NOW permits the SDK command-list interface. BackbufferRegion
// and UNTIL_NEXT_PRESENT preserve raw values without invoking those objects.
RecordingIdentity recording;
uint32_t base_x = 0;
uint32_t base_y = 0;
uint32_t width = 0;
uint32_t height = 0;
uint32_t resource_state = 0;
uint32_t ui_mode = 0;
bool ui_composition = false;
};
struct XefgPresentObservation {
uintptr_t sdk_context = 0;
uintptr_t context_identity = 0;
uint64_t epoch = 0;
uint64_t generation = 0;
uint32_t present_id = 0;
bool has_present_id = false;
bool repeated = false;
ResourceIdentity proxy;
ResourceIdentity backbuffer;
uint32_t method = 0;
uint32_t interval = 0;
uint32_t flags = 0;
};
struct PresentStatus {
bool queried = false;
int32_t api_result = 0;
uint32_t frames_presented = 0;
int32_t frame_gen_result = 0;
uint32_t enabled = 0;
};
struct Event {
Kind kind = Kind::SR_COPYBACK;
uint64_t sequence = 0;
int64_t observed_ns = 0;
int64_t api_return_ns = 0;
uint32_t thread_id = 0;
uint64_t call_id = 0;
int64_t api_result = 0;
SrCopyback sr;
NativeFgObservation native_fg;
XefgTagObservation xefg_tag;
XefgPresentObservation xefg_present;
PresentStatus present_status;
};
struct Snapshot {
bool enabled = false;
State state = State::DISABLED;
const char* stop_reason = "disabled";
uint64_t request_generation = 0;
uint32_t source_frames = 0;
uint32_t count = 0;
uint64_t overflow = 0;
SrCopyback source;
// These are CPU observations. Neither a recorded copy nor an SDK return
// supplies GPU submission/completion or source-to-FG pixel-consumption proof.
std::array<Event, 64> events{};
};
struct Ticket {
uint64_t request_generation = 0;
uint64_t call_id = 0;
Kind kind = Kind::SR_COPYBACK;
};
struct ObservationState {
std::shared_mutex mutex;
Snapshot snapshot;
uint64_t next_generation = 0;
uint64_t last_sr_sequence = 0;
};
static ObservationState observation_state;
inline void SetEnabled(bool enabled) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (snapshot.enabled == enabled) return;
if (enabled) {
snapshot = {.enabled = true, .state = State::ARMED, .stop_reason = "awaiting_sr_copyback",
.request_generation = ++observation_state.next_generation};
observation_state.last_sr_sequence = 0;
} else {
snapshot.enabled = false;
if (snapshot.state == State::ARMED || snapshot.state == State::CAPTURING) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "experiment_disabled";
}
}
}
inline void Stop(const char* reason) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (snapshot.state == State::ARMED || snapshot.state == State::CAPTURING) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = reason;
}
}
inline void StopSource(uintptr_t handle) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (snapshot.state == State::CAPTURING && snapshot.source.source_handle == handle) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "source_released";
}
}
inline void StopEnvironment(uint32_t provider, uintptr_t context) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (snapshot.state == State::CAPTURING && snapshot.source.environment_provider == provider
&& (context == 0 || snapshot.source.environment_context == context)) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "environment_changed";
}
}
inline uint64_t CaptureGeneration(bool include_armed = false) {
std::shared_lock lock(observation_state.mutex);
const auto& snapshot = observation_state.snapshot;
if (snapshot.state == State::CAPTURING || (include_armed && snapshot.state == State::ARMED)) {
return snapshot.request_generation;
}
return 0;
}
inline int64_t NowNs() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
// Only call while the API owns a genuine resource argument. The numbers do not
// retain it or establish object lifetime across unrelated requests.
inline ResourceIdentity ObserveResource(IUnknown* resource) {
return {.raw = reinterpret_cast<uintptr_t>(resource),
.canonical = reinterpret_cast<uintptr_t>(renodx::utils::directx::NativeIdentity(resource))};
}
inline RecordingIdentity ObserveRecording(ID3D12CommandList* list) {
RecordingIdentity observation{.raw = reinterpret_cast<uintptr_t>(list)};
if (list == nullptr) return observation;
// Do not assign a fresh initial generation to an unobserved foreign list.
const auto identity = submission::ListIdentity(list, false);
std::lock_guard lock(submission::tracker_mutex);
if (const auto it = submission::tracked_lists.find(identity);
identity != 0 && (identity & (uint64_t{1} << 63)) == 0
&& it != submission::tracked_lists.end() && !it->second.destroyed) {
observation.list_id = identity;
observation.generation = it->second.current;
}
return observation;
}
inline void ObserveSourceFrame(uintptr_t handle, uint64_t generation, uint64_t sequence) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (snapshot.state != State::CAPTURING) return;
if (snapshot.source.source_handle != handle || snapshot.source.source_generation != generation
|| sequence < observation_state.last_sr_sequence) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "source_changed";
return;
}
if (sequence == observation_state.last_sr_sequence) return;
if (snapshot.source_frames == 4) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "source_frame_limit";
return;
}
observation_state.last_sr_sequence = sequence;
++snapshot.source_frames;
}
// The caller holds the trace lock. No COM calls, SDK calls or tracker locks
// occur while it is held; every stored object is a scalar borrowed observation.
inline Ticket Append(Event event) {
auto& snapshot = observation_state.snapshot;
if (snapshot.count == snapshot.events.size()) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "event_limit";
++snapshot.overflow;
return {};
}
event.sequence = snapshot.count + 1;
event.observed_ns = NowNs();
event.thread_id = GetCurrentThreadId();
if (event.call_id == 0 && event.kind != Kind::SR_COPYBACK) {
event.call_id = event.sequence;
}
snapshot.events[snapshot.count++] = event;
return {.request_generation = snapshot.request_generation, .call_id = event.call_id, .kind = event.kind};
}
inline void RecordSrCopyback(uint64_t generation, const SrCopyback& source) {
std::unique_lock lock(observation_state.mutex);
auto& snapshot = observation_state.snapshot;
if (generation == 0 || generation != snapshot.request_generation
|| source.source_handle == 0 || source.source_generation == 0 || source.sr_sequence == 0
|| (snapshot.state != State::ARMED && snapshot.state != State::CAPTURING)) return;
if (snapshot.state == State::ARMED) {
snapshot.state = State::CAPTURING;
snapshot.stop_reason = "capturing";
snapshot.source = source;
snapshot.source_frames = 1;
observation_state.last_sr_sequence = source.sr_sequence;
} else if (snapshot.source.source_handle != source.source_handle
|| snapshot.source.source_generation != source.source_generation
|| snapshot.source.environment_provider != source.environment_provider
|| snapshot.source.environment_context != source.environment_context
|| snapshot.source.environment_generation != source.environment_generation) {
snapshot.state = State::STOPPED;
snapshot.stop_reason = "source_or_environment_changed";
return;
}
Append({.kind = Kind::SR_COPYBACK, .sr = source});
}
inline Ticket Record(uint64_t generation, Event event) {
std::unique_lock lock(observation_state.mutex);
const auto& snapshot = observation_state.snapshot;
if (generation == 0 || generation != snapshot.request_generation || snapshot.state != State::CAPTURING) return {};
if (event.kind != Kind::NATIVE_FG_ENTRY && event.kind != Kind::XEFG_TAG_ENTRY
&& event.kind != Kind::XEFG_PRESENT_ENTRY) return {};
return Append(event);
}
inline void RecordResult(const Ticket& ticket, int64_t result, int64_t api_return_ns,
const PresentStatus& present_status = {}) {
std::unique_lock lock(observation_state.mutex);
const auto& snapshot = observation_state.snapshot;
if (ticket.call_id == 0 || ticket.request_generation != snapshot.request_generation
|| snapshot.state != State::CAPTURING) return;
Kind kind;
switch (ticket.kind) {
case Kind::NATIVE_FG_ENTRY: kind = Kind::NATIVE_FG_RETURN; break;
case Kind::XEFG_TAG_ENTRY: kind = Kind::XEFG_TAG_RETURN; break;
case Kind::XEFG_PRESENT_ENTRY: kind = Kind::XEFG_PRESENT_RETURN; break;
default: return;
}
Append({.kind = kind, .api_return_ns = api_return_ns, .call_id = ticket.call_id,
.api_result = result, .present_status = present_status});
}
inline void CopySnapshot(Snapshot* destination) {
std::shared_lock lock(observation_state.mutex);
*destination = observation_state.snapshot;
}
} // namespace renodx::addons::dlss5::edit_fg_observation
#endif