-
Notifications
You must be signed in to change notification settings - Fork 22
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
Non-square matrices. #27
Comments
One important issue to keep in mind if/when adding non-square matrices is that HLSL and GLSL interpret the same notation differently. In GLSL, matrices are logically column-major, and the type is written
In HLSL, matrices are logically row-major, and the type is written
Now, anybody looking at the above would probably say "those look identical, and you just switched up what you were calling a 'row' or 'column.'" And you are pretty much right, and I'd actually strongly advocate that any front-end that wants to be portable should pick a uniform convention where you generate both Where the rubber hits the road is two places:
Issue (1) is pretty easy to deal with, since both GLSL and HLSL support modifiers on declarations that change the matrix layout conventions. The main difficulty for any portable shading language is just being consistent, and remembering when you've flipped conventions (so if you want HLSL conventiosn and row-major layout, then you'll actually want to ask GLSL for column-major layout). Issue (2) is just pointing out the fact that because the conventions are flipped in GLSL/HLSL, if I have HLSL code like this:
Then the equivalent GLSL requires that I swap the order of operands to my multiply:
Long story short, if you generate both HLSL and GLSL from a common input language then the Right Answer is:
My own bias on this is that the HLSL RxC row-major convention is the Right Way, and confusion is minimized if you always use row-major storage (or at least default to it). |
Thanks for the notes, Tim. |
Ideally, I'd want to have the whole suite of matNxM types, where 2 <= N, M <= 4, and be able to index into them etc.
I started trying to add this myself, but the number of places I had to touch was daunting. The main issue is that each matrix type is reflected direclty in an
enum
(with hard-coded values, which seem to need to match anotherenum
of IL-level types (?)). As a result, there are long conditional chains all throughout the code that need to handle all the cases (I would have preferredswitch
statements, since they are easier to add a case to on the fly), and all of those places need to be fixed up to handle a new type. One particularly worrying spot is trying to guess the IL type to use based on the size of a constant (so 9 floats -> mat3 and 16 floats -> mat4), but this breaks down for non-square matrices.I'd really like to see a pretty fundamental change to the type representation in Spire, so that is has a general thing like a
struct MatrixType { Type* elementType; int rows; int cols; }
and thenType
is either an inheritance hierarchy or a tagged union...The text was updated successfully, but these errors were encountered: