-
I am playing around with the idea of making a library on top of wgpu and naga that can dynamically generate shaders and render pipelines. Currently I am stuck on creating a In particular, I am trying to compile the following function: fn vertex_buffer_layout<'a>(shader_location_offset: u32) -> VertexBufferLayout<'a> {
VertexBufferLayout {
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x3,
offset: 0,
shader_location: shader_location_offset + 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32,
offset: 3 * 4,
shader_location: shader_location_offset + 1,
},
],
array_stride: 4 * 4 as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
}
} However, it fails with the following error message:
This is somewhat related to #1790, but the solution suggested there does unfortunately not apply here since the function is non-const now. I am considering making a hacky workaround by returning the members of As a side-note: I am also curious to know the background for storing a reference to |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I faced this as well in Baryon, and there is a solution I found, which works if impl Vertex {
const fn layout<const LOCATION: u32>() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Self>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x3,
offset: 0,
shader_location: LOCATION,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: LOCATION + 1,
},
],
}
}
} Would this wok for your case as well?
You are totally right. There are many solutions with trade-offs. We prioritize consistency. If it makes sense for most APIs to work with bare references, then we use them for all the APIs. But this particular issue has been coming back to us more and more, so we may make an exception here. |
Beta Was this translation helpful? Give feedback.
-
Personally, as a rust newbie, I struggled with those type of things and in the end I solved with the builder pattern. `#[derive(Clone, Debug)] impl<'a> VertexStateBuilder<'a> {
} (I would like to know why Github doesn't quote the rust code correcly...) |
Beta Was this translation helpful? Give feedback.
Personally, as a rust newbie, I struggled with those type of things and in the end I solved with the builder pattern.
Brutal copy and paste from my framework:
`#[derive(Clone, Debug)]
pub struct VertexStateBuilder<'a> {
vertex_state: wgpu::VertexState<'a>
}
impl<'a> VertexStateBuilder<'a> {
pub const DEFAULT_ENTRY_POINT: &'static str = "main";