1
0
Fork 0

Modularize + Refactoring code

This commit is contained in:
Florian RICHER 2020-12-04 21:44:51 +01:00
parent 813280350f
commit 00ab67b212
23 changed files with 2001 additions and 87 deletions

5
.gitignore vendored
View file

@ -1,3 +1,6 @@
/target target
engine_core/target
engine_math/target
engine_utils/target
.idea .idea
*.png *.png

28
Cargo.lock generated
View file

@ -506,6 +506,26 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "engine_core"
version = "0.0.1"
dependencies = [
"engine_utils",
"image",
"vulkano",
"vulkano-shaders",
"vulkano-win",
"winit 0.23.0",
]
[[package]]
name = "engine_math"
version = "0.0.1"
[[package]]
name = "engine_utils"
version = "0.0.1"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -1441,11 +1461,9 @@ checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc"
name = "tuto1" name = "tuto1"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"image", "engine_core",
"vulkano", "engine_math",
"vulkano-shaders", "engine_utils",
"vulkano-win",
"winit 0.23.0",
] ]
[[package]] [[package]]

View file

@ -7,8 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
vulkano = "0.19" engine_utils = { path = "engine_utils" }
vulkano-shaders = "0.19" engine_math = { path = "engine_math" }
image = "0.23" engine_core = { path = "engine_core" }
vulkano-win = "0.19"
winit = "0.23"

1812
engine_core/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

