Skip to content

Commit

Permalink
add simple vertex3d
Browse files Browse the repository at this point in the history
fixes validation error
  • Loading branch information
Snowiiii committed Jul 27, 2024
1 parent 657d871 commit ea148c9
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 124 deletions.
7 changes: 3 additions & 4 deletions crates/vent-assets/src/model/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use ash::{
vk::{self, PipelineShaderStageCreateInfo},
};
use vent_rendering::{
image::VulkanImage, instance::VulkanInstance, mesh::Mesh3D, MaterialPipelineInfo, Vertex3D,
DEFAULT_TEXTURE_FILTER,
image::VulkanImage, instance::VulkanInstance, mesh::Mesh3D, vertex::Vertex3D, MaterialPipelineInfo, DEFAULT_TEXTURE_FILTER
};

use crate::{Material, Model3D, ModelPipeline};
Expand Down Expand Up @@ -360,8 +359,8 @@ impl ModelLoader {
.iter()
.map(|vertex| Vertex3D {
position: vertex.position,
tex_coord: vertex.tex_coord.unwrap(), // TODO
normal: vertex.normal.unwrap(), // TODO
tex_coord: vertex.tex_coord.unwrap_or_default(), // TODO
normal: vertex.normal.unwrap_or_default(), // TODO
})
.collect()
}
Expand Down
3 changes: 2 additions & 1 deletion crates/vent-assets/src/model/optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use vent_rendering::Vertex3D;
use vent_rendering::vertex::Vertex3D;


#[allow(dead_code)]
pub fn optimize_vertices(_vertices: Vec<Vertex3D>) -> Vec<Vertex3D> {
Expand Down
74 changes: 1 addition & 73 deletions crates/vent-rendering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
mem::{self, offset_of},
os::raw::c_void,
};

Expand All @@ -16,6 +15,7 @@ pub mod instance;
pub mod mesh;
pub mod pipeline;
mod surface;
pub mod vertex;

pub const DEFAULT_TEXTURE_FILTER: vk::Filter = vk::Filter::LINEAR;
pub const DEFAULT_MIPMAP_MODE: vk::SamplerMipmapMode = vk::SamplerMipmapMode::LINEAR;
Expand All @@ -30,20 +30,6 @@ pub struct MaterialPipelineInfo {
pub double_sided: bool,
}

#[derive(Clone, Copy, PartialEq)]
pub struct Vertex3D {
pub position: [f32; 3],
pub tex_coord: [f32; 2],
pub normal: [f32; 3],
}

#[derive(Clone, Copy, PartialEq)]
pub struct Vertex2D {
pub position: [f32; 2],
pub tex_coord: [f32; 2],
pub color: u32,
}

pub enum Indices {
U8(Vec<u8>), // TODO: Enable uin8 vulkan feature
U16(Vec<u16>),
Expand Down Expand Up @@ -97,64 +83,6 @@ impl Indices {
}
}

impl Vertex3D {
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)
.stride(mem::size_of::<Self>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
}
pub fn input_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
[
// offset_of macro got stabilized in rust 1.77
vk::VertexInputAttributeDescription::default()
.location(0)
.binding(0)
.format(vk::Format::R32G32B32A32_SFLOAT)
.offset(offset_of!(Self, position) as u32),
vk::VertexInputAttributeDescription::default()
.location(1)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, tex_coord) as u32),
vk::VertexInputAttributeDescription::default()
.location(2)
.binding(0)
.format(vk::Format::R32G32B32A32_SFLOAT)
.offset(offset_of!(Self, normal) as u32),
]
}
}

impl Vertex2D {
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)
.stride(mem::size_of::<Self>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
}
pub fn input_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
[
// offset_of macro got stabilized in rust 1.77
vk::VertexInputAttributeDescription::default()
.location(0)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, position) as u32),
vk::VertexInputAttributeDescription::default()
.location(1)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, tex_coord) as u32),
vk::VertexInputAttributeDescription::default()
.location(2)
.binding(0)
.format(vk::Format::R8G8B8A8_UNORM)
.offset(offset_of!(Self, color) as u32),
]
}
}

