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

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

@ -0,0 +1,226 @@
mod physical_device;
mod vertex;
use physical_device::get_physical_device;
pub use vertex::Vertex;
use image::ImageBuffer;
use image::Rgba;
use std::sync::Arc;
use vulkano::buffer::BufferUsage;
use vulkano::buffer::CpuAccessibleBuffer;
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::command_buffer::CommandBuffer;
use vulkano::command_buffer::DynamicState;
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::InstanceExtensions;
use vulkano::instance::{Instance, QueueFamily};
use vulkano::pipeline::viewport::Viewport;
use vulkano::pipeline::GraphicsPipeline;
use vulkano::sync::GpuFuture;
mod vs {
vulkano_shaders::shader! {
ty: "vertex",
src: "
#version 450
layout(location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}"
}
}
mod fs {
vulkano_shaders::shader! {
ty: "fragment",
src: "
#version 450
layout(location = 0) out vec4 f_color;
void main() {
f_color = vec4(1.0, 0.0, 0.0, 1.0);
}"
}
}
pub struct VulkanInstance {
instance: Arc<Instance>,
device: Arc<Device>,
queues: QueuesIter,
}
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 physical_device = get_physical_device(&instance)
.ok_or(engine_utils::format_error!("No physical device found"))?;
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,
})
}
fn get_queue(&mut self) -> Option<Arc<Queue>> {
self.queues.next()
}
fn get_device(&self) -> Arc<Device> {
self.device.clone()
}
}
pub fn test() {
let mut vulkan = VulkanInstance::init().unwrap();
let queue = vulkan.get_queue().unwrap();
let device = vulkan.get_device();
let image = StorageImage::new(
device.clone(),
Dimensions::Dim2d {
width: 1024,
height: 1024,
},
Format::R8G8B8A8Unorm,
Some(queue.family()),
)
.unwrap();
let buf = CpuAccessibleBuffer::from_iter(
device.clone(),
BufferUsage::all(),
false,
(0..1024 * 1024 * 4).map(|_| 0u8),
)
.expect("failed to create buffer");
let vertex1 = Vertex {
position: [-0.5, -0.5],
};
let vertex2 = Vertex {
position: [0.0, 0.5],
};
let vertex3 = Vertex {
position: [0.5, -0.25],
};
let vertex_buffer = CpuAccessibleBuffer::from_iter(
device.clone(),
BufferUsage::all(),
false,
vec![vertex1, vertex2, vertex3].into_iter(),
)
.unwrap();
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: {}
}
)
.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(),
);
let dynamic_state = DynamicState {
viewports: Some(vec![Viewport {
origin: [0.0, 0.0],
dimensions: [1024.0, 1024.0],
depth_range: 0.0..1.0,
}]),
..DynamicState::none()
};
let mut builder =
AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family()).unwrap();
builder
.begin_render_pass(
framebuffer.clone(),
false,
vec![[0.0, 0.0, 1.0, 1.0].into()],
)
.unwrap()
.draw(
pipeline.clone(),
&dynamic_state,
vertex_buffer.clone(),
(),
(),
)
.unwrap()
.end_render_pass()
.unwrap()
.copy_image_to_buffer(image.clone(), buf.clone())
.unwrap();
let command_buffer = builder.build().unwrap();
let finished = command_buffer.execute(queue.clone()).unwrap();
finished
.then_signal_fence_and_flush()
.unwrap()
.wait(None)
.unwrap();
let buffer_content = buf.read().unwrap();
let image = ImageBuffer::<Rgba<u8>, _>::from_raw(1024, 1024, &buffer_content[..]).unwrap();
image.save("image.png").unwrap();
}

View file

@ -0,0 +1,40 @@
use vulkano::instance::{Instance, PhysicalDevice, PhysicalDeviceType};
use std::sync::Arc;
pub fn get_physical_device(instance: &Arc<Instance>) -> Option<PhysicalDevice> {
#[cfg(debug_assertions)]
{
println!("###################################### PRINT PHYSICAL DEVICES ######################################");
for physical_device in PhysicalDevice::enumerate(instance) {
println!(
"Available device: {} (type: {:?})\
\n\t\t\t\t\tDriver version: {}\
\n\t\t\t\t\tAPI Version: {:?}\
\n\t\t\t\t\tVendor ID: {}\
\n\t\t\t\t\tDevice ID: {}",
physical_device.name(),
physical_device.ty(),
physical_device.driver_version(),
physical_device.api_version(),
physical_device.pci_vendor_id(),
physical_device.pci_device_id()
);
}
}
let physical_device = PhysicalDevice::enumerate(instance)
.find(|physical_device| physical_device.ty() == PhysicalDeviceType::DiscreteGpu)
.or_else(|| PhysicalDevice::enumerate(instance).next());
#[cfg(debug_assertions)]
{
match physical_device {
Some(physical_device) => println!(
"--- Using device: {} (type: {:?})",
physical_device.name(),
physical_device.ty()
),
None => println!("--- Error: No device found")
}
println!("####################################### END PHYSICAL DEVICES #######################################");
}
Some(physical_device?)
}

View file

@ -0,0 +1,6 @@
#[derive(Default, Copy, Clone)]
pub struct Vertex {
pub position: [f32; 2],
}
vulkano::impl_vertex!(Vertex, position);