16
engine_core/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "engine_core"
version = "0.0.1"
authors = ["Florian RICHER <florian.richer@unova.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
engine_utils = { path = "../engine_utils" }
vulkano = "0.19"
vulkano-shaders = "0.19"
image = "0.23"
vulkano-win = "0.19"
winit = "0.23"

3
engine_core/src/lib.rs Normal file
View file

@ -0,0 +1,3 @@
mod render;
pub use render::test;

View file

@ -0,0 +1,3 @@
mod vulkan;
pub use vulkan::test;

View file

@ -1,8 +1,8 @@
mod vertex;
mod physical_device; mod physical_device;
mod vertex;
pub use vertex::Vertex;
use physical_device::get_physical_device; use physical_device::get_physical_device;
pub use vertex::Vertex;
use image::ImageBuffer; use image::ImageBuffer;
use image::Rgba; use image::Rgba;
@ -12,23 +12,22 @@ use vulkano::buffer::CpuAccessibleBuffer;
use vulkano::command_buffer::AutoCommandBufferBuilder; use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::command_buffer::CommandBuffer; use vulkano::command_buffer::CommandBuffer;
use vulkano::command_buffer::DynamicState; use vulkano::command_buffer::DynamicState;
use vulkano::device::{Device, QueuesIter, Queue};
use vulkano::device::DeviceExtensions; use vulkano::device::DeviceExtensions;
use vulkano::device::Features; use vulkano::device::Features;
use vulkano::device::{Device, Queue, QueuesIter};
use vulkano::format::Format; use vulkano::format::Format;
use vulkano::framebuffer::Framebuffer; use vulkano::framebuffer::Framebuffer;
use vulkano::framebuffer::Subpass; use vulkano::framebuffer::Subpass;
use vulkano::image::Dimensions; use vulkano::image::Dimensions;
use vulkano::image::StorageImage; use vulkano::image::StorageImage;
use vulkano::instance::{Instance, PhysicalDeviceType, QueueFamily};
use vulkano::instance::InstanceExtensions; use vulkano::instance::InstanceExtensions;
use vulkano::instance::PhysicalDevice; use vulkano::instance::{Instance, QueueFamily};
use vulkano::pipeline::viewport::Viewport; use vulkano::pipeline::viewport::Viewport;
use vulkano::pipeline::GraphicsPipeline; use vulkano::pipeline::GraphicsPipeline;
use vulkano::sync::GpuFuture; use vulkano::sync::GpuFuture;
mod vs { mod vs {
vulkano_shaders::shader!{ vulkano_shaders::shader! {
ty: "vertex", ty: "vertex",
src: " src: "
#version 450 #version 450
@ -42,7 +41,7 @@ mod vs {
} }
mod fs { mod fs {
vulkano_shaders::shader!{ vulkano_shaders::shader! {
ty: "fragment", ty: "fragment",
src: " src: "
#version 450 #version 450
@ -55,34 +54,37 @@ mod fs {
} }
} }
pub struct Vulkan { pub struct VulkanInstance {
instance: Arc<Instance>, instance: Arc<Instance>,
device: Arc<Device>, device: Arc<Device>,
queues: QueuesIter, queues: QueuesIter,
} }
impl Vulkan { impl VulkanInstance {
fn init() -> Option<Vulkan> { fn init() -> Result<VulkanInstance, String> {
let instance = match Instance::new(None, &InstanceExtensions::none(), None) { let instance = Instance::new(None, &InstanceExtensions::none(), None)
Ok(instance) => instance, .map_err(|_| engine_utils::format_error!("Instance creation failed"))?;
Err(_) => return None
};
let physical_device = get_physical_device(&instance)?;
let queue_family = physical_device.queue_families()
.find(|&q| q.supports_graphics())?;
let (device, queues) = { let physical_device = get_physical_device(&instance)
match Device::new(physical_device, &Features::none(), &DeviceExtensions::none(), .ok_or(engine_utils::format_error!("No physical device found"))?;
[(queue_family, 0.5)].iter().cloned()) {
Ok((device, queues)) => (device, queues),
Err(_) => return None
}
};
Some(Vulkan { let queue_family = physical_device
.queue_families()
.find(|&q| q.supports_graphics())
.ok_or(engine_utils::format_error!("No queue found"))?;
let (device, queues) = Device::new(
physical_device,
&Features::none(),
&DeviceExtensions::none(),
[(queue_family, 0.5)].iter().cloned(),
)
.map_err(|_| engine_utils::format_error!("Virtual device creation failed"))?;
Ok(VulkanInstance {
instance, instance,
device, device,
queues queues,
}) })
} }
@ -96,8 +98,7 @@ impl Vulkan {
} }
pub fn test() { pub fn test() {
let mut vulkan = VulkanInstance::init().unwrap();
let mut vulkan = Vulkan::init().unwrap();
let queue = vulkan.get_queue().unwrap(); let queue = vulkan.get_queue().unwrap();
let device = vulkan.get_device(); let device = vulkan.get_device();
@ -138,37 +139,44 @@ pub fn test() {
) )
.unwrap(); .unwrap();
let render_pass = Arc::new(vulkano::single_pass_renderpass!(device.clone(), let render_pass = Arc::new(
attachments: { vulkano::single_pass_renderpass!(device.clone(),
color: { attachments: {
load: Clear, color: {
store: Store, load: Clear,
format: Format::R8G8B8A8Unorm, store: Store,
samples: 1, format: Format::R8G8B8A8Unorm,
samples: 1,
}
},
pass: {
color: [color],
depth_stencil: {}
} }
}, )
pass: { .unwrap(),
color: [color], );
depth_stencil: {}
}
).unwrap());
let framebuffer = Arc::new(Framebuffer::start(render_pass.clone()) let framebuffer = Arc::new(
.add(image.clone()).unwrap() Framebuffer::start(render_pass.clone())
.build().unwrap()); .add(image.clone())
.unwrap()
.build()
.unwrap(),
);
let vs = vs::Shader::load(device.clone()).expect("failed to create shader module"); let vs = vs::Shader::load(device.clone()).expect("failed to create shader module");
let fs = fs::Shader::load(device.clone()).expect("failed to create shader module"); let fs = fs::Shader::load(device.clone()).expect("failed to create shader module");
let pipeline = Arc::new( let pipeline = Arc::new(
GraphicsPipeline::start() GraphicsPipeline::start()
.vertex_input_single_buffer::<Vertex>() .vertex_input_single_buffer::<Vertex>()
.vertex_shader(vs.main_entry_point(), ()) .vertex_shader(vs.main_entry_point(), ())
.viewports_dynamic_scissors_irrelevant(1) .viewports_dynamic_scissors_irrelevant(1)
.fragment_shader(fs.main_entry_point(), ()) .fragment_shader(fs.main_entry_point(), ())
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap()) .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
.build(device.clone()) .build(device.clone())
.unwrap(), .unwrap(),
); );
let dynamic_state = DynamicState { let dynamic_state = DynamicState {
@ -215,4 +223,4 @@ pub fn test() {
let buffer_content = buf.read().unwrap(); let buffer_content = buf.read().unwrap();
let image = ImageBuffer::<Rgba<u8>, _>::from_raw(1024, 1024, &buffer_content[..]).unwrap(); let image = ImageBuffer::<Rgba<u8>, _>::from_raw(1024, 1024, &buffer_content[..]).unwrap();
image.save("image.png").unwrap(); image.save("image.png").unwrap();
} }

5
engine_math/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "engine_math"
version = "0.0.1"

9
engine_math/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "engine_math"
version = "0.0.1"
authors = ["Florian RICHER <florian.richer@unova.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -1,5 +1,5 @@
mod tranform; mod tranform;
mod vec2; mod vec2;
pub use tranform::Transform;
pub use vec2::Vec2; pub use vec2::Vec2;
pub use tranform::Transform;

5
engine_utils/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "engine_utils"
version = "0.0.1"

9
engine_utils/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "engine_utils"
version = "0.0.1"
authors = ["Florian RICHER <florian.richer@unova.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

41
engine_utils/src/lib.rs Normal file
View file

@ -0,0 +1,41 @@
#[macro_export]
macro_rules! format_error {
($($args:tt)*) => {{
format!("[ERROR] {}", format_args!($($args)*))
}};
}
#[macro_export]
macro_rules! format_warning {
($($args:tt)*) => {{
format!("[WARN] {}", format_args!($($args)*))
}};
}
#[macro_export]
macro_rules! format_info {
($($args:tt)*) => {{
format!("[INFO] {}", format_args!($($args)*))
}};
}
#[macro_export]
macro_rules! print_error {
($($args:tt)*) => {{
println!("[ERROR] {}", format_args!($($args)*))
}};
}
#[macro_export]
macro_rules! print_warning {
($($args:tt)*) => {{
println!("[WARN] {}", format_args!($($args)*))
}};
}
#[macro_export]
macro_rules! print_info {
($($args:tt)*) => {{
println!("[INFO] {}", format_args!($($args)*))
}};
}

View file

@ -1,5 +1,5 @@
use crate::math::Transform; use engine_math::Transform;
pub trait Entity { pub trait Entity {
fn get_transform(&mut self) -> &mut Transform; fn get_transform(&mut self) -> &mut Transform;
} }

View file

@ -1,5 +1,5 @@
use crate::math::Transform;
use crate::entities::Entity; use crate::entities::Entity;
use engine_math::Transform;
#[derive(Debug)] #[derive(Debug)]
pub struct Player { pub struct Player {
@ -9,7 +9,7 @@ pub struct Player {
impl Player { impl Player {
pub fn new() -> Player { pub fn new() -> Player {
Player { Player {
transform: Transform::new() transform: Transform::new(),
} }
} }
} }
@ -18,4 +18,4 @@ impl Entity for Player {
fn get_transform(&mut self) -> &mut Transform { fn get_transform(&mut self) -> &mut Transform {
&mut self.transform &mut self.transform
} }
} }

View file

@ -1,16 +1,7 @@
mod render;
mod entities; mod entities;
mod math;
use entities::Entity; use engine_core::test;
use entities::Player;
use render::debug;
use render::vulkan::test;
fn main() { fn main() {
// let mut player = Player::new();
// debug(&player);
// player.get_transform().translate(10.0, 20.0);
// debug(&player);
test(); test();
} }

View file

@ -1,3 +0,0 @@
pub fn debug(data: &impl std::fmt::Debug) {
println!("{:#?}", data);
}

View file

@ -1,4 +0,0 @@
mod display;
pub mod vulkan;
pub use display::debug;