Direct3D 8 - Pixel Shader - Instructions - mad

Last edited 2026-02-20


The mad instruction multiplies x and y, then adds z and assigns the whole thing to r.

The advantage of this instruction is that it uses 1 slot, if multiplication and addition were performed separately by the mul and add functions, they would use 2 instruction slots.


The mad syntax looks like this:

mad r, x, y, z;


(This instruction returns a value from the given mathematical function: r = (x * y) + z)


r: The entire calculation will be returned to this value

x: The first value of the multiplication

y: The second value of the multiplication

z: This value is added after the multiplication.

Example code:


texture T_Image;                            // <- Main texture

technique tech_main
{
    pass P0
    {
        Texture[0] = <T_Image>;
        PixelShader = asm
        {
            ps.1.1;                         // <- Pixel shader version


            def c0, 0.3, 0.15, 0.15, 0.0;   // <- Declaration of a constant color
            
            tex t0;                         // <- Load the T_Image texture

            mad r0, t0, c0, c0;             // <- Multiplies the colors from T_Image with the declared color, then adds the declared color
                                            // "mad" does the same thing as this, but uses one instruction:
                                            //  mul r0, t0, c0;
                                            //  add r0, r0, c0;

            mov r0.a, t0.a;
        };
    }
}



This example requires the following in the .XML file:
  • Flag <dx8>yes</dx8>


(More information about .XML files can be found here)

Details table:

Type

Instruction

Description

Instruction slots

ps.1.0

ps.1.1

ps.1.2

ps.1.3

ps.1.4

Note

asm

mad

Multiply and add

1

Yes

Yes

Yes

Yes

Yes

Assigns the sum of two values ​​to the target.

Created with the Personal Edition of HelpNDoc: Don't be left in the past: convert your WinHelp HLP help files to CHM with HelpNDoc