buffer - hlsl 中使用的常量缓冲区 (cbuffer) 究竟是什么?

标签 buffer directx constants hlsl

目前我的顶点着色器类中有这段代码:

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 without cbuffer 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/

相关文章:

winapi - IDirectSoundBuffer锁,为什么要传递2部分?

c++ - 我应该使用 DirectInput 还是 Windows 消息循环?

java - 为什么我们不能在 INTERFACE 的静态 block 内分配变量?奥卡

objective-c - 在 Objective-C 中存储 iOS 应用程序常量

c# - SerialPort DataReceived Event 会重复触发吗?

EC2 实例 : slow results after the first query 上的 Mysql

stack - 堆喷射、堆溢出、堆溢出有什么区别?

c# - 由于极不可能的情况发生而丢失数据包?

DirectX11 : Pass data from ComputeShader to VertexShader?

常量的另一个名字