1 unstable release
new 0.1.0 | Jan 14, 2025 |
---|
#464 in Graphics APIs
65 downloads per month
19KB
78 lines
WGPU Context Creator
I created this small crate in order to make my life easier when I wanted to created projects using wgpu. It's not very usable for now, since it deserves only my purposes but I plan to add possibility of configuration through toml files and a structure later. To see a very basic example, go to the example directory.
lib.rs
:
Help to create wgpu context using winit.
Example :
use winit::window::Window;
use wgpu_context::*;
struct TestApp {
context : Option<Context>,
}
impl TestApp {
fn new() -> Self {
Self {
context : None,
}
}
}
impl winit::application::ApplicationHandler for TestApp {
fn resumed(&mut self, event_loop : &winit::event_loop::ActiveEventLoop) {
let window = event_loop.create_window(Window::default_attributes()
.with_title("Hello from wgpu context"))
.expect("Could not create window");
self.context = Some(Context::new(window, ContextCreateInfo::new()))
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
let window = self.context.as_ref().expect("Got an event before the context was created.")
.window();
if window.id() == window_id {
match event {
winit::event::WindowEvent::CloseRequested => {
event_loop.exit();
}
winit::event::WindowEvent::Resized(_physical_size) => {
//TODO self.context.resize(physical_size);
}
winit::event::WindowEvent::RedrawRequested => {
//TODO : some draw stuff here
let context = self.context.as_ref().unwrap();
let output = context.surface.get_current_texture().unwrap();
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = context.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: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 1.0,
g: 0.2,
b: 0.3,
a: 1.0,
}),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
});
}
context.queue.submit(std::iter::once(encoder.finish()));
output.present();
}
_ => {}
}
}
}
}
fn main() {
let mut app = TestApp::new();
let event_loop = Context::event_loop();
event_loop.run_app(&mut app).unwrap();
}
Dependencies
~6–41MB
~648K SLoC