Ciubix8513
Ciubix8513

Reputation: 61

Matrix transposition before passing to the vertex shader

I'm very confused about passing matrices to a vertex shader, as far as i know you have to transpose matrices before passing them to a vertex shader.

But my world matrix when i pass it to a vertex shader did not work correctly, it worked fine with scaling and rotation but translation caused weird visual glitches. So through trial and error i found that this problem could be solved by not transposing the world matrix before passing it to a vertex shader, but when i tried the same with view and projection matrices nothing worked.

I don't understand why I'm very confused, do i have to transpose all matrices except world matrices?

Upvotes: 1

Views: 853

Answers (1)

Soonts
Soonts

Reputation: 21936

It depends on the code of your shaders.

Without any of the /Zpr or /Zpc HLSL compiler options, when your HLSL code says pos = mul( matrix, vector ) the matrix is expected to be column major. When HLSL code says pos = mul( vector, matrix ), the matrix is expected to be row major.

Column major matrices are slightly faster to handle on GPUs, following reasons.

  1. The HLSL for multiplication compiles into four dp4 instructions. Dot products are fast on GPUs, used everywhere a lot, especially in pixel shaders.

  2. VRAM access pattern is slightly better. If you want to know more, the keyword is “memory coalescing”, most sources are about CUDA but that thing is equally applicable to graphics.

That’s why Direct3D defaults to column major layout.

Upvotes: 1

Related Questions