Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better custom vertex buffer layouts support #2119

Merged
merged 11 commits into from
Jan 3, 2023
8 changes: 2 additions & 6 deletions examples/src/bin/teapot/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use vulkano::{
graphics::{
depth_stencil::DepthStencilState,
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
vertex_input::VertexInput,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
Expand Down Expand Up @@ -429,11 +429,7 @@ fn window_size_dependent_setup(
// This allows the driver to optimize things, at the cost of slower window resizes.
// https://computergraphics.stackexchange.com/questions/5742/vulkan-best-way-of-updating-pipeline-viewport
let pipeline = GraphicsPipeline::start()
.vertex_input_state(
BuffersDefinition::new()
.vertex::<Position>()
.vertex::<Normal>(),
)
.vertex_input_state([Position::per_vertex(), Normal::per_vertex()])
.vertex_shader(vs.entry_point("main").unwrap(), ())
.input_assembly_state(InputAssemblyState::new())
.viewport_state(ViewportState::viewport_fixed_scissor_irrelevant([
Expand Down
38 changes: 23 additions & 15 deletions vulkano/macros/src/derive_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ pub fn derive_vertex(ast: syn::DeriveInput) -> Result<TokenStream> {
FoundCrate::Name(name) => Ident::new(&name, Span::call_site()),
};

let mut member_cases = quote! {
let mut members = quote! {
let mut offset = 0;
let mut members = VertexMemberMap::default();
};

for field in fields.iter() {
let field_name = field.ident.to_owned().unwrap();
let field_name_lit = LitStr::new(&field_name.to_string(), Span::call_site());
let field_ty = &field.ty;
let mut names = vec![LitStr::new(&field_name.to_string(), Span::call_site())];
let mut names = vec![field_name_lit.clone()];
let mut format = quote! {};
for attr in &field.attrs {
let attr_ident = if let Some(ident) = attr.path.get_ident() {
Expand All @@ -73,21 +75,24 @@ pub fn derive_vertex(ast: syn::DeriveInput) -> Result<TokenStream> {
));
}
for name in &names {
member_cases = quote! {
#member_cases
members = quote! {
#members

let field_size = std::mem::size_of::<#field_ty>() as u32;
if name == #name {
{
#format
let format_size = format.block_size().expect("no block size for format") as u32;
let num_elements = field_size / format_size;
let remainder = field_size % format_size;
assert!(remainder == 0, "struct field `{}` size does not fit multiple of format size", name);
return Some(VertexMemberInfo {
offset,
format,
num_elements,
});
assert!(remainder == 0, "struct field `{}` size does not fit multiple of format size", #field_name_lit);
members.insert(
#name.to_string(),
VertexMemberInfo {
offset,
format,
num_elements,
},
);
}
offset += field_size as usize;
};
Expand All @@ -98,15 +103,18 @@ pub fn derive_vertex(ast: syn::DeriveInput) -> Result<TokenStream> {
#[allow(unsafe_code)]
unsafe impl #crate_ident::pipeline::graphics::vertex_input::Vertex for #struct_name {
#[inline(always)]
fn member(name: &str) -> Option<#crate_ident::pipeline::graphics::vertex_input::VertexMemberInfo> {
fn info() -> #crate_ident::pipeline::graphics::vertex_input::VertexInfo {
#[allow(unused_imports)]
use #crate_ident::format::Format;
use #crate_ident::pipeline::graphics::vertex_input::VertexMemberInfo;
use #crate_ident::pipeline::graphics::vertex_input::VertexMember;
use #crate_ident::pipeline::graphics::vertex_input::VertexMemberMap;

#member_cases
#members

None
#crate_ident::pipeline::graphics::vertex_input::VertexInfo {
members,
stride: std::mem::size_of::<#struct_name>() as u32,
}
}
}
}))
Expand Down
27 changes: 2 additions & 25 deletions vulkano/src/pipeline/graphics/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use super::{
render_pass::{PipelineRenderPassType, PipelineRenderingCreateInfo},
tessellation::TessellationState,
vertex_input::{
BuffersDefinition, Vertex, VertexDefinition, VertexInputAttributeDescription,
VertexInputBindingDescription, VertexInputState,
VertexDefinition, VertexInputAttributeDescription, VertexInputBindingDescription,
VertexInputState,
},
viewport::{Scissor, Viewport, ViewportState},
GraphicsPipeline, GraphicsPipelineCreationError,
Expand Down Expand Up @@ -3997,29 +3997,6 @@ impl<'vs, 'tcs, 'tes, 'gs, 'fs, Vdef, Vss, Tcss, Tess, Gss, Fss>
self
}

/// Sets the vertex input to a single vertex buffer.
///
/// You will most likely need to explicitly specify the template parameter to the type of a
/// vertex.
#[deprecated(since = "0.27.0", note = "Use `vertex_input_state` instead")]
pub fn vertex_input_single_buffer<V: Vertex>(
self,
) -> GraphicsPipelineBuilder<
'vs,
'tcs,
'tes,
'gs,
'fs,
BuffersDefinition,
Vss,
Tcss,
Tess,
Gss,
Fss,
> {
self.vertex_input_state(BuffersDefinition::new().vertex::<V>())
}

/// Sets whether primitive restart is enabled.
#[deprecated(since = "0.27.0", note = "Use `input_assembly_state` instead")]
#[inline]
Expand Down
174 changes: 0 additions & 174 deletions vulkano/src/pipeline/graphics/vertex_input/buffers.rs

This file was deleted.

Loading