Skip to content

Scene textures (.TEX)

Every scene archive (SCENES/**.ZIP) holds one .TEX texture buffer of ZTexturePS2 records, which CompactTextureBuffer installs. This file describes how the texels, palettes and sampler state are stored, read from the PS2 upload code (zolddrawps2.cpp, zoldviewps2.cpp) and checked against all 71 scene buffers. Run python3 tools/port/tex_dump.py survey to write build/tex/survey.json.

What the disc holds

The 71 scene buffers hold 24,869 textures:

m_Format GS format Colours Normal map Textures
ZPFP16 PSMT4 16 no 9,431
ZPFP256 PSMT8 256 no 9,311
ZPF8888 PSMCT32 0 no 5,642
ZPFP256 PSMT8 256 yes 451
ZPFP16 PSMT4 16 yes 34

All sizes are powers of two. 514 textures have 2 to 6 mip levels, and the rest have one. No data size, palette size or mip count disagrees with the PS2 upload code. Fourteen textures are animated (their frames are other texture records) and 105 scroll.

Texel layout

The PS2 code ignores m_Format. AddGifChainTexture (zolddrawps2.cpp) picks the GS format from the record alone: no palette is PSMCT32, a palette with fewer than 17 colours is PSMT4, and a larger one is PSMT8. Level 0 comes first in the data, then level 1 and so on, each a quarter of the previous level's qwords.

MakeLocations decides how each level is uploaded. A PSMT8 level whose width is a multiple of 16 and height a multiple of 4 is uploaded as a PSMCT32 rectangle of (w/2, h/2) with buffer width ceil(w/2 / 64). The equivalent PSMT4 condition gives a rectangle of (h/2, w/4), with the axes swapped. The GS then reads the level back as PSMT8 or PSMT4 with buffer width 2·ceil(w/128). The stored bytes are what makes that write-then-read land the right texels. To read a texel from the file, take its PSMT8/PSMT4 address, find the PSMCT32 pixel of the upload rectangle holding that word, and read the byte or nibble there. 20,150 levels take this path. The other 6,321 levels (all PSMCT32, plus PSMT4 levels 8×8, 16×8, 16×16, 32×8, 64×8 and 256×8, and PSMT8 8×8) are uploaded in their own format and are row-major. MakeLocations gives those levels a buffer width the read does not use (it tests up_mode - 2 < 2 where 0x13/0x14 were meant), but on this disc that never changes an address.

Palettes

The CLUT is PSMCT32 in CSM1, uploaded as an 8×2 (16 colours) or 16×16 (256 colours) raster. Sixteen-colour entries are in index order. A 256-colour entry i sits at raster position i with bits 3 and 4 swapped.

Normal maps upload five CLUTs side by side (40×2 or 48×32), and the dot3 pass in TextureSwitchInDot3Pass selects them by CBP (block offsets 0, 1, 4, 5, 16 for 16 colours and 0, 4, 8, 12, 16 for 256). For every entry of all 485 normal maps, CLUTs 0 to 2 are grey (128,128,128) with alpha nx, ny and nz. CLUT 3 holds max(0, n − 128) per axis, CLUT 4 holds max(0, 128 − n), and both share one alpha w (54 to 185). A normal-map texel is therefore (nx, ny, nz, w), which reproduces all five CLUTs.

Alpha

Stored alpha uses the full byte, and 255 is opaque. 75% of all 32-bit texels on the disc are 0xff, and the font pages ramp their palette alpha 0, 16, … 240. The engine compensates on the GS side. For textured sprites, zoldviewps2.cpp sends vertex alpha as lColor >> 26 (A/4), where untextured sprites get lColor >> 25 (A/2). With MODULATE, 255 × 63 >> 7 gives about 0x80. The loader sequence player halves palette alpha before its own upload (PALETTE_ALPHA_TO_GS). An alpha-test reference from a prim (lAlphaCutoff, compared on the GS after modulation) corresponds to about twice its value in texture alpha.

Sampler state

The bitfield word at 0x44 carries TEX1's MMAG (bit 28), MMIN (bits 29 to 31), L (bits 14 and 15) and K (bits 16 to 27, signed, 1/16 units). On the disc, 24,355 textures are bilinear with no mips (K = −9.0). The rest are LINEAR_MIPMAP_NEAREST (391) or LINEAR_MIPMAP_LINEAR (120), with K between −18.4 and 0. Three are point-sampled. The GS chooses the LOD from Q, as (log2(1/Q) << L) + K. Clamping is not part of the texture. CalcGSRegs clamps U and V from the prim's lDrawMode bits 0x4000 and 0x8000, and repeats otherwise.

Writing a buffer

tools/port/scene/tex.py parses and rewrites all 71 buffers byte for byte (TEX: 71/71 identical; also in roundtrip.py). Its header differs from the PRM header. Both use the same handle entry (offset, size, reference count and padding) and block ordering.

  • ZTextureBufferHeader (renderbase.h:65) is lTextureOffsets (the handle of the texture list), lHandleTableOffset, lUsedSize (the file size) and lNumHandles. Handle 0 is the header.
  • Blocks are laid out back to back in handle order from 0x10, and the handle table ends the file. The texture list (a count, then one record handle per texture) is always the last block, padded to 16 bytes, and has no zero entries. Texture ids rise with list order.
  • A texture is three blocks in handle order: palette (absent for PSMCT32), texels, then the 0x60-byte ZTexturePS2 record. Records may share palette and texel blocks (a font's pages share one palette, for example), and shared blocks have reference counts above 1.
  • Animated textures add a block with a u32 frame count followed by one u32 texture ID per frame. The frames share this block.
  • In every record, m_pUserData is the texel handle, m_lDrawEntryId is 0, m_fScaleFactor is 1.0, m_fOffset is (0, 0), m_lPad* are 0 and m_dwDataSize is the sum of the level sizes. m_lMaterialDescription is a small index (1 in 20,340 records, range 1–19).

The record holds size at 0x00, offset at 0x04, scroll speed at 0x0c, name at 0x14, texel and animation handles at 0x2c and 0x30, format at 0x34, byte size at 0x38 and scale at 0x3c. The texture and draw-entry IDs are halfwords at 0x40 and 0x42. In the 0x44 word, bits 0–12 give the colour count and bit 13 selects a normal map. Palette and texture handles follow at 0x48 and 0x4c, material at 0x50 and mip count at 0x54. Padding ends at 0x60.

Adding a texture

tex.add_texture(buf, rgba, width, height, name, fmt="p8", texture_id=None, mips=1) appends a texture and returns its id, by default one more than the largest. Pass that id as a prim's lTextureId.

  • fmt is "rgba32" (ZPF8888, PSMCT32), "p8" (ZPFP256, PSMT8, 256 colours) or "p4" (ZPFP16, PSMT4, 16 colours). Sizes are powers of two from 8 to 1,024. Alpha is stored as given (255 is opaque).
  • Palettised images are quantised by median cut, or kept exact when they have few enough colours. The levels are written through the inverse of tex_dump's upload model (level_permutation), so both swizzled and row-major levels land where the GS reads them. The CLUT is written in CSM1 order.
  • m_lFlags gets the colour count and the disc's usual sampler, 0x3f700000 (bilinear, K = −9.0). With mips > 1 the levels are box-filtered and the sampler is LINEAR_MIPMAP_NEAREST with K = −8.
  • The new palette, texels and record go before the texture list, which is rewritten as the last block.

tex.py selftest FILE adds ten textures: PSMCT32 64×32, 256×256 and 64×64 with 3 mip levels, PSMT8 128×64, 8×8, 32×16 and 256×128, and PSMT4 64×128, 16×8 and 256×8. They cover both upload paths. Decoded with tex_dump.decode_texture, every lossless case returns the input pixels exactly, and every level of the mipmapped one matches. The 256×128 gradient, quantised to 256 colours, differs by at most 8 per channel. The port decodes the same records (texturedecodesdl.cpp).

PC version

Measured over the 68 scene .TEX members of the Steam release (27,669 textures) with tools/port/tests/pc_texture_test.cpp, and read from the PC renderer's install code in the unpacked executable: 0x0048e9d0 installs a buffer, 0x0048f130 one texture, 0x0043e230 parses a record, and 0x0043f830 and 0x0043fb20 convert PALN and RGBA texels. The PC file is not a handle-table buffer.

Layout

  • Header, 16 bytes: the offset of the id table, the offset of the extra table, 0, 4. The id table holds 2,048 record offsets indexed by texture id (0: no texture). The extra table follows it, also 2,048 offsets, and ends the file. The records lie between the header and the id table.
  • Record: u32 size (from the record start to the end of its palette), the FourCC twice (stored reversed: the bytes 1TXD), u32 id (always its slot), u16 height, u16 width, u32 mip count, u32 flags, f32, u32, the NUL-terminated name, then for each level a u32 size and the texels, and for PALN a u32 colour count and the colours. No record has bytes left over.
  • Formats (all sizes powers of two, at most 1,024 a side; 18,375 chains end at a 1-pixel side, the rest stop earlier or have one level; 1 to 10 levels). Each level's size is exactly what its format needs:
FourCC Textures Texels PC upload
DXT1 10,244 BC1, 8 bytes per 4×4 block; colour 3 is transparent black when c0 ≤ c1 (680 have such texels) D3DFMT_DXT1, or converted to A8R8G8B8
DXT3 4,816 BC2, 16 bytes per block D3DFMT_DXT3, or converted
RGBA 7,683 R, G, B, A bytes A8R8G8B8 (source masks R 0xff, G 0xff00, B 0xff0000, A 0xff000000)
I8 801 one grey byte D3DFMT_L8 (grey, opaque)
U8V8 2,256 two bytes D3DFMT_A8L8: byte 0 is L, byte 1 is A
PALN 1,869 one index byte, 16 colours (1,398) or 256 (471), colours R, G, B, A A8R8G8B8
  • Flags word: 0x40 marks a tangent-space normal map (2,081 DXT3, 2,150 U8V8, and 14 I8 that cannot be one), 0x100 an animated texture (14), 0x400 face 0 of a cube map (204), 0x2 is set on 1,538 one-level interface pictures and font pages, and 0x1000 on 269 I8 textures. The installer also tests 0x80 (bump environment map), 0x800 (displacement map) and 0x400; no file sets 0x80 or 0x800. The f32 is always n/255: 1.0 in 23,527 records, 4/255 to 254/255 in the rest; the installer copies it into m_fScaleFactor, whose meaning on PC is unknown (the PS2 code never reads it). The last u32 is non-zero only on the 1,188 font pages.
  • Names are asset paths (Architecture/Walls/WallWood_10, up to 64 characters, 19,530 longer than 23) and are not unique: a material's diffuse texture, normal map and specular mask share one. 7,016 textures have no name, among them every PALN texture and 5,066 RGBA ones. The installer stores the last 23 characters in ZTextureBase::m_szName.
  • Extra table: for an animated texture, the frame count and then the frame ids (the same list the PS2 buffer's animation block holds and ZTextureDrawEntry::GetTextureFrame reads); for a cube map, 6 and then the ids of its six faces, the first being itself. Every other entry is 0. All frame ids name textures.
  • Ids do not correspond to the PS2 build's. Of 22,101 named PS2 textures, 10,120 have a PC texture of the same name in the same scene, but only 446 at the same id. The PC prims, materials and .PRP properties use the PC ids.
  • Normal maps are read in the PC shaders as tosigned(tex2D(samplerNormal, uv).agb) with z rebuilt from x and y (Common.fx, CalculateBumpNormalPS): x is the alpha (DXT3 alpha, U8V8 byte 1), y the green (DXT3 green, U8V8 byte 0), both unsigned with 128 as 0. The flagged maps average close to 128 in both. 102 of the 106 U8V8 textures without the flag have an alpha byte of 255 throughout and are grey pictures. The tangent space is the vertex tangent, binormal and normal (CalculateTangentSpace), as in the port's dot3 pass.
  • Alpha in a colour texture is only ever output alpha (OUT.col.a = CalculateAlpha(IN.Diffuse.a * cTexel.a)); whether it blends or is tested is the material's state. Specular masks are separate textures (samplerSpecularMask, read as .rgb). Of the colour textures, 680 DXT1, 1,783 DXT3, 6,146 RGBA, 4 U8V8 and 1,656 PALN have some alpha below 255 in level 0; 1,351 RGBA textures are opaque, 239 RGBA and 194 PALN have alpha 0 everywhere.

Conversion

port/engine/system/_sdl/pcdata/pctexture.{h,cpp}: PcConvertTextures(pData, lSize, &pOut, &lOut, &Report) returns a malloc'd buffer in the PS2 layout above, which ZEngineDataBase::InstallTextureBuffer, ZRenderDllSDL::CompactTextureBuffer and InstallTextureBuffer take unchanged. Handle 0 is the header; each texture is its data block, then its frame list if animated, then its 0x60-byte record; the texture list (ascending ids) is the last block and the handle table ends the buffer. Every block has one reference and is padded to 16 bytes.

The record keeps the PC id, the PC size and mip count, and the PC renderer's name (the last 23 characters). m_fOffset and m_fMoveSpeed are 0, m_fScaleFactor 1.0, m_lDrawEntryId 0 and m_lMaterialDescription 1, the disc's most common value. The sampler word is bilinear without mips (0x30000000) or LINEAR_MIPMAP_LINEAR with K = 0 (0xb0000000), plus bit 13 for a normal map. There is no palette handle and the colour count is 0, so nothing takes the texture for PSMT4 or PSMT8. m_Format is PCTEXTURE_RECORD_FORMAT | format (0x50430001 to 0x50430006), outside ZPFORMAT; no code in src/ or port/ reads m_Format, and the PS2 GS upload code (ZOldDrawPS2::TextureCreate) does not run in the port. The data block is an SPcTextureBlock: a 128-byte header (magic PCTX, format, size, level count, the PC flags word, the offset and size of each level, the palette offset and count) followed by the PC levels and palette as they are. PcTexturesFindRecord finds a record in the converted buffer by id, so a converter that knows a texture's scrolling or collision material can fill them in before the buffer is installed.

DecodeTexture (texturedecodesdl.cpp) recognises such a record by its m_Format and the block's magic and decodes each level to RGBA8 with PcTextureDecodeLevel when the texture is first drawn: BC1 and BC2 as Direct3D does, RGBA as stored, I8 as opaque grey, PALN through its palette, and a non-normal U8V8 as grey with alpha. A normal map becomes (nx, ny, nz, 255) with 128 as 0, x and y from the alpha and green as above and z rebuilt, which is the texel dot3.hlsl reads. The port keeps up to 12 supplied levels (TEXTURE_MAX_MIPS).

All 68 members convert and all 161,955 levels decode. The converted buffers are 0.14% larger than the PC members (2,362.2 MiB against 2,358.8 MiB for all 68; M00 main 92.5 MiB against 92.4 MiB). Converting takes 0.30 s for the 68, at most 12 ms for one member; it is a copy. Decoding every level of every texture to RGBA8 would take 2.5 to 3 s and 6.4 GiB for the 68 (M00 main: about 130 ms and 292 MiB). The port decodes a texture only when it is first drawn and keeps only the GPU copy, which is RGBA8: a scene whose textures are all drawn uses about three times its .TEX size in GPU memory.

zig c++ -target x86_64-windows-gnu -std=gnu++98 -O2 -w -fsigned-char -fno-strict-aliasing \
    -fno-exceptions -fno-rtti -nostdinc++ -DNON_MATCHING -Isrc -Iport tools/port/tests/pc_texture_test.cpp \
    port/engine/drawing/old/_sdl/texturedecodesdl.cpp port/engine/system/_sdl/pcdata/pctexture.cpp -o build/pc_texture_test.exe
pc_texture_test convert PC.TEX ...          # convert, compact, check every record against the PC member, decode every level
pc_texture_test compare PC.TEX PS2.TEX [-v] # pair textures by name and compare the decoded pictures
pc_texture_test fuzz PC.TEX COUNT           # damaged copies, each ending against an inaccessible page

The converter checks every offset, size and count it uses (sides up to 4,096, at most 12 levels, each level exactly its format's size, the name and the palette inside the record, the frame list inside the member). 6,000 damaged copies of saveandcontinue.TEX and the M00 loader sequence were each rejected or converted and decoded without a read past the member.

What the engine sees: PC against PS2

compare decodes both buffers through DecodeTexture and pairs a PS2 texture with the PC texture whose name ends with the PS2 name up to its #layer suffix, choosing among same-named PC textures the one of the same kind (normal map or not) that is closest. Over the 68 scene pairs, 9,947 PS2 textures are paired; the rest are loader-sequence tiles, news pages and interface pieces the PC build replaced, and textures without a name on one side.

  • Size. 9,589 pairs have the same aspect ratio: the PC texture is the same size in 1,570, twice as large each way in 6,509, four times in 1,425, eight times in 63 and half in 22. The other 358 are textures the PS2 build squeezed, most often a 512×256 PC texture into 64×64 (88). Every draw path uses normalised coordinates, so the PC size can be used as it is.
  • Colour. Resampled to the smaller size (32×32 when the aspect differs) and compared on colours multiplied by alpha, 8,120 of 9,477 colour pairs differ by less than 8 per channel on average, 1,033 by 8 to 16, 179 by 16 to 32, 130 by 32 to 64 and 15 by more; the mean is 5.6. By PC format: DXT1 6,381 pairs (mean 6.2), DXT3 2,157 (3.6), RGBA 862 (5.9), U8V8 grey 77 (3.0). Swapping red and blue would make 3 pairs closer. The pairs that differ are PS2 layers with no PC counterpart that pair with the diffuse texture of the same name (#Specular_b, #bumpA, #Square_PS2), retouched pictures (Painting_M01_01, cloud backdrops at another brightness) and PS2 textures with graded alpha where the PC DXT1 one has 1-bit alpha. PALN textures have no names; 938 have a PS2 texture of the same size in the same scene, and for 25 the closest is the same picture (mean difference below 1, for example horizontal_line_01). The colour order of RGBA and PALN comes from the installer's source masks.
  • Alpha. Of 5,201 pairs whose PS2 texture is opaque, 5 PC textures have any alpha below 255. Of 2,426 pairs where both have alpha, 2,402 agree within 32 on average. The PC colour textures therefore carry transparency where the PS2 ones do, not specular masks. The alpha that looks like a specular mask in a PC picture is the x component of a DXT3 normal map, which shares the diffuse texture's name.
  • Normal maps. 470 PS2 normal maps pair with a PC one. Correlating the components, x agrees in sign in 458 and y is negated in 380 (every well-correlated pair: typically x +0.95, y −0.95); the pairs near 0 are PS2 layers paired with the wrong PC map. The decoder keeps the PC sign, which is right with the PC vertex binormals; it is wrong if PC normal maps are drawn with PS2-convention binormals.

Not carried over

The PC .TEX has no scrolling speed and no collision material: on PC both come from elsewhere (the materials), and the converted records carry 0 and 1 until another converter sets them through PcTexturesFindRecord. A cube map is six 2D textures; face 0 keeps the cube's id. The PC f32, the 0x2 and 0x1000 flags and the PC's half-texel m_fOffset (0.5 / size) are not used. Sampler filtering and addressing on PC are per material (gm_mapDiffuseMinFilter and so on in the shaders' sampler states); clamping in the port still comes from the prim's draw mode.