I am learning D3D11 by myself. I am wondering how is variable in c++ got matched by variables in shader?
For example, from the tutorial of D3D11: https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-buffers-constant-how-to
The layout of CBuffer in C++ is exact same as the layout in shader. However, in some game engines like Unity, shader variables can be placed in any order as long as I declare all of them. How is this achieved?
Many engines like Unity have dynamic binding systems for shaders. This is some combination of shader reflection and other metadata. This was implemented in the old DirectX Effects system as well.
In the end, there's still a match-up of the layout of the CPU-side memory buffer and the shader-side constant buffer, it's just done through code at runtime instead of by the developer.