Wii Graphics Code

From Custom Mario Kart
Jump to: navigation, search
Under Construction
This article is not finished. Help improve it by adding accurate information or correcting grammar and spelling.

The Wii graphics card uses a system of command code which is transimitted in several ways by BRRES Files. This page is an instruction set listing of the command code. The instruction set is initially being created to just cover codes found in Mario Kart Wii.

Code

There are several types of command code. Graphics code is sent by first sending a byte to indicate which type of command is being used, and then multiple bytes to indicate the command.

Command Command Length Description
0x00 1 NOP (No Operation)
0x08 6 Load CP (Command Processor) Register
0x10 Varies Load XF (Transform Unit) Register
0x20 5 Load Indexed 3x3 Position Matrix
0x28 5 Load Indexed 3x3 Normal Matrix
0x30 5 Load Indexed Texture Matrix
0x38 5 Load Indexed Light Object
0x40 Varies Call Display List
0x44 1 Unknown
0x48 1 Invalidate Vertex Cache
0x61 5 Load BP (Blitting Processor) Register
0x80 Varies Draw Quads
0x90 Varies Draw Triangles
0x98 Varies Draw Triangle Strip
0xa0 Varies Draw Triangle Fan
0xa8 Varies Draw Lines
0xb0 Varies Draw Line Strip
0xb8 Varies Draw Points

Command length is the total length including the first command byte.

[0x08] Load CP (Command Processor) Register

The command processor commands are found in the vertex declaration section of polygons in MDL0 files. The command processor command is structured as follows.

0 1 2 3 4 5
0x08 Sub Command Parameters

The values for the sub command and it's parameters are listed below. In the table Pos means position; TexN means texture coordinate N; ColN means color N; Norm means normal; TMN means texture coordinate matrix N; and PM means position matrix. Measured in bits with lsb first.

Sub Command Parameters Description
0x50
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
PM TM0 TM1 TM2 TM3 TM4 TM5 TM6 TM7 Pos Norm Col2 Col1
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Col1 Unused

The one bit fields indicate whether or not the 8 bit index is present or not. For the 2 bit fields, 00 indicates not present, 01 indicates direct, 10 indicates 8 bit index, 11 indicates 16 bit index.

Vertex Declaration Setup. Controls how data is encoded into the draw command streams. Indexed data references the relevant group. Direct data indicates the data is embeded in the command rather than indexed.
Values are encoded into draw commands in the order they appear here.
0x60
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Tex0 Tex1 Tex2 Tex3 Tex4 Tex5 Tex6 Tex7
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Unused

00 indicates not present, 01 indicates direct, 10 indicates 8 bit index, 11 indicates 16 bit index.

0x70
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Pos E Pos Format Pos Divisor Norm E Norm Format Col0 E Col0 Format
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Col0 Format Col1 E Col1 Format Tex0 E Tex0 Format Tex0 Divisor Dequant NormI3

The E value for each item is Unknown. The format value should match the formats values in the headers of the respective groups in the MDL0 file. The divisors should also match these. The Dequant and NormI3 values are Unknown.

Vertex Declaration Setup. Controls how data is encoded into the draw command streams. Indicates how the elements are stored either at the indexed location, or directly in the draw command. This must match the values in the groups of the MDL0 file.
0x80
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Tex1 E Tex1 Format Tex1 Divisor Tex2 E Tex2 Format Tex2 Divisor
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Tex2 Divisor Tex3 E Tex3 Format Tex3 Divisor Tex4 E Tex4 Format Unused

The E value for each item is Unknown. The format value should match the formats values in the headers of the respective groups in the MDL0 file. The divisors should also match these.

0x90
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Tex4 Divisor Tex5 E Tex5 Format Tex5 Divisor Tex6 E Tex6 Format
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Tex6 Format Tex6 Divisor Tex7 E Tex7 Format Tex7 Divisor

The E value for each item is Unknown. The format value should match the formats values in the headers of the respective groups in the MDL0 file. The divisors should also match these.

[0x10] Load XF (Transform Unit) Register

The XF (Transform Unit) register set commands are found in both the vertex declarations, and in some material files. They are used in vertex declarations to indicate how many of each item the transform unit will recieve, and in materials to setup texture matricies and post effect matricies. The format for each command is as follows. Measured in bytes.

0 1 2 3 4
0x10 Transfer Size Address

The address is the XF register to set. The transfer size is the amount of data being sent. This is always 1 less than the actual amount. The amount is measured in 4 byte units. The values follow directly after the command structure. The meanings of XF register addresses are given below. Measured in bits with lsb first.

Address Parameters Description
0x1008
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Color Count Normal Count Texture Coordinate Count Unused
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Unused

The counts refer to the amount of each type of item used in the draw commands.

Transform unit input size. Must match the rest of the vertex declaration in polygons in MDL0 Files.
0x104N
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Unknown Projection Input form Unknown Tex gen type Source row Generated texcoord to use Light index to use
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
Light index to use Unused

Projection is either ST projection or STQ projection. STQ projection allows for perspective correction.

Input form is either AB11 or ABC1 and controls perspective as well, in most cases AB11 is used with ST projection and ABC1 with STQ projection.

The texture generation type can have the following values: 0 for regular, 1 for emboss mapping and 2 and 3 for two variants of color-based mapping.

Source row determines how the texture coordinates are transformed: possible values are: vertex geometry (0), normals (1), colors (2), binormalT (3), binormalB (4) and UV coordinates 1 to 7 (5-12).

Sets the Nth texture matrix.
0x105N Unknown Sets the Nth post effect matrix.

