Skip to content

Commit

Permalink
[vent-assets] gltf: add pipeline caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jun 27, 2024
1 parent b4c5291 commit 7695631
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 76 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/vent-assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2021"
vent-sdk = { path = "../vent-sdk" }
vent-rendering = { path = "../vent-rendering"}

ordered-float = "4.2.0"

image = "0.25"

log = "0.4"
Expand Down
135 changes: 74 additions & 61 deletions crates/vent-assets/src/model/gltf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashMap,
ffi::CStr,
fs::{self, File},
io::BufReader,
Expand Down Expand Up @@ -195,6 +196,8 @@ impl GLTFLoader {
loaded_materials.push(material);
});

let mut cached_pipeline = HashMap::new();

for (i, material) in loaded_materials.into_iter().enumerate() {
let mut all_meshes = vec![];
for primitive in mesh
Expand All @@ -214,7 +217,7 @@ impl GLTFLoader {
}
let pipeline_info = MaterialPipelineInfo {
mode: vk::PrimitiveTopology::TRIANGLE_LIST, // TODO
alpha_cut: Some(material.alpha_cut),
alpha_cut: Some(ordered_float::OrderedFloat(material.alpha_cut)),
double_sided: material.double_sided,
};

Expand All @@ -224,71 +227,81 @@ impl GLTFLoader {
meshes: all_meshes,
};

let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
topology: pipeline_info.mode,
..Default::default()
};
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
line_width: 1.0,
polygon_mode: vk::PolygonMode::FILL,
cull_mode: if pipeline_info.double_sided {
vk::CullModeFlags::NONE
} else {
vk::CullModeFlags::BACK
},
..Default::default()
};

{
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
rasterization_samples: vk::SampleCountFlags::TYPE_1,
if cached_pipeline.contains_key(&pipeline_info) {
pipelines.push(ModelPipeline {
pipeline: *cached_pipeline.get(&pipeline_info).unwrap(),
materials: vec![model_material], // TODO
});
} else {
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
topology: pipeline_info.mode,
..Default::default()
};

let depth_state_info = vk::PipelineDepthStencilStateCreateInfo::default()
.depth_test_enable(true)
.depth_write_enable(true)
.depth_compare_op(vk::CompareOp::LESS)
.max_depth_bounds(1.0);
let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
color_write_mask: vk::ColorComponentFlags::RGBA,
let rasterization_info = vk::PipelineRasterizationStateCreateInfo {
front_face: vk::FrontFace::COUNTER_CLOCKWISE,
line_width: 1.0,
polygon_mode: vk::PolygonMode::FILL,
cull_mode: if pipeline_info.double_sided {
vk::CullModeFlags::NONE
} else {
vk::CullModeFlags::BACK
},
..Default::default()
}];
let color_blend_state = vk::PipelineColorBlendStateCreateInfo::default()
.logic_op(vk::LogicOp::COPY)
.attachments(&color_blend_attachment_states);

let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; // TODO
let dynamic_state_info =
vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&dynamic_state);

let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::default()
.stages(&shader_stage_create_info)
.vertex_input_state(&vertex_input_state_info)
.input_assembly_state(&vertex_input_assembly_state_info)
.viewport_state(&viewport_state_info)
.rasterization_state(&rasterization_info)
.multisample_state(&multisample_state_info)
.depth_stencil_state(&depth_state_info)
.color_blend_state(&color_blend_state)
.dynamic_state(&dynamic_state_info)
.layout(pipeline_layout)
.render_pass(instance.render_pass);

let graphics_pipelines = unsafe {
instance.device.create_graphics_pipelines(
vk::PipelineCache::null(),
&[graphic_pipeline_info],
None,
)
}
.expect("Unable to create graphics pipeline");
};

pipelines.push(ModelPipeline {
pipeline: graphics_pipelines[0],
materials: vec![model_material], // TODO
});
{
let multisample_state_info = vk::PipelineMultisampleStateCreateInfo {
rasterization_samples: vk::SampleCountFlags::TYPE_1,
..Default::default()
};

let depth_state_info = vk::PipelineDepthStencilStateCreateInfo::default()
.depth_test_enable(true)
.depth_write_enable(true)
.depth_compare_op(vk::CompareOp::LESS)
.max_depth_bounds(1.0);
let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
color_write_mask: vk::ColorComponentFlags::RGBA,
..Default::default()
}];
let color_blend_state = vk::PipelineColorBlendStateCreateInfo::default()
.logic_op(vk::LogicOp::COPY)
.attachments(&color_blend_attachment_states);

