Repository navigation
Expand file tree
/
Copy pathmonitor_gpu.hpp
More file actions
136 lines (124 loc) · 6.23 KB
/
Copy pathmonitor_gpu.hpp
File metadata and controls
136 lines (124 loc) · 6.23 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
/*
* Copyright (C) 2026
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include "gpu_timers.hpp"
#include "stage_plan.hpp"
namespace renodx::addons::dlss5 {
enum class PresentEditReuseRejection : uint8_t { NONE, SOURCE_UNCONFIRMED, HDR10_SOURCE_UNCONFIRMED };
inline std::atomic_uint64_t present_edit_reuse_monitor_revision{0};
// Owned by the swapchain's PresentStream under runtime_mutex. An early
// rejection has no source sequence, submission or completion to publish.
struct PresentEditReuseMonitor {
const void* device = nullptr;
const void* backbuffer = nullptr;
uint64_t generation = 0;
uint64_t attempt = 0;
uint64_t configuration = 0;
uint64_t reset_epoch = 0;
int64_t rejected_ns = 0;
PresentEditReuseRejection reason = PresentEditReuseRejection::NONE;
void Reject(const void* device_identity, const void* backbuffer_identity,
uint64_t stream_generation, uint64_t present_attempt, PresentEditReuseRejection rejection,
int64_t now, uint64_t current_configuration, uint64_t current_reset_epoch) {
if (device_identity == nullptr || backbuffer_identity == nullptr || present_attempt == 0 || now <= 0
|| rejection == PresentEditReuseRejection::NONE
|| (generation == stream_generation && present_attempt <= attempt)) return;
const bool changed = device != device_identity || generation != stream_generation || reason != rejection
|| configuration != current_configuration || reset_epoch != current_reset_epoch;
*this = {.device = device_identity, .backbuffer = backbuffer_identity, .generation = stream_generation,
.attempt = present_attempt, .configuration = current_configuration, .reset_epoch = current_reset_epoch,
.rejected_ns = now, .reason = rejection};
if (changed) {
present_edit_reuse_monitor_revision.fetch_add(1, std::memory_order_release);
}
}
void Deliver(uint64_t stream_generation, uint64_t present_attempt) {
if (rejected_ns == 0 || generation != stream_generation || present_attempt <= attempt) return;
attempt = present_attempt;
// The next refresh must not use x2 proof from before the rejection.
if (reason != PresentEditReuseRejection::NONE) {
reason = PresentEditReuseRejection::NONE;
present_edit_reuse_monitor_revision.fetch_add(1, std::memory_order_release);
}
}
void Reset(uint64_t stream_generation = 0) {
if (stream_generation == 0 && rejected_ns == 0) return;
*this = {};
present_edit_reuse_monitor_revision.fetch_add(1, std::memory_order_release);
}
};
struct MonitorGpuTotal {
bool valid = false;
bool reference = false;
bool incompatible = false;
uint32_t missing_stages = 0;
float milliseconds = 0.f;
};
// The caller validates recording/completion age, live configuration and source
// epochs. This layer verifies complete group coverage and comparable contracts.
inline MonitorGpuTotal SummarizeMonitorGpu(const stage_plan::Plan& plan,
const std::array<gpu_timers::Stats, 3>& samples, const std::array<bool, 3>& current,
const std::array<uint32_t, 4>& resolved_models) {
MonitorGpuTotal result;
if (!plan.valid || plan.group_count == 0 || plan.group_count > 3) return result;
const gpu_timers::Stats* first = nullptr;
uint64_t total_us = 0;
uint32_t covered = 0;
for (uint32_t group_index = 0; group_index < plan.group_count; ++group_index) {
const auto& group = plan.groups[group_index];
const size_t stage = static_cast<size_t>(group.stage);
if (stage >= samples.size() || group.count == 0 || group.begin + group.count > plan.pass_count) return result;
const auto& sample = samples[stage];
bool complete = current[stage] && sample.valid && sample.state == gpu_timers::SampleState::VALID
&& gpu_timers::StageIndex(sample.path) == stage;
for (uint32_t local = 0; local < gpu_timers::kTimerMaxPasses; ++local) {
const auto& pass = sample.passes[local];
if (local < group.count) {
const auto id = group.begin + local;
complete &= pass.id == id && pass.state == gpu_timers::SampleState::VALID
&& pass.model == resolved_models[id] && pass.width != 0 && pass.height != 0
&& pass.output_width != 0 && pass.output_height != 0;
if ((covered & (1u << id)) != 0) complete = false;
covered |= 1u << id;
} else {
complete &= pass.state != gpu_timers::SampleState::VALID;
}
}
if (!complete) result.missing_stages |= 1u << stage;
if (first != nullptr) {
const auto& a = first->identity;
const auto& b = sample.identity;
// Durations are already converted with each exact submission's queue
// frequency. This first reference metric deliberately requires one queue.
// Stage-local work sizes, processing flags and rebuild epochs may differ;
// their own current-contract checks must pass before reaching this layer.
result.incompatible |= first->queue == nullptr || sample.queue != first->queue
|| a.source_generation == 0 || a.source_generation != b.source_generation
|| a.configuration_generation != b.configuration_generation || a.config_epoch != b.config_epoch
|| a.settings_hash != b.settings_hash || a.resolution_hash != b.resolution_hash
|| a.device_session != b.device_session || a.route != b.route || a.fg != b.fg
|| a.source_feature != b.source_feature || a.hdr != b.hdr || a.sr_mode != b.sr_mode
|| a.sr_width == 0 || a.sr_height == 0 || a.out_width == 0 || a.out_height == 0
|| a.sr_width != b.sr_width || a.sr_height != b.sr_height
|| a.out_width != b.out_width || a.out_height != b.out_height
|| a.display_format != b.display_format;
} else {
first = &sample;
}
// Each total includes that group's copies/linearize/commit once. Pass
// totals and Look (already inside Resolve) must never be added again.
total_us += sample.total_us;
}
result.valid = result.missing_stages == 0 && !result.incompatible
&& covered == (1u << plan.pass_count) - 1u;
result.reference = result.valid && plan.group_count > 1;
if (result.valid) result.milliseconds = static_cast<float>(total_us) / 1000.f;
return result;
}
} // namespace renodx::addons::dlss5