Encountering array stride issues with DynamicUniformBuffer #20965
-
I’ve run into an issue while working with Bevy’s render pipeline and was hoping to get some help figuring it out! I created a pipeline resource that includes a BindGroupLayout, defined like this: let view_bind_group_layout = render_device.create_bind_group_layout(
"纹理变换矩阵布局",
&BindGroupLayoutEntries::single(ShaderStages::VERTEX, uniform_buffer::<Mat4>(true)),
); I also set up a DynamicUniformBuffer resource: #[derive(Resource)]
pub struct FilterUniformBuffers {
pub view_uniform_buffer: DynamicUniformBuffer<Mat4>,
pub color_matrix_uniform_buffer: DynamicUniformBuffer<ColorMatrixUniform>,
pub blur_uniform_buffer: DynamicUniformBuffer<BlurUniform>,
pub glow_uniform_buffer: DynamicUniformBuffer<GlowFilterUniform>,
pub bevel_uniform_buffer: DynamicUniformBuffer<BevelUniform>,
pub filter_vertex_with_double_blur_buffer: BufferVec<FilterVertexWithDoubleBlur>,
} Then I added data to the buffer: let view_matrix = Mat4::from_cols_array_2d(&[
[2.0 * scale.x / size.x, 0.0, 0.0, 0.0],
[0.0, 2.0 * scale.y / size.y, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]);
let view_offset = filter_uniform_buffers
.view_uniform_buffer
.push(&view_matrix); But when I tried to create a BindGroup with it: let view_bind_group = render_device.create_bind_group(
"offscreen_bind_group",
&offscreen_mesh2d_pipeline.view_bind_group_layout,
&BindGroupEntries::single(view_buffer.binding().unwrap()),
); I got an error: array stride must be a multiple of 16 (current stride: 4). Has anyone run into a similar issue with DynamicUniformBuffer or bind group stride mismatches? Any ideas on what I might be missing here? Thanks in advance for the help! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Instead of using |
Beta Was this translation helpful? Give feedback.
-
Thanks, I'll give it another try. I'd also like to confirm something: currently, I'm pushing data using .push() first, and then calling .write_buffer() at the end. Does this method not handle automatic alignment? You mentioned wrapping the Mat4 in a struct with padding—could you provide an example? I'm not quite sure I understand. |
Beta Was this translation helpful? Give feedback.
-
I found that the error shouldn't be in |
Beta Was this translation helpful? Give feedback.
Instead of using
.push()
I think you should useDynamicUniformBuffer::get_writer()
then use that to write the value to the buffer. This will handle aligment correctly for you. Alternatively, you can wrap the Mat4 in a struct that has the necessary padding.