Vulkan再探(5):推送常量(push constants)

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{
   
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值