Nugget
Loading...
Searching...
No Matches
rectangle-clipping.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// Rectangle clipping suite. GP0(0x60) flat variable-size rectangle.
28// Drawing area = (0,0) .. (1024, 512) by default. Tests:
29// - Rect entirely inside the draw area (R1).
30// - Rect with top-left outside the draw area (R2). Top-left clipping.
31// - Rect that exactly equals the draw area edge.
32// - Rect with negative coordinates after drawing offset.
33
35
36// R1: 4x4 rectangle at (10, 10), GREEN. Fully inside draw area.
37static void rasterDrawRectR1(void) {
38 rasterReset();
39 rasterClearTestRegion(0, 0, 32, 32);
40 rasterFlatRect(RASTER_CMD_GREEN, 10, 10, 4, 4);
41 rasterFlushPrimitive();
42}
43
44// R2: 8x8 rectangle starting at (-2, -2), GREEN. Top-left 4x4 quadrant
45// of the rect is outside the draw area; only the bottom-right 6x6 region
46// remains and should fill VRAM (0,0)..(5,5).
47//
48// Note: GP0(0x60) takes UNSIGNED 16-bit coordinates in the second word
49// (x | y<<16). Negative -2 sign-extends to 0xfffe which would naturally
50// be a huge X. But the drawing-area clipping should clamp this. The
51// observation IS the test - if the GPU treats -2 as 0xfffe and clips
52// everything (no draw), we want to see that in the OBS log.
53//
54// To get a meaningful test of "negative top-left clipping" we instead
55// shift the drawing area so the test rect crosses its top-left edge
56// with VALID 11-bit coordinates. Set draw area to (4, 4)..(end), draw
57// rect at (2, 2) size 6x6. Then VRAM (4,4)..(7,7) should fill and
58// VRAM (2,2)..(3,3) should stay sentinel.
59static void rasterDrawRectR2(void) {
60 rasterReset();
61 rasterClearTestRegion(0, 0, 16, 16);
62 // Shrink draw area to (4, 4)..(16, 16).
63 setDrawingArea(4, 4, 16, 16);
64 rasterFlatRect(RASTER_CMD_GREEN, 2, 2, 6, 6);
65 rasterFlushPrimitive();
66}
67
68) // CESTER_BODY
69
70// --------------------------------------------------------------------------
71// Rect R1: fully inside draw area
72// --------------------------------------------------------------------------
73
74CESTER_TEST(rectR1_top_left, gpu_raster_phase1,
75 rasterDrawRectR1();
77)
78
79CESTER_TEST(rectR1_bottom_right_interior, gpu_raster_phase1,
80 rasterDrawRectR1();
82)
83
84CESTER_TEST(rectR1_right_edge_excluded, gpu_raster_phase1,
85 rasterDrawRectR1();
87)
88
89CESTER_TEST(rectR1_bottom_edge_excluded, gpu_raster_phase1,
90 rasterDrawRectR1();
92)
93
94CESTER_TEST(rectR1_before_left_edge, gpu_raster_phase1,
95 rasterDrawRectR1();
97)
98
99// --------------------------------------------------------------------------
100// Rect R2: top-left clipped by drawing area (4,4)
101// --------------------------------------------------------------------------
102//
103// EXPECT_RECT_R2_PIXEL_0_0 in raster-expected.h was written assuming the
104// default draw area (0,0). Here we use a shifted draw area (4,4) and
105// expect the rect at (2,2) size 6x6 to fill VRAM (4,4)..(7,7) only.
106// The pixels we assert against differ from the placeholders; redirect
107// to the actual coordinates this test exercises.
108
109CESTER_TEST(rectR2_clipped_pixel_4_4_corner, gpu_raster_phase1,
110 rasterDrawRectR2();
111 // (4,4) is inside both the rect and the shrunk draw area: should fill.
113)
114
115CESTER_TEST(rectR2_clipped_pixel_7_7_bottom_right, gpu_raster_phase1,
116 rasterDrawRectR2();
117 // (7,7) is the last interior pixel given the rect (2,2)..(8,8)
118 // exclusive right/bottom under top-left rule.
120)
121
122CESTER_TEST(rectR2_clipped_pixel_2_2_top_left_outside_draw_area,
123 gpu_raster_phase1,
124 rasterDrawRectR2();
125 // (2,2) is inside the rect but OUTSIDE the draw area (which starts
126 // at 4). Should be clipped -> sentinel.
128)
129
130CESTER_TEST(rectR2_clipped_pixel_3_3_just_inside_rect_outside_draw,
131 gpu_raster_phase1,
132 rasterDrawRectR2();
133 // (3,3) is inside the rect but just outside the shifted draw area.
134 // Should be sentinel.
136)
137
138CESTER_TEST(rectR2_clipped_pixel_8_8_outside_rect, gpu_raster_phase1,
139 rasterDrawRectR2();
140 // (8,8) is past the rect's exclusive right/bottom edge.
142)
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
#define EXPECT_RECT_R1_PIXEL_10_14
Definition raster-expected.h:216
#define EXPECT_RECT_R1_PIXEL_9_10
Definition raster-expected.h:217
#define EXPECT_RECT_R1_PIXEL_10_10
Definition raster-expected.h:213
#define EXPECT_RECT_R1_PIXEL_13_13
Definition raster-expected.h:214
#define EXPECT_RECT_R1_PIXEL_14_10
Definition raster-expected.h:215
#define RASTER_VRAM_GREEN
Definition raster-helpers.h:126
#define ASSERT_PIXEL_UNTOUCHED(x_, y_)
Definition raster-helpers.h:485
#define RASTER_CMD_GREEN
Definition raster-helpers.h:125
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:472