0
votes

Currently, I am calculating the World View Projection Matrix in my application instead of the GPU. I want to move this calculation to the GPU, but I am currently unable to do so.

Case 1 (see below) works very well, but case 2 doesn't and I have no idea what I've done wrong.

In my camera class, I calculate the View and the Projection matrices like this:

ViewMatrix = SharpDX.Matrix.LookAtLH(_cameraPosition, _lookAtPosition, SharpDX.Vector3.UnitY);
ProjectionMatrix = SharpDX.Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, renderForm.ClientSize.Width / (float)renderForm.ClientSize.Height, 0.1f, 500.0f);

Then, I calculate the World matrix for each of my models during the render process:

SharpDX.Matrix worldMatrix = SharpDX.Matrix.Translation(_position);

Case 1: Calculation of matrix in my application

When rendering a model, I calculate the World View Projection Matrix like this:

SharpDX.Matrix matrix = SharpDX.Matrix.Multiply(worldMatrix, camera.ViewMatrix);
matrix = SharpDX.Matrix.Multiply(matrix, camera.ProjectionMatrix);

matrix.Transpose();

And in my vertex shader, I calculate the final position of my vertices by calling:

output.pos = mul(input.pos, WVP);

And everything works fine!

Case 2: Calculation of matrix in HLSL

Instead of calculating anything in my application, I just write the three matrices World, View and Projection into my vertex shader's constant buffer and calculate everything in HLSL:

matrix mat = mul(World, View);
mat = mul(mat, Projection);

mat = transpose(mat);

output.pos = mul(input.pos, mat);

It does work. I don't see anything in my scene. So I assume some calculations were wrong. I checked my code several times.

Either, I am blind or stupid. What did I do wrong?

2
It could be the results of differences in whether matrices are treated as row-major or column-major. Have you tried removing the transpose from the shader in Case 2?Adam Miles
Hello Adam Miles! Thanks for your response. I already tried this. When I remove the transpose in case 1, I don't see my models. When removing the transpose in case 2, I still don't see them.Endgegner85
Have you tried using any GPU debuggers such as the one built into Visual Studio? It may be worth checking the contents of the Constant Buffer are as you would expect.Adam Miles
No. To be honest, I did not know that there is one. I did web research before posting my problem here, but I only found dozens of comments stating, that there are no ways to output the input of shader variables like file output or console output. I will give it a try this weekend!Endgegner85

2 Answers

0
votes

in hlsl you don't need to calculate the transpose. You shoul also use the float4x4, so it's easy to see which dimensions you use. Your matrices just should look like:

float4x4 worldViewProj = mul(world, mul(view, proj));
float4 pos = mul(input.pos, worldViewProj);

Keep in mind that points are from type float4(x,y,z,1) and vectors are float4(x,y,z,0).

In linear algebra multiplication of a vector is

𝑝′=𝑀⋅𝑝

so you need the transpose for changing side of M

𝑝′𝑇=pT⋅𝑀𝑇 (the T means the transpose)

HLSL is a bit different. For the easiest way, just multiply all matrices from left to right and then use the mul function with your vector on the left just as in my example above.

For more information read this: HLSL mul()

0
votes

I used several days to experiment with the HLSL shaders and with my render functions. It turned out, that I transposed one matrix which I shouldn't have done. As a result, my whole scene was messed up. It was not much fun to debug, but it works now! :-)