2

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?

1
  • 1
    link doesnt work Commented Sep 7, 2021 at 5:35

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I haven't dug into the implementation of how rendering engine automatically binds the shader variable to the layout of graphics pipeline. But here is my guess.

  • While game engine compiling shader codes, it will write some shader header infos including shader varaible's layouts.

This can be implemented by the tools like glslang. This kind of tool can convert shader source codes into abstract syntax tree(AST), which means the shader codes are parsed into many types of ast nodes, such as input attribute and output variable. At this time, it can assign a explicit layout to the input shader variables and save the layout in shader header.

  • While initializing the graphics pipeline, the pipeline layout can be created according to the shader header.

Above is just my guess of possible implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.