summaryrefslogblamecommitdiffstats
path: root/src/tests/video_core/buffer_base.cpp
blob: 1275cca24255ffb9daccbc4189e7ba37b988a2c5 (plain) (tree)
1
2
3
4
5
6
7
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473

                                                               



                        
                                       
















                                                                  


                                                                             












                                                              
                                                                             




                                                       
                                                       









































































































































































































































































































































































































































                                                                                                
































































                                                                             
                                                                                       
                                                                             
                      







                                                         
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <stdexcept>
#include <unordered_map>

#include <catch2/catch_test_macros.hpp>

#include "common/alignment.h"
#include "common/common_types.h"
#include "video_core/buffer_cache/buffer_base.h"

namespace {
using VideoCommon::BufferBase;
using Range = std::pair<u64, u64>;

constexpr u64 PAGE = 4096;
constexpr u64 WORD = 4096 * 64;

constexpr VAddr c = 0x1328914000;

class RasterizerInterface {
public:
    void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) {
        const u64 page_start{addr >> Core::Memory::YUZU_PAGEBITS};
        const u64 page_end{(addr + size + Core::Memory::YUZU_PAGESIZE - 1) >>
                           Core::Memory::YUZU_PAGEBITS};
        for (u64 page = page_start; page < page_end; ++page) {
            int& value = page_table[page];
            value += delta;
            if (value < 0) {
                throw std::logic_error{"negative page"};
            }
            if (value == 0) {
                page_table.erase(page);
            }
        }
    }

    [[nodiscard]] int Count(VAddr addr) const noexcept {
        const auto it = page_table.find(addr >> Core::Memory::YUZU_PAGEBITS);
        return it == page_table.end() ? 0 : it->second;
    }

    [[nodiscard]] unsigned Count() const noexcept {
        unsigned count = 0;
        for (const auto& [index, value] : page_table) {
            count += value;
        }
        return count;
    }

private:
    std::unordered_map<u64, int> page_table;
};
} // Anonymous namespace

TEST_CASE("BufferBase: Small buffer", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    REQUIRE(rasterizer.Count() == 0);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == WORD / PAGE);
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD) == Range{0, 0});

    buffer.MarkRegionAsCpuModified(c + PAGE, 1);
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD) == Range{PAGE * 1, PAGE * 2});
}

TEST_CASE("BufferBase: Large buffer", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 32);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 32);
    buffer.MarkRegionAsCpuModified(c + 4096, WORD * 4);
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD + PAGE * 2) == Range{PAGE, WORD + PAGE * 2});
    REQUIRE(buffer.ModifiedCpuRegion(c + PAGE * 2, PAGE * 6) == Range{PAGE * 2, PAGE * 8});
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD * 32) == Range{PAGE, WORD * 4 + PAGE});
    REQUIRE(buffer.ModifiedCpuRegion(c + WORD * 4, PAGE) == Range{WORD * 4, WORD * 4 + PAGE});
    REQUIRE(buffer.ModifiedCpuRegion(c + WORD * 3 + PAGE * 63, PAGE) ==
            Range{WORD * 3 + PAGE * 63, WORD * 4});

    buffer.MarkRegionAsCpuModified(c + WORD * 5 + PAGE * 6, PAGE);
    buffer.MarkRegionAsCpuModified(c + WORD * 5 + PAGE * 8, PAGE);
    REQUIRE(buffer.ModifiedCpuRegion(c + WORD * 5, WORD) ==
            Range{WORD * 5 + PAGE * 6, WORD * 5 + PAGE * 9});

    buffer.UnmarkRegionAsCpuModified(c + WORD * 5 + PAGE * 8, PAGE);
    REQUIRE(buffer.ModifiedCpuRegion(c + WORD * 5, WORD) ==
            Range{WORD * 5 + PAGE * 6, WORD * 5 + PAGE * 7});

    buffer.MarkRegionAsCpuModified(c + PAGE, WORD * 31 + PAGE * 63);
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD * 32) == Range{PAGE, WORD * 32});

    buffer.UnmarkRegionAsCpuModified(c + PAGE * 4, PAGE);
    buffer.UnmarkRegionAsCpuModified(c + PAGE * 6, PAGE);

    buffer.UnmarkRegionAsCpuModified(c, WORD * 32);
    REQUIRE(buffer.ModifiedCpuRegion(c, WORD * 32) == Range{0, 0});
}

