diff --git a/src/context.rs b/src/context.rs index 76aeaa3..df8d4c1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -8,6 +8,9 @@ use winit::{ event_loop::{EventLoop, EventLoopWindowTarget}, window::{Window, WindowBuilder}, }; +pub trait Pass { + fn execute(&self, encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView); +} #[derive(Error, Debug)] pub enum ContextError { @@ -23,6 +26,8 @@ pub enum ContextError { EventLoop(#[from] EventLoopError), #[error("Window creation failed: {0}")] Os(#[from] OsError), + #[error("Render failed: {0}")] + Render(#[from] wgpu::SurfaceError), } pub struct Context<'window> { @@ -138,6 +143,28 @@ impl<'window> Context<'window> { handled } + + pub fn render(&self, passes: &[Box]) -> Result<(), ContextError> { + let frame = self.surface.get_current_texture()?; + let view = frame + .texture + .create_view(&wgpu::TextureViewDescriptor::default()); + + let mut encoder = self + .device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Base Render Encoder"), + }); + + for pass in passes.iter() { + pass.execute(&mut encoder, &view); + } + + self.queue.submit(Some(encoder.finish())); + frame.present(); + + Ok(()) + } } pub struct ContextBuilder { diff --git a/src/lib.rs b/src/lib.rs index ed2438e..e64a1df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ mod texture; pub use self::{ bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, buffer::{BufferExt, BulkBufferBuilder}, - context::{Context, ContextBuilder}, + context::{Context, ContextBuilder, Pass}, pipeline::{ComputePipelineBuilder, RenderPipelineBuilder}, texture::{Texture, TextureBuilder}, };