Nugget
Loading...
Searching...
No Matches
draw-area.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// Drawing-area + drawing-offset interactions. GP0(E3) sets the area
28// upper-left, GP0(E4) the area lower-right (inclusive). GP0(E5) sets
29// the offset added to every primitive vertex.
30//
31// Documents:
32// - Offset (50, 50): primitive at logical (0,0) draws at VRAM (50,50).
33// - Offset combined with draw-area clip.
34// - Primitive at negative logical coords after offset.
35
37
38// Offset-A: Triangle A geometry (0,0),(4,0),(0,4) with drawing offset
39// (50, 50). Expected to fill exactly the same pixel set as Triangle A
40// from triangle-edges.c, shifted by (50, 50).
41static void rasterDrawOffsetA(void) {
42 rasterReset();
43 rasterClearTestRegion(48, 48, 16, 16);
44 // Also clear the original origin so we can assert it stays sentinel.
45 rasterClearTestRegion(0, 0, 8, 8);
46 setDrawingOffset(50, 50);
47 rasterFlatTri(RASTER_CMD_RED, 0, 0, 4, 0, 0, 4);
48 rasterFlushPrimitive();
49}
50
51// Offset-B: Drawing offset that pushes geometry partially OFF the draw
52// area. Logical (-3, -3), (5, -3), (-3, 5) with offset (3, 3) gives
53// destination triangle (0,0), (8,0), (0,8). The drawing-area clip is
54// (0, 0)..(1024, 512) so nothing should be cut. This documents that
55// offset itself does not introduce clipping when destinations are
56// non-negative.
57static void rasterDrawOffsetB(void) {
58 rasterReset();
59 rasterClearTestRegion(0, 0, 16, 16);
60 setDrawingOffset(3, 3);
61 rasterFlatTri(RASTER_CMD_GREEN, -3, -3, 5, -3, -3, 5);
62 rasterFlushPrimitive();
63}
64
65// Offset-C: Drawing offset PLUS shrunk draw area. The primitive lands
66// inside VRAM but partially outside the active draw area. Should clip
67// to the area, not silently extend past it.
68// Draw area: (10, 10)..(20, 20)
69// Offset: (10, 10)
70// Primitive: triangle (0,0),(8,0),(0,8) -> VRAM (10,10),(18,10),(10,18)
71// All of the destination is inside the draw area, so the triangle
72// should render fully. This is a sanity check that offset + draw area
73// compose additively.
74static void rasterDrawOffsetC(void) {
75 rasterReset();
76 rasterClearTestRegion(0, 0, 32, 32);
77 setDrawingArea(10, 10, 20, 20);
78 setDrawingOffset(10, 10);
79 rasterFlatTri(RASTER_CMD_BLUE, 0, 0, 8, 0, 0, 8);
80 rasterFlushPrimitive();
81}
82
83) // CESTER_BODY
84
85// --------------------------------------------------------------------------
86// Offset A: triangle shifted by (50, 50)
87// --------------------------------------------------------------------------
88
89CESTER_TEST(offsetA_origin_top_left_at_50_50, gpu_raster_phase1,
90 rasterDrawOffsetA();
92)
93
94CESTER_TEST(offsetA_origin_top_inner_53_50, gpu_raster_phase1,
95 rasterDrawOffsetA();
97)
98
99CESTER_TEST(offsetA_right_edge_54_50_excluded, gpu_raster_phase1,
100 rasterDrawOffsetA();
102)
103
104CESTER_TEST(offsetA_bottom_edge_50_54_excluded, gpu_raster_phase1,
105 rasterDrawOffsetA();
107)
108
109CESTER_TEST(offsetA_origin_0_0_untouched, gpu_raster_phase1,
110 rasterDrawOffsetA();
111 // With offset 50,50, the logical (0,0) corner of the triangle should
112 // NOT write VRAM (0,0). Sentinel confirms the offset took effect.
114)
115
116// --------------------------------------------------------------------------
117// Offset B: negative logical coordinates compensated by positive offset
118// --------------------------------------------------------------------------
119
120CESTER_TEST(offsetB_negative_compensated_pixel_0_0, gpu_raster_phase1,
121 rasterDrawOffsetB();
122 // Logical (-3, -3) + offset (3, 3) = VRAM (0, 0). The triangle
123 // (-3,-3)(5,-3)(-3,5) -> (0,0)(8,0)(0,8). Top-left rule says (0,0)
124 // is drawn (inclusive corner).
126)
127
128CESTER_TEST(offsetB_negative_compensated_pixel_7_0, gpu_raster_phase1,
129 rasterDrawOffsetB();
130 // Last drawn pixel of the top edge under top-left rule.
132)
133
134CESTER_TEST(offsetB_negative_compensated_pixel_8_0, gpu_raster_phase1,
135 rasterDrawOffsetB();
136 // Right-vertex corner; top-left rule excludes.
138)
139
140CESTER_TEST(offsetB_negative_compensated_pixel_0_8, gpu_raster_phase1,
141 rasterDrawOffsetB();
142 // Bottom-vertex; bottom edge excluded.
144)
145
146// --------------------------------------------------------------------------
147// Offset C: offset + shifted draw area compose additively
148// --------------------------------------------------------------------------
149
150CESTER_TEST(offsetC_compose_pixel_10_10_inside_area, gpu_raster_phase1,
151 rasterDrawOffsetC();
153)
154
155CESTER_TEST(offsetC_compose_pixel_17_10_top_inner, gpu_raster_phase1,
156 rasterDrawOffsetC();
157 // Logical (7,0) + offset 10 = VRAM (17,10). Inside draw area (ends
158 // at 20). Under top-left rule for triangle (0,0)(8,0)(0,8), pixel
159 // x=7 y=0 is the last included on the top edge.
161)
162
163CESTER_TEST(offsetC_compose_pixel_18_10_right_vertex, gpu_raster_phase1,
164 rasterDrawOffsetC();
165 // Right-vertex corner excluded under top-left rule.
167)
168
169CESTER_TEST(offsetC_compose_pixel_9_9_before_draw_area, gpu_raster_phase1,
170 rasterDrawOffsetC();
171 // Just outside the shifted draw area (which starts at 10).
173)
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_OFFSET_PIXEL_50_50
Definition raster-expected.h:235
#define EXPECT_OFFSET_PIXEL_54_50
Definition raster-expected.h:237
#define EXPECT_OFFSET_PIXEL_53_50
Definition raster-expected.h:236
#define EXPECT_OFFSET_PIXEL_0_0
Definition raster-expected.h:239
#define EXPECT_OFFSET_PIXEL_50_54
Definition raster-expected.h:238
#define RASTER_VRAM_GREEN
Definition raster-helpers.h:126
#define RASTER_VRAM_BLUE
Definition raster-helpers.h:128
#define ASSERT_PIXEL_UNTOUCHED(x_, y_)
Definition raster-helpers.h:485
#define RASTER_CMD_BLUE
Definition raster-helpers.h:127
#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