pub fn any_as_u8_slice<T: Sized>(any: &T) -> &[u8] {
let ptr = (any as *const T) as *const u8;
unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<T>()) }
Expand Down
6 changes: 3 additions & 3 deletions crates/vent-rendering/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ash::vk;

use crate::{
begin_single_time_command, buffer::VulkanBuffer, end_single_time_command,
instance::VulkanInstance, Indices, Vertex3D,
instance::VulkanInstance, Indices,
};

/// This is a simple mesh that consists of vertices and indices. It is useful when you need to hard-code 3D data into your application.
Expand All @@ -19,9 +19,9 @@ pub struct Mesh3D {
}

impl Mesh3D {
pub fn new(
pub fn new<V: Copy>(
instance: &VulkanInstance,
vertices: &[Vertex3D],
vertices: &[V],
indices: Indices,
name: Option<&str>,
) -> Self {
Expand Down
99 changes: 99 additions & 0 deletions crates/vent-rendering/src/vertex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::mem::offset_of;

use ash::vk;

#[derive(Clone, Copy, PartialEq)]
pub struct Vertex3D {
pub position: [f32; 3],
pub tex_coord: [f32; 2],
pub normal: [f32; 3],
}

#[derive(Clone, Copy, PartialEq)]
pub struct VertexPos3D {
pub position: [f32; 3],
}

#[derive(Clone, Copy, PartialEq)]
pub struct Vertex2D {
pub position: [f32; 2],
pub tex_coord: [f32; 2],
pub color: u32,
}

impl Vertex3D {
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)
.stride(std::mem::size_of::<Self>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
}
pub fn input_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
[
// offset_of macro got stabilized in rust 1.77
vk::VertexInputAttributeDescription::default()
.location(0)
.binding(0)
.format(vk::Format::R32G32B32A32_SFLOAT)
.offset(offset_of!(Self, position) as u32),
vk::VertexInputAttributeDescription::default()
.location(1)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, tex_coord) as u32),
vk::VertexInputAttributeDescription::default()
.location(2)
.binding(0)
.format(vk::Format::R32G32B32A32_SFLOAT)
.offset(offset_of!(Self, normal) as u32),
]
}
}

impl VertexPos3D {
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)
.stride(std::mem::size_of::<Self>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
}
pub fn input_descriptions() -> [vk::VertexInputAttributeDescription; 1] {
[
// offset_of macro got stabilized in rust 1.77
vk::VertexInputAttributeDescription::default()
.location(0)
.binding(0)
.format(vk::Format::R32G32B32A32_SFLOAT)
.offset(offset_of!(Self, position) as u32),
]
}
}

impl Vertex2D {
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
.binding(0)
.stride(std::mem::size_of::<Self>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
}
pub fn input_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
[
// offset_of macro got stabilized in rust 1.77
vk::VertexInputAttributeDescription::default()
.location(0)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, position) as u32),
vk::VertexInputAttributeDescription::default()
.location(1)
.binding(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(offset_of!(Self, tex_coord) as u32),
vk::VertexInputAttributeDescription::default()
.location(2)
.binding(0)
.format(vk::Format::R8G8B8A8_UNORM)
.offset(offset_of!(Self, color) as u32),
]
}
}
2 changes: 1 addition & 1 deletion crates/vent-runtime/src/render/d3/light_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ash::vk;
use vent_math::vec::vec3::Vec3;
use vent_rendering::{instance::VulkanInstance, mesh::Mesh3D, pipeline::VulkanPipeline, Vertex3D};
use vent_rendering::{instance::VulkanInstance, mesh::Mesh3D, pipeline::VulkanPipeline, vertex::Vertex3D};

#[allow(dead_code)]
#[repr(C)]
Expand Down
39 changes: 11 additions & 28 deletions crates/vent-runtime/src/render/d3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use vent_math::{
vec::{vec3::Vec3, vec4::Vec4},
};
use vent_rendering::{
any_as_u8_slice, buffer::VulkanBuffer, image::SkyBoxImages, instance::VulkanInstance,
mesh::Mesh3D, Vertex3D,
any_as_u8_slice, buffer::VulkanBuffer, image::SkyBoxImages, instance::VulkanInstance, mesh::Mesh3D, vertex::VertexPos3D
};

