Add basic render/compute pass system
This commit is contained in:
		
							parent
							
								
									ee5308ac23
								
							
						
					
					
						commit
						a0ed278d24
					
				|  | @ -8,6 +8,9 @@ use winit::{ | ||||||
|     event_loop::{EventLoop, EventLoopWindowTarget}, |     event_loop::{EventLoop, EventLoopWindowTarget}, | ||||||
|     window::{Window, WindowBuilder}, |     window::{Window, WindowBuilder}, | ||||||
| }; | }; | ||||||
|  | pub trait Pass { | ||||||
|  |     fn execute(&self, encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| #[derive(Error, Debug)] | #[derive(Error, Debug)] | ||||||
| pub enum ContextError { | pub enum ContextError { | ||||||
|  | @ -23,6 +26,8 @@ pub enum ContextError { | ||||||
|     EventLoop(#[from] EventLoopError), |     EventLoop(#[from] EventLoopError), | ||||||
|     #[error("Window creation failed: {0}")] |     #[error("Window creation failed: {0}")] | ||||||
|     Os(#[from] OsError), |     Os(#[from] OsError), | ||||||
|  |     #[error("Render failed: {0}")] | ||||||
|  |     Render(#[from] wgpu::SurfaceError), | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub struct Context<'window> { | pub struct Context<'window> { | ||||||
|  | @ -138,6 +143,28 @@ impl<'window> Context<'window> { | ||||||
| 
 | 
 | ||||||
|         handled |         handled | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     pub fn render(&self, passes: &[Box<dyn Pass>]) -> 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 { | pub struct ContextBuilder { | ||||||
|  |  | ||||||
|  | @ -7,7 +7,7 @@ mod texture; | ||||||
| pub use self::{ | pub use self::{ | ||||||
|     bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, |     bind_group::{BindGroupBuilder, BindGroupLayoutBuilder}, | ||||||
|     buffer::{BufferExt, BulkBufferBuilder}, |     buffer::{BufferExt, BulkBufferBuilder}, | ||||||
|     context::{Context, ContextBuilder}, |     context::{Context, ContextBuilder, Pass}, | ||||||
|     pipeline::{ComputePipelineBuilder, RenderPipelineBuilder}, |     pipeline::{ComputePipelineBuilder, RenderPipelineBuilder}, | ||||||
|     texture::{Texture, TextureBuilder}, |     texture::{Texture, TextureBuilder}, | ||||||
| }; | }; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue