Skip to content

Commit

Permalink
support different indices types
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jul 4, 2024
1 parent 8008b4b commit 6fca7af
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/vent-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Mesh3D {
// Basic
vertex_buf: VulkanBuffer,
index_buf: VulkanBuffer,
index_type: vk::IndexType,
index_count: u32,
}

Expand Down
23 changes: 16 additions & 7 deletions crates/vent-assets/src/model/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ash::{
use gltf::{material::AlphaMode, mesh::Mode, texture::Sampler};
use image::DynamicImage;
use vent_rendering::{
image::VulkanImage, instance::VulkanInstance, MaterialPipelineInfo, Vertex, Vertex3D,
image::VulkanImage, instance::VulkanInstance, Indices, MaterialPipelineInfo, Vertex, Vertex3D,
};

use crate::{Material, Model3D, ModelPipeline};
Expand Down Expand Up @@ -210,7 +210,7 @@ impl GLTFLoader {
instance,
&instance.memory_allocator,
&final_primitive.0,
&final_primitive.1,
final_primitive.1,
mesh.name(),
);
all_meshes.push(loaded_mesh);
Expand Down Expand Up @@ -471,10 +471,10 @@ impl GLTFLoader {
}
}

fn load_primitive(
buffer_data: &[gltf::buffer::Data],
primitive: gltf::Primitive,
) -> (Vec<Vertex3D>, Vec<u32>) {
fn load_primitive<'a>(
buffer_data: &'a [gltf::buffer::Data],
primitive: gltf::Primitive<'a>,
) -> (Vec<Vertex3D>, Indices) {
let reader = primitive.reader(|buffer| Some(&buffer_data[buffer.index()]));

let mut vertices: Vec<Vertex3D> = reader
Expand All @@ -501,7 +501,16 @@ impl GLTFLoader {

let vertices = optimizer::optimize_vertices(vertices);

let indices: Vec<_> = reader.read_indices().unwrap().into_u32().collect();
// TODO Handle where there are no indices
let indices = match reader.read_indices().unwrap() {
gltf::mesh::util::ReadIndices::U8(indices) => Indices::U8(indices.collect::<Vec<_>>()),
gltf::mesh::util::ReadIndices::U16(indices) => {
Indices::U16(indices.collect::<Vec<_>>())
}
gltf::mesh::util::ReadIndices::U32(indices) => {
Indices::U32(indices.collect::<Vec<_>>())
}
};
(vertices, indices)
}
}
21 changes: 10 additions & 11 deletions crates/vent-assets/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ash::vk;
use vent_rendering::allocator::MemoryAllocator;
use vent_rendering::buffer::VulkanBuffer;
use vent_rendering::instance::VulkanInstance;
use vent_rendering::{begin_single_time_command, end_single_time_command, Vertex3D};
use vent_rendering::{begin_single_time_command, end_single_time_command, Indices, Vertex3D};
use vent_sdk::utils::stopwatch::Stopwatch;

