【问题标题】:UBOs and their alignments in VulkanUBO 及其在 Vulkan 中的排列
【发布时间】:2018-01-20 04:01:00
【问题描述】:
struct Light {

   glm::vec4 position;
   glm::vec4 color;
   glm::vec4 attenuation;

};

struct UBO {

   // View and projection matrix
   glm::mat4 view;
   glm::mat4 proj;

   // Fog
   glm::vec4 skyColor;
   float density;
   float gradient;

   // Clipping plane
   glm::vec4 clippingPlane;

   // Lights
   Light lights[4];

};

Vulkan API:

UBO 结构每帧更新一次,然后通过统一缓冲区传递给顶点着色器。 渲染的图像颜色错误,因为光阵列没有正确传递。

顶点着色器绑定:

layout (binding = 1) uniform UBO {

    // View and projection matrix
    mat4 view;
    mat4 proj;

    // Fog
    vec4 skyColor;
    float density;
    float gradient;

    // Clipping plane
    vec4 clippingPlane;

    // Lights
    Light lights[4];

} ubo;

我是否需要以某种方式对齐数据?

【问题讨论】:

    标签: shader vulkan


    【解决方案1】:

    来自规范:

    标准统一缓冲区布局

    OpTypeStruct 成员类型的“基本对齐”递归定义如下:

    • 大小为 N 的标量的基本对齐方式为 N。

    • 具有大小为 N 的分量的双分量向量的基本对齐为 2 N。

    • 具有大小为 N 的分量的三分量或四分量向量的基本对齐为 4 N。

    • 数组的基本对齐方式等于其元素类型的基本对齐方式,四舍五入为 16 的倍数。

    • 结构的基对齐等于其任何成员的最大基对齐,四舍五入为 16 的倍数。

    • C 列的行主矩阵的基本对齐方式等于 C 矩阵分量向量的基本对齐方式。

    • 列主矩阵的基本对齐方式等于矩阵列类型的基本对齐方式。

    GLSL 中的 std140 布局满足这些规则。

    在规范的14.5.4. Offset and Stride Assignment 部分中了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多