Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrices passed to the shader (via the ShaderParameterCollection) are transposed #748

Open
greyepoxy opened this issue Aug 27, 2019 · 0 comments
Labels
Core Area: Duality runtime or launcher Discussion Nothing to be done until decided otherwise Rendering Related to rendering / graphics
Milestone

Comments

@greyepoxy
Copy link

Summary

When passing a matrix to the shader via the Camera or DrawDevice's shaderParameters the resulting matrix in the shader is a transposed version of the passed in matrix.

How to reproduce

Since debugging a shader is quite difficult, I am not sure how to demonstrate this. I ran into this when passing a matrix to the shader code but you can also see this behavior with the default duality view Matrix and shader code. Let me describe the flow,

  1. The view matrix is constructed as a combination of a Translation and Rotation here
  2. The view matrix is passed to the shader but also used as part of the full matrix in IsSphereInView
  3. The Vector4.Transform method treats the vector as a 1x4 row based matrix when multiplying it against the passed in 4x4 matrix. From a math perspective this is like putting the vector on the left (rowVector * matrix), vs putting it on the right (matrix * columnVector).
  4. Alternatively in the shader the multiplication is backwards and the vector is being used as a column vector. This works fine since the matrix has been transposed from the original and is actually how the glsl docs (search for transposed) recommend getting and using a transposed matrix.

Workaround

In the shader code to multiply against the original matrix put the vector on the right (instead of the left) when performing the multiplication.

  mat4 result = myPassedInMatrix * someVector;

This is pretty straight forward but still extremely confusing when trying to debug why your shader is broken.

Analysis

When the ShaderParameterCollection serializes the matrix it does it row by row, but when glsl creates a matrix it reads the data column by column.

@ilexp ilexp added this to the General milestone Sep 11, 2019
@ilexp ilexp added Core Area: Duality runtime or launcher Discussion Nothing to be done until decided otherwise Rendering Related to rendering / graphics labels Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Area: Duality runtime or launcher Discussion Nothing to be done until decided otherwise Rendering Related to rendering / graphics
Development

No branches or pull requests

2 participants