- voltex_math: Vec3 with arithmetic ops, dot, cross, length, normalize - voltex_platform: VoltexWindow (winit wrapper), InputState (keyboard/mouse), GameTimer (fixed timestep + variable render loop) - voltex_renderer: GpuContext (wgpu init), Vertex + buffer layout, WGSL shader, render pipeline - triangle example: colored triangle with ESC to exit All 13 tests passing. Window renders RGB triangle on dark background. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
84 lines
2.6 KiB
Rust
84 lines
2.6 KiB
Rust
use std::sync::Arc;
|
|
use winit::window::Window;
|
|
|
|
pub struct GpuContext {
|
|
pub surface: wgpu::Surface<'static>,
|
|
pub device: wgpu::Device,
|
|
pub queue: wgpu::Queue,
|
|
pub config: wgpu::SurfaceConfiguration,
|
|
pub surface_format: wgpu::TextureFormat,
|
|
}
|
|
|
|
impl GpuContext {
|
|
pub fn new(window: Arc<Window>) -> Self {
|
|
pollster::block_on(Self::new_async(window))
|
|
}
|
|
|
|
async fn new_async(window: Arc<Window>) -> Self {
|
|
let size = window.inner_size();
|
|
|
|
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
|
|
backends: wgpu::Backends::PRIMARY,
|
|
..Default::default()
|
|
});
|
|
|
|
let surface = instance.create_surface(window).expect("Failed to create surface");
|
|
|
|
let adapter = instance
|
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
|
power_preference: wgpu::PowerPreference::HighPerformance,
|
|
compatible_surface: Some(&surface),
|
|
force_fallback_adapter: false,
|
|
})
|
|
.await
|
|
.expect("Failed to find a suitable GPU adapter");
|
|
|
|
let (device, queue) = adapter
|
|
.request_device(&wgpu::DeviceDescriptor {
|
|
label: Some("Voltex Device"),
|
|
required_features: wgpu::Features::empty(),
|
|
required_limits: wgpu::Limits::default(),
|
|
memory_hints: Default::default(),
|
|
..Default::default()
|
|
})
|
|
.await
|
|
.expect("Failed to create device");
|
|
|
|
let surface_caps = surface.get_capabilities(&adapter);
|
|
let surface_format = surface_caps
|
|
.formats
|
|
.iter()
|
|
.find(|f| f.is_srgb())
|
|
.copied()
|
|
.unwrap_or(surface_caps.formats[0]);
|
|
|
|
let config = wgpu::SurfaceConfiguration {
|
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
format: surface_format,
|
|
width: size.width.max(1),
|
|
height: size.height.max(1),
|
|
present_mode: surface_caps.present_modes[0],
|
|
alpha_mode: surface_caps.alpha_modes[0],
|
|
view_formats: vec![],
|
|
desired_maximum_frame_latency: 2,
|
|
};
|
|
surface.configure(&device, &config);
|
|
|
|
Self {
|
|
surface,
|
|
device,
|
|
queue,
|
|
config,
|
|
surface_format,
|
|
}
|
|
}
|
|
|
|
pub fn resize(&mut self, width: u32, height: u32) {
|
|
if width > 0 && height > 0 {
|
|
self.config.width = width;
|
|
self.config.height = height;
|
|
self.surface.configure(&self.device, &self.config);
|
|
}
|
|
}
|
|
}
|