Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
texture-basic.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 triangle basic sampling correctness at 4-bit, 8-bit, and
28// 15-bit depths. Triangles are drawn with vertex UVs matching screen
29// positions 1:1 so pixel (x, y) samples texel (x, y). The fixture
30// textures encode known functions of (u, v) so we can predict and
31// assert the exact VRAM color at each rasterized pixel.
32//
33// The audit's drawPoly3TEx4 / drawPoly3TEx8 / drawPoly3TD paths get
34// exercised here for the first time - phase 1-3 were all untextured.
35//
36// Texture command color: 0x808080 (neutral modulation - texel passes
37// through unchanged at 128/128 = 1.0).
38
39// clang-format off
40
42
43static void drawTex4Tri(void) {
44 rasterReset();
45 rasterClearTestRegion(0, 0, 32, 16);
46 setTexpage(TEX4_TX, TEX4_TY, 0);
47 setTextureWindow(0, 0, 0, 0);
48 /* Triangle (0,0)-(16,0)-(0,8) with UV matching screen. */
49 rasterTexTri(TEX_MOD_NEUTRAL,
50 0, 0, 0, 0,
51 16, 0, 16, 0,
52 0, 8, 0, 8,
54 rasterFlushPrimitive();
55}
56
57static void drawTex8Tri(void) {
58 rasterReset();
59 rasterClearTestRegion(0, 0, 64, 16);
60 setTexpage(TEX8_TX, TEX8_TY, 1);
61 setTextureWindow(0, 0, 0, 0);
62 /* Wider triangle to span more 8-bit texels. */
63 rasterTexTri(TEX_MOD_NEUTRAL,
64 0, 0, 0, 0,
65 32, 0, 32, 0,
66 0, 8, 0, 8,
68 rasterFlushPrimitive();
69}
70
71static void drawTex15Tri(void) {
72 rasterReset();
73 rasterClearTestRegion(0, 0, 32, 16);
74 setTexpage(TEX15_TX, TEX15_TY, 2);
75 setTextureWindow(0, 0, 0, 0);
76 rasterTexTri(TEX_MOD_NEUTRAL,
77 0, 0, 0, 0,
78 16, 0, 16, 0,
79 0, 8, 0, 8,
81 rasterFlushPrimitive();
82}
83
84// Single-pixel 4-bit textured triangle. Reads back exactly one pixel
85// and expects the texel at UV (3, 3) -> CLUT[3].
86static void drawTex4Single(void) {
87 rasterReset();
88 rasterClearTestRegion(20, 20, 4, 4);
89 setTexpage(TEX4_TX, TEX4_TY, 0);
90 setTextureWindow(0, 0, 0, 0);
91 rasterTexTri(TEX_MOD_NEUTRAL,
92 20, 20, 3, 3,
93 21, 20, 4, 3,
94 20, 21, 3, 4,
96 rasterFlushPrimitive();
97}
98
99) // CESTER_BODY
100
101// --------------------------------------------------------------------------
102// 4-bit CLUT triangle - basic sampling
103// --------------------------------------------------------------------------
104
105CESTER_TEST(tex4_pixel_0_0, gpu_raster_phase4,
106 drawTex4Tri();
107 /* At screen (0, 0), UV = (0, 0), texel = 0 & 0xf = 0,
108 CLUT[0] = vram555(0, 31, 0) = 0x03E0 (green). */
109 ASSERT_PIXEL_EQ(expectedClut4Color(0), 0, 0);
110)
111
112CESTER_TEST(tex4_pixel_3_0, gpu_raster_phase4,
113 drawTex4Tri();
114 ASSERT_PIXEL_EQ(expectedClut4Color(3), 3, 0);
115)
116
117CESTER_TEST(tex4_pixel_7_0, gpu_raster_phase4,
118 drawTex4Tri();
119 ASSERT_PIXEL_EQ(expectedClut4Color(7), 7, 0);
120)
121
122CESTER_TEST(tex4_pixel_15_0, gpu_raster_phase4,
123 drawTex4Tri();
124 ASSERT_PIXEL_EQ(expectedClut4Color(15), 15, 0);
125)
126
127CESTER_TEST(tex4_pixel_16_0_right_edge, gpu_raster_phase4,
128 drawTex4Tri();
129 /* x=16 is the right edge of the triangle (top-left rule excludes). */
131)
132
133CESTER_TEST(tex4_pixel_0_4_interior, gpu_raster_phase4,
134 drawTex4Tri();
135 /* At y=4, the hypotenuse limits x to about (16 * (8-4)/8) = 8.
136 Pixel (0, 4) is well inside. UV at screen (0, 4) is (0, 4),
137 texel = 0, CLUT[0]. */
138 ASSERT_PIXEL_EQ(expectedClut4Color(0), 0, 4);
139)
140
141CESTER_TEST(tex4_pixel_5_4_interior, gpu_raster_phase4,
142 drawTex4Tri();
143 ASSERT_PIXEL_EQ(expectedClut4Color(5), 5, 4);
144)
145
146CESTER_TEST(tex4_pixel_0_7_bottom_inner, gpu_raster_phase4,
147 drawTex4Tri();
148 ASSERT_PIXEL_EQ(expectedClut4Color(0), 0, 7);
149)
150
151CESTER_TEST(tex4_pixel_0_8_bottom_excluded, gpu_raster_phase4,
152 drawTex4Tri();
154)
155
156CESTER_TEST(tex4_single_pixel_at_20_20, gpu_raster_phase4,
157 drawTex4Single();
158 /* 1-pixel triangle at (20,20) with UV (3,3) -> CLUT[3]. */
159 ASSERT_PIXEL_EQ(expectedClut4Color(3), 20, 20);
160)
161
162CESTER_TEST(tex4_single_pixel_complement_21_20, gpu_raster_phase4,
163 drawTex4Single();
165)
166
167// --------------------------------------------------------------------------
168// 8-bit CLUT triangle - basic sampling
169// --------------------------------------------------------------------------
170
171CESTER_TEST(tex8_pixel_0_0, gpu_raster_phase4,
172 drawTex8Tri();
173 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 0);
174)
175
176CESTER_TEST(tex8_pixel_7_0, gpu_raster_phase4,
177 drawTex8Tri();
178 ASSERT_PIXEL_EQ(expectedClut8Color(7), 7, 0);
179)
180
181CESTER_TEST(tex8_pixel_15_0, gpu_raster_phase4,
182 drawTex8Tri();
183 ASSERT_PIXEL_EQ(expectedClut8Color(15), 15, 0);
184)
185
186CESTER_TEST(tex8_pixel_31_0, gpu_raster_phase4,
187 drawTex8Tri();
188 ASSERT_PIXEL_EQ(expectedClut8Color(31), 31, 0);
189)
190
191CESTER_TEST(tex8_pixel_32_0_right_edge, gpu_raster_phase4,
192 drawTex8Tri();
194)
195
196CESTER_TEST(tex8_pixel_10_4_interior, gpu_raster_phase4,
197 drawTex8Tri();
198 ASSERT_PIXEL_EQ(expectedClut8Color(10), 10, 4);
199)
200
201CESTER_TEST(tex8_pixel_0_7_bottom_inner, gpu_raster_phase4,
202 drawTex8Tri();
203 ASSERT_PIXEL_EQ(expectedClut8Color(0), 0, 7);
204)
205
206CESTER_TEST(tex8_pixel_0_8_bottom_excluded, gpu_raster_phase4,
207 drawTex8Tri();
209)
210
211// --------------------------------------------------------------------------
212// 15-bit direct triangle - basic sampling
213// --------------------------------------------------------------------------
214
215CESTER_TEST(tex15_pixel_0_0_transparent, gpu_raster_phase4,
216 drawTex15Tri();
217 /* TEXEL 0x0000 IS TRANSPARENT - canonical PSX rule (HW_VERIFIED
218 2026-05-15). At screen (0, 0), UV = (0, 0), the fixture texel
219 is vram555(0, 0, 0) = 0x0000. The rasterizer detects all-zero
220 texel and skips the write, so the destination stays sentinel.
221 This applies to ALL three texture depths: a CLUT entry of
222 0x0000 OR a direct 15-bit texel of 0x0000 = transparent. */
224)
225
226CESTER_TEST(tex15_pixel_5_0, gpu_raster_phase4,
227 drawTex15Tri();
228 ASSERT_PIXEL_EQ(expectedTex15Color(5, 0), 5, 0);
229)
230
231CESTER_TEST(tex15_pixel_15_0, gpu_raster_phase4,
232 drawTex15Tri();
233 ASSERT_PIXEL_EQ(expectedTex15Color(15, 0), 15, 0);
234)
235
236CESTER_TEST(tex15_pixel_16_0_right_edge, gpu_raster_phase4,
237 drawTex15Tri();
239)
240
241CESTER_TEST(tex15_pixel_0_4_interior, gpu_raster_phase4,
242 drawTex15Tri();
243 ASSERT_PIXEL_EQ(expectedTex15Color(0, 4), 0, 4);
244)
245
246CESTER_TEST(tex15_pixel_5_4_interior, gpu_raster_phase4,
247 drawTex15Tri();
248 ASSERT_PIXEL_EQ(expectedTex15Color(5, 4), 5, 4);
249)
250
251CESTER_TEST(tex15_pixel_0_7_bottom_inner, gpu_raster_phase4,
252 drawTex15Tri();
253 ASSERT_PIXEL_EQ(expectedTex15Color(0, 7), 0, 7);
254)
255
256CESTER_TEST(tex15_pixel_0_8_bottom_excluded, gpu_raster_phase4,
257 drawTex15Tri();
259)
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 ASSERT_PIXEL_UNTOUCHED(x_, y_)
Definition raster-helpers.h:486
#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 CLUT4_FIELD
Definition texture-fixtures.h:83
#define TEX15_TPAGE
Definition texture-fixtures.h:102
#define TEX8_TPAGE
Definition texture-fixtures.h:101
#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
#define TEX4_TY
Definition texture-fixtures.h:56
#define TEX4_TX
Definition texture-fixtures.h:55
#define TEX4_TPAGE
Definition texture-fixtures.h:100