[oden] Oh boy here we go

This commit is contained in:
John Doty 2023-06-21 21:57:32 -07:00
parent 8218b88820
commit 14f9eb655f
4 changed files with 206 additions and 31 deletions

View file

@ -8,8 +8,7 @@ use winit::{
};
mod script;
// use script::ScriptContext;
use script::graphics::{ClearCommand, GraphicsCommand, PrintCommand};
mod texture;
#[repr(C)]
@ -298,7 +297,7 @@ impl State {
fn update(&mut self) {}
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
fn render(&mut self, commands: Vec<GraphicsCommand>) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?;
let view = output
.texture
@ -309,37 +308,95 @@ impl State {
label: Some("Render Encoder"),
});
{
// BEGIN GARBAGE
let r: f64 = (self.mouse_x / f64::from(self.size.width)).clamp(0.0, 1.0) * 0.1;
let g: f64 = (self.mouse_y / f64::from(self.size.height)).clamp(0.0, 1.0) * 0.2;
// END GARBAGE
// Group the commands into passes.
struct Pass {
color: Option<[f64; 4]>,
commands: Vec<GraphicsCommand>,
}
let mut passes = Vec::new();
for command in commands {
match command {
GraphicsCommand::Clear(ClearCommand { color }) => passes.push(Pass {
color: Some(color),
commands: Vec::new(),
}),
GraphicsCommand::EndFrame => (),
other => match passes.last_mut() {
Some(pass) => pass.commands.push(other),
None => passes.push(Pass {
color: None,
commands: vec![other],
}),
},
}
}
for pass in passes {
let mut 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, //0.1,
g, //0.2,
b: 0.3,
a: 1.0,
}),
load: if let Some([r, g, b, a]) = pass.color {
wgpu::LoadOp::Clear(wgpu::Color {
r, //0.1,
g, //0.2,
b,
a,
})
} else {
wgpu::LoadOp::Load
},
store: true,
},
})],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
for command in pass.commands {
match command {
GraphicsCommand::Print(PrintCommand { text }) => {
println!("{}", text);
}
GraphicsCommand::Clear(_) => (), // Already handled
GraphicsCommand::EndFrame => (), // Should never appear
}
}
}
// {
// // BEGIN GARBAGE
// let r: f64 = (self.mouse_x / f64::from(self.size.width)).clamp(0.0, 1.0) * 0.1;
// let g: f64 = (self.mouse_y / f64::from(self.size.height)).clamp(0.0, 1.0) * 0.2;
// // END GARBAGE
// let mut 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, //0.1,
// g, //0.2,
// b: 0.3,
// a: 1.0,
// }),
// store: true,
// },
// })],
// depth_stencil_attachment: None,
// });
// render_pass.set_pipeline(&self.render_pipeline);
// render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
// render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
// render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
// render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
// }
// Submit will accept anything that implements IntoIter
self.queue.submit(std::iter::once(encoder.finish()));
output.present();
@ -403,8 +460,7 @@ pub async fn run() {
context.update();
state.update();
context.render();
match state.render() {
match state.render(context.render()) {
Ok(_) => {}
// Reconfigure the surface if lost
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),