Nugget
Loading...
Searching...
No Matches
tex-rect.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// Textured rect probes at 8/15-bit. Naming:
28// TR8_<test> 8-bit CLUT textured rect (GP0 0x64)
29// TR15_<test> 15-bit direct textured rect
30// TR_SEMI_* semi-trans variant (GP0 0x66)
31// TR_MASK_* set-mask / check-mask E6 interaction
32
34
35// Basic 8-bit textured rect: 8x4 at (0, 0), UV (0, 0). Texel u maps
36// to CLUT8[u].
37static void drawTR8(int16_t w, int16_t h, uint8_t u, uint8_t v) {
38 rasterReset();
39 rasterClearTestRegion(0, 0, 32, 16);
40 setTexpage(TEX8_TX, TEX8_TY, 1);
41 setTextureWindow(0, 0, 0, 0);
42 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, u, v, w, h, CLUT8_FIELD);
43 rasterFlushPrimitive();
44}
45
46// Basic 15-bit direct textured rect.
47static void drawTR15(int16_t w, int16_t h, uint8_t u, uint8_t v) {
48 rasterReset();
49 rasterClearTestRegion(0, 0, 32, 16);
50 setTexpage(TEX15_TX, TEX15_TY, 2);
51 setTextureWindow(0, 0, 0, 0);
52 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, u, v, w, h, CLUT15_FIELD);
53 rasterFlushPrimitive();
54}
55
56// 8-bit semi-trans rect over a red-filled background. Texture uses
57// unmasked CLUT - per phase-8 (polygon) finding bit-15=0 should mean
58// no blend. Phase-13 verifies whether textured RECTS honor the same
59// gate semantics as polygons. Note: textured-rect commands carry NO
60// embedded tpage word, so E1 (set via setTexpageAbr) carries both
61// the texture page AND the ABR mode for this draw.
62static void drawTR8Semi(uint8_t abr) {
63 rasterReset();
64 rasterFillRect(0, 0, 32, 16, RASTER_VRAM_RED);
65 setTexpageAbr(TEX8_TX, TEX8_TY, 1, abr);
66 setTextureWindow(0, 0, 0, 0);
67 rasterTexRectSemi(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT8_FIELD);
68 rasterFlushPrimitive();
69 /* Restore default E1 for subsequent tests. */
70 setTexpage(0, 0, 0);
71}
72
73// 15-bit semi-trans rect with the masked-15-bit fixture installed.
74// Bit-15 = 1 on every texel. Per phase-12 polygon finding the gate
75// fires and ABR blend applies with bit-15 preserved into VRAM.
76// Phase-13 verifies that rule for the rect path.
77static void drawTR15SemiMasked(uint8_t abr) {
78 rasterReset();
79 rasterFillRect(0, 0, 32, 16, RASTER_VRAM_RED);
80 uploadTex15Masked();
81 setTexpageAbr(TEX15_TX, TEX15_TY, 2, abr);
82 setTextureWindow(0, 0, 0, 0);
83 rasterTexRectSemi(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT15_FIELD);
84 rasterFlushPrimitive();
85 setTexpage(0, 0, 0);
86 restoreTex15Standard();
87}
88
89// 8-bit textured rect with E6 set-mask. Output should carry bit-15.
90static void drawTR8SetMask(void) {
91 rasterReset();
92 rasterClearTestRegion(0, 0, 32, 16);
93 setTexpage(TEX8_TX, TEX8_TY, 1);
94 setTextureWindow(0, 0, 0, 0);
95 rasterSetMaskCtrl(1, 0);
96 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT8_FIELD);
97 rasterFlushPrimitive();
98 rasterSetMaskCtrl(0, 0);
99}
100
101// 15-bit textured rect with E6 check-mask. Pre-fill has bit-15 set
102// so writes are skipped; the pre-fill survives.
103static void drawTR15CheckMask(void) {
104 rasterReset();
105 uint16_t bg = (uint16_t)(rasterVram555(8, 0, 0) | 0x8000u);
106 rasterFillRect(0, 0, 32, 16, bg);
107 setTexpage(TEX15_TX, TEX15_TY, 2);
108 setTextureWindow(0, 0, 0, 0);
109 rasterSetMaskCtrl(0, 1);
110 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT15_FIELD);
111 rasterFlushPrimitive();
112 rasterSetMaskCtrl(0, 0);
113}
114
115) // CESTER_BODY
116
117// ============================================================================
118// TR8: basic 8-bit textured rect. Texel(u, v) = u & 0xff -> CLUT8[u].
119// ============================================================================
120
121CESTER_TEST(tr8_basic_8x4_0_0, gpu_raster_phase13,
122 drawTR8(8, 4, 0, 0);
123 /* Pixel (0,0) samples texel u=0 -> CLUT8[0]. */
124 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
125)
126CESTER_TEST(tr8_basic_8x4_7_0, gpu_raster_phase13,
127 drawTR8(8, 4, 0, 0);
128 /* Last column inside rect (x=7) samples texel u=7. */
129 ASSERT_PIXEL_EQ(expectedClut8Color(7), 7, 0);
130)
131CESTER_TEST(tr8_basic_8x4_just_past_8, gpu_raster_phase13,
132 drawTR8(8, 4, 0, 0);
133 /* x=8 is the right boundary - INCLUSIVE for rects per phase-5
134 finding (untextured/textured rects include their right and
135 bottom edges, unlike triangles). */
137)
138CESTER_TEST(tr8_basic_8x4_bottom_row, gpu_raster_phase13,
139 drawTR8(8, 4, 0, 0);
140 /* y=3 should be drawn (last row of 4-tall rect). */
141 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 3);
142)
143CESTER_TEST(tr8_basic_8x4_past_bottom, gpu_raster_phase13,
144 drawTR8(8, 4, 0, 0);
146)
147
148// 1x1 sprite - smallest case.
149CESTER_TEST(tr8_1x1_pixel, gpu_raster_phase13,
150 drawTR8(1, 1, 5, 0);
151 /* Single pixel at (0,0) samples texel u=5. */
152 ASSERT_PIXEL_EQ(expectedClut8Color(5), 0, 0);
153)
154CESTER_TEST(tr8_1x1_neighbor_right, gpu_raster_phase13,
155 drawTR8(1, 1, 5, 0);
157)
158CESTER_TEST(tr8_1x1_neighbor_below, gpu_raster_phase13,
159 drawTR8(1, 1, 5, 0);
161)
162
163// 1xN vertical strip.
164CESTER_TEST(tr8_1x4_top, gpu_raster_phase13,
165 drawTR8(1, 4, 3, 0);
166 /* All four pixels at x=0 sample texel u=3 (V doesn't change u in
167 our fixture). */
168 ASSERT_PIXEL_EQ(expectedClut8Color(3), 0, 0);
169)
170CESTER_TEST(tr8_1x4_bottom, gpu_raster_phase13,
171 drawTR8(1, 4, 3, 0);
172 ASSERT_PIXEL_EQ(expectedClut8Color(3), 0, 3);
173)
174
175// Nx1 horizontal strip.
176CESTER_TEST(tr8_4x1_left, gpu_raster_phase13,
177 drawTR8(4, 1, 0, 0);
178 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
179)
180CESTER_TEST(tr8_4x1_right, gpu_raster_phase13,
181 drawTR8(4, 1, 0, 0);
182 ASSERT_PIXEL_EQ(expectedClut8Color(3), 3, 0);
183)
184
185// UV non-zero offset.
186CESTER_TEST(tr8_uv_offset_10, gpu_raster_phase13,
187 drawTR8(4, 1, 10, 0);
188 /* x=0 samples texel u=10. */
189 ASSERT_PIXEL_EQ(expectedClut8Color(10), 0, 0);
190)
191CESTER_TEST(tr8_uv_offset_10_far, gpu_raster_phase13,
192 drawTR8(4, 1, 10, 0);
193 /* x=3 samples texel u=10+3=13. */
194 ASSERT_PIXEL_EQ(expectedClut8Color(13), 3, 0);
195)
196
197// ============================================================================
198// TR15: basic 15-bit direct textured rect.
199// Texel(u, v) = vram555(u & 0x1f, v & 0x1f, (u + v) & 0x1f).
200// ============================================================================
201
202CESTER_TEST(tr15_basic_8x4_0_0, gpu_raster_phase13,
203 drawTR15(8, 4, 0, 0);
204 /* (0,0) -> texel vram555(0, 0, 0) = 0x0000 - transparent! */
206)
207CESTER_TEST(tr15_basic_8x4_7_0, gpu_raster_phase13,
208 drawTR15(8, 4, 0, 0);
209 /* (7, 0) -> vram555(7, 0, 7) = 7 | (7<<10) = 0x1c07 */
210 ASSERT_PIXEL_EQ(expectedTex15Color(7, 0), 7, 0);
211)
212CESTER_TEST(tr15_basic_8x4_0_2, gpu_raster_phase13,
213 drawTR15(8, 4, 0, 0);
214 /* (0, 2) -> vram555(0, 2, 2) = (2<<5) | (2<<10) = 0x0840 */
215 ASSERT_PIXEL_EQ(expectedTex15Color(0, 2), 0, 2);
216)
217CESTER_TEST(tr15_basic_8x4_5_3, gpu_raster_phase13,
218 drawTR15(8, 4, 0, 0);
219 /* (5, 3) -> vram555(5, 3, 8) = 5 | (3<<5) | (8<<10) */
220 ASSERT_PIXEL_EQ(expectedTex15Color(5, 3), 5, 3);
221)
222
223// 1x1 sprite.
224CESTER_TEST(tr15_1x1, gpu_raster_phase13,
225 drawTR15(1, 1, 4, 4);
226 /* (0, 0) -> texel(4, 4) = vram555(4, 4, 8) */
227 ASSERT_PIXEL_EQ(expectedTex15Color(4, 4), 0, 0);
228)
229CESTER_TEST(tr15_1x1_neighbor, gpu_raster_phase13,
230 drawTR15(1, 1, 4, 4);
232)
233
234// 1xN vertical strip - V changes down the rect.
235CESTER_TEST(tr15_1x4_v_walk_row0, gpu_raster_phase13,
236 drawTR15(1, 4, 2, 0);
237 /* (0, 0) -> texel(2, 0) = vram555(2, 0, 2) */
238 ASSERT_PIXEL_EQ(expectedTex15Color(2, 0), 0, 0);
239)
240CESTER_TEST(tr15_1x4_v_walk_row3, gpu_raster_phase13,
241 drawTR15(1, 4, 2, 0);
242 /* (0, 3) -> texel(2, 3) = vram555(2, 3, 5) */
243 ASSERT_PIXEL_EQ(expectedTex15Color(2, 3), 0, 3);
244)
245
246// UV offset.
247CESTER_TEST(tr15_uv_offset, gpu_raster_phase13,
248 drawTR15(4, 1, 3, 5);
249 /* (0, 0) -> texel(3, 5) = vram555(3, 5, 8) */
250 ASSERT_PIXEL_EQ(expectedTex15Color(3, 5), 0, 0);
251)
252CESTER_TEST(tr15_uv_offset_far, gpu_raster_phase13,
253 drawTR15(4, 1, 3, 5);
254 /* (3, 0) -> texel(6, 5) = vram555(6, 5, 11) */
255 ASSERT_PIXEL_EQ(expectedTex15Color(6, 5), 3, 0);
256)
257
258// ============================================================================
259// TR8 + UV at texture-window edge. mask_u=0x01 collapses 8-texel
260// windows. Verifies the textured rect path applies E2 windowing the
261// same way the textured triangle path does.
262// ============================================================================
263
264CESTER_TEST(tr8_window_mask01_off00_basic, gpu_raster_phase13,
265 rasterReset();
266 rasterClearTestRegion(0, 0, 32, 16);
267 setTexpage(TEX8_TX, TEX8_TY, 1);
268 setTextureWindow(0x01, 0, 0, 0); /* bit 3 of u cleared */
269 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 16, 4, CLUT8_FIELD);
270 rasterFlushPrimitive();
271 /* x=10 samples texel u=10, &~0x08 = 2 -> CLUT8[2] */
272 ASSERT_PIXEL_EQ(expectedClut8Color(2), 10, 0);
273)
274
275CESTER_TEST(tr8_window_mask01_off00_wrap_7, gpu_raster_phase13,
276 rasterReset();
277 rasterClearTestRegion(0, 0, 32, 16);
278 setTexpage(TEX8_TX, TEX8_TY, 1);
279 setTextureWindow(0x01, 0, 0, 0);
280 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 16, 4, CLUT8_FIELD);
281 rasterFlushPrimitive();
282 /* x=15 -> u=15=0xF -> &~0x08 = 7 -> CLUT8[7] */
283 ASSERT_PIXEL_EQ(expectedClut8Color(7), 15, 0);
284)
285
286// ============================================================================
287// TR8_SEMI: 8-bit textured rect semi-trans. Bit-15 NOT set in our
288// fixture CLUT8 entries, so semi-trans gate does NOT fire - output
289// is the un-blended texel value (just CLUT8[u]).
290// ============================================================================
291
292CESTER_TEST(tr8_semi_abr0_no_blend, gpu_raster_phase13,
293 drawTR8Semi(0);
294 /* (0, 0) -> CLUT8[0], no blend (mask=0 gate). */
295 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
296)
297CESTER_TEST(tr8_semi_abr1_no_blend, gpu_raster_phase13,
298 drawTR8Semi(1);
299 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
300)
301
302// ============================================================================
303// TR15_SEMI_MASKED: 15-bit semi-trans rect with masked fixture.
304// Gate fires, blend applies, bit-15 preserved (phase-12 finding).
305// Probe (0, 0): texel(0,0) = vram555(0,0,0)|0x8000 = 0x8000.
306// B = vram555(31, 0, 0) = 0x001f (red pre-fill).
307// F8 = (0, 0, 0).
308// ABR=0: (B + F)/2 per channel = (31/0/0 / 2) -> R5=15 -> 0x000f | 0x8000 = 0x800f
309// ABR=1: B + F = 31/0/0 -> R5=31 -> 0x001f | 0x8000 = 0x801f
310// ABR=2: B - F = 31/0/0 -> 0x001f | 0x8000 = 0x801f
311// ABR=3: B + F/4 = 31/0/0 -> 0x001f | 0x8000 = 0x801f
312// ============================================================================
313
314CESTER_TEST(tr15_semi_masked_abr0, gpu_raster_phase13,
315 drawTR15SemiMasked(0);
316 /* B/2 + F/2 with F=0 -> B/2. Background red R8=248 -> 124 -> R5=15 */
318)
319CESTER_TEST(tr15_semi_masked_abr1, gpu_raster_phase13,
320 drawTR15SemiMasked(1);
321 /* B + F with F=0 -> B. R5=31. */
323)
324CESTER_TEST(tr15_semi_masked_abr2, gpu_raster_phase13,
325 drawTR15SemiMasked(2);
326 /* B - F with F=0 -> B. R5=31. */
328)
329CESTER_TEST(tr15_semi_masked_abr3, gpu_raster_phase13,
330 drawTR15SemiMasked(3);
331 /* B + F/4 with F=0 -> B. R5=31. */
333)
334
335// ============================================================================
336// TR_MASK_*: E6 mask interaction with textured rect.
337// ============================================================================
338
339CESTER_TEST(tr8_setmask_output_carries_bit15, gpu_raster_phase13,
340 drawTR8SetMask();
341 /* CLUT8[0] = vram555(0, 31, 0) = 0x03e0; with set-mask: 0x03e0 | 0x8000 = 0x83e0 */
343)
344
345CESTER_TEST(tr15_checkmask_writes_skipped, gpu_raster_phase13,
346 drawTR15CheckMask();
347 /* Pre-fill (R5=8, mask=1) survives. */
349)
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 TR15_CHECKMASK_PREFILL
Definition raster-expected-phase13.h:56
#define TR15_SEMI_ABR1_BLEND
Definition raster-expected-phase13.h:47
#define TR15_SEMI_ABR3_BLEND
Definition raster-expected-phase13.h:49
#define TR8_SETMASK_OUTPUT
Definition raster-expected-phase13.h:53
#define TR15_SEMI_ABR2_BLEND
Definition raster-expected-phase13.h:48
#define TR15_SEMI_ABR0_BLEND
Definition raster-expected-phase13.h:46
#define ASSERT_PIXEL_UNTOUCHED(x_, y_)
Definition raster-helpers.h:485
#define RASTER_VRAM_RED
Definition raster-helpers.h:124
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:472
#define CLUT8_FIELD
Definition texture-fixtures.h:84
#define TEX15_TY
Definition texture-fixtures.h:66
#define TEX8_TY
Definition texture-fixtures.h:61
#define CLUT15_FIELD
Definition texture-fixtures.h:85
#define TEX15_TX
Definition texture-fixtures.h:65
#define TEX8_TX
Definition texture-fixtures.h:60
#define TEX_MOD_NEUTRAL
Definition texture-fixtures.h:328