PS2 Linux Programming
2D Texture Mapping
This tutorial will provide guidance on the use of 2D texture mapping. An 8-bit bitmap files of size 256x256 pixels will be load into the main memory of the PS2 then transferred to GS memory.
The PS2 comes with 4MB (4096K) of video memory, often called VRAM, GSMem, or VMEM. This is a relatively small amount of memory and using it requires careful thought and design. Within the architecture of the PS2 it is possible to transfer textures from main memory to VRAM at high speed (maximum rate of 2.4Gb/sec) and this transfer can be maintained successfully within the game loop. As will become evident, the architectural structure of the PS2 has implications on how graphics/games application are designed - considerable differences exist for example, between applications written for the PS2 and applications written for a PC.
Under SPS2 in VESA graphics mode, the front and back frame buffers are 640*480*4 = 1200K making a total of 2400K of video memory being used for the frame buffers. The Z Buffer is also 640*480*4 = 1200K, so out of the 4096K of video memory 3600K is already used up, leaving just 496K for textures. The scenario is even worse if the PAL video mode is used. PAL buffers are 640 *512*4 = 1280K. This means that 3840K of video memory is used for the frame and z buffers leaving only 256K for textures.
In the texture loading code provided, two texture buffers are provided each of 128k in size. This assumes that in the worst case, PAL is being used and only 256k of VMEM is available for use. Two constants are defined in PS2Defines which specify the start of each texture buffer in VMEM, these being TEXBUF0 and TEXBUF1.
The texture loading code is contained in the files texture.cpp and texture.h. The loading code is in three parts: the first method LoadBitmap() loads a bitmap file from disk into SPS2 allocated memory; the second method UpLoad() uploads or transfers the image data from main memory into graphics memory so that it can be used by the graphics synthesiser, and the third method Select() selects the required texture for use by configuring the appropriate GS registers.
In the program code (main.cpp) the texture is uploaded into GSmem every frame. A GS packet is also built every frame to display a textured sprite on screen. The additional flags in the PRIM field of the GIFtag that must be set are the TME (parameter 3: texture map enable), which is set to ON, or 1. Since 2D drawing is being done U and V coordinates are used to specify the texture coordinates. The default setting is STQ, so to change this the FST field (parameter 7) of the PRIM register is set to 1 for UV. The GS batch is configured to contain the vertex colour first (GIF_REG_RGBAQ) then GIF_REG_UV before GIF_REG_XYZ2. The appropriate information is put into the primitive data, which comes after the GIFtag.
UV coordinates are very simple. UV (0, 0) corresponds to the top left of the texture. Each UV coordinate corresponds to a pixel right and down respectively. So with the 256 * 256 pixel textures the UV coordinate (255, 255) would be the pixel in the bottom right. UV coordinates should be given to the GS in 12:4 fixed point number format, so the numbers must be shifted left by 4.
Texture mapping has been illustrated allowing for the creation of PS2 Linux based games using texturing and other techniques such as texture animation, which will be described in later tutorials.
Dr Henry S Fortuna
University of Abertay Dundee