TEST_CASE("BufferBase: Rasterizer counting", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, PAGE * 2);
    REQUIRE(rasterizer.Count() == 0);
    buffer.UnmarkRegionAsCpuModified(c, PAGE);
    REQUIRE(rasterizer.Count() == 1);
    buffer.MarkRegionAsCpuModified(c, PAGE * 2);
    REQUIRE(rasterizer.Count() == 0);
    buffer.UnmarkRegionAsCpuModified(c, PAGE);
    buffer.UnmarkRegionAsCpuModified(c + PAGE, PAGE);
    REQUIRE(rasterizer.Count() == 2);
    buffer.MarkRegionAsCpuModified(c, PAGE * 2);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Basic range", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.MarkRegionAsCpuModified(c, PAGE);
    int num = 0;
    buffer.ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == 0U);
        REQUIRE(size == PAGE);
        ++num;
    });
    REQUIRE(num == 1U);
}

TEST_CASE("BufferBase: Border upload", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 2);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 2);
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c, WORD * 2, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE * 2);
    });
}

TEST_CASE("BufferBase: Border upload range", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 2);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 2);
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c + WORD - PAGE, PAGE * 2, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE * 2);
    });
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c + WORD - PAGE, PAGE, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE);
    });
    buffer.ForEachUploadRange(c + WORD, PAGE, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD);
        REQUIRE(size == PAGE);
    });
}

TEST_CASE("BufferBase: Border upload partial range", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 2);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 2);
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c + WORD - 1, 2, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE * 2);
    });
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c + WORD - 1, 1, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE);
    });
    buffer.ForEachUploadRange(c + WORD + 50, 1, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD);
        REQUIRE(size == PAGE);
    });
}

TEST_CASE("BufferBase: Partial word uploads", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, 0x9d000);
    int num = 0;
    buffer.ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == 0U);
        REQUIRE(size == WORD);
        ++num;
    });
    REQUIRE(num == 1);
    buffer.ForEachUploadRange(c + WORD, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == WORD);
        REQUIRE(size == WORD);
        ++num;
    });
    REQUIRE(num == 2);
    buffer.ForEachUploadRange(c + 0x79000, 0x24000, [&](u64 offset, u64 size) {
        REQUIRE(offset == WORD * 2);
        REQUIRE(size == PAGE * 0x1d);
        ++num;
    });
    REQUIRE(num == 3);
}

TEST_CASE("BufferBase: Partial page upload", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    int num = 0;
    buffer.MarkRegionAsCpuModified(c + PAGE * 2, PAGE);
    buffer.MarkRegionAsCpuModified(c + PAGE * 9, PAGE);
    buffer.ForEachUploadRange(c, PAGE * 3, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 2);
        REQUIRE(size == PAGE);
        ++num;
    });
    REQUIRE(num == 1);
    buffer.ForEachUploadRange(c + PAGE * 7, PAGE * 3, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 9);
        REQUIRE(size == PAGE);
        ++num;
    });
    REQUIRE(num == 2);
}

TEST_CASE("BufferBase: Partial page upload with multiple words on the right") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 8);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 8);
    buffer.MarkRegionAsCpuModified(c + PAGE * 13, WORD * 7);
    int num = 0;
    buffer.ForEachUploadRange(c + PAGE * 10, WORD * 7, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 13);
        REQUIRE(size == WORD * 7 - PAGE * 3);
        ++num;
    });
    REQUIRE(num == 1);
    buffer.ForEachUploadRange(c + PAGE, WORD * 8, [&](u64 offset, u64 size) {
        REQUIRE(offset == WORD * 7 + PAGE * 10);
        REQUIRE(size == PAGE * 3);
        ++num;
    });
    REQUIRE(num == 2);
}

TEST_CASE("BufferBase: Partial page upload with multiple words on the left", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 8);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 8);
    buffer.MarkRegionAsCpuModified(c + PAGE * 13, WORD * 7);
    int num = 0;
    buffer.ForEachUploadRange(c + PAGE * 16, WORD * 7, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 16);
        REQUIRE(size == WORD * 7 - PAGE * 3);
        ++num;
    });
    REQUIRE(num == 1);
    buffer.ForEachUploadRange(c + PAGE, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 13);
        REQUIRE(size == PAGE * 3);
        ++num;
    });
    REQUIRE(num == 2);
}

TEST_CASE("BufferBase: Partial page upload with multiple words in the middle", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 8);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 8);
    buffer.MarkRegionAsCpuModified(c + PAGE * 13, PAGE * 140);
    int num = 0;
    buffer.ForEachUploadRange(c + PAGE * 16, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 16);
        REQUIRE(size == WORD);
        ++num;
    });
    REQUIRE(num == 1);
    buffer.ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) {
        REQUIRE(offset == PAGE * 13);
        REQUIRE(size == PAGE * 3);
        ++num;
    });
    REQUIRE(num == 2);
    buffer.ForEachUploadRange(c, WORD * 8, [&](u64 offset, u64 size) {
        REQUIRE(offset == WORD + PAGE * 16);
        REQUIRE(size == PAGE * 73);
        ++num;
    });
    REQUIRE(num == 3);
}

TEST_CASE("BufferBase: Empty right bits", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 2048);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 2048);
    buffer.MarkRegionAsCpuModified(c + WORD - PAGE, PAGE * 2);
    buffer.ForEachUploadRange(c, WORD * 2048, [](u64 offset, u64 size) {
        REQUIRE(offset == WORD - PAGE);
        REQUIRE(size == PAGE * 2);
    });
}

TEST_CASE("BufferBase: Out of bound ranges 1", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.MarkRegionAsCpuModified(c, PAGE);
    int num = 0;
    buffer.ForEachUploadRange(c - WORD, WORD, [&](u64 offset, u64 size) { ++num; });
    buffer.ForEachUploadRange(c + WORD, WORD, [&](u64 offset, u64 size) { ++num; });
    buffer.ForEachUploadRange(c - PAGE, PAGE, [&](u64 offset, u64 size) { ++num; });
    REQUIRE(num == 0);
    buffer.ForEachUploadRange(c - PAGE, PAGE * 2, [&](u64 offset, u64 size) { ++num; });
    REQUIRE(num == 1);
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Out of bound ranges 2", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, 0x22000);
    REQUIRE_NOTHROW(buffer.UnmarkRegionAsCpuModified(c + 0x22000, PAGE));
    REQUIRE_NOTHROW(buffer.UnmarkRegionAsCpuModified(c + 0x28000, PAGE));
    REQUIRE(rasterizer.Count() == 0);
    REQUIRE_NOTHROW(buffer.UnmarkRegionAsCpuModified(c + 0x21100, PAGE - 0x100));
    REQUIRE(rasterizer.Count() == 1);
    REQUIRE_NOTHROW(buffer.UnmarkRegionAsCpuModified(c - 0x1000, PAGE * 2));
    buffer.UnmarkRegionAsCpuModified(c - 0x3000, PAGE * 2);
    buffer.UnmarkRegionAsCpuModified(c - 0x2000, PAGE * 2);
    REQUIRE(rasterizer.Count() == 2);
}