let dynamic_state = [vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]; // TODO
let dynamic_state_info = vk::PipelineDynamicStateCreateInfo::default()
.dynamic_states(&dynamic_state);

let graphic_pipeline_info = vk::GraphicsPipelineCreateInfo::default()
.stages(&shader_stage_create_info)
.vertex_input_state(&vertex_input_state_info)
.input_assembly_state(&vertex_input_assembly_state_info)
.viewport_state(&viewport_state_info)
.rasterization_state(&rasterization_info)
.multisample_state(&multisample_state_info)
.depth_stencil_state(&depth_state_info)
.color_blend_state(&color_blend_state)
.dynamic_state(&dynamic_state_info)
.layout(pipeline_layout)
.render_pass(instance.render_pass);

let graphics_pipelines = unsafe {
instance.device.create_graphics_pipelines(
vk::PipelineCache::null(),
&[graphic_pipeline_info],
None,
)
}
.expect("Unable to create graphics pipeline");

pipelines.push(ModelPipeline {
pipeline: graphics_pipelines[0],
materials: vec![model_material], // TODO
});

cached_pipeline.insert(pipeline_info, graphics_pipelines[0]);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/vent-rendering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ vent-window = { path= "../vent-window"}
ash = { version= "0.38", default-features = false, features = ["linked", "debug", "std"] }
spirv = "0.3.0"

ordered-float = "4.2.0"

image = "0.25"

raw-window-handle = "0.6"
Expand Down
13 changes: 6 additions & 7 deletions crates/vent-rendering/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ impl VulkanInstance {
tiling: vk::ImageTiling,
features: vk::FormatFeatureFlags,
) -> Option<vk::Format> {
candidates.iter().cloned().find(|f| {
let properties = unsafe { instance.get_physical_device_format_properties(pdevice, *f) };
candidates.iter().find(|f| {
let properties = unsafe { instance.get_physical_device_format_properties(pdevice, **f) };
match tiling {
vk::ImageTiling::LINEAR => properties.linear_tiling_features.contains(features),
vk::ImageTiling::OPTIMAL => properties.optimal_tiling_features.contains(features),
_ => false,
}
})
}).copied()
}

fn create_frame_buffers(
Expand Down Expand Up @@ -550,9 +550,8 @@ impl VulkanInstance {
.unwrap();
let present_mode = present_modes
.iter()
.cloned()
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(vk::PresentModeKHR::FIFO);
.find(|&mode| *mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(&vk::PresentModeKHR::FIFO);

let swapchain_create_info = vk::SwapchainCreateInfoKHR::default()
.surface(surface)
Expand All @@ -564,7 +563,7 @@ impl VulkanInstance {
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(pre_transform)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(present_mode)
.present_mode(*present_mode)
.clipped(true)
.image_array_layers(1)
.old_swapchain(old_swapchain.unwrap_or_default());
Expand Down
5 changes: 3 additions & 2 deletions crates/vent-rendering/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::mem::{self, offset_of};

use ash::vk;
use ordered_float::OrderedFloat;

pub mod allocator;
pub mod buffer;
Expand All @@ -12,10 +13,10 @@ mod surface;

const DEFAULT_FENCE_TIMEOUT: u64 = 100000000000;

#[derive(PartialEq)]
#[derive(PartialEq, Eq, Hash)]
pub struct MaterialPipelineInfo {
pub mode: vk::PrimitiveTopology,
pub alpha_cut: Option<f32>, // Default 0.5
pub alpha_cut: Option<OrderedFloat<f32>>, // Default 0.5
pub double_sided: bool,
}

Expand Down
6 changes: 1 addition & 5 deletions crates/vent-rendering/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

use std::os::raw::c_char;

use ash::{
khr::surface,
prelude::*,
vk, Entry, Instance,
};
use ash::{khr::surface, prelude::*, vk, Entry, Instance};
use raw_window_handle::{DisplayHandle, RawDisplayHandle, RawWindowHandle, WindowHandle};

pub unsafe fn create_surface(
Expand Down
5 changes: 4 additions & 1 deletion crates/vent-window/src/platform/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use std::{
time::Duration,
};

use sctk::{reexports::protocols::xdg::shell::client::xdg_toplevel::ResizeEdge as XdgResizeEdge, seat::pointer::{ThemeSpec, ThemedPointer}};
use sctk::{
reexports::protocols::xdg::shell::client::xdg_toplevel::ResizeEdge as XdgResizeEdge,
seat::pointer::{ThemeSpec, ThemedPointer},
};

use rwh_06::{RawDisplayHandle, RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle};
use sctk::{
Expand Down

0 comments on commit 7695631

Please sign in to comment.