directx - 在 HLSL 中使用一个包含所有相关数据的大缓冲区还是使用几个较小的缓冲区更好?

标签 directx shader directx-11 hlsl

如果在使用 HLSL 或其他高级着色器语言将数据发送到 GPU 时有一个单独的缓冲区,我对代码设计和性能方面都感兴趣。

这是一个特定的着色器需要有大量在运行时变化的可变数据的地方,因此需要缓冲区传递信息。

我举一个非常基本的例子:

cbuffer SomeLargeBuffer : register(cb0)
{
    float3 data;
    float someData;
    float4 largeArray[2500];
    float moreData;
    ...
    ...
    ... 
    ...
    ...
}

或拥有
cbuffer SamllerBuffer: register(cb0)
{
    float3 data;
    float someRelatedData;

}

cbuffer SecondSmallerBuffer : register(cb1)
{

    float4 largeArray[2500];
    float moreData;

}

cbuffer ThirdBuffer: register(cb2)
{
    ...
    ...
    ...
}

最佳答案

在效率方面,documentation on shader constants in HLSL给出以下建议:

The best way to efficiently use constant buffers is to organize shader variables into constant buffers based on their frequency of update. This allows an application to minimize the bandwidth required for updating shader constants. For example, a shader might declare two constant buffers and organize the data in each based on their frequency of update: data that needs to be updated on a per-object basis (like a world matrix) is grouped into a constant buffer which could be updated for each object. This is separate from data that characterizes a scene and is therefore likely to be updated much less often (when the scene changes).



因此,如果您以不同的速率更新数据,最好将所有以相同频率更新的数据分组在相同的常量缓冲区中。通常,数据要么更新 a) 每帧,b) 偶尔更新,要么 c) 从不更新(启动时一次)。减少总常量缓冲区的数量也有助于提高性能,因为它将减少绑定(bind)调用的数量和所需的资源跟踪。

在代码设计方面,很难说,尽管通常它自然地符合更新频率模式。

关于directx - 在 HLSL 中使用一个包含所有相关数据的大缓冲区还是使用几个较小的缓冲区更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42615351/

相关文章:

c++ - Windows 7 上 DirectX 9 应用程序中的多重采样

c++ - DirectX 背景图像

c++ - 使用 assimp 在 OpenGL 中使用法线作为颜色

c++ - 顶点颜色无法正常工作

constants - DirectX11 设置着色器常量

c++ - direct x 11 智能地创建顶点缓冲区

C++ DirectShow 视频和音频捕获 - 开始

C++ DirectX11 执行警告 #355 和 #356 = 步幅大小不正确

opengl - 编译顶点着色器程序失败

c# - 如何在 Visual Studio C# 项目中编译 hlsl 着色器文件