summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_vulkan/vk_state_tracker.h
blob: 8010ad26cc899b0988a1e75007f78eb222d38681 (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
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
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <cstddef>
#include <limits>

#include "common/common_types.h"
#include "video_core/dirty_flags.h"
#include "video_core/engines/maxwell_3d.h"

namespace Tegra {
namespace Control {
struct ChannelState;
}
} // namespace Tegra

namespace Vulkan {

namespace Dirty {

enum : u8 {
    First = VideoCommon::Dirty::LastCommonEntry,

    VertexInput,
    VertexAttribute0,
    VertexAttribute31 = VertexAttribute0 + 31,
    VertexBinding0,
    VertexBinding31 = VertexBinding0 + 31,

    Viewports,
    Scissors,
    DepthBias,
    BlendConstants,
    DepthBounds,
    StencilProperties,
    StencilReference,
    StencilWriteMask,
    StencilCompare,
    LineWidth,

    CullMode,
    DepthBoundsEnable,
    DepthTestEnable,
    DepthWriteEnable,
    DepthCompareOp,
    FrontFace,
    StencilOp,
    StencilTestEnable,
    PrimitiveRestartEnable,
    RasterizerDiscardEnable,
    DepthBiasEnable,
    StateEnable,
    LogicOp,
    LogicOpEnable,
    DepthClampEnable,

    Blending,
    BlendEnable,
    BlendEquations,
    ColorMask,
    ViewportSwizzles,

    Last,
};
static_assert(Last <= std::numeric_limits<u8>::max());

} // namespace Dirty

class StateTracker {
    using Maxwell = Tegra::Engines::Maxwell3D::Regs;

public:
    explicit StateTracker();

    void InvalidateCommandBufferState() {
        (*flags) |= invalidation_flags;
        current_topology = INVALID_TOPOLOGY;
        stencil_reset = true;
    }

    void InvalidateViewports() {
        (*flags)[Dirty::Viewports] = true;
    }

    void InvalidateScissors() {
        (*flags)[Dirty::Scissors] = true;
    }

    bool TouchViewports() {
        const bool dirty_viewports = Exchange(Dirty::Viewports, false);
        const bool rescale_viewports = Exchange(VideoCommon::Dirty::RescaleViewports, false);
        return dirty_viewports || rescale_viewports;
    }

    bool TouchScissors() {
        const bool dirty_scissors = Exchange(Dirty::Scissors, false);
        const bool rescale_scissors = Exchange(VideoCommon::Dirty::RescaleScissors, false);
        return dirty_scissors || rescale_scissors;
    }

    bool TouchDepthBias() {
        return Exchange(Dirty::DepthBias, false) ||
               Exchange(VideoCommon::Dirty::DepthBiasGlobal, false);
    }

    bool TouchBlendConstants() {
        return Exchange(Dirty::BlendConstants, false);
    }

    bool TouchDepthBounds() {
        return Exchange(Dirty::DepthBounds, false);
    }

    bool TouchStencilProperties() {
        return Exchange(Dirty::StencilProperties, false);
    }

    bool TouchStencilReference() {
        return Exchange(Dirty::StencilReference, false);
    }

    bool TouchStencilWriteMask() {
        return Exchange(Dirty::StencilWriteMask, false);
    }

    bool TouchStencilCompare() {
        return Exchange(Dirty::StencilCompare, false);
    }

    template <typename T>
    bool ExchangeCheck(T& old_value, T new_value) {
        bool result = old_value != new_value;
        old_value = new_value;
        return result;
    }

    bool TouchStencilSide(bool two_sided_stencil_new) {
        return ExchangeCheck(two_sided_stencil, two_sided_stencil_new) || stencil_reset;
    }

    bool CheckStencilReferenceFront(u32 new_value) {
        return ExchangeCheck(front.ref, new_value) || stencil_reset;
    }

    bool CheckStencilReferenceBack(u32 new_value) {
        return ExchangeCheck(back.ref, new_value) || stencil_reset;
    }

    bool CheckStencilWriteMaskFront(u32 new_value) {
        return ExchangeCheck(front.write_mask, new_value) || stencil_reset;
    }

    bool CheckStencilWriteMaskBack(u32 new_value) {
        return ExchangeCheck(back.write_mask, new_value) || stencil_reset;
    }

    bool CheckStencilCompareMaskFront(u32 new_value) {
        return ExchangeCheck(front.compare_mask, new_value) || stencil_reset;
    }

    bool CheckStencilCompareMaskBack(u32 new_value) {
        return ExchangeCheck(back.compare_mask, new_value) || stencil_reset;
    }

    void ClearStencilReset() {
        stencil_reset = false;
    }

    bool TouchLineWidth() const {
        return Exchange(Dirty::LineWidth, false);
    }

    bool TouchCullMode() {
        return Exchange(Dirty::CullMode, false);
    }

    bool TouchStateEnable() {
        return Exchange(Dirty::StateEnable, false);
    }

    bool TouchDepthBoundsTestEnable() {
        return Exchange(Dirty::DepthBoundsEnable, false);
    }

    bool TouchDepthTestEnable() {
        return Exchange(Dirty::DepthTestEnable, false);
    }

    bool TouchDepthWriteEnable() {
        return Exchange(Dirty::DepthWriteEnable, false);
    }

    bool TouchPrimitiveRestartEnable() {
        return Exchange(Dirty::PrimitiveRestartEnable, false);
    }

    bool TouchRasterizerDiscardEnable() {
        return Exchange(Dirty::RasterizerDiscardEnable, false);
    }

    bool TouchDepthBiasEnable() {
        return Exchange(Dirty::DepthBiasEnable, false);
    }

    bool TouchLogicOpEnable() {
        return Exchange(Dirty::LogicOpEnable, false);
    }

    bool TouchDepthClampEnable() {
        return Exchange(Dirty::DepthClampEnable, false);
    }

    bool TouchDepthCompareOp() {
        return Exchange(Dirty::DepthCompareOp, false);
    }

    bool TouchFrontFace() {
        return Exchange(Dirty::FrontFace, false);
    }

    bool TouchStencilOp() {
        return Exchange(Dirty::StencilOp, false);
    }

    bool TouchBlending() {
        return Exchange(Dirty::Blending, false);
    }

    bool TouchBlendEnable() {
        return Exchange(Dirty::BlendEnable, false);
    }

    bool TouchBlendEquations() {
        return Exchange(Dirty::BlendEquations, false);
    }

    bool TouchColorMask() {
        return Exchange(Dirty::ColorMask, false);
    }

    bool TouchStencilTestEnable() {
        return Exchange(Dirty::StencilTestEnable, false);
    }

    bool TouchLogicOp() {
        return Exchange(Dirty::LogicOp, false);
    }

    bool ChangePrimitiveTopology(Maxwell::PrimitiveTopology new_topology) {
        const bool has_changed = current_topology != new_topology;
        current_topology = new_topology;
        return has_changed;
    }

    void SetupTables(Tegra::Control::ChannelState& channel_state);

    void ChangeChannel(Tegra::Control::ChannelState& channel_state);

    void InvalidateState();

private:
    static constexpr auto INVALID_TOPOLOGY = static_cast<Maxwell::PrimitiveTopology>(~0u);

    bool Exchange(std::size_t id, bool new_value) const noexcept {
        const bool is_dirty = (*flags)[id];
        (*flags)[id] = new_value;
        return is_dirty;
    }

    struct StencilProperties {
        u32 ref = 0;
        u32 write_mask = 0;
        u32 compare_mask = 0;
    };

    Tegra::Engines::Maxwell3D::DirtyState::Flags* flags;
    Tegra::Engines::Maxwell3D::DirtyState::Flags default_flags;
    Tegra::Engines::Maxwell3D::DirtyState::Flags invalidation_flags;
    Maxwell::PrimitiveTopology current_topology = INVALID_TOPOLOGY;
    bool two_sided_stencil = false;
    StencilProperties front{};
    StencilProperties back{};
    bool stencil_reset = false;
};

} // namespace Vulkan