Last active
December 10, 2021 11:04
-
-
Save zesterer/db9e32b828c97719653c3a0da0430800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use graphael::*; | |
use winit::{ | |
event::Event, | |
window::WindowBuilder, | |
event_loop::{ControlFlow, EventLoop}, | |
}; | |
#[derive(Copy, Clone, Pod, Zeroable)] | |
#[repr(C)] | |
pub struct Globals { | |
vp: [[f32; 4]; 4], | |
} | |
#[derive(Copy, Clone, Pod, Zeroable)] | |
#[repr(C)] | |
pub struct TriVertex { | |
pos: [f32; 2], | |
uv: [f32; 2], | |
} | |
#[derive(Copy, Clone, Pod, Zeroable)] | |
#[repr(C)] | |
pub struct TriLocals { | |
m: [[f32; 4]; 4], | |
} | |
#[derive(BindingGroup)] | |
pub struct TriBindings { | |
#[binding(0)] locals: Uniform<TriLocals>, | |
#[binding(1)] albedo: Texture<[Norm<u8>; 3]>, | |
#[binding(2)] normal: Texture<[Norm<i8>; 2]>, | |
#[binding(3)] ao: Texture<f32>, | |
} | |
pub struct Tri; | |
impl Pipeline for Tri { | |
type Vertex = TriVertex; | |
type BindingGroups = ( | |
Globals, | |
TriBindings, | |
); | |
type Color = Forward::Color; | |
type Depth = Forward::Depth; | |
} | |
struct Forward; | |
impl Pass for Forward { | |
type Color = [f32; 4]; | |
type Depth = f32; | |
} | |
#[derive(BindingGroup)] | |
pub struct PostBindings { | |
#[binding(0)] hdr_color: Texture<[f32; 4]>, | |
#[binding(1)] noise: Texture<Norm<u8>>, | |
} | |
pub struct Post; | |
impl Pipeline for Post { | |
type Vertex = FullscreenVertex; | |
type BindingGroups = ( | |
Globals, | |
PostBindings, | |
); | |
type Color = Post::Color; | |
type Depth = Post::Depth; | |
} | |
impl Pass for Post { | |
type Color = [Norm<u8>; 4]; | |
type Depth = (); | |
} | |
fn main() { | |
let event_loop = EventLoop::new(); | |
let window = WindowBuilder::new().build(&event_loop).unwrap(); | |
let mut gfx = Graphics::build() | |
.with_backends(Backends::all()) | |
.finish(&window); | |
let globals = Uniform::new(Globals { | |
vp: [[0.0; 4]; 4], | |
}); | |
let mut hdr_color = FrameBuffer::<[f32; 4]>::new(); | |
let depth = DepthBuffer::<f32>::new(); | |
let tri_model = Model::from(Mesh::from(&[ | |
TriVertex { pos: [-1.0, -1.0], uv: [0.0, 0.0] }, | |
TriVertex { pos: [0.0, 1.0], uv: [0.5, 1.0] }, | |
TriVertex { pos: [1.0, -1.0], uv: [1.0, 0.0] }, | |
])); | |
let tri_binding_group = Bindings::new(TriBindings { | |
locals: Uniform::new(TriLocals { | |
m: [[0.0; 4]; 4], | |
}), | |
albedo: Texture::from(Image::load("assets/tri_albedo.png")), | |
normal: Texture::from(Image::load("assets/tri_normal.png")), | |
ao: Texture::from(Image::load("assets/tri_ao.png")), | |
}); | |
let tri = Pipeline::<Tri>::new(Shader::load("assets/tri.wgsl")); | |
let post_binding_group = Bindings::new(Post { | |
hdr_color: hdr_color.texture(), | |
noise: Texture::from(Image::load("assets/noise.png")), | |
}); | |
let post = Pipeline::<Post>::new(Shader::load("assets/post.wgsl")); | |
event_loop.run(move |event, _, cf| match event { | |
Event::RedrawRequested(_) => { | |
// Begin a new frame | |
gfx.frame(|gfx, mut surface| { | |
// Clear buffers before proceeding | |
hdr_color.clear([0.5, 0.75, 1.0, 1.0]); | |
depth.clear(0.0); | |
// Forward pass | |
gfx.pass::<Forward>(&mut hdr_color, &mut depth).run(|fwd| { | |
fwd.render(&tri, (&globals, &tri_binding_group), &tri_model); | |
}); | |
// Post-processing pass | |
gfx.pass::<Post>(&mut surface, &mut ()).run(|post| { | |
post.render_fullscreen(&post, (&globals, &post_binding_group)); | |
}); | |
}); | |
}, | |
// TODO: Handle other events | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment