HLSL trick #2

Saving some expensive instruction in shader model 2…
(Trick #1 is described here)

Instead of, e. g.,

float getTotalDiffuse()
{
 float l1 = getDiffuse(g_light1);
 float l2 = getDiffuse(g_light2);
 float l3 = getDiffuse(g_light3);
 float l4 = getDiffuse(g_light4);
 return l1+l2+l3+l4;
}

try

float getTotalDiffuse()
{
 float4 l = {getDiffuse(g_light1),
              getDiffuse(g_light2),
              getDiffuse(g_light3),
              getDiffuse(g_light4)};
 return dot (l, float4(1,1,1,1));
}

The trick is that sometimes if you want to sum some floats (or vectors), it may be cheaper to use dot product with vector (1, … , 1)

Advertisements

Tags: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: