Nugget
Bare-metal libraries and examples for the original PlayStation
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
33// clang-format off
34
36
37// Basic 8-bit textured rect: 8x4 at (0, 0), UV (0, 0). Texel u maps
38// to CLUT8[u].
39static void drawTR8(int16_t w, int16_t h, uint8_t u, uint8_t v) {
40 rasterReset();
41 rasterClearTestRegion(0, 0, 32, 16);
42 setTexpage(TEX8_TX, TEX8_TY, 1);
43 setTextureWindow(0, 0, 0, 0);
44 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, u, v, w, h, CLUT8_FIELD);
45 rasterFlushPrimitive();
46}
47
48// Basic 15-bit direct textured rect.
49static void drawTR15(int16_t w, int16_t h, uint8_t u, uint8_t v) {
50 rasterReset();
51 rasterClearTestRegion(0, 0, 32, 16);
52 setTexpage(TEX15_TX, TEX15_TY, 2);
53 setTextureWindow(0, 0, 0, 0);
54 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, u, v, w, h, CLUT15_FIELD);
55 rasterFlushPrimitive();
56}
57
58// 8-bit semi-trans rect over a red-filled background. Texture uses
59// unmasked CLUT - per phase-8 (polygon) finding bit-15=0 should mean
60// no blend. Phase-13 verifies whether textured RECTS honor the same
61// gate semantics as polygons. Note: textured-rect commands carry NO
62// embedded tpage word, so E1 (set via setTexpageAbr) carries both
63// the texture page AND the ABR mode for this draw.
64static void drawTR8Semi(uint8_t abr) {
65 rasterReset();
66 rasterFillRect(0, 0, 32, 16, RASTER_VRAM_RED);
67 setTexpageAbr(TEX8_TX, TEX8_TY, 1, abr);
68 setTextureWindow(0, 0, 0, 0);
69 rasterTexRectSemi(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT8_FIELD);
70 rasterFlushPrimitive();
71 /* Restore default E1 for subsequent tests. */
72 setTexpage(0, 0, 0);
73}
74
75// 15-bit semi-trans rect with the masked-15-bit fixture installed.
76// Bit-15 = 1 on every texel. Per phase-12 polygon finding the gate
77// fires and ABR blend applies with bit-15 preserved into VRAM.
78// Phase-13 verifies that rule for the rect path.
79static void drawTR15SemiMasked(uint8_t abr) {
80 rasterReset();
81 rasterFillRect(0, 0, 32, 16, RASTER_VRAM_RED);
82 uploadTex15Masked();
83 setTexpageAbr(TEX15_TX, TEX15_TY, 2, abr);
84 setTextureWindow(0, 0, 0, 0);
85 rasterTexRectSemi(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT15_FIELD);
86 rasterFlushPrimitive();
87 setTexpage(0, 0, 0);
88 restoreTex15Standard();
89}
90
91// 8-bit textured rect with E6 set-mask. Output should carry bit-15.
92static void drawTR8SetMask(void) {
93 rasterReset();
94 rasterClearTestRegion(0, 0, 32, 16);
95 setTexpage(TEX8_TX, TEX8_TY, 1);
96 setTextureWindow(0, 0, 0, 0);
97 rasterSetMaskCtrl(1, 0);
98 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT8_FIELD);
99 rasterFlushPrimitive();
100 rasterSetMaskCtrl(0, 0);
101}
102
103// 15-bit textured rect with E6 check-mask. Pre-fill has bit-15 set
104// so writes are skipped; the pre-fill survives.
105static void drawTR15CheckMask(void) {
106 rasterReset();
107 uint16_t bg = (uint16_t)(rasterVram555(8, 0, 0) | 0x8000u);
108 rasterFillRect(0, 0, 32, 16, bg);
109 setTexpage(TEX15_TX, TEX15_TY, 2);
110 setTextureWindow(0, 0, 0, 0);
111 rasterSetMaskCtrl(0, 1);
112 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 8, 4, CLUT15_FIELD);
113 rasterFlushPrimitive();
114 rasterSetMaskCtrl(0, 0);
115}
116
117) // CESTER_BODY
118
119// ============================================================================
120// TR8: basic 8-bit textured rect. Texel(u, v) = u & 0xff -> CLUT8[u].
121// ============================================================================
122
123CESTER_TEST(tr8_basic_8x4_0_0, gpu_raster_phase13,
124 drawTR8(8, 4, 0, 0);
125 /* Pixel (0,0) samples texel u=0 -> CLUT8[0]. */
126 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
127)
128CESTER_TEST(tr8_basic_8x4_7_0, gpu_raster_phase13,
129 drawTR8(8, 4, 0, 0);
130 /* Last column inside rect (x=7) samples texel u=7. */
131 ASSERT_PIXEL_EQ(expectedClut8Color(7), 7, 0);
132)
133CESTER_TEST(tr8_basic_8x4_just_past_8, gpu_raster_phase13,
134 drawTR8(8, 4, 0, 0);
135 /* x=8 is the right boundary - INCLUSIVE for rects per phase-5
136 finding (untextured/textured rects include their right and
137 bottom edges, unlike triangles). */
139)
140CESTER_TEST(tr8_basic_8x4_bottom_row, gpu_raster_phase13,
141 drawTR8(8, 4, 0, 0);
142 /* y=3 should be drawn (last row of 4-tall rect). */
143 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 3);
144)
145CESTER_TEST(tr8_basic_8x4_past_bottom, gpu_raster_phase13,
146 drawTR8(8, 4, 0, 0);
148)
149
150// 1x1 sprite - smallest case.
151CESTER_TEST(tr8_1x1_pixel, gpu_raster_phase13,
152 drawTR8(1, 1, 5, 0);
153 /* Single pixel at (0,0) samples texel u=5. */
154 ASSERT_PIXEL_EQ(expectedClut8Color(5), 0, 0);
155)
156CESTER_TEST(tr8_1x1_neighbor_right, gpu_raster_phase13,
157 drawTR8(1, 1, 5, 0);
159)
160CESTER_TEST(tr8_1x1_neighbor_below, gpu_raster_phase13,
161 drawTR8(1, 1, 5, 0);
163)
164
165// 1xN vertical strip.
166CESTER_TEST(tr8_1x4_top, gpu_raster_phase13,
167 drawTR8(1, 4, 3, 0);
168 /* All four pixels at x=0 sample texel u=3 (V doesn't change u in
169 our fixture). */
170 ASSERT_PIXEL_EQ(expectedClut8Color(3), 0, 0);
171)
172CESTER_TEST(tr8_1x4_bottom, gpu_raster_phase13,
173 drawTR8(1, 4, 3, 0);
174 ASSERT_PIXEL_EQ(expectedClut8Color(3), 0, 3);
175)
176
177// Nx1 horizontal strip.
178CESTER_TEST(tr8_4x1_left, gpu_raster_phase13,
179 drawTR8(4, 1, 0, 0);
180 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
181)
182CESTER_TEST(tr8_4x1_right, gpu_raster_phase13,
183 drawTR8(4, 1, 0, 0);
184 ASSERT_PIXEL_EQ(expectedClut8Color(3), 3, 0);
185)
186
187// UV non-zero offset.
188CESTER_TEST(tr8_uv_offset_10, gpu_raster_phase13,
189 drawTR8(4, 1, 10, 0);
190 /* x=0 samples texel u=10. */
191 ASSERT_PIXEL_EQ(expectedClut8Color(10), 0, 0);
192)
193CESTER_TEST(tr8_uv_offset_10_far, gpu_raster_phase13,
194 drawTR8(4, 1, 10, 0);
195 /* x=3 samples texel u=10+3=13. */
196 ASSERT_PIXEL_EQ(expectedClut8Color(13), 3, 0);
197)
198
199// ============================================================================
200// TR15: basic 15-bit direct textured rect.
201// Texel(u, v) = vram555(u & 0x1f, v & 0x1f, (u + v) & 0x1f).
202// ============================================================================
203
204CESTER_TEST(tr15_basic_8x4_0_0, gpu_raster_phase13,
205 drawTR15(8, 4, 0, 0);
206 /* (0,0) -> texel vram555(0, 0, 0) = 0x0000 - transparent! */
208)
209CESTER_TEST(tr15_basic_8x4_7_0, gpu_raster_phase13,
210 drawTR15(8, 4, 0, 0);
211 /* (7, 0) -> vram555(7, 0, 7) = 7 | (7<<10) = 0x1c07 */
212 ASSERT_PIXEL_EQ(expectedTex15Color(7, 0), 7, 0);
213)
214CESTER_TEST(tr15_basic_8x4_0_2, gpu_raster_phase13,
215 drawTR15(8, 4, 0, 0);
216 /* (0, 2) -> vram555(0, 2, 2) = (2<<5) | (2<<10) = 0x0840 */
217 ASSERT_PIXEL_EQ(expectedTex15Color(0, 2), 0, 2);
218)
219CESTER_TEST(tr15_basic_8x4_5_3, gpu_raster_phase13,
220 drawTR15(8, 4, 0, 0);
221 /* (5, 3) -> vram555(5, 3, 8) = 5 | (3<<5) | (8<<10) */
222 ASSERT_PIXEL_EQ(expectedTex15Color(5, 3), 5, 3);
223)
224
225// 1x1 sprite.
226CESTER_TEST(tr15_1x1, gpu_raster_phase13,
227 drawTR15(1, 1, 4, 4);
228 /* (0, 0) -> texel(4, 4) = vram555(4, 4, 8) */
229 ASSERT_PIXEL_EQ(expectedTex15Color(4, 4), 0, 0);
230)
231CESTER_TEST(tr15_1x1_neighbor, gpu_raster_phase13,
232 drawTR15(1, 1, 4, 4);
234)
235
236// 1xN vertical strip - V changes down the rect.
237CESTER_TEST(tr15_1x4_v_walk_row0, gpu_raster_phase13,
238 drawTR15(1, 4, 2, 0);
239 /* (0, 0) -> texel(2, 0) = vram555(2, 0, 2) */
240 ASSERT_PIXEL_EQ(expectedTex15Color(2, 0), 0, 0);
241)
242CESTER_TEST(tr15_1x4_v_walk_row3, gpu_raster_phase13,
243 drawTR15(1, 4, 2, 0);
244 /* (0, 3) -> texel(2, 3) = vram555(2, 3, 5) */
245 ASSERT_PIXEL_EQ(expectedTex15Color(2, 3), 0, 3);
246)
247
248// UV offset.
249CESTER_TEST(tr15_uv_offset, gpu_raster_phase13,
250 drawTR15(4, 1, 3, 5);
251 /* (0, 0) -> texel(3, 5) = vram555(3, 5, 8) */
252 ASSERT_PIXEL_EQ(expectedTex15Color(3, 5), 0, 0);
253)
254CESTER_TEST(tr15_uv_offset_far, gpu_raster_phase13,
255 drawTR15(4, 1, 3, 5);
256 /* (3, 0) -> texel(6, 5) = vram555(6, 5, 11) */
257 ASSERT_PIXEL_EQ(expectedTex15Color(6, 5), 3, 0);
258)
259
260// ============================================================================
261// TR8 + UV at texture-window edge. mask_u=0x01 collapses 8-texel
262// windows. Verifies the textured rect path applies E2 windowing the
263// same way the textured triangle path does.
264// ============================================================================
265
266CESTER_TEST(tr8_window_mask01_off00_basic, gpu_raster_phase13,
267 rasterReset();
268 rasterClearTestRegion(0, 0, 32, 16);
269 setTexpage(TEX8_TX, TEX8_TY, 1);
270 setTextureWindow(0x01, 0, 0, 0); /* bit 3 of u cleared */
271 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 16, 4, CLUT8_FIELD);
272 rasterFlushPrimitive();
273 /* x=10 samples texel u=10, &~0x08 = 2 -> CLUT8[2] */
274 ASSERT_PIXEL_EQ(expectedClut8Color(2), 10, 0);
275)
276
277CESTER_TEST(tr8_window_mask01_off00_wrap_7, gpu_raster_phase13,
278 rasterReset();
279 rasterClearTestRegion(0, 0, 32, 16);
280 setTexpage(TEX8_TX, TEX8_TY, 1);
281 setTextureWindow(0x01, 0, 0, 0);
282 rasterTexRect(TEX_MOD_NEUTRAL, 0, 0, 0, 0, 16, 4, CLUT8_FIELD);
283 rasterFlushPrimitive();
284 /* x=15 -> u=15=0xF -> &~0x08 = 7 -> CLUT8[7] */
285 ASSERT_PIXEL_EQ(expectedClut8Color(7), 15, 0);
286)
287
288// ============================================================================
289// TR8_SEMI: 8-bit textured rect semi-trans. Bit-15 NOT set in our
290// fixture CLUT8 entries, so semi-trans gate does NOT fire - output
291// is the un-blended texel value (just CLUT8[u]).
292// ============================================================================
293
294CESTER_TEST(tr8_semi_abr0_no_blend, gpu_raster_phase13,
295 drawTR8Semi(0);
296 /* (0, 0) -> CLUT8[0], no blend (mask=0 gate). */
297 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
298)
299CESTER_TEST(tr8_semi_abr1_no_blend, gpu_raster_phase13,
300 drawTR8Semi(1);
301 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
302)
303
304// ============================================================================
305// TR15_SEMI_MASKED: 15-bit semi-trans rect with masked fixture.
306// Gate fires, blend applies, bit-15 preserved (phase-12 finding).
307// Probe (0, 0): texel(0,0) = vram555(0,0,0)|0x8000 = 0x8000.
308// B = vram555(31, 0, 0) = 0x001f (red pre-fill).
309// F8 = (0, 0, 0).
310// ABR=0: (B + F)/2 per channel = (31/0/0 / 2) -> R5=15 -> 0x000f | 0x8000 = 0x800f
311// ABR=1: B + F = 31/0/0 -> R5=31 -> 0x001f | 0x8000 = 0x801f
312// ABR=2: B - F = 31/0/0 -> 0x001f | 0x8000 = 0x801f
313// ABR=3: B + F/4 = 31/0/0 -> 0x001f | 0x8000 = 0x801f
314// ============================================================================
315
316CESTER_TEST(tr15_semi_masked_abr0, gpu_raster_phase13,
317 drawTR15SemiMasked(0);
318 /* B/2 + F/2 with F=0 -> B/2. Background red R8=248 -> 124 -> R5=15 */
320)
321CESTER_TEST(tr15_semi_masked_abr1, gpu_raster_phase13,
322 drawTR15SemiMasked(1);
323 /* B + F with F=0 -> B. R5=31. */
325)
326CESTER_TEST(tr15_semi_masked_abr2, gpu_raster_phase13,
327 drawTR15SemiMasked(2);
328 /* B - F with F=0 -> B. R5=31. */
330)
331CESTER_TEST(tr15_semi_masked_abr3, gpu_raster_phase13,
332 drawTR15SemiMasked(3);
333 /* B + F/4 with F=0 -> B. R5=31. */
335)
336
337// ============================================================================
338// TR_MASK_*: E6 mask interaction with textured rect.
339// ============================================================================
340
341CESTER_TEST(tr8_setmask_output_carries_bit15, gpu_raster_phase13,
342 drawTR8SetMask();
343 /* CLUT8[0] = vram555(0, 31, 0) = 0x03e0; with set-mask: 0x03e0 | 0x8000 = 0x83e0 */
345)
346
347CESTER_TEST(tr15_checkmask_writes_skipped, gpu_raster_phase13,
348 drawTR15CheckMask();
349 /* Pre-fill (R5=8, mask=1) survives. */
351)
CESTER_TEST(cddaLevelHalf, test_instances, static const uint8_t atv[4]={0x40, 0x00, 0x40, 0x00};unsigned changes=0;int peak=cddaPeak(atv, 1, &changes);ramsyscall_printf("CD-DA level at half ATV: peak %i, %u sign changes\n", peak, changes);cester_assert_int_ge(peak, 11300);cester_assert_int_le(peak, 11800);cester_assert_uint_ge(changes, 4);) CESTER_TEST(cddaLevelClipped
CESTER_BODY(static uint16_t s_cddaCapture;static int cddaSpuDma(uint32_t spuAddr, void *buf, uint32_t bytes, int isRead) { const uint16_t tsa=spuAddr > > 3;const uint16_t mode=isRead ? 0x30 :0x20;SPU_RAM_DTA=tsa;for(int i=0;i< 0xf01 &&SPU_RAM_DTA !=tsa;i++);SPU_CTRL=(SPU_CTRL &~0x30)|mode;for(volatile int i=0;i< 60;i++);for(int i=0;i< 0xf01 &&(SPU_STATUS &0x30) !=mode;i++);CDDA_SPU_DELAY=(CDDA_SPU_DELAY &0xf0ffffff)|(isRead ? 0x22000000 :0x20000000);DMA_CTRL[DMA_SPU].MADR=(uint32_t) buf &0x1fffffff;DMA_CTRL[DMA_SPU].BCR=((bytes > > 6)<< 16)|0x10;DMA_CTRL[DMA_SPU].CHCR=isRead ? 0x01000200 :0x01000201;int ok=0;for(unsigned t=0;t< 4000000u;t++) { if((DMA_CTRL[DMA_SPU].CHCR &0x01000000)==0) { ok=1;break;} } SPU_CTRL=SPU_CTRL &~0x30;for(volatile int i=0;i< 60;i++);return ok;} static int cddaSimpleCmd(uint8_t cmd) { uint8_t response[16];CDROM_REG0=0;CDROM_REG1=cmd;uint32_t timeout=2000000;int ok=waitCDRomIRQWithTimeout(&timeout);ackCDRomCause();readResponse(response);return ok;} static void cddaWaitPauseComplete() { initializeTime();uint32_t elapsed=0;while(elapsed< 5000000u) { uint32_t budget=200000u;int ok=waitCDRomIRQWithTimeout(&budget);elapsed=budget;if(!ok) continue;uint8_t cause=ackCDRomCause();uint8_t response[16];readResponse(response);if(cause==2) return;} } static int cddaAudioSetup(const uint8_t atv[4], int demute) { if(!resetCDRom()) return 0;DPCR|=0x000b0000;SPU_CTRL=0;for(volatile int i=0;i< 20000;i++);SPU_VOL_MAIN_LEFT=0;SPU_VOL_MAIN_RIGHT=0;SPU_KEY_OFF_LOW=0xffff;SPU_KEY_OFF_HIGH=0xffff;SPU_RAM_DTC=4;SPU_CTRL=0x8001;for(volatile int i=0;i< 20000;i++);for(int i=0;i< 0x400;i++) s_cddaCapture[i]=CDDA_MARKER;if(!cddaSpuDma(0x000, s_cddaCapture, 0x800, 0)) return 0;if(!cddaSimpleCmd(demute ? CDL_DEMUTE :CDL_MUTE)) return 0;CDROM_REG0=2;CDROM_REG2=atv[0];CDROM_REG3=atv[1];CDROM_REG0=3;CDROM_REG1=atv[2];CDROM_REG2=atv[3];CDROM_REG0=3;CDROM_REG3=0x20;CDROM_REG0=0;SPU_VOL_CD_LEFT=0x7fff;SPU_VOL_CD_RIGHT=0x7fff;SPU_CTRL=SPU_CTRL|0x8001;return 1;} static int cddaBufferPeak(unsigned *signChanges) { int peak=0;unsigned changes=0;int prev=0;for(int i=0;i< 0x200;i++) { int v=(int16_t) s_cddaCapture[i];int a=v< 0 ? -v :v;if(a > peak) peak=a;int sign=v >=0 ? 1 :-1;if(i &&sign !=prev) changes++;prev=sign;} *signChanges=changes;return peak;} static int cddaCapturePeak(unsigned *signChanges) { int peak=0;for(unsigned attempt=0;attempt< 8;attempt++) { if(attempt) { initializeTime();while(updateTime()< 250000u);} if(!cddaSpuDma(0x000, s_cddaCapture, 0x800, 1)) return -1;peak=cddaBufferPeak(signChanges);if(peak) { if(attempt) ramsyscall_printf("capture was empty %u time(s) before audio\n", attempt);break;} } return peak;} static int cddaPeak(const uint8_t atv[4], int demute, unsigned *signChanges) { if(!cddaAudioSetup(atv, demute)) return -1;if(!setMode(0)) return -1;CDROM_REG0=0;CDROM_REG2=0x02;if(!cddaSimpleCmd(CDL_PLAY)) return -1;initializeTime();while(updateTime()< 1500000u);int peak=cddaCapturePeak(signChanges);CDROM_REG0=0;CDROM_REG1=CDL_PAUSE;cddaWaitPauseComplete();CDROM_REG0=0;CDROM_REG2=0x00;CDROM_REG2=0x02;CDROM_REG2=0x16;if(cddaSimpleCmd(CDL_SETLOC) &&cddaSimpleCmd(CDL_SEEKL)) cddaWaitPauseComplete();return peak;}[0x400])
Definition cdda.c:37
#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:486
#define RASTER_VRAM_RED
Definition raster-helpers.h:125
#define ASSERT_PIXEL_EQ(expected, x_, y_)
Definition raster-helpers.h:473
#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:308