doing some hardcore storming on this dynamic vertex situation

This commit is contained in:
2019-10-16 23:29:49 -07:00
parent c4b20bcd79
commit ffa04d0bb1
7 changed files with 120 additions and 72 deletions

View File

@@ -1,3 +1,10 @@
use vulkano::pipeline::vertex::{VertexDefinition, InputRate, AttributeInfo, IncompatibleVertexDefinitionError, VertexSource};
use vulkano::pipeline::shader::ShaderInterfaceDef;
use vulkano::buffer::BufferAccess;
use std::sync::Arc;
use cgmath::num_traits::real::Real;
use std::vec::IntoIter as VecIntoIter;
pub struct RuntimeVertexDef {
buffers: Vec<(u32, usize, InputRate)>,
vertex_buffer_ids: Vec<(usize, usize)>,
@@ -6,50 +13,50 @@ pub struct RuntimeVertexDef {
}
impl RuntimeVertexDef {
pub fn from_primitive(primitive: gltf::Primitive) -> RuntimeVertexDef {
use gltf::mesh::Attribute;
use gltf::accessor::{DataType, Dimensions};
pub fn from_primitive(primitive: u32) -> RuntimeVertexDef {
// use gltf::mesh::Attribute;
// use gltf::accessor::{DataType, Dimensions};
//
let mut buffers = Vec::new();
let mut vertex_buffer_ids = Vec::new();
let mut attributes = Vec::new();
let mut num_vertices = u32::max_value();
for (attribute_id, attribute) in primitive.attributes().enumerate() {
let (name, accessor) = match attribute.clone() {
Attribute::Positions(accessor) => ("i_position".to_owned(), accessor),
Attribute::Normals(accessor) => ("i_normal".to_owned(), accessor),
Attribute::Tangents(accessor) => ("i_tangent".to_owned(), accessor),
Attribute::Colors(0, accessor) => ("i_color_0".to_owned(), accessor),
Attribute::TexCoords(0, accessor) => ("i_texcoord_0".to_owned(), accessor),
Attribute::TexCoords(1, accessor) => ("i_texcoord_1".to_owned(), accessor),
Attribute::Joints(0, accessor) => ("i_joints_0".to_owned(), accessor),
Attribute::Weights(0, accessor) => ("i_weights_0".to_owned(), accessor),
_ => unimplemented!(),
};
if (accessor.count() as u32) < num_vertices {
num_vertices = accessor.count() as u32;
}
let infos = AttributeInfo {
offset: 0,
format: match (accessor.data_type(), accessor.dimensions()) {
(DataType::I8, Dimensions::Scalar) => Format::R8Snorm,
(DataType::U8, Dimensions::Scalar) => Format::R8Unorm,
(DataType::F32, Dimensions::Vec2) => Format::R32G32Sfloat,
(DataType::F32, Dimensions::Vec3) => Format::R32G32B32Sfloat,
(DataType::F32, Dimensions::Vec4) => Format::R32G32B32A32Sfloat,
_ => unimplemented!()
},
};
let view = accessor.view();
buffers.push((attribute_id as u32, view.stride().unwrap_or(accessor.size()), InputRate::Vertex));
attributes.push((name, attribute_id as u32, infos));
vertex_buffer_ids.push((view.buffer().index(), view.offset() + accessor.offset()));
}
//
// for (attribute_id, attribute) in primitive.attributes().enumerate() {
// let (name, accessor) = match attribute.clone() {
// Attribute::Positions(accessor) => ("i_position".to_owned(), accessor),
// Attribute::Normals(accessor) => ("i_normal".to_owned(), accessor),
// Attribute::Tangents(accessor) => ("i_tangent".to_owned(), accessor),
// Attribute::Colors(0, accessor) => ("i_color_0".to_owned(), accessor),
// Attribute::TexCoords(0, accessor) => ("i_texcoord_0".to_owned(), accessor),
// Attribute::TexCoords(1, accessor) => ("i_texcoord_1".to_owned(), accessor),
// Attribute::Joints(0, accessor) => ("i_joints_0".to_owned(), accessor),
// Attribute::Weights(0, accessor) => ("i_weights_0".to_owned(), accessor),
// _ => unimplemented!(),
// };
//
// if (accessor.count() as u32) < num_vertices {
// num_vertices = accessor.count() as u32;
// }
//
// let infos = AttributeInfo {
// offset: 0,
// format: match (accessor.data_type(), accessor.dimensions()) {
// (DataType::I8, Dimensions::Scalar) => Format::R8Snorm,
// (DataType::U8, Dimensions::Scalar) => Format::R8Unorm,
// (DataType::F32, Dimensions::Vec2) => Format::R32G32Sfloat,
// (DataType::F32, Dimensions::Vec3) => Format::R32G32B32Sfloat,
// (DataType::F32, Dimensions::Vec4) => Format::R32G32B32A32Sfloat,
// _ => unimplemented!()
// },
// };
//
// let view = accessor.view();
// buffers.push((attribute_id as u32, view.stride().unwrap_or(accessor.size()), InputRate::Vertex));
// attributes.push((name, attribute_id as u32, infos));
// vertex_buffer_ids.push((view.buffer().index(), view.offset() + accessor.offset()));
// }
RuntimeVertexDef {
buffers: buffers,
@@ -100,9 +107,9 @@ unsafe impl<I> VertexDefinition<I> for RuntimeVertexDef
}
}
unsafe impl VertexSource<Vec<Arc<BufferAccess + Send + Sync>>> for RuntimeVertexDef {
fn decode(&self, bufs: Vec<Arc<BufferAccess + Send + Sync>>)
-> (Vec<Box<BufferAccess + Send + Sync>>, usize, usize)
unsafe impl VertexSource<Vec<Arc<dyn BufferAccess + Send + Sync>>> for RuntimeVertexDef {
fn decode(&self, bufs: Vec<Arc<dyn BufferAccess + Send + Sync>>)
-> (Vec<Box<dyn BufferAccess + Send + Sync>>, usize, usize)
{
(bufs.into_iter().map(|b| Box::new(b) as Box<_>).collect(), self.num_vertices as usize, 1)
}