gpt4 book ai didi

rust - wgpu-rs : Putting a Matrix3 into a vertex shader results in odd behavior but using a Matrix4 works fine

转载 作者:行者123 更新时间:2023-12-03 11:26:34 31 4
gpt4 key购买 nike

使用 wgpu-rs,我试图将 3x3 cgmath 矩阵放入着色器(使用 glsl-to-spirv 编译)。但是,着色器中生成的 mat3 数据不正确。当我用 mat4 和 Matrix4 替换 mat3 和 Matrix3 时,一切正常,矩阵有正确的数据。

顶点着色器:

layout(set=0, binding=0) uniform Uniforms {
mat3 camera_transform;
};

渲染循环:
let mut encoder = self.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor {
label: Some("update encoder"),
},
);
let staging_buffer = self.device.create_buffer_with_data(
bytemuck::cast_slice(&[self.uniforms]),
wgpu::BufferUsage::COPY_SRC,
);
encoder.copy_buffer_to_buffer(&staging_buffer, 0, &self.uniform_buffer, 0, std::mem::size_of::<Uniforms>() as wgpu::BufferAddress);
self.queue.submit(&[encoder.finish()]);

// ...

render_pass.set_bind_group(0, &self.uniform_bind_group, &[]);

制服:
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Uniforms {
pub camera_transform: Matrix3::<f32>,
}

unsafe impl bytemuck::Pod for Uniforms {}
unsafe impl bytemuck::Zeroable for Uniforms {}

impl Uniforms {
pub fn new() -> Self {
Self {
camera_transform: Matrix3::identity(),
}
}
}

最佳答案

这是一个 open issuewgpu-rs .事实上,最简单的解决方法可能是将您的 mat3 变成 mat4,直到它得到解决。

问题好像是生成SPIR-V的对齐错误.实际的对齐方式是:

  1. If the member is a scalar consuming N basic machine units, the base alignment is N.
  2. If the member is a two- or four-component vector with components consuming N basic machine units, the base alignment is 2N or 4N, respectively.
  3. If the member is a three-component vector with components consuming N basic machine units, the base alignment is 4N.
  4. If the member is an array of scalars or vectors, the base alignment and array stride are set to match the base alignment of a single array element, according to rules (1), (2), and (3), and rounded up to the base alignment of a vec4. The array may have padding at the end; the base offset of the member following the array is rounded up to the next multiple of the base alignment.


您是第 4 种情况。使用 mat4 应该不会在末端留下额外的填充物,并且不会出现任何未对准问题的可能性。

关于rust - wgpu-rs : Putting a Matrix3 into a vertex shader results in odd behavior but using a Matrix4 works fine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61768628/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com