[0x61] Load BP (Blitting Processor) Register

The BP (Blitting Processor) register set commands are used by materials and shaders in MDL0 files. They control the look of the textures, by controlling the operation the graphics card performs on each vertex in order to determine its color. Each command has the following structure. Measured in bytes.

0 1 2 3 4
0x61 Address Value

The address is the BP register to set. The meanings of XF register addresses are given below. Measured in bits with lsb first.

Address Parameters Description
0x27
00 01 02 03 04 05 06 07 08 09 0a 0b
Map 0 Coord 0 Map 1 Coord 1
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Map 2 Coord 2 Map 3 Coord 3

All values are indices to either a texture map or a texture coordinate of a material.

Sets the indirect texture sources

0x28-0x2f
00 01 02 03 04 05 06 07 08 09 0a 0b
Unknown Unknown Tex0 E RAS0 value Unused
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Unknown Unknown Tex1 E RAS1 value Unused

TexN E sets whether reading the Nth texture is enabled. N is the value in the table + the instrction's lowest 3 bits multiplied by 4 (e.g. 0x29 tex0 is the 2nd texture). The ras values use a similar index, and represent which value to use in pixel shader calculations for the ras constant. The values are 000: Col0; 001: Col1; 101: AlphaBump; 110: AlphaBump Corrected; 111: 0.

Controls whether or not texture reading is enabled, and the value for the RAS constants in pixel shader calculations.
0x41
00 01 02 03 04 05 06 07 08 09 0a 0b
Enable Alpha Unknown Blend Destination Blend Soruce Unknown
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Unknown Unused

The values for blend source and destination control the weighting of the current and new pixels during alpha blending. The values for destination are: 000: 0; 001: 1; 010: Source Color; 011: Inverse Soruce Color; 100: Source Alpha; 101: Inverse Source Alpha; 110: Destination Alpha; 111: Inverse Destination Alpha. The values for blend source are the same except it uses destination values for 010 and 011.

Controls whether or not alpha blending is enabled, and if so how it occurs.
0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce
00 01 02 03 04 05 06 07 08 09 0a 0b
Argument A Argument B Argument C
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Argument D Bias Op Clamp Shift Dest

The command performed on each layer by the graphics card obeys the format:
Dest = Shift * (ArgD Op lerp(ArgA, ArgB, ArgC) + Bias)
The values for dest are 00:pixel output; 01:temp0; 10:temp1; and 11:temp2.
The values for shift are 00:1; 01;2; 10:4; 11:0.5;.
The values for the args are 0000:pixel output; 0001:pixel output alpha; 0010:temp0; 0011:temp0 alpha; 0100:temp1; 0101:temp1 alpha; ; 0110:temp2; 0111:temp2 alpha; 1000:tex; 1001:tex alpha; 1010:ras; 1011:ras alpha; 1100:1; 1101:0.5; 1110:const; 1111:0.
The values for bias are 00:0; 01:0.5; 10:-0.5; 11:special case.
If the operation bit is set, Op is - otherwise +. If the Clamp bit is set, the result is clamped to be between 0 and 1.

Controls how color is blended in each layer of the coloring operation. The number of the command indicates which layer is being controlled, with 0xce being the last layer. These layers also correspond to which texture is read at each stage.
0xc1, 0xc3, 0xc5, 0xc7, 0xc9, 0xcb, 0xcd, 0xcf
00 01 02 03 04 05 06 07 08 09 0a 0b
Unknown Unknown Argument A Argument B Argument C
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Argument C Argument D Bias Op Clamp Shift Dest

The command performed on each layer by the graphics card obeys the format:
Dest = Shift * (ArgD Op lerp(ArgA, ArgB, ArgC) + Bias)
The values for dest are 00:pixel output; 01:temp0; 10:temp1; and 11:temp2.
The values for shift are 00:1; 01;2; 10:4; 11:0.5;.
The values for the args are 000:pixel output; 001:temp0; 010:temp1; 011:temp2; 100:tex; 101:ras; 110:const; 111:0.
The values for bias are 00:0; 01:0.5; 10:-0.5; 11:special case.
If the operation bit is set, Op is - otherwise +. If the Clamp bit is set, the result is clamped to be between 0 and 1.

Controls how alpha is blended in each layer of the coloring operation. The number of the command indicates which layer is being controlled, with 0xcf being the last layer. These layers also correspond to which texture is read at each stage.
0xf6-0xfd
00 01 02 03 04 05 06 07 08 09 0a 0b
Swap 1 Swap 2 Color 0 idx Alpha 0 idx
0c 0d 0e 0f 10 11 12 13 14 15 16 17
Alpha 0 idx Color 1 idx Alpha 1 idx

Swaps the color channel defined by the table indices with the channel defined in swap N. Swap N has the following values: 00: red, 01: green, 10: blue,11: alpha. The table index is determined by the least significant 3 bits of the address.

Controls how the swap mode table is accessed when rendering a texture on a polygon.
0xfe This address can be used to limit to which bits of BP registers is actually written to. This mask is reset after the next BP write.

Draw Commands

The draw commands (0x80, 0x90, 0x98, 0xa0, 0xa8, 0xb0, 0xb8) are found in the vertex data section of MDL0 files. The draw commands all obey the same basic format. They have a 16 bit count of the amount of vertices in the command directly after the command byte. The vertex data then follows. The format of this varies, and is set using the 0x08 Command Processor commands. The order of items is the same as the order in the 0x08:0x50 and 0x08:0x60 commands.