Skip to content

Commit

Permalink
Better custom vertex buffer layouts support (#2119)
Browse files Browse the repository at this point in the history
* Implement VertexDefinition for VertexBufferInfo, change Vertex signature to return VertexInfo and implement VertexInput-trait helper for Vertex to construct VertexBufferInfo objects from Vertex types. Updated teapot example to show usage.

* Reimplement BuffersDefinition leveraging VertexBufferInfo

* Reduce the amount of types required, remove VertexInfo, expose VertexBufferInfo methods on Vertex and use std HashMap transparently instead.

* Fix teapot example

* Fix outdated docs and use full path to Vertex trait to avoid issues

* Fix formatting of imports

* Remove clone and directly instantiate VertexInputBindingDescription.

* Rename VertexBufferInfo to VertexBufferDescription and being update of examples to use it

* Run cargo clippy fix to cleanup imports in examples

* Remove obsolete comment

* cargo fmt
  • Loading branch information
trevex authored Jan 3, 2023
1 parent 21f850d commit 834cc1e
Show file tree
Hide file tree
Showing 41 changed files with 374 additions and 317 deletions.
10 changes: 5 additions & 5 deletions examples/src/bin/buffer-allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use vulkano::{
pipeline::{
graphics::{
input_assembly::InputAssemblyState,
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline,
Expand Down Expand Up @@ -225,7 +225,7 @@ fn main() {
.unwrap();

let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
.vertex_input_state(Vertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down Expand Up @@ -277,7 +277,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -296,7 +296,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -385,7 +385,7 @@ fn main() {
previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<_>);
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<_>);
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/src/bin/clear_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -209,7 +209,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -289,7 +289,7 @@ fn main() {
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use vulkano::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl AmbientLightingSystem {
let fs = fs::load(gfx_queue.device().clone()).expect("failed to create shader module");

GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<LightingVertex>())
.vertex_input_state(LightingVertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use vulkano::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl DirectionalLightingSystem {
let fs = fs::load(gfx_queue.device().clone()).expect("failed to create shader module");

GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<LightingVertex>())
.vertex_input_state(LightingVertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/deferred/frame/point_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use vulkano::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl PointLightingSystem {
let fs = fs::load(gfx_queue.device().clone()).expect("failed to create shader module");

GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<LightingVertex>())
.vertex_input_state(LightingVertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down
6 changes: 3 additions & 3 deletions examples/src/bin/deferred/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};
let new_images = new_images
.into_iter()
Expand All @@ -235,7 +235,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -285,7 +285,7 @@ fn main() {
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/deferred/triangle_draw_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use vulkano::{
graphics::{
depth_stencil::DepthStencilState,
input_assembly::InputAssemblyState,
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl TriangleDrawSystem {
let fs = fs::load(gfx_queue.device().clone()).expect("failed to create shader module");

GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<TriangleVertex>())
.vertex_input_state(TriangleVertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down
7 changes: 2 additions & 5 deletions examples/src/bin/dynamic-buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ fn main() {
.physical_device()
.properties()
.min_uniform_buffer_offset_alignment as usize;
println!(
"Minimum uniform buffer offset alignment: {}",
min_dynamic_align
);
println!("Input: {:?}", data);
println!("Minimum uniform buffer offset alignment: {min_dynamic_align}");
println!("Input: {data:?}");
// Round size up to the next multiple of align.
let align = (size_of::<u32>() + min_dynamic_align - 1) & !(min_dynamic_align - 1);
let aligned_data = {
Expand Down
7 changes: 2 additions & 5 deletions examples/src/bin/dynamic-local-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn main() {
// local size can lead to significant performance penalty.
let (local_size_x, local_size_y) = match device.physical_device().properties().subgroup_size {
Some(subgroup_size) => {
println!("Subgroup size is {}", subgroup_size);
println!("Subgroup size is {subgroup_size}");

// Most of the subgroup values are divisors of 8
(8, subgroup_size / 8)
Expand All @@ -179,10 +179,7 @@ fn main() {
}
};

println!(
"Local size will be set to: ({}, {}, 1)",
local_size_x, local_size_y
);
println!("Local size will be set to: ({local_size_x}, {local_size_y}, 1)");

let spec_consts = cs::SpecializationConstants {
red: 0.2,
Expand Down
10 changes: 5 additions & 5 deletions examples/src/bin/gl-interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod linux {
graphics::{
color_blend::ColorBlendState,
input_assembly::{InputAssemblyState, PrimitiveTopology},
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Scissor, Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -293,7 +293,7 @@ mod linux {
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => {
return
}
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -312,7 +312,7 @@ mod linux {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -375,7 +375,7 @@ mod linux {
previous_frame_end = Some(vulkano::sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(vulkano::sync::now(device.clone()).boxed());
}
};
Expand Down Expand Up @@ -608,7 +608,7 @@ mod linux {
let subpass = Subpass::from(render_pass.clone(), 0).unwrap();

let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<MyVertex>())
.vertex_input_state(MyVertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(
InputAssemblyState::new().topology(PrimitiveTopology::TriangleStrip),
Expand Down
10 changes: 5 additions & 5 deletions examples/src/bin/image-self-copy-blit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use vulkano::{
graphics::{
color_blend::ColorBlendState,
input_assembly::{InputAssemblyState, PrimitiveTopology},
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -326,7 +326,7 @@ fn main() {

let subpass = Subpass::from(render_pass.clone(), 0).unwrap();
let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
.vertex_input_state(Vertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new().topology(PrimitiveTopology::TriangleStrip))
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down Expand Up @@ -390,7 +390,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -406,7 +406,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -466,7 +466,7 @@ fn main() {
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/src/bin/image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use vulkano::{
graphics::{
color_blend::ColorBlendState,
input_assembly::{InputAssemblyState, PrimitiveTopology},
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -257,7 +257,7 @@ fn main() {

let subpass = Subpass::from(render_pass.clone(), 0).unwrap();
let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
.vertex_input_state(Vertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new().topology(PrimitiveTopology::TriangleStrip))
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down Expand Up @@ -321,7 +321,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -337,7 +337,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -397,7 +397,7 @@ fn main() {
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/src/bin/immutable-sampler/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use vulkano::{
graphics::{
color_blend::ColorBlendState,
input_assembly::{InputAssemblyState, PrimitiveTopology},
vertex_input::{BuffersDefinition, Vertex},
vertex_input::Vertex,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -263,7 +263,7 @@ fn main() {

let subpass = Subpass::from(render_pass.clone(), 0).unwrap();
let pipeline = GraphicsPipeline::start()
.vertex_input_state(BuffersDefinition::new().vertex::<Vertex>())
.vertex_input_state(Vertex::per_vertex())
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new().topology(PrimitiveTopology::TriangleStrip))
.viewport_state(ViewportState::viewport_dynamic_scissor_irrelevant())
Expand Down Expand Up @@ -334,7 +334,7 @@ fn main() {
}) {
Ok(r) => r,
Err(SwapchainCreationError::ImageExtentNotSupported { .. }) => return,
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
};

swapchain = new_swapchain;
Expand All @@ -350,7 +350,7 @@ fn main() {
recreate_swapchain = true;
return;
}
Err(e) => panic!("Failed to acquire next image: {:?}", e),
Err(e) => panic!("Failed to acquire next image: {e:?}"),
};

if suboptimal {
Expand Down Expand Up @@ -410,7 +410,7 @@ fn main() {
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
Err(e) => {
println!("Failed to flush future: {:?}", e);
println!("Failed to flush future: {e:?}");
previous_frame_end = Some(sync::now(device.clone()).boxed());
}
}
Expand Down
Loading

0 comments on commit 834cc1e

Please sign in to comment.