TEST_CASE("BufferBase: Out of bound ranges 3", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, 0x310720);
    buffer.UnmarkRegionAsCpuModified(c, 0x310720);
    REQUIRE(rasterizer.Count(c) == 1);
    REQUIRE(rasterizer.Count(c + PAGE) == 1);
    REQUIRE(rasterizer.Count(c + WORD) == 1);
    REQUIRE(rasterizer.Count(c + WORD + PAGE) == 1);
}

TEST_CASE("BufferBase: Sparse regions 1", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.MarkRegionAsCpuModified(c + PAGE * 1, PAGE);
    buffer.MarkRegionAsCpuModified(c + PAGE * 3, PAGE * 4);
    buffer.ForEachUploadRange(c, WORD, [i = 0](u64 offset, u64 size) mutable {
        static constexpr std::array<u64, 2> offsets{PAGE, PAGE * 3};
        static constexpr std::array<u64, 2> sizes{PAGE, PAGE * 4};
        REQUIRE(offset == offsets.at(i));
        REQUIRE(size == sizes.at(i));
        ++i;
    });
}

TEST_CASE("BufferBase: Sparse regions 2", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, 0x22000);
    buffer.UnmarkRegionAsCpuModified(c, 0x22000);
    REQUIRE(rasterizer.Count() == 0x22);
    buffer.MarkRegionAsCpuModified(c + PAGE * 0x1B, PAGE);
    buffer.MarkRegionAsCpuModified(c + PAGE * 0x21, PAGE);
    buffer.ForEachUploadRange(c, WORD, [i = 0](u64 offset, u64 size) mutable {
        static constexpr std::array<u64, 2> offsets{PAGE * 0x1B, PAGE * 0x21};
        static constexpr std::array<u64, 2> sizes{PAGE, PAGE};
        REQUIRE(offset == offsets.at(i));
        REQUIRE(size == sizes.at(i));
        ++i;
    });
}

TEST_CASE("BufferBase: Single page modified range", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, PAGE);
    REQUIRE(buffer.IsRegionCpuModified(c, PAGE));
    buffer.UnmarkRegionAsCpuModified(c, PAGE);
    REQUIRE(!buffer.IsRegionCpuModified(c, PAGE));
}

TEST_CASE("BufferBase: Two page modified range", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, PAGE * 2);
    REQUIRE(buffer.IsRegionCpuModified(c, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c, PAGE * 2));
    buffer.UnmarkRegionAsCpuModified(c, PAGE);
    REQUIRE(!buffer.IsRegionCpuModified(c, PAGE));
}

TEST_CASE("BufferBase: Multi word modified ranges", "[video_core]") {
    for (int offset = 0; offset < 4; ++offset) {
        const VAddr address = c + WORD * offset;
        RasterizerInterface rasterizer;
        BufferBase buffer(rasterizer, address, WORD * 4);
        REQUIRE(buffer.IsRegionCpuModified(address, PAGE));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 48, PAGE));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 56, PAGE));

        buffer.UnmarkRegionAsCpuModified(address + PAGE * 32, PAGE);
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE, WORD));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 31, PAGE));
        REQUIRE(!buffer.IsRegionCpuModified(address + PAGE * 32, PAGE));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 33, PAGE));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 31, PAGE * 2));
        REQUIRE(buffer.IsRegionCpuModified(address + PAGE * 32, PAGE * 2));

        buffer.UnmarkRegionAsCpuModified(address + PAGE * 33, PAGE);
        REQUIRE(!buffer.IsRegionCpuModified(address + PAGE * 32, PAGE * 2));
    }
}

TEST_CASE("BufferBase: Single page in large buffer", "[video_core]") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 16);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 16);
    REQUIRE(!buffer.IsRegionCpuModified(c, WORD * 16));

    buffer.MarkRegionAsCpuModified(c + WORD * 12 + PAGE * 8, PAGE);
    REQUIRE(buffer.IsRegionCpuModified(c, WORD * 16));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 10, WORD * 2));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 11, WORD * 2));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 12, WORD * 2));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 12 + PAGE * 4, PAGE * 8));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 12 + PAGE * 6, PAGE * 8));
    REQUIRE(!buffer.IsRegionCpuModified(c + WORD * 12 + PAGE * 6, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 12 + PAGE * 7, PAGE * 2));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 12 + PAGE * 8, PAGE * 2));
}

