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

cnd

Conditionally chooses between src1 and src2, based on the comparison src0 > 0.5.

cnd 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 Source register r0.a 1.0, 1.1, 1.2, 1.3
src1, src2 xx x x 1.0, 1.1, 1.2, 1.3
src0, src1, src2 x x 1.4 phase 1
xx x 1.4 phase 2

To learn more about registers, see Registers.

Remarks

For versions 1.0 to 1.3, src0 must be r0.a. Version 1.4 has no such restriction.

// Version 1.1 to 1.3
if (r0.a > 0.5)
  dest = src1
else
  dest = src2

// Version 1.4 compares each channel separately.
for each component in src0
{
   if (src0.component > 0.5)
     dest.component = src1.component
   else
     dest.component = src2.component
}

Example

These examples show a four-channel comparison done in a version 1.4 shader, as well as a single-channel comparison possible in a version 1.1 shader.

// Version 1.4 compares all four components.
ps.1.4
def c0, -0.5, 0.5, 0, 0.6
def c1  0,0,0,0
def c2  1,1,1,1

cnd r1, c0, c1, c2   // r0 contains 1,1,1,0 because,
// r1.x = c2.x because c0.x ≤ 0.5
// r1.y = c2.y because c0.y ≤ 0.5
// r1.z = c2.z because c0.z ≤ 0.5
// r1.w = c1.w because c0.w > 0.5


// Version 1.1 to 1.3 compares against the replicated alpha channel 
// of r0 only.
ps.1.1
def c0, -0.5, 0.5, 0, 0.6
def c1  0,0,0,0
def c2  1,1,1,1
mov r0, c0
cnd r1, r0.a, c1, c2   // r1 gets assigned 0,0,0,0 because 
// r0.a > 0.5, therefore r1.xyzw = c1.xyzw

// This example compares two values, A and B, to each other. 
// This example assumes A is loaded into v0 and B is loaded into v1.
// Both A and B must be in the range of -1 to +1, and since the
// color registers (vn) are defined to be between 0 and 1,
// the restriction happens to be satisfied in this example.

// The shader is shown below.
ps.1.0                // version instruction
sub r0, v0, v1_bias   // r0 = A - (B - 0.5)
cnd r0, r0.a, c0, c1  // r0 = ( A > B ? c0 : c1 )

// The result in r0 is c0 if A > B. Otherwise, the result in r0 is c1.