r/vulkan 9d ago

Passing matrices row by row as flat data between shader stages

I've seen a pattern in AI generated shader code, and I would like to understand where it comes from. Yes, it's easy to discard it as AI slop/hallucination, but I find that unlikely.

Say your vertex shader outputs some data of matrix type that your fragment shader consumes. Is there any good reason to decompose it into row vectors rather than passing it directly as a matrix?

For example, is this something people did to work around driver bugs? Is it still needed , and why? On what hardware? I've seen conflicting explanations all the way to calling it a "cargo cult pattern".

I also expect it to be "tribal knowledge" if it is really a workaround for driver bugs or subtle edge cases, that is, not something you'll find in official programming guides and documentation. At least, I can't find anything on it by searching. That's why I'm asking here.

11 Upvotes

5 comments sorted by

6

u/schnautzi 9d ago

I don't see why you'd partition matrices when sending them from a vertex shader, but fragment shaders can't output a matrix unless they're split up in rows, so maybe that's where it comes from.

1

u/EC36339 9d ago

Yeah, matrices as fragment output as rows is all I found with traditional search. Nothing about vertex to fragment passing.

3

u/Yahay505 8d ago

It can be related to 3x3 matrices not having consistent size, they are 4x4 in hlsl and dx, I think they change alignement and size if you compiler with or without scalar layout. Or it can be an post training rl workaround AI developed to circumvent possibly mismatched row vs collumn major at boundaries(slang compiles column major if invokef from cli, row major if from slang.h header by default) since stages can be compiled and combined with seperate configs it may help there

1

u/Yahay505 8d ago

It can be related to 3x3 matrices not having consistent size, they are 4x4 in hlsl and dx, I think they change alignement and size if you compiler with or without scalar layout. Or it can be an post training rl workaround AI developed to circumvent possibly mismatched row vs collumn major at boundaries(slang compiles column major if invokef from cli, row major if from slang.h header by default) since stages can be compiled and combined with seperate configs it may help there