Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

TLM : Why the dot() ?

Sign in to follow this  


So, in reading my comments I came across this one by jjd;

Original post by jjd
Ok, I'm a little confused here about the dot function thing. I know that the two formulations are equivalent, but why create an object and use a function call to evaluate expressions that you already had in a clear, efficient form?

So, I figured I'd take a few mins to explain (partly because I'm in full on procastination mode right now...), this might come across as a little GPU 101 so I applogise if I pitch the answer a little low [smile]

The long and the short of it was, while the expression might be clear to a human and good when dealing with a CPU (assuming non-SIMD code) it's not remotely efficient for a GPU. GPUs tend to like vectored operations, dealing with floats isn't great, dealing with floats and sourcing them from the same register is major bad karma indeed (thus my brain twitching).

Now, I don't know for certain how the compiler dealt with the code I gave it, however given it's constraints it probably went something like this;

float SinkWest = 0.5 * (east.x + east.y - east.z + east.w);

PARM half = 0.5
temp SinkWest
mul SinkWest.x east.x half.x
madd SinkWest.x east.y half.x SinkWest.x
madd SinkWest.x east.w half.x SinkWest.x
madd SinkWest.x -east.z half.x SinkWest.x

However, a dotproduct is a common operation, as such it is reasonable to assume that it will be fast to execute, and when done as a dot product the above code would look like this

float SinkWest = dot(east,vec4( 0.5, 0.5,-0.5, 0.5));

PARM coef = {0.5, 0.5, -0.5, 0.5}
temp SinkWest
dp4 SinkWest.x east, coef

Job done.

It might well cost 2 constants, however we can probably execute 4 dot products in the same space of time it takes as to do the first method and it's probably much more prefered by the GPU in general.

I hope that clears up my reasoning behind it [smile]
Sign in to follow this  

1 Comment

Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!