Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
texture-window.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// Texture window mask + offset behavior. GP0(E2) sets:
28// bits 0-4 mask_x (5-bit, in 8-texel units)
29// bits 5-9 mask_y
30// bits 10-14 offset_x
31// bits 15-19 offset_y
32//
33// Sampling formula (per psx-spx):
34// texel.u = (u AND NOT (mask.u * 8)) OR ((offset.u * 8) AND (mask.u * 8))
35// texel.v = same for v
36//
37// Mask bits set in mask.u force those high bits of u to be taken from
38// offset.u (each unit = 8 texels). Mask=0 means no windowing.
39//
40// These tests use the 4-bit CLUT fixture (16 distinct texels, u=0..15
41// per pattern row) at TEX4_TPAGE. With mask.u=0x01 (8-texel unit), bit
42// 3 of u gets forced to bit 3 of offset.u*8. So:
43// mask.u=0x01, offset.u=0x00 -> u with bit 3 cleared
44// (u=8..15 all collapse to texel u=0..7)
45// mask.u=0x01, offset.u=0x01 -> u with bit 3 set (u=0..7 collapse to 8..15)
46
47// clang-format off
48
50
51// Texture-window test setup: 4-bit triangle large enough to span 16
52// texel positions horizontally, with chosen E2 window state.
53static void drawTexWindowTri(uint8_t mask_x, uint8_t off_x) {
54 rasterReset();
55 rasterClearTestRegion(0, 0, 32, 16);
56 setTexpage(TEX4_TX, TEX4_TY, 0);
57 setTextureWindow(mask_x, 0, off_x, 0);
58 /* Triangle (0,0)-(16,0)-(0,8), UV (0,0)-(16,0)-(0,8). At pixel
59 (x, y), sample texel (x, y) -> window-filter -> CLUT[filtered_u]. */
60 rasterTexTri(TEX_MOD_NEUTRAL,
61 0, 0, 0, 0,
62 16, 0, 16, 0,
63 0, 8, 0, 8,
65 rasterFlushPrimitive();
66}
67
68) // CESTER_BODY
69
70// --------------------------------------------------------------------------
71// mask_x = 0, offset_x = 0: identity (no windowing)
72// --------------------------------------------------------------------------
73// Already covered by phase-4a's texture-basic.c. Skipping duplicates.
74
75// --------------------------------------------------------------------------
76// mask_x = 0x01 (= 8-texel-unit mask), offset_x = 0
77// filtered_u = u & ~0x08 = u with bit 3 cleared
78// u=0..7 -> texel 0..7
79// u=8..15 -> texel 0..7 (bit 3 cleared maps 8->0, 9->1, ..., 15->7)
80// --------------------------------------------------------------------------
81
82CESTER_TEST(texwin_mask01_off00_pixel_0_0, gpu_raster_phase4,
83 drawTexWindowTri(0x01, 0x00);
84 /* u=0, filtered=0, CLUT[0]. But CLUT[0] is texel 0 from
85 expectedClut4Color(0) = vram555(0, 31, 0). NOTE: at (0,0) this
86 reads as transparent if texel value happens to be 0x0000 (after
87 CLUT lookup). CLUT[0] = vram555(0, 31, 0) = 0x03E0 != 0x0000,
88 so the pixel SHOULD be drawn. */
89 ASSERT_PIXEL_EQ(expectedClut4Color(0), 0, 0);
90)
91
92CESTER_TEST(texwin_mask01_off00_pixel_7_0_last_unwrapped, gpu_raster_phase4,
93 drawTexWindowTri(0x01, 0x00);
94 /* u=7, filtered=7, CLUT[7]. */
95 ASSERT_PIXEL_EQ(expectedClut4Color(7), 7, 0);
96)
97
98CESTER_TEST(texwin_mask01_off00_pixel_8_0_wrapped_to_0, gpu_raster_phase4,
99 drawTexWindowTri(0x01, 0x00);
100 /* u=8, filtered=8&~8=0, CLUT[0]. So pixel 8 samples same texel as
101 pixel 0. */
102 ASSERT_PIXEL_EQ(expectedClut4Color(0), 8, 0);
103)
104
105CESTER_TEST(texwin_mask01_off00_pixel_11_0_wrapped_to_3, gpu_raster_phase4,
106 drawTexWindowTri(0x01, 0x00);
107 /* u=11=0xB, filtered=0xB&~0x8=0x3, CLUT[3]. */
108 ASSERT_PIXEL_EQ(expectedClut4Color(3), 11, 0);
109)
110
111CESTER_TEST(texwin_mask01_off00_pixel_15_0_wrapped_to_7, gpu_raster_phase4,
112 drawTexWindowTri(0x01, 0x00);
113 /* u=15=0xF, filtered=0xF&~0x8=0x7, CLUT[7]. */
114 ASSERT_PIXEL_EQ(expectedClut4Color(7), 15, 0);
115)
116
117// --------------------------------------------------------------------------
118// mask_x = 0x01, offset_x = 0x01: bit 3 forced to 1
119// filtered_u = (u & ~0x08) | (0x08) = u with bit 3 SET
120// u=0..7 -> 0x08..0x0F (texels 8..15)
121// u=8..15 -> texels 8..15
122// --------------------------------------------------------------------------
123
124CESTER_TEST(texwin_mask01_off01_pixel_0_0_forced_to_8, gpu_raster_phase4,
125 drawTexWindowTri(0x01, 0x01);
126 /* u=0, filtered=0|8=8, CLUT[8]. */
127 ASSERT_PIXEL_EQ(expectedClut4Color(8), 0, 0);
128)
129
130CESTER_TEST(texwin_mask01_off01_pixel_3_0_forced_to_b, gpu_raster_phase4,
131 drawTexWindowTri(0x01, 0x01);
132 /* u=3, filtered=3|8=0xB, CLUT[0xB]. */
133 ASSERT_PIXEL_EQ(expectedClut4Color(0x0b), 3, 0);
134)
135
136CESTER_TEST(texwin_mask01_off01_pixel_8_0_already_set, gpu_raster_phase4,
137 drawTexWindowTri(0x01, 0x01);
138 /* u=8, filtered=8 (bit 3 already set), CLUT[8]. */
139 ASSERT_PIXEL_EQ(expectedClut4Color(8), 8, 0);
140)
141
142// --------------------------------------------------------------------------
143// mask_x = 0x03 (= 24-bit mask = bits 3,4 of u): bits 3-4 forced from offset
144// With offset_x=0: filtered_u = u & ~0x18 (bits 3 and 4 cleared)
145// u=0..7 -> 0..7
146// u=8..15 -> 0..7 (bit 3 cleared)
147// (Same as mask=0x01 for u in 0..15 since bit 4 doesn't apply yet.)
148// We test u values where bit 4 would matter at higher u, but my 4-bit
149// fixture only covers u=0..15. So just one probe to characterize.
150// --------------------------------------------------------------------------
151
152CESTER_TEST(texwin_mask03_off00_pixel_13_0, gpu_raster_phase4,
153 drawTexWindowTri(0x03, 0x00);
154 /* u=13=0xD, filtered=0xD & ~0x18 = 5, CLUT[5]. */
155 ASSERT_PIXEL_EQ(expectedClut4Color(5), 13, 0);
156)
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_EQ(expected, x_, y_)
Definition raster-helpers.h:473
#define CLUT4_FIELD
Definition texture-fixtures.h:83
#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