So I follow the learnwgpu tutorial.
The code uses a bunch of temporaries when configuring everything.
But I didn't really think about it until I run into this:
In the Texture and Bind groups chapter, the Vertex impl desc() associated function return VertexBufferLayout<'a>:
fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
offset: 0,
},
wgpu::VertexAttribute {
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
},
],
}
}
this compiles fine (why? Constant promotion ?) However as mentioned in the tutorial, this does not compile:
fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2],
}
}
error[E0515]: cannot return value referencing temporary value
However this is expanded in almost exactly the same code:
fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
::wgpu::VertexAttribute {
format: ::wgpu::VertexFormat::Float32x3,
offset: 0,
shader_location: 0,
},
::wgpu::VertexAttribute {
format: ::wgpu::VertexFormat::Float32x2,
offset: 0 + ::wgpu::VertexFormat::Float32x3.size(),
shader_location: 1,
},
],
}
}
The only significant difference (I think) is the call to VertexFormat::size() which is const.
So am I right to think that what allow the 1st version to compile is constant promotion of the temporary VertexAttribute array ?
If so, what prevent the second version using the macro to compile ?
Thanks for your help