a skeleton app

This commit is contained in:
numzero 2025-11-10 21:20:38 +03:00
commit 9054765bcc
4 changed files with 2613 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

2457
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "photon-light"
version = "0.1.0"
edition = "2024"
[profile.dev]
panic = 'abort'
[profile.dev.package."*"]
opt-level = 3
[profile.test.package."*"]
opt-level = 3
[dependencies]
bytemuck = "1.24.0"
glam = { version = "0.30.9", features = ["bytemuck"] }
pollster = "0.4.0"
rand = "0.9.2"
rand_distr = "0.5.1"
rand_pcg = "0.9.0"
wgpu = "27.0.1"
winit = "0.30.12"

132
src/main.rs Normal file
View File

@ -0,0 +1,132 @@
use std::{error::Error, sync::Arc};
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
window::Window,
};
const TITLE: &str = "WGPU example";
const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
struct MainWindow {
handle: Arc<Window>,
device: wgpu::Device,
queue: wgpu::Queue,
surface: wgpu::Surface<'static>,
surface_configured: bool,
}
impl MainWindow {
fn new(event_loop: &ActiveEventLoop) -> Self {
let handle = event_loop
.create_window(Window::default_attributes().with_title(TITLE))
.unwrap();
let handle = Arc::new(handle);
let (device, queue, surface) = pollster::block_on(init_gpu(Arc::clone(&handle))).unwrap();
queue.submit([]);
Self {
handle,
device,
queue,
surface,
surface_configured: false,
}
}
fn event(&mut self, event_loop: &ActiveEventLoop, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::Resized(physical_size) => {
self.surface.configure(
&self.device,
&wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_DST,
format: OUTPUT_FORMAT,
width: physical_size.width,
height: physical_size.height,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
desired_maximum_frame_latency: 2,
},
);
self.surface_configured = true;
}
WindowEvent::RedrawRequested => {
self.handle.request_redraw();
if !self.surface_configured {
return;
}
let output = self.surface.get_current_texture().unwrap();
output.present();
}
_ => {}
}
}
}
struct Application {
main_window: Option<MainWindow>,
}
impl Application {
fn new() -> Self {
Self { main_window: None }
}
}
impl ApplicationHandler for Application {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
self.main_window = Some(MainWindow::new(event_loop));
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
window_id: winit::window::WindowId,
event: WindowEvent,
) {
let window = self
.main_window
.as_mut()
.expect("window must exist to recieve events");
assert_eq!(window.handle.id(), window_id);
window.event(event_loop, event);
}
}
async fn init_gpu<'window>(
wnd: impl wgpu::WindowHandle + 'window,
) -> Result<(wgpu::Device, wgpu::Queue, wgpu::Surface<'window>), Box<dyn Error>> {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..Default::default()
});
let surface = instance.create_surface(wnd)?;
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();
let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor::default())
.await
.unwrap();
Ok((device, queue, surface))
}
fn main() {
let event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(ControlFlow::Wait);
let mut app = Application::new();
event_loop.run_app(&mut app).unwrap();
}