properly automatically importing a shader of any type

main
Nathan Buck 9 months ago
parent a6caf232d9
commit 0029f39840

@ -1,18 +1,16 @@
use std::process::Command;
use std::path::Path;
use std::ffi::OsStr;
use std::fs::File;
use std::io::Write;
use std::io::prelude::*;
use std::io::Write;
use std::path::Path;
use std::process::Command;
fn main() -> std::io::Result<()> {
println!("building shaders...");
//shaders path
let shaders = Path::new("./src/shaders");
//shader target path
let out = Command::new("mkdir")
.arg("target/shaders/")
.output();
let out = Command::new("mkdir").arg("target/shaders/").output();
let shader_target = Path::new("./target/shaders/");
//compile all glsl shaders
@ -20,29 +18,48 @@ fn main() -> std::io::Result<()> {
if let Ok(entry) = entry {
let shader_path = entry.path();
println!("compiling shader: {:?}", shader_path);
let shader_path_string: String = "./target/shaders/".to_string() + shader_path.file_name().unwrap().to_str().unwrap() + ".spv";
let shader_path_string: String = "./target/shaders/".to_string()
+ shader_path.file_name().unwrap().to_str().unwrap()
+ ".spv";
let shader_file: &OsStr = OsStr::new::<str>(shader_path_string.as_str());
let out = Command::new("glslc")
Command::new("glslc")
.arg("-c")
.arg(shader_path)
.arg("-o")
.arg(shader_file)
.output();
.status();
}
}
//include all compiled shaders in shaders.rs file in src dir
let mut txt: String = String::new();
for entry in shader_target.read_dir().expect("reading compiled shader target directory failed") {
let mut txt = "use ash::vk::ShaderStageFlags;\n\n".to_string()
+ "pub fn shaders() -> Vec<(Vec<u8>, ShaderStageFlags)> {\nvec![\n";
for entry in shader_target
.read_dir()
.expect("reading compiled shader target directory failed")
{
if let Ok(entry) = entry {
let bin_path = entry.path();
let bin_path_string = bin_path.file_name().unwrap().to_str().unwrap().to_string();
txt += &("const ".to_owned() +
&bin_path_string.replace(".spv", "").replace(".", "_").to_uppercase() +
" = include_bytes!(\"../target/shaders/" +
&bin_path_string +
"\");\n");
let shader_type = (
match bin_path_string.split(".").nth(1).unwrap() {
"vert" => "VERTEX",
"frag" => "FRAGMENT",
"comp" => "COMPUTE",
"tesc" => "TESSELLATION_CONTROL",
"tese" => "TESSELLATION_EVALUATION",
"geom" => "GEOMETRY",
_ => panic!("unknown shader type"),
}
).to_string();
txt += &("(include_bytes!(\"../target/shaders/".to_owned()
+ &bin_path_string
+ "\").to_vec(), ShaderStageFlags::"
+ &shader_type
+ "),\n");
}
}
txt += "]}";
let mut file = File::create("./src/shaders.rs")?;
file.write_all(txt.as_bytes())?;
Ok(())

@ -1,23 +1,21 @@
pub mod shaders;
pub mod utility;
use crate::{
utility::constants::*,
utility::debug::*,
utility::share,
utility::structures::*,
shaders::*, utility::constants::*, utility::debug::*, utility::share, utility::structures::*,
};
use ash::{vk, Entry};
use vk::*;
use memoffset::offset_of;
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
use vk::*;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use std::ffi::CString;
use std::ptr;
// Constants
const WINDOW_TITLE: &'static str = "18.Vertex Buffer";
const WINDOW_TITLE: &'static str = "Template";
#[repr(C)]
#[derive(Clone, Debug, Copy)]
@ -113,8 +111,8 @@ struct VulkanApp {
impl VulkanApp {
pub fn new(event_loop: &winit::event_loop::EventLoop<()>) -> VulkanApp {
let window = utility::window::init_window(event_loop, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
let window =
utility::window::init_window(event_loop, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);
// init vulkan stuff
let entry = unsafe { Entry::load().unwrap() };
@ -237,7 +235,9 @@ impl VulkanApp {
p_next: ptr::null(),
flags: vk::BufferCreateFlags::empty(),
size: std::mem::size_of_val(&VERTICES_DATA) as u64,
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
usage: vk::BufferUsageFlags::VERTEX_BUFFER
| vk::BufferUsageFlags::STORAGE_BUFFER
| vk::BufferUsageFlags::TRANSFER_DST,
sharing_mode: vk::SharingMode::EXCLUSIVE,
queue_family_index_count: 0,
p_queue_family_indices: ptr::null(),
@ -410,39 +410,22 @@ impl VulkanApp {
render_pass: vk::RenderPass,
swapchain_extent: vk::Extent2D,
) -> (vk::Pipeline, vk::PipelineLayout) {
let vert_shader_module = share::create_shader_module(
device,
include_bytes!("../target/shaders/tri.vert.spv").to_vec(),
);
let frag_shader_module = share::create_shader_module(
device,
include_bytes!("../target/shaders/tri.frag.spv").to_vec(),
);
let main_function_name = CString::new("main").unwrap(); // the beginning function name in shader code.
let shader_stages = [
let mut shader_modules: Vec<vk::PipelineShaderStageCreateInfo> = vec![];
let main_function = CString::new("main").unwrap();
for (shader, stage_i) in shaders() {
shader_modules.push(
vk::PipelineShaderStageCreateInfo {
// Vertex Shader
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineShaderStageCreateFlags::empty(),
module: vert_shader_module,
p_name: main_function_name.as_ptr(),
stage: vk::ShaderStageFlags::VERTEX,
module: share::create_shader_module(device, shader),
p_name: main_function.as_ptr(),
stage: stage_i,
p_specialization_info: ptr::null(),
},
vk::PipelineShaderStageCreateInfo {
// Fragment Shader
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineShaderStageCreateFlags::empty(),
module: frag_shader_module,
p_name: main_function_name.as_ptr(),
stage: vk::ShaderStageFlags::FRAGMENT,
p_specialization_info: ptr::null(),
},
];
}
)
}
let binding_description = Vertex::get_binding_description();
let attribute_description = Vertex::get_attribute_descriptions();
@ -583,8 +566,8 @@ impl VulkanApp {
s_type: vk::StructureType::GRAPHICS_PIPELINE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineCreateFlags::empty(),
stage_count: shader_stages.len() as u32,
p_stages: shader_stages.as_ptr(),
stage_count: shader_modules.len() as u32,
p_stages: shader_modules.as_ptr(),
p_vertex_input_state: &vertex_input_state_create_info,
p_input_assembly_state: &vertex_input_assembly_state_info,
p_tessellation_state: ptr::null(),
@ -612,8 +595,9 @@ impl VulkanApp {
};
unsafe {
device.destroy_shader_module(vert_shader_module, None);
device.destroy_shader_module(frag_shader_module, None);
for shader in shader_modules {
device.destroy_shader_module(shader.module, None)
}
}
(graphics_pipelines[0], pipeline_layout)
@ -822,62 +806,51 @@ impl Drop for VulkanApp {
}
}
impl VulkanApp {
pub fn main_loop(mut self, event_loop: EventLoop<()>) {
let mut tick_counter = utility::fps_limiter::FPSLimiter::new();
event_loop.run(move |event, _, control_flow| {
match event {
| Event::WindowEvent { event, .. } => {
match event {
| WindowEvent::CloseRequested => {
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { input, .. } => match input {
KeyboardInput {
virtual_keycode,
state,
..
} => match (virtual_keycode, state) {
(Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
*control_flow = ControlFlow::Exit
},
| WindowEvent::KeyboardInput { input, .. } => {
match input {
| KeyboardInput { virtual_keycode, state, .. } => {
match (virtual_keycode, state) {
| (Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
*control_flow = ControlFlow::Exit
},
| _ => {},
}
_ => {}
},
}
},
| _ => {},
}
_ => {}
},
| Event::MainEventsCleared => {
Event::MainEventsCleared => {
self.window.request_redraw();
},
| Event::RedrawRequested(_window_id) => {
}
Event::RedrawRequested(_window_id) => {
self.draw_frame();
tick_counter.tick_frame();
if true {
print!("FPS: {}\r", tick_counter.fps());
}
},
| Event::LoopDestroyed => {
}
Event::LoopDestroyed => {
unsafe {
self.device.device_wait_idle()
self.device
.device_wait_idle()
.expect("Failed to wait device idle!")
};
},
_ => (),
}
_ => (),
})
}
}
fn main() {
let event_loop = EventLoop::new();
let vulkan_app = VulkanApp::new(&event_loop);

@ -1,2 +1,7 @@
const TRI_FRAG = include_bytes!("../target/shaders/tri.frag.spv");
const TRI_VERT = include_bytes!("../target/shaders/tri.vert.spv");
use ash::vk::ShaderStageFlags;
pub fn shaders() -> Vec<(Vec<u8>, ShaderStageFlags)> {
vec![
(include_bytes!("../target/shaders/tri.frag.spv").to_vec(), ShaderStageFlags::FRAGMENT),
(include_bytes!("../target/shaders/tri.vert.spv").to_vec(), ShaderStageFlags::VERTEX),
]}

@ -0,0 +1,21 @@
#version 450
#extension GL_ARB_separate_shader_objects: enable
struct Particle {
vec2 position;
vec2 velocity;
vec4 color;
}
layout (std140, binding = 1) readonly buffer ParticleSSBOIn {
Particle particlesIn[ ];
};
layout (std140, binding = 2) buffer ParticleSSBOOut {
Particle particlesOut[ ];
};
void main() {
particlesOut[index].position = particlesIn[index].position + particlesIn[index].velocity.xy * ubo.deltaTime;
}

@ -72,216 +72,6 @@ pub mod v1 {
}
}
pub fn create_graphics_pipeline(
device: &ash::Device,
render_pass: vk::RenderPass,
swapchain_extent: vk::Extent2D,
) -> (vk::Pipeline, vk::PipelineLayout) {
let vert_shader_module = create_shader_module(
device,
include_bytes!("../../target/shaders/tri.vert.spv").to_vec(),
);
let frag_shader_module = create_shader_module(
device,
include_bytes!("../../target/shaders/tri.frag.spv").to_vec(),
);
let main_function_name = CString::new("main").unwrap(); // the beginning function name in shader code.
let shader_stages = [
vk::PipelineShaderStageCreateInfo {
// Vertex Shader
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineShaderStageCreateFlags::empty(),
module: vert_shader_module,
p_name: main_function_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::ShaderStageFlags::VERTEX,
},
vk::PipelineShaderStageCreateInfo {
// Fragment Shader
s_type: vk::StructureType::PIPELINE_SHADER_STAGE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineShaderStageCreateFlags::empty(),
module: frag_shader_module,
p_name: main_function_name.as_ptr(),
p_specialization_info: ptr::null(),
stage: vk::ShaderStageFlags::FRAGMENT,
},
];
let vertex_input_state_create_info = vk::PipelineVertexInputStateCreateInfo {
s_type: vk::StructureType::PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineVertexInputStateCreateFlags::empty(),
vertex_attribute_description_count: 0,
p_vertex_attribute_descriptions: ptr::null(),
vertex_binding_description_count: 0,
p_vertex_binding_descriptions: ptr::null(),
};
let vertex_input_assembly_state_info = vk::PipelineInputAssemblyStateCreateInfo {
s_type: vk::StructureType::PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
flags: vk::PipelineInputAssemblyStateCreateFlags::empty(),
p_next: ptr::null(),
primitive_restart_enable: vk::FALSE,
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
};
let viewports = [vk::Viewport {
x: 0.0,
y: 0.0,
width: swapchain_extent.width as f32,
height: swapchain_extent.height as f32,
min_depth: 0.0,
max_depth: 1.0,
}];
let scissors = [vk::Rect2D {
offset: vk::Offset2D { x: 0, y: 0 },
extent: swapchain_extent,
}];
let viewport_state_create_info = vk::PipelineViewportStateCreateInfo {
s_type: vk::StructureType::PIPELINE_VIEWPORT_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineViewportStateCreateFlags::empty(),
scissor_count: scissors.len() as u32,
p_scissors: scissors.as_ptr(),
viewport_count: viewports.len() as u32,
p_viewports: viewports.as_ptr(),
};
let rasterization_statue_create_info = vk::PipelineRasterizationStateCreateInfo {
s_type: vk::StructureType::PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineRasterizationStateCreateFlags::empty(),
depth_clamp_enable: vk::FALSE,
cull_mode: vk::CullModeFlags::BACK,
front_face: vk::FrontFace::CLOCKWISE,
line_width: 1.0,
polygon_mode: vk::PolygonMode::FILL,
rasterizer_discard_enable: vk::FALSE,
depth_bias_clamp: 0.0,
depth_bias_constant_factor: 0.0,
depth_bias_enable: vk::FALSE,
depth_bias_slope_factor: 0.0,
};
let multisample_state_create_info = vk::PipelineMultisampleStateCreateInfo {
s_type: vk::StructureType::PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
flags: vk::PipelineMultisampleStateCreateFlags::empty(),
p_next: ptr::null(),
rasterization_samples: vk::SampleCountFlags::TYPE_1,
sample_shading_enable: vk::FALSE,
min_sample_shading: 0.0,
p_sample_mask: ptr::null(),
alpha_to_one_enable: vk::FALSE,
alpha_to_coverage_enable: vk::FALSE,
};
let stencil_state = vk::StencilOpState {
fail_op: vk::StencilOp::KEEP,
pass_op: vk::StencilOp::KEEP,
depth_fail_op: vk::StencilOp::KEEP,
compare_op: vk::CompareOp::ALWAYS,
compare_mask: 0,
write_mask: 0,
reference: 0,
};
let depth_state_create_info = vk::PipelineDepthStencilStateCreateInfo {
s_type: vk::StructureType::PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineDepthStencilStateCreateFlags::empty(),
depth_test_enable: vk::FALSE,
depth_write_enable: vk::FALSE,
depth_compare_op: vk::CompareOp::LESS_OR_EQUAL,
depth_bounds_test_enable: vk::FALSE,
stencil_test_enable: vk::FALSE,
front: stencil_state,
back: stencil_state,
max_depth_bounds: 1.0,
min_depth_bounds: 0.0,
};
let color_blend_attachment_states = [vk::PipelineColorBlendAttachmentState {
blend_enable: vk::FALSE,
color_write_mask: vk::ColorComponentFlags::empty(),
src_color_blend_factor: vk::BlendFactor::ONE,
dst_color_blend_factor: vk::BlendFactor::ZERO,
color_blend_op: vk::BlendOp::ADD,
src_alpha_blend_factor: vk::BlendFactor::ONE,
dst_alpha_blend_factor: vk::BlendFactor::ZERO,
alpha_blend_op: vk::BlendOp::ADD,
}];
let color_blend_state = vk::PipelineColorBlendStateCreateInfo {
s_type: vk::StructureType::PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineColorBlendStateCreateFlags::empty(),
logic_op_enable: vk::FALSE,
logic_op: vk::LogicOp::COPY,
attachment_count: color_blend_attachment_states.len() as u32,
p_attachments: color_blend_attachment_states.as_ptr(),
blend_constants: [0.0, 0.0, 0.0, 0.0],
};
let pipeline_layout_create_info = vk::PipelineLayoutCreateInfo {
s_type: vk::StructureType::PIPELINE_LAYOUT_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineLayoutCreateFlags::empty(),
set_layout_count: 0,
p_set_layouts: ptr::null(),
push_constant_range_count: 0,
p_push_constant_ranges: ptr::null(),
};
let pipeline_layout = unsafe {
device
.create_pipeline_layout(&pipeline_layout_create_info, None)
.expect("Failed to create pipeline layout!")
};
let graphic_pipeline_create_infos = [vk::GraphicsPipelineCreateInfo {
s_type: vk::StructureType::GRAPHICS_PIPELINE_CREATE_INFO,
p_next: ptr::null(),
flags: vk::PipelineCreateFlags::empty(),
stage_count: shader_stages.len() as u32,
p_stages: shader_stages.as_ptr(),
p_vertex_input_state: &vertex_input_state_create_info,
p_input_assembly_state: &vertex_input_assembly_state_info,
p_tessellation_state: ptr::null(),
p_viewport_state: &viewport_state_create_info,
p_rasterization_state: &rasterization_statue_create_info,
p_multisample_state: &multisample_state_create_info,
p_depth_stencil_state: &depth_state_create_info,
p_color_blend_state: &color_blend_state,
p_dynamic_state: ptr::null(),
layout: pipeline_layout,
render_pass,
subpass: 0,
base_pipeline_handle: vk::Pipeline::null(),
base_pipeline_index: -1,
}];
let graphics_pipelines = unsafe {
device
.create_graphics_pipelines(
vk::PipelineCache::null(),
&graphic_pipeline_create_infos,
None,
)
.expect("Failed to create Graphics Pipeline!.")
};
unsafe {
device.destroy_shader_module(vert_shader_module, None);
device.destroy_shader_module(frag_shader_module, None);
}
(graphics_pipelines[0], pipeline_layout)
}
pub fn create_framebuffers(
device: &ash::Device,
render_pass: vk::RenderPass,
@ -740,7 +530,8 @@ pub mod v1 {
.expect("Failed to create Texture Image!")
};
let image_memory_requirement = unsafe { device.get_image_memory_requirements(texture_image) };
let image_memory_requirement =
unsafe { device.get_image_memory_requirements(texture_image) };
let memory_allocate_info = vk::MemoryAllocateInfo {
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
p_next: ptr::null(),
@ -1379,7 +1170,6 @@ use std::os::raw::c_void;
use std::path::Path;
use std::ptr;
use crate::utility::constants::*;
use crate::utility::debug;
use crate::utility::platforms;
@ -1427,8 +1217,7 @@ pub fn create_instance(
let create_info = vk::InstanceCreateInfo {
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
p_next: if VALIDATION.is_enable {
&debug_utils_create_info as *const vk::DebugUtilsMessengerCreateInfoEXT
as *const c_void
&debug_utils_create_info as *const vk::DebugUtilsMessengerCreateInfoEXT as *const c_void
} else {
ptr::null()
},
@ -1638,7 +1427,8 @@ pub fn find_queue_family(
physical_device,
index as u32,
surface_stuff.surface,
).unwrap()
)
.unwrap()
};
if queue_family.queue_count > 0 && is_present_support {
queue_family_indices.present_family = Some(index);
@ -1793,7 +1583,6 @@ pub fn create_swapchain(
pub fn choose_swapchain_format(
available_formats: &Vec<vk::SurfaceFormatKHR>,
) -> vk::SurfaceFormatKHR {
for available_format in available_formats {
if available_format.format == vk::Format::B8G8R8A8_SRGB
&& available_format.color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
@ -1826,8 +1615,7 @@ pub fn choose_swapchain_extent(
} else {
use num::clamp;
let window_size = window
.inner_size();
let window_size = window.inner_size();
println!(
"\t\tInner Window Size: ({}, {})",
window_size.width, window_size.height

@ -1,6 +1,5 @@
use winit::event::{Event, VirtualKeyCode, ElementState, KeyboardInput, WindowEvent};
use winit::event_loop::{EventLoop, ControlFlow};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
const IS_PAINT_FPS_COUNTER: bool = true;
@ -31,7 +30,6 @@ pub struct ProgramProc {
}
impl ProgramProc {
pub fn new() -> ProgramProc {
// init window stuff
let event_loop = EventLoop::new();
@ -40,42 +38,38 @@ impl ProgramProc {
}
pub fn main_loop<A: 'static + VulkanApp>(self, mut vulkan_app: A) {
let mut tick_counter = super::fps_limiter::FPSLimiter::new();
self.event_loop.run(move |event, _, control_flow| {
match event {
| Event::WindowEvent { event, .. } => {
match event {
| WindowEvent::CloseRequested => {
self.event_loop
.run(move |event, _, control_flow| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => {
vulkan_app.wait_device_idle();
*control_flow = ControlFlow::Exit
},
| WindowEvent::KeyboardInput { input, .. } => {
match input {
| KeyboardInput { virtual_keycode, state, .. } => {
match (virtual_keycode, state) {
| (Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
}
WindowEvent::KeyboardInput { input, .. } => match input {
KeyboardInput {
virtual_keycode,
state,
..
} => match (virtual_keycode, state) {
(Some(VirtualKeyCode::Escape), ElementState::Pressed) => {
vulkan_app.wait_device_idle();
*control_flow = ControlFlow::Exit
},
| _ => {},
}
_ => {}
},
}
},
| WindowEvent::Resized(_new_size) => {
WindowEvent::Resized(_new_size) => {
vulkan_app.wait_device_idle();
vulkan_app.resize_framebuffer();
},
| _ => {},
}
_ => {}
},
| Event::MainEventsCleared => {
Event::MainEventsCleared => {
vulkan_app.window_ref().request_redraw();
},
| Event::RedrawRequested(_window_id) => {
}
Event::RedrawRequested(_window_id) => {
let delta_time = tick_counter.delta_time();
vulkan_app.draw_frame(delta_time);
@ -84,14 +78,11 @@ impl ProgramProc {
}
tick_counter.tick_frame();
},
| Event::LoopDestroyed => {
}
Event::LoopDestroyed => {
vulkan_app.wait_device_idle();
},
_ => (),
}
_ => (),
})
}
}

Loading…
Cancel
Save