use super::{
Expand Down Expand Up @@ -263,7 +262,7 @@ impl Renderer for Renderer3D {

mesh_renderer.insert(world.create_entity(), mesh);

let tmp_light_mesh = create_tmp_cube(instance);
let tmp_light_mesh = create_simple_cube(instance);
// let light_renderer = LightRenderer::new(instance);

Self {
Expand Down Expand Up @@ -360,7 +359,7 @@ impl Renderer for Renderer3D {
}
}

fn create_tmp_cube(instance: &VulkanInstance) -> Mesh3D {
fn create_simple_cube(instance: &VulkanInstance) -> Mesh3D {
let indices = [
//Top
2, 6, 7, 2, 3, 7, //Bottom
Expand All @@ -372,45 +371,29 @@ fn create_tmp_cube(instance: &VulkanInstance) -> Mesh3D {
];

let vertices = [
Vertex3D {
VertexPos3D {
position: [-1.0, -1.0, 0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //0
Vertex3D {
VertexPos3D {
position: [1.0, -1.0, 0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //1
Vertex3D {
VertexPos3D {
position: [-1.0, 1.0, 0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //2
Vertex3D {
VertexPos3D {
position: [1.0, 1.0, 0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //3
Vertex3D {
VertexPos3D {
position: [-1.0, -1.0, -0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //4
Vertex3D {
VertexPos3D {
position: [0.0, -1.0, -0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //5
Vertex3D {
VertexPos3D {
position: [-1.0, 1.0, -0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //6
Vertex3D {
VertexPos3D {
position: [1.0, 1.0, -0.5],
tex_coord: [0.0, 0.0],
normal: [0.0, 0.0, 0.0],
}, //7
];

Expand Down
17 changes: 5 additions & 12 deletions crates/vent-runtime/src/render/d3/skybox_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ use ash::vk;
use image::GenericImageView;
use vent_math::scalar::mat4::Mat4;
use vent_rendering::{
any_as_u8_slice,
image::{SkyBoxImages, VulkanImage},
instance::VulkanInstance,
mesh::Mesh3D,
pipeline::VulkanPipeline,
Vertex3D,
any_as_u8_slice, image::{SkyBoxImages, VulkanImage}, instance::VulkanInstance, mesh::Mesh3D, pipeline::VulkanPipeline, vertex::VertexPos3D
};

use crate::render::camera::Camera3D;

use super::create_tmp_cube;
use crate::render::{camera::Camera3D, d3::create_simple_cube};

#[allow(dead_code)]
pub struct SkyBoxRenderer {
Expand Down Expand Up @@ -60,13 +53,13 @@ impl SkyBoxRenderer {
instance,
vertex_shader.as_ref(),
fragment_shader.as_ref(),
&[Vertex3D::binding_description()],
&Vertex3D::input_descriptions(),
&[VertexPos3D::binding_description()],
&VertexPos3D::input_descriptions(),
instance.surface_resolution,
&[push_constant_range],
&desc_layout_bindings,
);
let cube = create_tmp_cube(instance);
let cube = create_simple_cube(instance);
let push_constants = SkyBoxUBO {
projection: Mat4::IDENTITY,
model: Mat4::IDENTITY,
Expand Down
2 changes: 1 addition & 1 deletion crates/vent-ui/src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use ash::vk;
use vent_math::vec::vec2::Vec2;
use vent_rendering::{buffer::VulkanBuffer, instance::VulkanInstance, Vertex2D};
use vent_rendering::{buffer::VulkanBuffer, instance::VulkanInstance, vertex::Vertex2D};

pub mod ab_glyph;

Expand Down
2 changes: 1 addition & 1 deletion crates/vent-ui/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::mem::size_of;
use ash::vk::{self};
use vent_math::vec::vec2::Vec2;
use vent_rendering::{
any_as_u8_slice, instance::VulkanInstance, pipeline::VulkanPipeline, Vertex2D,
any_as_u8_slice, instance::VulkanInstance, pipeline::VulkanPipeline, vertex::Vertex2D,
};

use crate::font::{ab_glyph::AbGlyphLoader, Font};
Expand Down

0 comments on commit ea148c9

Please sign in to comment.