08/18/2020
推送常量
推送常量是一种着色器使用uniform变量,可以像uniform快那样使用,但是并不需要存储在内存里,它由Vulkan自身持有和更新。这些常量的新值可以被直接从命令缓冲区推送到管线。
推送常量逻辑上被视为管理资源的一部分,因此和管线布局(用来创建管线对象)中其他资源一同声明
推送球数据
vkglTF::Model model; //单个球的模型,比如顶点索引缓冲区,纹理贴图等等
// Color and position data for each sphere is uploaded using push constants
struct SpherePushConstantData {
glm::vec4 color;
glm::vec4 position;
};
std::array<SpherePushConstantData, 16> spheres;
//随机生成球的颜色和位置
void setupSpheres()
{
// Setup random colors and fixed positions for every spheres in the scene
for (uint32_t i = 0; i < spheres.size(); i++) {
spheres[i].color = glm::vec4(rnd(), rnd(), rnd(), 1.0f);
const float rad = glm::radians(i * 360.0f / static_cast<uint32_t>(spheres.size()));
spheres[i].position = glm::vec4(glm::vec3(sin(rad), cos(rad), 0.0f) * 3.5f, 1.0f);
}
}
推送常量布局
- 推送常量布局绑定在管道布局中
typedef struct VkPushConstantRange
{
VkShaderStageFlags stageFlags;
uint32_t offset;
uint32_t size;
}VkPushConstantRange;
每一个管道布局都有自己的常量存储空间,
// Define the push constant range used by the pipeline layout
// Note that the spec only requires a minimum of 128 bytes, so for passing larger blocks of data you'd use UBOs or SSBOs
VkPushConstantRange pushConstantRange{