GSLS中std430的意外

博客作者在使用GLSL时遇到std430布局的buffer对齐问题,发现vec3类型的数据在存储和读取时与预期不符,经过查阅规范和资料,发现vec3在std430布局中并未进行紧密对齐,这导致了与CPU端数据不一致。规范中的描述不明确让作者踩了坑,通过记录此问题以备将来参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚写了个小测试程序,却偶然发现GLSL中buffer的对齐似乎不太对劲。我的声明是这样的:

layout(std430, binding = 1) buffer PickBuffer{vec3 Result[];};

但是使用中发现,无论是输入还是输出,都与CPU端不一致,似乎每个元素都是vec4才对。但是std430应该要求紧密对齐吧?试试vec2的结果是对的。

GLSL的规范中是这样写的:

When using the std430 storage layout, shader storage blocks will be laid out in buffer storage
identically to uniform and shader storage blocks using the std140 layout, except that the base alignment and stride of arrays of scalars and vectors in rule 4 and of structures in rule 9 are not rounded up a multiple of the base alignment of a vec4 .
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值