use crate::{Material, Mesh3D, Model3D};
Expand Down Expand Up @@ -149,11 +149,11 @@ impl Mesh3D {
instance: &VulkanInstance,
allocator: &MemoryAllocator,
vertices: &[Vertex3D],
indices: &[u32],
indices: Indices,
name: Option<&str>,
) -> Self {
let vertex_size = std::mem::size_of_val(vertices) as vk::DeviceSize;
let index_size = std::mem::size_of_val(indices) as vk::DeviceSize;
let index_size = std::mem::size_of_val(indices.get_slice()) as vk::DeviceSize;

let vertex_buf = VulkanBuffer::new(
instance,
Expand Down Expand Up @@ -187,13 +187,11 @@ impl Mesh3D {
// copy vertex buffer
unsafe { staging_buf.upload_data(memory, vertices, vertex_size) };
// copy index buffer
unsafe {
staging_buf.upload_data(
memory.wrapping_add(vertex_size as usize),
indices,
index_size,
)
};
indices.upload(
&staging_buf,
memory.wrapping_add(vertex_size as usize),
index_size,
);

let command_buffer = begin_single_time_command(&instance.device, instance.command_pool);

Expand Down Expand Up @@ -231,6 +229,7 @@ impl Mesh3D {
Self {
vertex_buf,
index_buf,
index_type: indices.vk_type(),
index_count: indices.len() as u32,
}
}
Expand All @@ -245,7 +244,7 @@ impl Mesh3D {
None,
None,
);
device.cmd_bind_index_buffer(command_buffer, *self.index_buf, 0, vk::IndexType::UINT32);
device.cmd_bind_index_buffer(command_buffer, *self.index_buf, 0, self.index_type);
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/vent-assets/src/model/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl OBJLoader {

let mut meshes = Vec::new();
for model in models {
let mesh = Self::load_mesh(&model.mesh);
let vertices = Self::load_mesh(&model.mesh);

let _matieral = Self::load_material(
instance,
Expand All @@ -39,8 +39,8 @@ impl OBJLoader {
meshes.push(Mesh3D::new(
instance,
&instance.memory_allocator,
&mesh.0,
mesh.1,
&vertices,
vent_rendering::Indices::U32(model.mesh.indices),
Some(&model.name),
));
}
Expand Down Expand Up @@ -94,7 +94,7 @@ impl OBJLoader {
}
}

fn load_mesh(mesh: &tobj::Mesh) -> (Vec<Vertex3D>, &[u32]) {
fn load_mesh(mesh: &tobj::Mesh) -> Vec<Vertex3D> {
let vertices = (0..mesh.positions.len() / 3)
.map(|i| Vertex3D {
position: [
Expand All @@ -110,6 +110,6 @@ impl OBJLoader {
],
})
.collect::<Vec<_>>();
(vertices, &mesh.indices)
vertices
}
}
2 changes: 1 addition & 1 deletion crates/vent-editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vent-common = { path = "../vent-common"}

vent-rendering = { path = "../vent-rendering"}

ash = { version= "0.38", default-features = false, features = ["linked", "debug"] }
ash = { version= "0.38", default-features = false, features = ["linked"] }

pollster = "0.3.0"

Expand Down
1 change: 1 addition & 0 deletions crates/vent-rendering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ash = { version= "0.38", default-features = false, features = ["linked", "debug"
spirv = "0.3.0"

ordered-float = "4.2.0"
bytemuck = "1.16"

image = "0.25"

Expand Down
10 changes: 6 additions & 4 deletions crates/vent-rendering/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,15 @@ impl VulkanInstance {
portability_subset::NAME.as_ptr(),
];

let available_features = unsafe { instance.get_physical_device_features(pdevice) };

let mut features_1_3 = vk::PhysicalDeviceVulkan13Features::default()
.synchronization2(true)
.maintenance4(true);

let features = vk::PhysicalDeviceFeatures::default()
.shader_clip_distance(true)
.sampler_anisotropy(true);
let mut features = vk::PhysicalDeviceFeatures::default();
features.sampler_anisotropy = available_features.sampler_anisotropy;
// features.shader_clip_distance = available_features.shader_clip_distance;

let priorities = [1.0];

Expand Down Expand Up @@ -556,7 +558,7 @@ impl VulkanInstance {
.unwrap();
let present_mode = present_modes
.iter()
.find(|&mode| *mode == vk::PresentModeKHR::MAILBOX)
.find(|&mode| *mode == vk::PresentModeKHR::IMMEDIATE)
.unwrap_or(&vk::PresentModeKHR::FIFO);

let swapchain_create_info = vk::SwapchainCreateInfoKHR::default()
Expand Down
57 changes: 56 additions & 1 deletion crates/vent-rendering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::mem::{self, offset_of};
use std::{
mem::{self, offset_of},
os::raw::c_void,
};

use ash::vk;
use buffer::VulkanBuffer;
use bytemuck::cast_slice;
use ordered_float::OrderedFloat;

pub mod allocator;
Expand Down Expand Up @@ -32,6 +37,56 @@ pub struct Vertex3D {
pub normal: [f32; 3],
}

pub enum Indices {
U8(Vec<u8>),
U16(Vec<u16>),
U32(Vec<u32>),
}

impl Indices {
/// Returns the number of indices.
pub fn len(&self) -> usize {
match self {
Indices::U8(vec) => vec.len(),
Indices::U16(vec) => vec.len(),
Indices::U32(vec) => vec.len(),
}
}

pub fn upload(&self, buffer: &VulkanBuffer, memory: *mut c_void, size: vk::DeviceSize) {
match self {
Indices::U8(vec) => unsafe { buffer.upload_data(memory, vec, size) },
Indices::U16(vec) => unsafe { buffer.upload_data(memory, vec, size) },
Indices::U32(vec) => unsafe { buffer.upload_data(memory, vec, size) },
}
}

pub fn get_slice(&self) -> &[u8] {
match self {
Indices::U8(indices) => cast_slice(&indices[..]),
Indices::U16(indices) => cast_slice(&indices[..]),
Indices::U32(indices) => cast_slice(&indices[..]),
}
}

/// Returns `true` if there are no indices.
pub fn is_empty(&self) -> bool {
match self {
Indices::U8(vec) => vec.is_empty(),
Indices::U16(vec) => vec.is_empty(),
Indices::U32(vec) => vec.is_empty(),
}
}

pub fn vk_type(&self) -> vk::IndexType {
match self {
Indices::U8(_) => vk::IndexType::UINT8_KHR,
Indices::U16(_) => vk::IndexType::UINT16,
Indices::U32(_) => vk::IndexType::UINT32,
}
}
}

impl Vertex for Vertex3D {
fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/vent-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ vent-window = { path = "../vent-window"}
vent-logging = { path = "../vent-logging"}
vent-math = { path = "../vent-math"}

ash = { version= "0.38", default-features = false, features = ["linked", "debug"] }
ash = { version= "0.38", default-features = false, features = ["linked"] }

glam = "0.28"

Expand Down
2 changes: 1 addition & 1 deletion crates/vent-runtime/src/render/d3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn create_tmp_cube(instance: &VulkanInstance) -> vent_assets::Mesh3D {
instance,
&instance.memory_allocator,
&vertices,
&indices,
vent_rendering::Indices::U8(indices.to_vec()),
None,
)
}
6 changes: 3 additions & 3 deletions crates/vent-runtime/src/render/gui/gui_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::{debug_gui::RenderData, GUI};

#[allow(dead_code)]
pub struct EguiRenderer {
pub struct GuiRenderer {
// renderer: egui_winit_ash_integration::Integration,
// state: egui_winit::State,
guis: Vec<Box<dyn GUI>>,
}

impl Default for EguiRenderer {
impl Default for GuiRenderer {
fn default() -> Self {
Self::new()
}
}

impl EguiRenderer {
impl GuiRenderer {
pub fn new() -> Self {
Self {
// state,
Expand Down
8 changes: 4 additions & 4 deletions crates/vent-runtime/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::{Duration, Instant};

use ash::vk;
use gui::gui_renderer::GuiRenderer;
use vent_rendering::instance::VulkanInstance;

use self::camera::{from_dimension, Camera};
use self::d2::Renderer2D;
use self::d3::Renderer3D;
use self::gui::debug_gui::{DebugGUI, RenderData};
use self::gui::gui_renderer::EguiRenderer;

pub mod camera;
pub mod gui;
Expand Down Expand Up @@ -89,7 +89,7 @@ pub trait Renderer {
}

pub struct RawRuntimeRenderer {
gui_renderer: EguiRenderer,
gui_renderer: GuiRenderer,
multi_renderer: Box<dyn Renderer>,

current_data: RenderData,
Expand All @@ -105,7 +105,7 @@ impl RawRuntimeRenderer {
Dimension::D2 => Box::new(Renderer2D::init(instance, camera)),
Dimension::D3 => Box::new(Renderer3D::init(instance, camera)),
};
let egui = EguiRenderer::new()
let gui_renderer = GuiRenderer::new()
// TODO
.add_gui(Box::new(DebugGUI::new(unsafe {
instance
Expand All @@ -115,7 +115,7 @@ impl RawRuntimeRenderer {

Self {
multi_renderer,
gui_renderer: egui,
gui_renderer,
current_frames: 0,
current_data: RenderData::default(),
last_fps: Instant::now(),
Expand Down
1 change: 0 additions & 1 deletion crates/vent-runtime/src/render/model_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ash::vk::{self};
use std::collections::HashMap;
use vent_ecs::entity::Entity;
use vent_math::scalar::{mat4::Mat4, quat::Quat};
use vent_rendering::instance::VulkanInstance;

use super::{d3::Camera3DData, model::Entity3D};
Expand Down
1 change: 1 addition & 0 deletions crates/vent-window/src/platform/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl PointerHandler for WaylandWindow {
let (x, y) = event.position;
match event.kind {
PointerEventKind::Enter { serial } => {
self.set_cursor = true;
self.decorations_cursor = self.window_frame.as_mut().and_then(|frame| {
frame.click_point_moved(Duration::ZERO, &event.surface.id(), x, y)
});
Expand Down

0 comments on commit 6fca7af

Please sign in to comment.