Nugget
Loading...
Searching...
No Matches
line-endpoints.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// Line endpoints suite. GP0(0x40) flat untextured line - two vertex
28// words after the command. Documents:
29// - Whether start AND end pixels are both drawn (inclusive on both ends).
30// - Bresenham octant symmetry: same shape across +X+Y, -X+Y, etc.
31// - Zero-length lines (start == end): does anything draw?
32// - Shallow vs steep lines (major axis selection).
33// - Diagonal at exactly 45 deg vs slope-0 / slope-infinity edge cases.
34
36
37// Horizontal 1px line at y=10 from x=5 to x=10.
38static void rasterDrawLineH(void) {
39 rasterReset();
40 rasterClearTestRegion(0, 0, 16, 16);
41 rasterFlatLine(RASTER_CMD_GREEN, 5, 10, 10, 10);
42 rasterFlushPrimitive();
43}
44
45// Vertical 1px line at x=10 from y=5 to y=10.
46static void rasterDrawLineV(void) {
47 rasterReset();
48 rasterClearTestRegion(0, 0, 16, 16);
49 rasterFlatLine(RASTER_CMD_RED, 10, 5, 10, 10);
50 rasterFlushPrimitive();
51}
52
53// Diagonal +45 deg from (5,5) to (10,10).
54static void rasterDrawLineD45(void) {
55 rasterReset();
56 rasterClearTestRegion(0, 0, 16, 16);
57 rasterFlatLine(RASTER_CMD_BLUE, 5, 5, 10, 10);
58 rasterFlushPrimitive();
59}
60
61// Diagonal -45 deg from (5,10) to (10,5).
62static void rasterDrawLineDN45(void) {
63 rasterReset();
64 rasterClearTestRegion(0, 0, 16, 16);
65 rasterFlatLine(RASTER_CMD_WHITE, 5, 10, 10, 5);
66 rasterFlushPrimitive();
67}
68
69// Zero-length line: same start and end point.
70static void rasterDrawLineZero(void) {
71 rasterReset();
72 rasterClearTestRegion(16, 16, 16, 8);
73 rasterFlatLine(RASTER_CMD_RED, 20, 20, 20, 20);
74 rasterFlushPrimitive();
75}
76
77// Shallow slope line (more horizontal than vertical): (0,0) to (10,3).
78// Major axis is X. Tests Bresenham major-axis selection.
79static void rasterDrawLineShallow(void) {
80 rasterReset();
81 rasterClearTestRegion(0, 0, 16, 8);
82 rasterFlatLine(RASTER_CMD_GREEN, 0, 0, 10, 3);
83 rasterFlushPrimitive();
84}
85
86) // CESTER_BODY
87
88// --------------------------------------------------------------------------
89// Horizontal line (5,10) - (10,10)
90// --------------------------------------------------------------------------
91
92CESTER_TEST(lineH_before_start_pixel_4_10, gpu_raster_phase2,
93 rasterDrawLineH();
95)
96
97CESTER_TEST(lineH_start_pixel_5_10, gpu_raster_phase2,
98 rasterDrawLineH();
100)
101
102CESTER_TEST(lineH_interior_pixel_7_10, gpu_raster_phase2,
103 rasterDrawLineH();
105)
106
107CESTER_TEST(lineH_end_pixel_10_10_inclusive, gpu_raster_phase2,
108 rasterDrawLineH();
110)
111
112CESTER_TEST(lineH_past_end_pixel_11_10, gpu_raster_phase2,
113 rasterDrawLineH();
115)
116
117CESTER_TEST(lineH_below_line_pixel_5_11, gpu_raster_phase2,
118 rasterDrawLineH();
120)
121
122// --------------------------------------------------------------------------
123// Vertical line (10,5) - (10,10)
124// --------------------------------------------------------------------------
125
126CESTER_TEST(lineV_before_start_pixel_10_4, gpu_raster_phase2,
127 rasterDrawLineV();
129)
130
131CESTER_TEST(lineV_start_pixel_10_5, gpu_raster_phase2,
132 rasterDrawLineV();
134)
135
136CESTER_TEST(lineV_interior_pixel_10_7, gpu_raster_phase2,
137 rasterDrawLineV();
139)
140
141CESTER_TEST(lineV_end_pixel_10_10_inclusive, gpu_raster_phase2,
142 rasterDrawLineV();
144)
145
146CESTER_TEST(lineV_past_end_pixel_10_11, gpu_raster_phase2,
147 rasterDrawLineV();
149)
150
151// --------------------------------------------------------------------------
152// Diagonal +45 deg (5,5) - (10,10)
153// --------------------------------------------------------------------------
154
155CESTER_TEST(lineD45_start_pixel_5_5, gpu_raster_phase2,
156 rasterDrawLineD45();
158)
159
160CESTER_TEST(lineD45_interior_pixel_7_7, gpu_raster_phase2,
161 rasterDrawLineD45();
163)
164
165CESTER_TEST(lineD45_end_pixel_10_10, gpu_raster_phase2,
166 rasterDrawLineD45();
168)
169
170CESTER_TEST(lineD45_off_diag_pixel_5_6, gpu_raster_phase2,
171 rasterDrawLineD45();
173)
174
175CESTER_TEST(lineD45_off_diag_pixel_6_5, gpu_raster_phase2,
176 rasterDrawLineD45();
178)
179
180// --------------------------------------------------------------------------
181// Diagonal -45 deg (5,10) - (10,5)
182// --------------------------------------------------------------------------
183
184CESTER_TEST(lineDN45_start_pixel_5_10, gpu_raster_phase2,
185 rasterDrawLineDN45();
187)
188
189CESTER_TEST(lineDN45_interior_pixel_7_8, gpu_raster_phase2,
190 rasterDrawLineDN45();
192)
193
194CESTER_TEST(lineDN45_end_pixel_10_5, gpu_raster_phase2,
195 rasterDrawLineDN45();
197)
198
199// --------------------------------------------------------------------------
200// Zero-length line (20,20) - (20,20)
201// --------------------------------------------------------------------------
202
203CESTER_TEST(lineZero_start_pixel_20_20, gpu_raster_phase2,
204 rasterDrawLineZero();
206)
207
208CESTER_TEST(lineZero_neighbor_pixel_21_20, gpu_raster_phase2,
209 rasterDrawLineZero();
211)
212
213// --------------------------------------------------------------------------
214// Shallow line (0,0) - (10,3): major axis is X
215// --------------------------------------------------------------------------
216
217CESTER_TEST(lineShallow_start_pixel_0_0, gpu_raster_phase2,
218 rasterDrawLineShallow();
220)
221
222CESTER_TEST(lineShallow_midpoint_pixel_5_2, gpu_raster_phase2,
223 rasterDrawLineShallow();
225)
226
227CESTER_TEST(lineShallow_end_pixel_10_3, gpu_raster_phase2,
228 rasterDrawLineShallow();
230)
231
232CESTER_TEST(lineShallow_x_2_y_0_bresenham_choice, gpu_raster_phase2,
233 rasterDrawLineShallow();
234 // At x=2, the Bresenham accumulator should have stepped y to 1.
235 // Pixel (2, 0) on the original major-axis row should be empty if
236 // the line steps y up before x=2. Verifies major-axis stepping.
238)
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_LINE_ZERO_PIXEL_20_20
Definition raster-expected-phase2.h:77
#define EXPECT_LINE_H_PIXEL_5_11
Definition raster-expected-phase2.h:53
#define EXPECT_LINE_D45_PIXEL_7_7
Definition raster-expected-phase2.h:65
#define EXPECT_LINE_H_PIXEL_4_10
Definition raster-expected-phase2.h:48
#define EXPECT_LINE_ZERO_PIXEL_21_20
Definition raster-expected-phase2.h:78
#define EXPECT_LINE_V_PIXEL_10_5
Definition raster-expected-phase2.h:57
#define EXPECT_LINE_H_PIXEL_11_10
Definition raster-expected-phase2.h:52
#define EXPECT_LINE_SHALLOW_PIXEL_2_0
Definition raster-expected-phase2.h:88
#define EXPECT_LINE_V_PIXEL_10_4
Definition raster-expected-phase2.h:56
#define EXPECT_LINE_SHALLOW_PIXEL_5_2
Definition raster-expected-phase2.h:86
#define EXPECT_LINE_V_PIXEL_10_11
Definition raster-expected-phase2.h:60
#define EXPECT_LINE_V_PIXEL_10_10
Definition raster-expected-phase2.h:59
#define EXPECT_LINE_DN45_PIXEL_10_5
Definition raster-expected-phase2.h:73
#define EXPECT_LINE_H_PIXEL_7_10
Definition raster-expected-phase2.h:50
#define EXPECT_LINE_D45_PIXEL_6_5
Definition raster-expected-phase2.h:68
#define EXPECT_LINE_H_PIXEL_10_10
Definition raster-expected-phase2.h:51
#define EXPECT_LINE_SHALLOW_PIXEL_0_0
Definition raster-expected-phase2.h:85
#define EXPECT_LINE_D45_PIXEL_5_5
Definition raster-expected-phase2.h:64
#define EXPECT_LINE_H_PIXEL_5_10
Definition raster-expected-phase2.h:49
#define EXPECT_LINE_D45_PIXEL_5_6
Definition raster-expected-phase2.h:67
#define EXPECT_LINE_DN45_PIXEL_5_10
Definition raster-expected-phase2.h:71
#define EXPECT_LINE_SHALLOW_PIXEL_10_3
Definition raster-expected-phase2.h:87
#define EXPECT_LINE_V_PIXEL_10_7
Definition raster-expected-phase2.h:58
#define EXPECT_LINE_DN45_PIXEL_7_8
Definition raster-expected-phase2.h:72
#define EXPECT_LINE_D45_PIXEL_10_10
Definition raster-expected-phase2.h:66
#define RASTER_CMD_BLUE
Definition raster-helpers.h:127
#define RASTER_CMD_RED
Definition raster-helpers.h:123
#define RASTER_CMD_WHITE
Definition raster-helpers.h:129
#define RASTER_CMD_GREEN
Definition raster-helpers.h:125
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:472