今天写了一些dx11的东西,有一个bug调了好长时间才抓到,总结下。
Constant Buffer是shader model 4.0以上的一个新的特性,主要的目的就是为了把离散的变量集中起来,从而提高传输数据的效率。
constanb buffer是需要做padding的,和cpu的padding逻辑相同,不过constant buffer的padding是128位对齐的。例如下面的结构
// 2 x 16byte elements
cbuffer IE
{
float4 Val1;
float2 Val2; // starts a new vector
float2 Val3;
};
// 3 x 16byte elements
cbuffer IE
{
float2 Val1;
float4 Val2; // starts a new vector
float2 Val3; // starts a new vector
};
// 1 x 16byte elements
cbuffer IE
{
float1 Val1;
float1 Val2;
float2 Val3;
};
// 1 x 16byte elements
cbuffer IE
{
float1 Val1;
float2 Val2;
float1 Val3;
};
// 2 x 16byte elements
cbuffer IE
{
float1 Val1;
float1 Val1;
float1 Val1;
float2 Val2; // starts a new vector
};
// 1 x 16byte elements
cbuffer IE
{
float3 Val1;
float1 Val2;
};
// 1 x 16byte elements
cbuffer IE
{
float1 Val1;
float3 Val2;
};
// 2 x 16byte elements

本文介绍了DX11中Constant Buffer的padding原理,重点强调了其与CPU padding的不同,即128位对齐。通过示例展示了不正确处理padding可能导致的数据混乱和计算错误,指出这对shader计算结果的准确性有很大影响。
最低0.47元/天 解锁文章

1318

被折叠的 条评论
为什么被折叠?



