Add crates, start wgpu tutorial

This commit is contained in:
Jonathan Flueren 2023-01-12 18:05:10 +01:00
parent 86d9ac587d
commit d6dacba08e
4 changed files with 1892 additions and 2 deletions

1806
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,12 @@
[package]
name = "gpu-renderer"
version = "0.1.0"
edition = "2022"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
env_logger = "0.10.0"
log = "0.4.17"
wgpu = "0.14.2"
winit = "0.27.5"

76
src/lib.rs Normal file
View file

@ -0,0 +1,76 @@
use winit::{
event::*,
event_loop::{ControlFlow, EventLoop},
window::{WindowBuilder, Window},
};
struct State {
surface: wgpu::Surface,
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
size: winit::dpi::PhysicalSize<u32>,
}
impl State {
// Creating some of the wgpu types requires async code
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();
}
fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
todo!()
}
fn input(&mut self, event: &WindowEvent) -> bool {
todo!()
}
fn update(&mut self) {
todo!()
}
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
todo!()
}
}
pub fn run() {
env_logger::init();
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => *control_flow = ControlFlow::Exit,
_ => {}
},
_ => {}
});
}

View file

@ -1,3 +1,7 @@
// https://sotrh.github.io/learn-wgpu/beginner/tutorial1-window/#boring-i-know
use gpu_renderer::run;
fn main() {
println!("Hello, world!");
run();
}