Nugget
Loading...
Searching...
No Matches
mask-bit.c
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2026 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27// Mask-bit suite. GP0(E6) sets two bits of GPU state:
28// bit 0: set-mask - drawn pixels have VRAM bit 15 forced to 1
29// bit 1: check-mask - skip writes to VRAM pixels with bit 15 == 1
30//
31// Tests:
32// 1. set-mask only: drawn pixels should be 0x801f (RED + mask bit).
33// 2. set-mask then check-mask overlay: mask-set pixels preserved,
34// non-mask pixels overwritten.
35//
36// SENTINEL OVERRIDE: the default RASTER_SENTINEL (0xDEAD) has bit 15
37// set. Check-mask would skip writes to pixels containing the sentinel,
38// which conflates "rasterizer chose to skip" with "sentinel collided".
39// Per the documented mitigation in raster-helpers.h, this suite uses a
40// local mask-clear sentinel.
41
42#define MASK_SUITE_SENTINEL 0x5555u /* R=21,G=10,B=21,mask=0 - no bit 15 */
43
45
46// Set-mask + draw RED triangle (same as triangle A but with E6=0x01).
47static void rasterDrawMaskSet(void) {
48 rasterReset();
49 // Use the local mask-clear sentinel so absence-tests work even
50 // though check-mask is not active in this draw - keeps the suite
51 // internally consistent.
52 rasterFillRect(0, 0, 16, 16, MASK_SUITE_SENTINEL);
53 // GP0(E6): set bit 0 (set-mask). Pixels drawn after this command
54 // get VRAM bit 15 forced to 1.
55 sendGPUData(0xe6000001u);
56 rasterFlatTri(RASTER_CMD_RED, 0, 0, 4, 0, 0, 4);
57 rasterFlushPrimitive();
58}
59
60// Set-mask first triangle, then check-mask second overlapping triangle.
61// Geometry: tri A (0,0)(4,0)(0,4) RED with set-mask, then tri (4,0)(8,0)(4,4)
62// GREEN with check-mask. They overlap on the shared edge from (4,0)
63// down; mask-set RED pixels should preserve, non-overlap GREEN pixels
64// should fill.
65static void rasterDrawMaskCheck(void) {
66 rasterReset();
67 rasterFillRect(0, 0, 16, 16, MASK_SUITE_SENTINEL);
68
69 // First pass: RED tri with set-mask, no check.
70 sendGPUData(0xe6000001u);
71 rasterFlatTri(RASTER_CMD_RED, 0, 0, 4, 0, 0, 4);
72 rasterFlushPrimitive();
73
74 // Second pass: GREEN tri with check-mask, no set.
75 sendGPUData(0xe6000002u);
76 rasterFlatTri(RASTER_CMD_GREEN, 4, 0, 8, 0, 4, 4);
77 rasterFlushPrimitive();
78
79 // Restore mask state to default for subsequent tests.
80 sendGPUData(0xe6000000u);
81}
82
83) // CESTER_BODY
84
85// --------------------------------------------------------------------------
86// Set-mask: drawn pixels have bit 15 forced
87// --------------------------------------------------------------------------
88
89CESTER_TEST(maskSet_origin_has_mask_bit, gpu_raster_phase2,
90 rasterDrawMaskSet();
92)
93
94CESTER_TEST(maskSet_interior_has_mask_bit, gpu_raster_phase2,
95 rasterDrawMaskSet();
97)
98
99CESTER_TEST(maskSet_right_edge_excluded, gpu_raster_phase2,
100 rasterDrawMaskSet();
101 // Note: this suite uses MASK_SUITE_SENTINEL (0x5555) not RASTER_SENTINEL.
102 // The macro in raster-expected-phase2.h still references the global
103 // RASTER_SENTINEL for absence cases; we override locally here.
104 ASSERT_PIXEL_EQ((unsigned)MASK_SUITE_SENTINEL, 4, 0);
105)
106
107// --------------------------------------------------------------------------
108// Check-mask: pixels already masked are preserved
109// --------------------------------------------------------------------------
110
111CESTER_TEST(maskCheck_preserves_red_at_overlap_1_0, gpu_raster_phase2,
112 rasterDrawMaskCheck();
113 // (1, 0) is in both triangles. First pass (RED + set-mask) writes
114 // 0x801f. Second pass (GREEN + check-mask) should see bit 15 set
115 // and skip, preserving 0x801f.
117)
118
119CESTER_TEST(maskCheck_fills_green_in_non_overlap_5_0, gpu_raster_phase2,
120 rasterDrawMaskCheck();
121 // (5, 0) is in GREEN tri only and the local sentinel 0x5555 has bit
122 // 15 clear, so check-mask should allow the write. Expect GREEN.
124)
CESTER_BODY(static int s_got40;static int s_got80;static uint32_t s_cause;static uint32_t s_epc;static uint32_t s_from;static uint32_t *s_resume;static uint32_t *s_regs;static uint32_t(*s_customhandler)()=NULL;static uint32_t s_oldIMASK;static uint32_t s_oldDPCR;static uint32_t s_oldDICR;uint32_t handler(uint32_t *regs, uint32_t from) { if(from==0x40) s_got40=1;if(from==0x80) s_got80=1;uint32_t cause;uint32_t epc;s_from=from;asm("mfc0 %0, $13\nnop\nmfc0 %1, $14\nnop" :"=r"(cause), "=r"(epc));s_cause=cause;s_epc=epc;if(s_customhandler) { return s_customhandler();} else { return s_resume ?((uint32_t) s_resume) :(epc+4);} } void installExceptionHandlers(uint32_t(*handler)(uint32_t *regs, uint32_t from));void uninstallExceptionHandlers();uint32_t branchbranch1();uint32_t branchbranch2();uint32_t jumpjump1();uint32_t jumpjump2();uint32_t cpu_LWR_LWL_half(uint32_t buff[], uint32_t initial);uint32_t cpu_LWR_LWL_nodelay(uint32_t buff[], uint32_t initial);uint32_t cpu_LWR_LWL_delayed(uint32_t buff[], uint32_t initial);uint32_t cpu_LWR_LWL_load_different(uint32_t buff[], uint32_t initial);uint32_t cpu_LW_LWR(uint32_t buff[], uint32_t initial);uint32_t cpu_delayed_load(uint32_t buff[], uint32_t override);uint32_t cpu_delayed_load_cancelled(uint32_t buff[], uint32_t override);uint64_t cpu_delayed_load_load(uint32_t buff[], uint32_t override);uint32_t linkandload();uint32_t lwandlink();uint32_t nolink();static int s_interruptsWereEnabled;) CESTER_BEFORE_EACH(cpu_tests
CESTER_TEST(cpu_cop0_basic_write_bp, cpu_tests, uint32_t expectedEPC;uint32_t t;volatile uint32_t *ptr=(volatile uint32_t *) 0x58; *ptr=1;__asm__ volatile("" " lui %0, 0b1100101010000000\n" " mtc0 %0, $7\n" " li %0, 0x58\n" " mtc0 %0, $5\n" " li %0, 0xfffffff0\n" " mtc0 %0, $9\n" :"=r"(t));cester_assert_uint_eq(1, *ptr);__asm__ volatile("la %0, 1f\n1:\nsw $0, 0x58($0)" :"=r"(expectedEPC));__asm__ volatile("mtc0 $0, $7\n");cester_assert_uint_eq(0, *ptr);cester_assert_uint_eq(1, s_got40);cester_assert_uint_eq(0, s_got80);cester_assert_uint_eq(0x40, s_from);cester_assert_uint_eq(expectedEPC, s_epc);) CESTER_TEST(cpu_cop0_kseg_write_bp
sendGPUData(0xe1000000)
#define MASK_SUITE_SENTINEL
Definition mask-bit.c:42
#define EXPECT_MASK_CHECK_PIXEL_5_0_filled
Definition raster-expected-phase2.h:115
#define EXPECT_MASK_SET_PIXEL_0_0
Definition raster-expected-phase2.h:101
#define EXPECT_MASK_SET_PIXEL_2_1
Definition raster-expected-phase2.h:102
#define EXPECT_MASK_CHECK_PIXEL_1_0_preserved
Definition raster-expected-phase2.h:114
#define RASTER_CMD_RED
Definition raster-helpers.h:123
#define RASTER_CMD_GREEN
Definition raster-helpers.h:125
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:472