Microsoft DirectX 8.1 (pixel shader versions 1.0, 1.1, 1.2, 1.3, 1.4)

mad

Multiply and add instruction. Sets the destination register to (src0 * src1) + src2.

mad dest, src0, src1, src2

Registers

Argument Description RegistersVersion
vn cn tn rn
dest Destination register x 1.0
x x 1.1, 1.2, 1.3
x 1.4
src0, src1, src2 Source register x x x x 1.0, 1.1, 1.2, 1.3
x x 1.4 phase 1
x x x 1.4 phase 2

To learn more about registers, see Registers.

Remarks

This instruction performs a multiply accumulate operation based on the following formula.

dest = src0 * src1 + src2

Example

This example is for illustration only. The C code accompanying the shader has not been optimized for performance.

// This example blends a diffuse color, a texture color and a constant color.

// The shader is shown below.
ps.1.0              // Version instruction.
tex t0              // Declare texture.
mad r0, v0, t0, v0  // Mix diffuse color and texture color.


// The following four images show the contents of the three source registers
// and the resulting output register. The output register shows the result of 
// the gradient in the center of the destination image, where the mid tones 
// appear. This is a result of adding the pixels from the center of src2 with 
// the pixel color created from the center of the product of src0*src1.

// Additional code loads a texture in texture stage 0.
LPDIRECT3DDEVICE8   m_pd3dDevice;   // initialize this pointer
LPDIRECT3DTEXTURE8  m_pTexture0;    // a pointer to the texture.
TCHAR               strPath[512] = "textureFile.jpg";

D3DUtil_CreateTexture( m_pd3dDevice, strPath, &m_pTexture0, D3DFMT_R5G6B5 );
m_pd3dDevice->SetTexture( 0, m_pTexture0 );