[TUTO] Finish learn gpu
This commit is contained in:
parent
9fff11fb19
commit
79b61ea333
8 changed files with 554 additions and 342 deletions
651
Cargo.lock
generated
651
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -2,10 +2,11 @@
|
|||
name = "tuto1"
|
||||
version = "0.1.0"
|
||||
authors = ["Florian RICHER <florian.richer@unova.fr>"]
|
||||
edition = "2018"
|
||||
resolver = "2"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
engine_core = { path = "engine_core" }
|
||||
engine_core = { path = "engine_core" }
|
||||
simplelog = "0.12.0"
|
||||
pollster = "0.2"
|
|
@ -9,4 +9,5 @@
|
|||
|
||||
## USEFULL LINK
|
||||
|
||||
- https://arewegameyet.rs/
|
||||
- https://arewegameyet.rs/
|
||||
- https://sotrh.github.io/learn-wgpu/beginner/tutorial2-surface/#render
|
|
@ -2,13 +2,13 @@
|
|||
name = "engine_core"
|
||||
version = "0.0.1"
|
||||
authors = ["Florian RICHER <florian.richer@unova.fr>"]
|
||||
edition = "2018"
|
||||
resolver = "2"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
image = "0.23"
|
||||
winit = "0.25"
|
||||
log = "0.4.17"
|
||||
image = "0.24.2"
|
||||
winit = "0.26.1"
|
||||
cgmath = "0.18"
|
||||
wgpu = "0.10.1"
|
||||
wgpu = "0.12.0"
|
|
@ -1,7 +1,9 @@
|
|||
mod state;
|
||||
|
||||
use winit::{
|
||||
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::{Window, WindowBuilder},
|
||||
window::{WindowBuilder},
|
||||
};
|
||||
|
||||
pub struct Engine {
|
||||
|
@ -15,28 +17,57 @@ impl Engine {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run(self) {
|
||||
pub async fn run(self) {
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new()
|
||||
.with_title(self.title)
|
||||
.build(&event_loop).unwrap();
|
||||
|
||||
let mut state = state::State::new(&window).await;
|
||||
|
||||
event_loop
|
||||
.run(move |event: Event<()>, _, control_flow: &mut ControlFlow | match event {
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
} if window_id == window.id() => match event {
|
||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::KeyboardInput { input, .. } => match input {
|
||||
KeyboardInput {
|
||||
state: ElementState::Pressed,
|
||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
} if window_id == window.id() => if !state.input(&event) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::KeyboardInput { input, .. } => match input {
|
||||
KeyboardInput {
|
||||
state: ElementState::Pressed,
|
||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
_ => {}
|
||||
},
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
state.resize(*physical_size);
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
// new_inner_size is &&mut so we have to dereference it twice
|
||||
state.resize(**new_inner_size);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||
state.update();
|
||||
match state.render() {
|
||||
Ok(_) => {}
|
||||
// Reconfigure the surface if lost
|
||||
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
||||
// The system is out of memory, we should probably quit
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||||
Err(e) => eprintln!("{:?}", e),
|
||||
}
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
// RedrawRequested will only trigger once, unless we manually
|
||||
// request it.
|
||||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
|
|
121
engine_core/src/state.rs
Normal file
121
engine_core/src/state.rs
Normal file
|
@ -0,0 +1,121 @@
|
|||
use winit::{window::Window, event::WindowEvent};
|
||||
|
||||
pub struct State {
|
||||
pub surface: wgpu::Surface,
|
||||
pub device: wgpu::Device,
|
||||
pub queue: wgpu::Queue,
|
||||
pub config: wgpu::SurfaceConfiguration,
|
||||
pub size: winit::dpi::PhysicalSize<u32>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
// Creating some of the wgpu types requires async code
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
let size = window.inner_size();
|
||||
|
||||
// The instance is a handle to our GPU
|
||||
// Backends::all => Vulkan + Metal + DX12 + Browser WebGPU
|
||||
let instance = wgpu::Instance::new(wgpu::Backends::all());
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
let adapter = instance.request_adapter(
|
||||
&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::default(),
|
||||
compatible_surface: Some(&surface),
|
||||
force_fallback_adapter: false,
|
||||
},
|
||||
).await.unwrap();
|
||||
|
||||
// let adapter = instance
|
||||
// .enumerate_adapters(wgpu::Backends::all())
|
||||
// .filter(|adapter| {
|
||||
// // Check if this adapter supports our surface
|
||||
// surface.get_preferred_format(&adapter).is_some()
|
||||
// })
|
||||
// .next()
|
||||
// .unwrap();
|
||||
|
||||
|
||||
let (device, queue) = adapter.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
features: wgpu::Features::empty(),
|
||||
// WebGL doesn't support all of wgpu's features, so if
|
||||
// we're building for the web we'll have to disable some.
|
||||
limits: if cfg!(target_arch = "wasm32") {
|
||||
wgpu::Limits::downlevel_webgl2_defaults()
|
||||
} else {
|
||||
wgpu::Limits::default()
|
||||
},
|
||||
label: None,
|
||||
},
|
||||
None, // Trace path
|
||||
).await.unwrap();
|
||||
|
||||
let config = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: surface.get_preferred_format(&adapter).unwrap(),
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: wgpu::PresentMode::Fifo,
|
||||
};
|
||||
surface.configure(&device, &config);
|
||||
|
||||
Self {
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
config,
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||
if new_size.width > 0 && new_size.height > 0 {
|
||||
self.size = new_size;
|
||||
self.config.width = new_size.width;
|
||||
self.config.height = new_size.height;
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
|
||||
}
|
||||
|
||||
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||
let output = self.surface.get_current_texture()?;
|
||||
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Render Encoder"),
|
||||
});
|
||||
|
||||
{
|
||||
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
},
|
||||
}],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
self.queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
#[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,4 +1,10 @@
|
|||
use simplelog::{TermLogger, LevelFilter, Config, TerminalMode, ColorChoice};
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = TermLogger::init(LevelFilter::Warn, Config::default(), TerminalMode::Mixed, ColorChoice::Auto) {
|
||||
println!("Failed to start logger : {}", err);
|
||||
}
|
||||
|
||||
let engine = engine_core::Engine::new("Test 123");
|
||||
engine.run();
|
||||
pollster::block_on(engine.run());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue