Skip to content

Commit

Permalink
Reimplement BuffersDefinition leveraging VertexBufferInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
trevex committed Dec 29, 2022
1 parent d1e38e7 commit 6238e72
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
66 changes: 66 additions & 0 deletions vulkano/src/pipeline/graphics/vertex_input/buffers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2016 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

use super::VertexBufferInfo;
use crate::{
pipeline::graphics::vertex_input::{
IncompatibleVertexDefinitionError, Vertex, VertexDefinition, VertexInputState,
},
shader::ShaderInterface,
};

/// A vertex definition for any number of vertex and instance buffers.
#[derive(Clone, Debug, Default)]
pub struct BuffersDefinition(Vec<VertexBufferInfo>);

impl BuffersDefinition {
/// Constructs a new definition.
#[inline]
pub fn new() -> Self {
BuffersDefinition(Vec::new())
}

/// Adds a new vertex buffer containing elements of type `V` to the definition.
pub fn vertex<V: Vertex>(mut self) -> Self {
self.0.push(V::info().per_vertex());
self
}

/// Adds a new instance buffer containing elements of type `V` to the definition.
pub fn instance<V: Vertex>(mut self) -> Self {
self.0.push(V::info().per_instance());
self
}

/// Adds a new instance buffer containing elements of type `V` to the definition, with the
/// specified input rate divisor.
///
/// This requires the [`vertex_attribute_instance_rate_divisor`] feature has been enabled on
/// the device, unless `divisor` is 1.
///
/// `divisor` can be 0 if the [`vertex_attribute_instance_rate_zero_divisor`] feature is also
/// enabled. This means that every vertex will use the same vertex and instance data.
///
/// [`vertex_attribute_instance_rate_divisor`]: crate::device::Features::vertex_attribute_instance_rate_divisor
/// [`vertex_attribute_instance_rate_zero_divisor`]: crate::device::Features::vertex_attribute_instance_rate_zero_divisor
pub fn instance_with_divisor<V: Vertex>(mut self, divisor: u32) -> Self {
self.0.push(V::info().per_instance_with_divisor(divisor));
self
}
}

unsafe impl VertexDefinition for BuffersDefinition {
#[inline]
fn definition(
&self,
interface: &ShaderInterface,
) -> Result<VertexInputState, IncompatibleVertexDefinitionError> {
self.0.definition(interface)
}
}
2 changes: 2 additions & 0 deletions vulkano/src/pipeline/graphics/vertex_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#[allow(deprecated)]
pub use self::{
buffers::BuffersDefinition,
collection::VertexBuffersCollection,
definition::{IncompatibleVertexDefinitionError, VertexDefinition},
impl_vertex::VertexMember,
Expand All @@ -110,6 +111,7 @@ pub use self::{
use crate::format::Format;
use ahash::HashMap;

mod buffers;
mod collection;
mod definition;
mod impl_vertex;
Expand Down
46 changes: 28 additions & 18 deletions vulkano/src/pipeline/graphics/vertex_input/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ pub struct VertexInfo {
pub stride: u32,
}

impl VertexInfo {
#[inline]
pub fn per_vertex(self) -> VertexBufferInfo {
let VertexInfo { members, stride } = self;
VertexBufferInfo {
members,
stride,
input_rate: VertexInputRate::Vertex,
}
}
#[inline]
pub fn per_instance(self) -> VertexBufferInfo {
self.per_instance_with_divisor(1)
}
#[inline]
pub fn per_instance_with_divisor(self, divisor: u32) -> VertexBufferInfo {
let VertexInfo { members, stride } = self;
VertexBufferInfo {
members,
stride,
input_rate: VertexInputRate::Instance { divisor },
}
}
}

/// Information about a member of a vertex struct.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VertexMemberInfo {
Expand Down Expand Up @@ -115,30 +140,15 @@ pub unsafe trait VertexInput {
unsafe impl<T: Vertex> VertexInput for T {
#[inline]
fn per_vertex() -> VertexBufferInfo {
let VertexInfo { members, stride } = <T as Vertex>::info();
VertexBufferInfo {
members,
stride,
input_rate: VertexInputRate::Vertex,
}
<T as Vertex>::info().per_vertex()
}
#[inline]
fn per_instance() -> VertexBufferInfo {
let VertexInfo { members, stride } = <T as Vertex>::info();
VertexBufferInfo {
members,
stride,
input_rate: VertexInputRate::Instance { divisor: 1 },
}
<T as Vertex>::info().per_instance()
}
#[inline]
fn per_instance_with_divisor(divisor: u32) -> VertexBufferInfo {
let VertexInfo { members, stride } = <T as Vertex>::info();
VertexBufferInfo {
members,
stride,
input_rate: VertexInputRate::Instance { divisor },
}
<T as Vertex>::info().per_instance_with_divisor(divisor)
}
}

Expand Down

0 comments on commit 6238e72

Please sign in to comment.