Difference between revisions of "Texture Native Struct"

From GTAMods Wiki
Jump to navigation Jump to search
(PlayStation 2)
(Replaced content with "{{request-delete}}")
(Tag: Replaced)
 
(31 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{RW Section|Struct|0x0001}}
+
{{request-delete}}
 
 
''This article is about the [[Struct (RW Section)|Struct]] section accompanied by a [[Texture Native (RW Section)|Texture Native]] parent section.''
 
 
 
The '''Texture Native Struct''' section can usually be found in [[texture archive]]s used by the PC, XBOX and PS2 versions of the GTA III game trilogy. They are the most important sections inside a <code>.txd</code> file since they contain the names, dimensions and the actual image data for textures. The typical location in the file's section hierarchy is ''[[Texture Dictionary (RW Section)|Texture Dictionary]] > [[Texture Native (RW Section)|Texture Native]] > Struct''.
 
 
 
==Images Header==
 
 
 
There is a ''86 byte'' header with general image information:
 
 
 
<source lang="cpp">struct NativeTexturePC_Header
 
{
 
    struct {
 
        unsigned int platformId;
 
        unsigned int filterMode : 8;
 
        unsigned int uAddressing : 4;
 
        unsigned int vAddressing : 4;
 
        char name[32];
 
        char maskName[32];
 
    } TextureFormat;
 
 
 
    struct {
 
        unsigned int rasterFormat;
 
        union {
 
            D3DFORMAT d3dFormat; // SA
 
            unsigned int hasAlpha // GTA3 & VC
 
        };
 
        unsigned short width;
 
        unsigned short height;
 
        unsigned char depth;
 
        unsigned char numLevels;
 
        unsigned char rasterType;
 
        union {
 
            unsigned char compression; // GTA3 & VC
 
            struct { // SA
 
                unsigned char alpha : 1;
 
                unsigned char cubeTexture : 1;
 
                unsigned char autoMipMaps : 1;
 
                unsigned char compressed : 1;
 
            };
 
        };
 
    } RasterFormat;
 
};</source>
 
 
 
<source lang="cpp">struct NativeTexturePS2_Header
 
{
 
    struct {
 
        unsigned int platformId;
 
        unsigned int filterMode : 8;
 
        unsigned int uAddressing : 4;
 
        unsigned int vAddressing : 4;
 
    } TextureFormat;
 
 
 
    // CHUNK_STRING for name
 
    // CHUNK_STRING for alpha-mask name
 
 
 
    //size: 64 bytes
 
    struct RasterFormat
 
    {
 
        unsigned int width, height;
 
        unsigned int depth;
 
        unsigned int rasterFormat;
 
       
 
        // See http://mtz01-a.stanford.edu/resources/SonyPS2/pdf00005.pdf
 
        unsigned long long tex0;    // PS2 TEX0 GS register
 
        unsigned long long tex1;    // PS2 TEX1 GS register
 
        unsigned long long miptbp1;  // PS2 MIPTBP1 GS register
 
        unsigned long long miptbp2;  // PS2 MIPTBP2 GS register
 
 
 
        unsigned int texelDataSectionSize;
 
        unsigned int paletteDataSectionSize;
 
        unsigned int gpuDataAlignedSize;      // size of mipmap texels + palette texels aligned to 2048
 
 
 
        unsigned int skyMipmapVal;    // always 4032
 
    };
 
};</source>
 
 
 
;Platform ID: This is '''8''' for GTA3 and VC on the PC, '''9''' for GTA SA on the PC, '''PS2\0''' FourCC on PS2 (any game version), and '''5''' for GTA3/VC/SA on the XBOX.
 
;Filter Mode:
 
FILTER_NONE                0x00
 
FILTER_NEAREST            0x01
 
FILTER_LINEAR              0x02
 
FILTER_MIP_NEAREST        0x03
 
FILTER_MIP_LINEAR          0x04
 
FILTER_LINEAR_MIP_NEAREST  0x05
 
FILTER_LINEAR_MIP_LINEAR  0x06
 
 
 
;Addressing Mode:
 
WRAP_NONE    0x00
 
WRAP_WRAP    0x01
 
WRAP_MIRROR  0x02
 
WRAP_CLAMP    0x03
 
 
 
;Raster Format:
 
FORMAT_DEFAULT        0x0000
 
FORMAT_1555            0x0100 (1 bit alpha, RGB 5 bits each; also used for DXT1 with alpha)
 
FORMAT_565            0x0200 (5 bits red, 6 bits green, 5 bits blue; also used for DXT1 without alpha)
 
FORMAT_4444            0x0300 (RGBA 4 bits each; also used for DXT3)
 
FORMAT_LUM8            0x0400 (gray scale)
 
FORMAT_8888            0x0500 (RGBA 8 bits each)
 
FORMAT_888            0x0600 (RGB 8 bits each, D3DFMT_X8R8G8B8)
 
FORMAT_555            0x0A00 (RGB 5 bits each - rare, use 565 instead, D3DFMT_X1R5G5B5)
 
 
FORMAT_EXT_AUTO_MIPMAP 0x1000 (RW generates mipmaps)
 
FORMAT_EXT_PAL8        0x2000 (2^8 = 256 palette colors)
 
FORMAT_EXT_PAL4        0x4000 (2^4 = 16 palette colors)
 
FORMAT_EXT_MIPMAP      0x8000 (mipmaps included)
 
 
 
;Depth: 4, 8, 16 or 32; 8 usually comes with palette
 
;Compression: for more information, refer to [[Wikipedia:S3 Texture Compression|S3 Texture Compression]].
 
 
 
