-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtriangle.rs
More file actions
167 lines (142 loc) · 4.5 KB
/
triangle.rs
File metadata and controls
167 lines (142 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::{path::Path, time::Instant};
use glam::Vec4;
use lazy_vulkan::{Context, LazyVulkan, StateFamily, SubRenderer};
use winit::{
application::ApplicationHandler,
dpi::PhysicalSize,
event::{ElementState, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{KeyCode, PhysicalKey},
window::WindowAttributes,
};
pub struct TriangleRenderer {
pipeline: lazy_vulkan::Pipeline,
pub colour: glam::Vec4,
}
impl TriangleRenderer {
pub fn new(renderer: &lazy_vulkan::Renderer<RenderStateFamily>) -> Self {
let pipeline = renderer.create_pipeline::<Registers>(
Path::new("examples/shaders/triangle.vert.spv"),
Path::new("examples/shaders/colour.frag.spv"),
);
Self {
pipeline,
colour: glam::Vec4::ONE,
}
}
}
impl<'a> SubRenderer<'a> for TriangleRenderer {
type State = RenderState;
fn draw_opaque(&mut self, state: &Self::State, context: &Context) {
self.begin_rendering(context, &self.pipeline);
self.colour = psychedelic_vec4(state.t);
unsafe {
self.pipeline.update_registers(&Registers {
colour: self.colour,
});
context
.device
.cmd_draw(context.draw_command_buffer, 3, 1, 0, 0)
}
}
fn label(&self) -> &'static str {
"Triangle Renderer"
}
}
pub struct RenderState {
t: f32,
}
pub struct RenderStateFamily;
impl StateFamily for RenderStateFamily {
type For<'s> = RenderState;
}
#[repr(C)]
#[derive(Copy, Clone)]
struct Registers {
colour: glam::Vec4,
}
unsafe impl bytemuck::Zeroable for Registers {}
unsafe impl bytemuck::Pod for Registers {}
#[derive(Default)]
struct App {
state: Option<State>,
}
struct State {
lazy_vulkan: LazyVulkan<RenderStateFamily>,
window: winit::window::Window,
last_render_time: Instant,
t: f32,
}
// ------------
// BOILERPLATE
// ------------
impl<'a> ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
let window = event_loop
.create_window(
WindowAttributes::default()
.with_title("Triangle Example")
.with_inner_size(PhysicalSize::new(1024, 768)),
)
.unwrap();
let mut lazy_vulkan = LazyVulkan::from_window(&window);
let sub_renderer = TriangleRenderer::new(&lazy_vulkan.renderer);
lazy_vulkan.add_sub_renderer(Box::new(sub_renderer));
self.state = Some(State {
lazy_vulkan,
window,
last_render_time: Instant::now(),
t: 0.,
});
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
_: winit::window::WindowId,
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Escape),
..
},
..
} => event_loop.exit(),
WindowEvent::Resized(size) => {
let state = self.state.as_mut().unwrap();
state.lazy_vulkan.resize(size);
}
WindowEvent::RedrawRequested => {
let state = self.state.as_mut().unwrap();
state.t += state.last_render_time.elapsed().as_secs_f32();
let lazy_vulkan = &mut state.lazy_vulkan;
lazy_vulkan.draw(&RenderState { t: state.t });
state.last_render_time = Instant::now();
}
_ => (),
}
}
fn about_to_wait(&mut self, _: &winit::event_loop::ActiveEventLoop) {
let state = self.state.as_mut().unwrap();
state.window.request_redraw();
}
}
// from chatGPT
pub fn psychedelic_vec4(mut time: f32) -> Vec4 {
time *= 50.0;
let r = (time * 1.0 + 0.0).sin() * 0.5 + 0.5;
let g = (time * 1.3 + std::f32::consts::FRAC_PI_2).sin() * 0.5 + 0.5;
let b = (time * 1.6 + std::f32::consts::PI).sin() * 0.5 + 0.5;
let a = (time * 0.4).cos() * 0.5 + 0.5;
Vec4::new(r, g, b, a)
}
pub fn main() {
env_logger::init();
let event_loop = EventLoop::builder().build().unwrap();
event_loop.set_control_flow(ControlFlow::Poll);
event_loop.run_app(&mut App::default()).unwrap();
}