Skip to content

Commit

Permalink
gpu: Simplify VertexBufferLayout code to be just consts.
Browse files Browse the repository at this point in the history
No need for the function or any other tricks.
  • Loading branch information
kpreid committed Dec 10, 2024
1 parent 94e87af commit cd2c902
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
4 changes: 2 additions & 2 deletions all-is-cubes-gpu/src/in_wgpu/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Pipelines {
unclipped_depth: false,
conservative: false,
};
let vertex_buffers = &[WgpuBlockVertex::desc(), WgpuInstanceData::desc()];
let vertex_buffers = &[WgpuBlockVertex::LAYOUT, WgpuInstanceData::LAYOUT];

let multisample = fb.linear_scene_multisample_state();

Expand Down Expand Up @@ -317,7 +317,7 @@ impl Pipelines {
module: shaders.blocks_and_lines.get(),
entry_point: Some("lines_vertex"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[WgpuLinesVertex::desc()],
buffers: &[WgpuLinesVertex::LAYOUT],
},
fragment: Some(wgpu::FragmentState {
module: shaders.blocks_and_lines.get(),
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/shader_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ where
module: &shader,
entry_point: Some("block_vertex_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[WgpuBlockVertex::desc(), WgpuInstanceData::desc()],
buffers: &[WgpuBlockVertex::LAYOUT, WgpuInstanceData::LAYOUT],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
Expand Down
68 changes: 29 additions & 39 deletions all-is-cubes-gpu/src/in_wgpu/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,17 @@ pub(crate) struct WgpuBlockVertex {
}

impl WgpuBlockVertex {
const ATTRIBUTE_LAYOUT: &'static [wgpu::VertexAttribute] = &wgpu::vertex_attr_array![
0 => Uint32, // cube_packed
1 => Uint32x2, // position_in_cube_and_normal_packed
2 => Float32x4, // color_or_texture
3 => Uint32x3, // clamp_min_max
// location numbers must not clash with WgpuInstanceData
];

pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: Self::ATTRIBUTE_LAYOUT,
}
}
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Uint32, // cube_packed
1 => Uint32x2, // position_in_cube_and_normal_packed
2 => Float32x4, // color_or_texture
3 => Uint32x3, // clamp_min_max
// location numbers must not clash with WgpuInstanceData
],
};
}

impl From<BlockVertex<TexPoint>> for WgpuBlockVertex {
Expand Down Expand Up @@ -188,18 +184,16 @@ pub(crate) struct WgpuInstanceData {
}

impl WgpuInstanceData {
const ATTRIBUTE_LAYOUT: &'static [wgpu::VertexAttribute] = &wgpu::vertex_attr_array![
// location numbers must start after WgpuBlockVertex ends
6 => Float32x3,
];

pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: Self::ATTRIBUTE_LAYOUT,
}
}
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: const {
&wgpu::vertex_attr_array![
// location numbers must start after WgpuBlockVertex ends
6 => Float32x3,
]
},
};

pub fn new(translation: GridVector) -> Self {
Self {
Expand All @@ -217,18 +211,14 @@ pub(crate) struct WgpuLinesVertex {
}

impl WgpuLinesVertex {
const ATTRIBUTE_LAYOUT: &'static [wgpu::VertexAttribute] = &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x4,
];

pub fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: Self::ATTRIBUTE_LAYOUT,
}
}
pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x4,
],
};
}

impl DebugLineVertex for WgpuLinesVertex {
Expand Down

0 comments on commit cd2c902

Please sign in to comment.