==Raster Data==
 
 
 
The header is followed by an optional palette and the raster data (pixels), according to the Raster Format flags.
 
 
 
If Raster Format is <code>FORMAT_EXT_PAL8 | FORMAT_8888</code> or <code>FORMAT_EXT_PAL8 | FORMAT_888</code>, then a ''color palette'' of (up to) 256 RGBA colors is included. This is used in GTA3 quite often. The raster's one byte for each pixel is an index into the palette.
 
 
 
1024 byte  - BYTE[256][4] - palette of RGBA colors
 
4 byte    - DWORD        - raster size
 
w*h*1 byte - BYTE[w*h]    - pixels, 1 byte each
 
 
 
If no palette is included, the header is followed by raster size and data. If ''[[Wikipedia:mipmap|mipmap]]s'' are included (<code>FORMAT_EXT_MIPMAP</code>) then this is repeated for each mipmap. With increasing mipmap levels the raster size is decreased by the factor 4 (side lengths halved). Small initial textures can result in mipmaps of the size 0. For ''uncompressed'' images:
 
 
 
4 byte        - DWORD            - raster size
 
w*h*bpp/8 byte - BYTE[w*h][bpp/8] - pixels
 
 
 
'''Note:''' FORMAT_888(8) rasters use BGRA colors!
 
 
 
If DXT compression is used, blocks of 4x4 pixels occupy only 8 or 16 bytes (depending on the type).
 
 
 
===PlayStation 2===
 
 
 
[[File:ps2texalloc.png|thumb|Example of texture allocation.]]
 
 
 
The image and palette data is both handled in a (generic) texture format. There are special raster flags reserved.
 
 
 
<source lang="cpp">
 
#define RASTER_SWIZZLED          0x10000    // the images are "swizzled"
 
#define RASTER_HAS_TEXEL_HEADERS 0x20000    // the image data is preceeded by headers
 
</source>
 
 
 
If the rasterFlags contain the RASTER_HAS_TEXEL_HEADERS flag, then each image data and palette data are a chain of GIF packets that are directly uploaded to the GS. Each mipmap consists of one register list and the image data. The GIFtag struct that is the header of each packet could look like this.
 
 
 
<source lang="cpp">struct GIFtag
 
{
 
    unsigned long long nloop : 15;
 
    unsigned long long eop : 1;
 
    unsigned long long pad1 : 30;
 
    unsigned long long pre : 1;
 
    unsigned long long prim : 11;
 
    unsigned long long flg : 2;
 
    unsigned long long nreg : 4;
 
   
 
    unsigned long long regs;
 
 
 
    uint32 getRegisterID(uint32 i) const
 
    {
 
        assert(i < 16);
 
 
 
        unsigned long long shiftPos = i * 4;
 
 
 
        return ( this->regs & ( 0xF << shiftPos ) ) >> shiftPos;
 
    }
 
 
 
    void setRegisterID(uint32 i, uint32 regContent)
 
    {
 
        assert(i < 16);
 
 
 
        unsigned long long shiftPos = i * 4;
 
 
 
        this->regs &= ~( 0xF << shiftPos );
 
 
 
        this->regs |= regContent >> shiftPos;
 
    }
 
};</source>
 
 
 
By default, each texture that is made of GIF packets has the '''TRXPOS''', '''TRXREG''' and '''TRXDIR''' registers including the '''TEX0''', '''TEX1''', '''MIPTBP1''' and '''MIPTBP2''' registers from the native header. This practically makes the image and palette headers look like this.
 
 
 
<source lang="cpp">
 
// Note: this is just an illustration, not an actual header.
 
struct commonTexelHeader
 
{
 
    GIFtag regListTag;
 
    /*
 
        regListTag.flg = 0;
 
        regListTag.eop = false;
 
        regListTag.pre = false;
 
        regListTag.prim = 0;
 
        regListTag.nreg = 1;
 
        regListTag.regs = 0;
 
        regListTag.setRegisterID(0, 0xE);
 
        regListTag.pad1 = 0;
 
    */
 
 
 
    unsigned long long trxpos;      // ssax = 0, ssay = 0, dsax = ?, dsay = ?, dir = 0
 
    unsigned long long trxpos_id;    // = 0x51
 
 
 
    unsigned long long trxreg;      // width = ?, height = ?
 
    unsigned long long trxreg_id;    // = 0x52
 
 
 
    unsigned long long trxdir;      // xdir = 0
 
    unsigned long long trxdir_id;    // = 0x53
 
 
 
    GIFtag imgDataTag;
 
    /*
 
        imgDataTag.flg = 2;
 
        imgDataTag.eop = false;
 
        imgDataTag.pre = false;
 
        imgDataTag.prim = 0;
 
        imgDataTag.nreg = 0;
 
        imgDataTag.regs = 0;
 
        imgDataTag.pad1 = 0;
 
    */
 
};
 
</source>
 
For more details, see the [https://www.dropbox.com/s/onjaprt82y81sj7/EE_Users_Manual.pdf| Emotion Engine User Manual] under the GIF Interface chapter.
 
 
 
The actual image data is stored afterward. It is packed in either PSMCT32, PSMCT16, PSMT8 or PSMT4 internal GS types according to the documentation and game version. So before you can get usable images in linear format out of PS2 textures, you have to "unswizzle" them. This is done by permuting the data.
 
 
 
==Related Pages==
 
*[[Texture_%28RW_Section%29]]
 
*[[Sky_Mipmap_Val_(RW_Section)]]
 

Latest revision as of 12:04, 11 September 2020

Delete.png This article has been requested for deletion
You may suggest different in the discussion page.