Modularize + Refactoring code
This commit is contained in:
parent
813280350f
commit
00ab67b212
23 changed files with 2001 additions and 87 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
|||
/target
|
||||
target
|
||||
engine_core/target
|
||||
engine_math/target
|
||||
engine_utils/target
|
||||
.idea
|
||||
*.png
|
||||
|
|
28
Cargo.lock
generated
28
Cargo.lock
generated
|
@ -506,6 +506,26 @@ version = "1.6.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
|
@ -1441,11 +1461,9 @@ checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc"
|
|||
name = "tuto1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"image",
|
||||
"vulkano",
|
||||
"vulkano-shaders",
|
||||
"vulkano-win",
|
||||
"winit 0.23.0",
|
||||
"engine_core",
|
||||
"engine_math",
|
||||
"engine_utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -7,8 +7,6 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
vulkano = "0.19"
|
||||
vulkano-shaders = "0.19"
|
||||
image = "0.23"
|
||||
vulkano-win = "0.19"
|
||||
winit = "0.23"
|
||||
engine_utils = { path = "engine_utils" }
|
||||
engine_math = { path = "engine_math" }
|
||||
engine_core = { path = "engine_core" }
|
||||
|
|
1812
engine_core/Cargo.lock
generated
Normal file
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
16
engine_core/Cargo.toml
Normal 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
3
engine_core/src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod render;
|
||||
|
||||
pub use render::test;
|
3
engine_core/src/render/mod.rs
Normal file
3
engine_core/src/render/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod vulkan;
|
||||
|
||||
pub use vulkan::test;
|
|
@ -1,8 +1,8 @@
|
|||
mod vertex;
|
||||
mod physical_device;
|
||||
mod vertex;
|
||||
|
||||
pub use vertex::Vertex;
|
||||
use physical_device::get_physical_device;
|
||||
pub use vertex::Vertex;
|
||||
|
||||
use image::ImageBuffer;
|
||||
use image::Rgba;
|
||||
|
@ -12,23 +12,22 @@ use vulkano::buffer::CpuAccessibleBuffer;
|
|||
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
||||
use vulkano::command_buffer::CommandBuffer;
|
||||
use vulkano::command_buffer::DynamicState;
|
||||
use vulkano::device::{Device, QueuesIter, Queue};
|
||||
use vulkano::device::DeviceExtensions;
|
||||
use vulkano::device::Features;
|
||||
use vulkano::device::{Device, Queue, QueuesIter};
|
||||
use vulkano::format::Format;
|
||||
use vulkano::framebuffer::Framebuffer;
|
||||
use vulkano::framebuffer::Subpass;
|
||||
use vulkano::image::Dimensions;
|
||||
use vulkano::image::StorageImage;
|
||||
use vulkano::instance::{Instance, PhysicalDeviceType, QueueFamily};
|
||||
use vulkano::instance::InstanceExtensions;
|
||||
use vulkano::instance::PhysicalDevice;
|
||||
use vulkano::instance::{Instance, QueueFamily};
|
||||
use vulkano::pipeline::viewport::Viewport;
|
||||
use vulkano::pipeline::GraphicsPipeline;
|
||||
use vulkano::sync::GpuFuture;
|
||||
|
||||
mod vs {
|
||||
vulkano_shaders::shader!{
|
||||
vulkano_shaders::shader! {
|
||||
ty: "vertex",
|
||||
src: "
|
||||
#version 450
|
||||
|
@ -42,7 +41,7 @@ mod vs {
|
|||
}
|
||||
|
||||
mod fs {
|
||||
vulkano_shaders::shader!{
|
||||
vulkano_shaders::shader! {
|
||||
ty: "fragment",
|
||||
src: "
|
||||
#version 450
|
||||
|
@ -55,34 +54,37 @@ mod fs {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Vulkan {
|
||||
pub struct VulkanInstance {
|
||||
instance: Arc<Instance>,
|
||||
device: Arc<Device>,
|
||||
queues: QueuesIter,
|
||||
}
|
||||
|
||||
impl Vulkan {
|
||||
fn init() -> Option<Vulkan> {
|
||||
let instance = match Instance::new(None, &InstanceExtensions::none(), None) {
|
||||
Ok(instance) => instance,
|
||||
Err(_) => return None
|
||||
};
|
||||
let physical_device = get_physical_device(&instance)?;
|
||||
let queue_family = physical_device.queue_families()
|
||||
.find(|&q| q.supports_graphics())?;
|
||||
impl VulkanInstance {
|
||||
fn init() -> Result<VulkanInstance, String> {
|
||||
let instance = Instance::new(None, &InstanceExtensions::none(), None)
|
||||
.map_err(|_| engine_utils::format_error!("Instance creation failed"))?;
|
||||
|
||||
let (device, queues) = {
|
||||
match Device::new(physical_device, &Features::none(), &DeviceExtensions::none(),
|
||||
[(queue_family, 0.5)].iter().cloned()) {
|
||||
Ok((device, queues)) => (device, queues),
|
||||
Err(_) => return None
|
||||
}
|
||||
};
|
||||
let physical_device = get_physical_device(&instance)
|
||||
.ok_or(engine_utils::format_error!("No physical device found"))?;
|
||||
|
||||
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,
|
||||
device,
|
||||
queues
|
||||
queues,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -96,8 +98,7 @@ impl Vulkan {
|
|||
}
|
||||
|
||||
pub fn test() {
|
||||
|
||||
let mut vulkan = Vulkan::init().unwrap();
|
||||
let mut vulkan = VulkanInstance::init().unwrap();
|
||||
|
||||
let queue = vulkan.get_queue().unwrap();
|
||||
let device = vulkan.get_device();
|
||||
|
@ -138,37 +139,44 @@ pub fn test() {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let render_pass = Arc::new(vulkano::single_pass_renderpass!(device.clone(),
|
||||
attachments: {
|
||||
color: {
|
||||
load: Clear,
|
||||
store: Store,
|
||||
format: Format::R8G8B8A8Unorm,
|
||||
samples: 1,
|
||||
let render_pass = Arc::new(
|
||||
vulkano::single_pass_renderpass!(device.clone(),
|
||||
attachments: {
|
||||
color: {
|
||||
load: Clear,
|
||||
store: Store,
|
||||
format: Format::R8G8B8A8Unorm,
|
||||
samples: 1,
|
||||
}
|
||||
},
|
||||
pass: {
|
||||
color: [color],
|
||||
depth_stencil: {}
|
||||
}
|
||||
},
|
||||
pass: {
|
||||
color: [color],
|
||||
depth_stencil: {}
|
||||
}
|
||||
).unwrap());
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let framebuffer = Arc::new(Framebuffer::start(render_pass.clone())
|
||||
.add(image.clone()).unwrap()
|
||||
.build().unwrap());
|
||||
let framebuffer = Arc::new(
|
||||
Framebuffer::start(render_pass.clone())
|
||||
.add(image.clone())
|
||||
.unwrap()
|
||||
.build()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
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 pipeline = Arc::new(
|
||||
GraphicsPipeline::start()
|
||||
.vertex_input_single_buffer::<Vertex>()
|
||||
.vertex_shader(vs.main_entry_point(), ())
|
||||
.viewports_dynamic_scissors_irrelevant(1)
|
||||
.fragment_shader(fs.main_entry_point(), ())
|
||||
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
|
||||
.build(device.clone())
|
||||
.unwrap(),
|
||||
GraphicsPipeline::start()
|
||||
.vertex_input_single_buffer::<Vertex>()
|
||||
.vertex_shader(vs.main_entry_point(), ())
|
||||
.viewports_dynamic_scissors_irrelevant(1)
|
||||
.fragment_shader(fs.main_entry_point(), ())
|
||||
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
|
||||
.build(device.clone())
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let dynamic_state = DynamicState {
|
||||
|
@ -215,4 +223,4 @@ pub fn test() {
|
|||
let buffer_content = buf.read().unwrap();
|
||||
let image = ImageBuffer::<Rgba<u8>, _>::from_raw(1024, 1024, &buffer_content[..]).unwrap();
|
||||
image.save("image.png").unwrap();
|
||||
}
|
||||
}
|
5
engine_math/Cargo.lock
generated
Normal file
5
engine_math/Cargo.lock
generated
Normal 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
9
engine_math/Cargo.toml
Normal 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]
|
|
@ -1,5 +1,5 @@
|
|||
mod tranform;
|
||||
mod vec2;
|
||||
|
||||
pub use tranform::Transform;
|
||||
pub use vec2::Vec2;
|
||||
pub use tranform::Transform;
|
5
engine_utils/Cargo.lock
generated
Normal file
5
engine_utils/Cargo.lock
generated
Normal 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
9
engine_utils/Cargo.toml
Normal 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
41
engine_utils/src/lib.rs
Normal 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)*))
|
||||
}};
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
use crate::math::Transform;
|
||||
use engine_math::Transform;
|
||||
|
||||
pub trait Entity {
|
||||
fn get_transform(&mut self) -> &mut Transform;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::math::Transform;
|
||||
use crate::entities::Entity;
|
||||
use engine_math::Transform;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
|
@ -9,7 +9,7 @@ pub struct Player {
|
|||
impl Player {
|
||||
pub fn new() -> Player {
|
||||
Player {
|
||||
transform: Transform::new()
|
||||
transform: Transform::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,4 +18,4 @@ impl Entity for Player {
|
|||
fn get_transform(&mut self) -> &mut Transform {
|
||||
&mut self.transform
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,16 +1,7 @@
|
|||
mod render;
|
||||
mod entities;
|
||||
mod math;
|
||||
|
||||
use entities::Entity;
|
||||
use entities::Player;
|
||||
use render::debug;
|
||||
use render::vulkan::test;
|
||||
use engine_core::test;
|
||||
|
||||
fn main() {
|
||||
// let mut player = Player::new();
|
||||
// debug(&player);
|
||||
// player.get_transform().translate(10.0, 20.0);
|
||||
// debug(&player);
|
||||
test();
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
pub fn debug(data: &impl std::fmt::Debug) {
|
||||
println!("{:#?}", data);
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
mod display;
|
||||
pub mod vulkan;
|
||||
|
||||
pub use display::debug;
|
Loading…
Reference in a new issue