Jump to content
  • Advertisement
Sign in to follow this  
reaperrar

Use on matrix math in shader?

This topic is 2232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

"Use own matrix math in shader?"*
Was just curious, does anyone think it's worth using your own matrix math in say a hlsl shader?

I'm assuming:


float3x3 g_mWorld;
float3x3 g_mViewProj;

mul( g_mWorld, g_mViewProj);


Results in:


g_mModel[0][0] * g_mViewProj[0][0] + g_mModel[1][0] * g_mViewProj[0][1] + g_mModel[2][0] * g_mViewProj[0][2]
g_mModel[0][1] * g_mViewProj[0][0] + g_mModel[1][1] * g_mViewProj[0][1] + g_mModel[2][1] * g_mViewProj[0][2]
g_mModel[0][2] * g_mViewProj[0][0] + g_mModel[1][2] * g_mViewProj[0][1] + g_mModel[2][2] * g_mViewProj[0][2]
g_mModel[0][0] * g_mViewProj[1][0] + g_mModel[1][0] * g_mViewProj[1][1] + g_mModel[2][0] * g_mViewProj[1][2]
g_mModel[0][1] * g_mViewProj[1][0] + g_mModel[1][1] * g_mViewProj[1][1] + g_mModel[2][1] * g_mViewProj[1][2]
g_mModel[0][2] * g_mViewProj[1][0] + g_mModel[1][2] * g_mViewProj[1][1] + g_mModel[2][2] * g_mViewProj[1][2]
g_mModel[0][0] * g_mViewProj[2][0] + g_mModel[1][0] * g_mViewProj[2][1] + g_mModel[2][0] * g_mViewProj[2][2]
g_mModel[0][1] * g_mViewProj[2][0] + g_mModel[1][1] * g_mViewProj[2][1] + g_mModel[2][1] * g_mViewProj[2][2]
g_mModel[0][2] * g_mViewProj[2][0] + g_mModel[1][2] * g_mViewProj[2][1] + g_mModel[2][2] * g_mViewProj[2][2]


But couldn't we save time by using our own math?


g_mModel[0][0] * g_mViewProj[0][0] + g_mModel[1][0] * g_mViewProj[0][1]
g_mModel[0][1] * g_mViewProj[0][0] + g_mModel[1][1] * g_mViewProj[0][1]
0.0f
g_mModel[0][0] * g_mViewProj[1][0] + g_mModel[1][0] * g_mViewProj[1][1]
g_mModel[0][1] * g_mViewProj[1][0] + g_mModel[1][1] * g_mViewProj[1][1]
0.0f
g_mModel[0][0] * g_mViewProj[2][0] + g_mModel[1][0] * g_mViewProj[2][1] + g_mModel[2][0]
g_mModel[0][1] * g_mViewProj[2][0] + g_mModel[1][1] * g_mViewProj[2][1] + g_mModel[2][1]
1.0f


That would save 8 addition and 10 multiplications per matrix * matrix operation per object. The same could be done for vertex transforms too. Is it a worthy optimization?

Share this post


Link to post
Share on other sites
Advertisement
You'd have to profile it to be sure, but my gut feeling is that things like Matrix Math would already be optimized in the shader since its a VERY common GPU operation.

Share this post


Link to post
Share on other sites
I'd think the mul operation in HLSL would be hardware-accelerated with special SIMD units for matrix multiplication since that's the single most common operation in a vertex shader. Try and look at the GPU assembly output and see what "mul" actually does, and then time them to see if it's faster? It could be but I doubt it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!