目前我的顶点着色器类中有这段代码:
cbuffer MatrixBuffer {
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix; };
我不知道为什么我需要将这些变量包装在 cbuffer 中。如果我删除缓冲区,我的代码也能正常工作。如果有人能给我一个简短的解释为什么需要使用 cbuffer,我将不胜感激。
最佳答案
它以任何一种方式工作的原因是由于在 Direct3D 8/Direct3D 9 中处理常量的传统方式。那时,整个着色器只有一个共享常量数组(一个用于 VS,一个用于 PS) .这要求您每次调用 Draw
时都必须更改常量数组。
在 Direct3D 10 中,常量被重新组织到一个或多个常量缓冲区中,以便更轻松地更新一些常量,同时保留其他常量,从而减少向 GPU 发送的数据。
See the classic presentation Windows to Reality: Getting the Most out of Direct3D 10 Graphics in Your Games for a lot of details on the impacts of constant update.
这里的结果是,如果您不指定 cbuffer
,所有常量都会被放入一个绑定(bind)到注册 b0
的隐式常量缓冲区中模拟旧的“一个常量数组”行为。
There are compiler flags to control the acceptance of legacy constructs:
/Gec
for backwards compatibility mode to support old Direct3D 8/9 intrinsics, and/Ges
to enable a more strict compilation to weed out older constructs. That said, the HLSL compiler will pretty much always accept global constants withoutcbuffer
and stick them into a single implicit constant buffer because this pattern is extremely common in shader code.
关于buffer - hlsl 中使用的常量缓冲区 (cbuffer) 究竟是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196001/