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
*.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"
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]]

View file

@ -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

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 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
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 vec2;
pub use tranform::Transform;
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 {
fn get_transform(&mut self) -> &mut Transform;
}
}

View file

@ -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
}
}
}

View file

@ -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();
}

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;