How is constant buffer variables got matched between C++ code and shader code?

Viewed 200
2 Answers

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.

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.

Related