summaryrefslogtreecommitdiffstats
path: root/src/video_core/invalidation_accumulator.h
blob: 42420e31c81359668f0d2cc2b528a5c267cf5297 (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <vector>

#include "common/common_types.h"

namespace VideoCommon {

class InvalidationAccumulator {
public:
    InvalidationAccumulator() = default;
    ~InvalidationAccumulator() = default;

    void Add(GPUVAddr address, size_t size) {
        const auto reset_values = [&]() {
            if (has_collected) {
                buffer.emplace_back(start_address, accumulated_size);
            }
            start_address = address;
            accumulated_size = size;
            last_collection = start_address + size;
        };
        if (address >= start_address && address + size <= last_collection) [[likely]] {
            return;
        }
        size = (address + size + atomicy_side_mask) & atomicy_mask - address;
        address = address & atomicy_mask;
        if (!has_collected) [[unlikely]] {
            reset_values();
            has_collected = true;
            return;
        }
        if (address != last_collection) [[unlikely]] {
            reset_values();
            return;
        }
        accumulated_size += size;
        last_collection += size;
    }

    void Clear() {
        buffer.clear();
        start_address = 0;
        last_collection = 0;
        has_collected = false;
    }

    bool AnyAccumulated() const {
        return has_collected;
    }

    template <typename Func>
    void Callback(Func&& func) {
        if (!has_collected) {
            return;
        }
        buffer.emplace_back(start_address, accumulated_size);
        for (auto& [address, size] : buffer) {
            func(address, size);
        }
    }

private:
    static constexpr size_t atomicy_bits = 5;
    static constexpr size_t atomicy_size = 1ULL << atomicy_bits;
    static constexpr size_t atomicy_side_mask = atomicy_size - 1;
    static constexpr size_t atomicy_mask = ~atomicy_side_mask;
    GPUVAddr start_address{};
    GPUVAddr last_collection{};
    size_t accumulated_size{};
    bool has_collected{};
    std::vector<std::pair<VAddr, size_t>> buffer;
};

} // namespace VideoCommon