TEST_CASE("BufferBase: Out of bounds region query") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 16);
    REQUIRE(!buffer.IsRegionCpuModified(c - PAGE, PAGE));
    REQUIRE(!buffer.IsRegionCpuModified(c - PAGE * 2, PAGE));
    REQUIRE(!buffer.IsRegionCpuModified(c + WORD * 16, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + WORD * 16 - PAGE, WORD * 64));
    REQUIRE(!buffer.IsRegionCpuModified(c + WORD * 16, WORD * 64));
}

TEST_CASE("BufferBase: Wrap word regions") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD * 2);
    buffer.UnmarkRegionAsCpuModified(c, WORD * 2);
    buffer.MarkRegionAsCpuModified(c + PAGE * 63, PAGE * 2);
    REQUIRE(buffer.IsRegionCpuModified(c, WORD * 2));
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE * 62, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 63, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 64, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 63, PAGE * 2));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 63, PAGE * 8));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 60, PAGE * 8));

    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE * 127, WORD * 16));
    buffer.MarkRegionAsCpuModified(c + PAGE * 127, PAGE);
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 127, WORD * 16));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 127, PAGE));
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE * 126, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 126, PAGE * 2));
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE * 128, WORD * 16));
}

TEST_CASE("BufferBase: Unaligned page region query") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.MarkRegionAsCpuModified(c + 4000, 1000);
    REQUIRE(buffer.IsRegionCpuModified(c, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + 4000, 1000));
    REQUIRE(buffer.IsRegionCpuModified(c + 4000, 1));
}

TEST_CASE("BufferBase: Cached write") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.CachedCpuWrite(c + PAGE, PAGE);
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.FlushCachedWrites();
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Multiple cached write") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.CachedCpuWrite(c + PAGE, PAGE);
    buffer.CachedCpuWrite(c + PAGE * 3, PAGE);
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE * 3, PAGE));
    buffer.FlushCachedWrites();
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE * 3, PAGE));
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Cached write unmarked") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.CachedCpuWrite(c + PAGE, PAGE);
    buffer.UnmarkRegionAsCpuModified(c + PAGE, PAGE);
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.FlushCachedWrites();
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Cached write iterated") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    buffer.CachedCpuWrite(c + PAGE, PAGE);
    int num = 0;
    buffer.ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) { ++num; });
    REQUIRE(num == 0);
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.FlushCachedWrites();
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}

TEST_CASE("BufferBase: Cached write downloads") {
    RasterizerInterface rasterizer;
    BufferBase buffer(rasterizer, c, WORD);
    buffer.UnmarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 64);
    buffer.CachedCpuWrite(c + PAGE, PAGE);
    REQUIRE(rasterizer.Count() == 63);
    buffer.MarkRegionAsGpuModified(c + PAGE, PAGE);
    int num = 0;
    buffer.ForEachDownloadRangeAndClear(c, WORD, [&](u64 offset, u64 size) { ++num; });
    buffer.ForEachUploadRange(c, WORD, [&](u64 offset, u64 size) { ++num; });
    REQUIRE(num == 1);
    REQUIRE(!buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(!buffer.IsRegionGpuModified(c + PAGE, PAGE));
    buffer.FlushCachedWrites();
    REQUIRE(buffer.IsRegionCpuModified(c + PAGE, PAGE));
    REQUIRE(!buffer.IsRegionGpuModified(c + PAGE, PAGE));
    buffer.MarkRegionAsCpuModified(c, WORD);
    REQUIRE(rasterizer.Count() == 0);
}