Use on matrix math in shader?

Started by
1 comment, last by Bacterius 12 years ago
"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?
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.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement