Trying to reverse engineer shader code, having a hard time understanding what is going on in vertex shader
I am pretty new to graphics programming (and not super great at math) and I am trying to reverse engineer some shader code from the game to understand its inner workings but I have a hard time understanding why it does transformations using those formulas and, what is more important, I can't understand the logic behind the values stored in buffers, mostly SSBO. Can someone help me?
SSBO values:
ssbo0_uvec4_1: 0.99755, -0.02315, -0.06597, 0.03339
ssbo0_uvec4_2: -0.00643, 0.90921, -0.41628, -1.11786
ssbo0_uvec4_3: 0.06962, 0.41569, 0.90684, -1.18517
buffer values:
cbuf3_vec_3: 1.3580, 0, -0.00052, 0
cbuf3_vec_4: 0, 2.41421, 0.00092, 0
cbuf3_vec_5: 0, 0, -1.0001, -0.7
cbuf3_vec_6: 0, 0, -1, 0
The code: https://pastebin.com/GftRLcE8
Posted Dec 29, 2022, 4:26 PM UTC
Those fma instructions are fused multiply adds, implementing a n element dot product, eg.
x * x + y * y + z * z + w * w. If you chain enough dot products like this together, you end up with a matrix multiply.Essentially you are looking at something like
mvp_matrix * vec4(vertex, 1)(or similar) broken down into its individual instructions. GPUs are really good at fused multiply adds, so a multiplication and addition fused together. And it turns out fma’s are perfect building blocks for dot products which are used in graphics programming everywhere. The fact that dot products are essentially dirt cheap is proof that god loves graphics programmers by the way.All hail Joseph Lagrange
Thanks, that definitely helps understand what those formulas do, although the idea behind SSBO values is still something that I can't figure out.
Supplying
projection * view * modelseems to be the only thing that produces at least some result, but it definitely seems like it's not working properly with that. The fact that SSBO seems to be 3x4 matrix while transformation matrices are 4x4 makes it even more confusing.I'm on mobile, so looking at the shader SSA soup isn't super nice. But squinting at the data in the buffers a bit, I'd guess that the constant buffer contains the projection matrix. A 4x3 matrix isn't insane, you only really need the last row if you also want translation. If you only need rotation and scale, you can stuff it into a 4x3 matrix and walk away happy. The reason you will most likely see 4x4 matrices for everything is that GPUs used to have quite strict alignment rules and you might as well pack everything into a 4x4 matrix, not waste any bytes and not worry about specializations. You can always downcast a 4x4 matrix to a 3x3 one for example if all you need is the rotational component.
Hope that helps untangle the rest of the shader code.
> A 4x3 matrix isn't insane, you only really need the last row if you also want translation.
It's likely not dropping translation but the homogenous portion of the matrix. You only need a 3x4 matrix to fully encode transformations without any perspective projection. It's fairly common to use 3x4 matrices for storing these transformations since the final row will always be (0,0,0,1) you get 25% memory savings for free. The shader will turn the 3x4 into a 4x4 before multiplying it with the MVP.
Well... in reality I want to say that an FMA is just your affine transformation building block and while this is the basis of a projective transform, real world camera lenses and our eyes actually distort the views in a non-affine manner. We choose not to do this (except maybe as a lens distortion post fx) due to performance.