-
Notifications
You must be signed in to change notification settings - Fork 9
/
render_texture.rs
175 lines (145 loc) · 5.22 KB
/
render_texture.rs
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
168
169
170
171
172
173
174
175
extern crate crayon;
use crayon::impl_vertex;
use crayon::prelude::*;
impl_vertex! {
Vertex {
position => [Position; Float; 2; false],
}
}
struct Pass {
surface: SurfaceHandle,
shader: ShaderHandle,
mesh: MeshHandle,
}
struct Window {
pass: Pass,
post_effect: Pass,
texture: RenderTextureHandle,
batch: CommandBuffer,
time: f32,
}
impl Window {
pub fn build() -> CrResult<Self> {
let attributes = AttributeLayoutBuilder::new()
.with(Attribute::Position, 2)
.finish();
//
let (pass, rendered_texture) = {
let verts: [Vertex; 3] = [
Vertex::new([0.0, 0.5]),
Vertex::new([0.5, -0.5]),
Vertex::new([-0.5, -0.5]),
];
let idxes: [u16; 3] = [0, 1, 2];
// Create vertex buffer object.
let mut params = MeshParams::default();
params.num_verts = 3;
params.num_idxes = 3;
params.layout = Vertex::layout();
let data = MeshData {
vptr: Vertex::encode(&verts[..]).into(),
iptr: IndexFormat::encode(&idxes).into(),
};
let mesh = video::create_mesh(params, Some(data))?;
// Create render texture for post effect.
let mut params = RenderTextureParams::default();
params.format = RenderTextureFormat::RGBA8;
params.dimensions = (568, 320).into();
let rendered_texture = video::create_render_texture(params)?;
// Create the surface state for pass 1.
let mut params = SurfaceParams::default();
params.set_attachments(&[rendered_texture], None)?;
params.set_clear(Color::gray(), None, None);
let surface = video::create_surface(params)?;
// Create shader state.
let mut params = ShaderParams::default();
params.attributes = attributes;
let vs = include_str!("shaders/render_target_p1.vs").to_owned();
let fs = include_str!("shaders/render_target_p1.fs").to_owned();
let shader = video::create_shader(params, vs, fs)?;
(
Pass {
surface,
shader,
mesh,
},
rendered_texture,
)
};
let post_effect = {
let verts: [Vertex; 4] = [
Vertex::new([-1.0, -1.0]),
Vertex::new([1.0, -1.0]),
Vertex::new([1.0, 1.0]),
Vertex::new([-1.0, 1.0]),
];
let idxes: [u16; 6] = [0, 1, 2, 0, 2, 3];
let mut params = MeshParams::default();
params.num_verts = 4;
params.num_idxes = 6;
params.layout = Vertex::layout();
let data = MeshData {
vptr: Vertex::encode(&verts[..]).into(),
iptr: IndexFormat::encode(&idxes).into(),
};
let mesh = video::create_mesh(params, Some(data))?;
let params = SurfaceParams::default();
let surface = video::create_surface(params)?;
let uniforms = UniformVariableLayout::build()
.with("renderedTexture", UniformVariableType::RenderTexture)
.with("time", UniformVariableType::F32)
.finish();
let mut params = ShaderParams::default();
params.attributes = attributes;
params.uniforms = uniforms;
let vs = include_str!("shaders/render_target_p2.vs").to_owned();
let fs = include_str!("shaders/render_target_p2.fs").to_owned();
let shader = video::create_shader(params, vs, fs)?;
Pass {
surface,
shader,
mesh,
}
};
Ok(Window {
pass,
post_effect,
texture: rendered_texture,
batch: CommandBuffer::new(),
time: 0.0,
})
}
}
impl Drop for Window {
fn drop(&mut self) {
video::delete_render_texture(self.texture);
video::delete_mesh(self.pass.mesh);
video::delete_shader(self.pass.shader);
video::delete_surface(self.pass.surface);
video::delete_mesh(self.post_effect.mesh);
video::delete_shader(self.post_effect.shader);
video::delete_surface(self.post_effect.surface);
}
}
impl LifecycleListener for Window {
fn on_update(&mut self) -> CrResult<()> {
let surface = self.pass.surface;
let dc = Draw::new(self.pass.shader, self.pass.mesh);
self.batch.draw(dc);
self.batch.submit(surface)?;
let surface = self.post_effect.surface;
let mut dc = Draw::new(self.post_effect.shader, self.post_effect.mesh);
dc.set_uniform_variable("renderedTexture", self.texture);
dc.set_uniform_variable("time", self.time);
self.batch.draw(dc);
self.batch.submit(surface)?;
self.time += 0.05;
Ok(())
}
}
main!({
let mut params = Params::default();
params.window.title = "CR: RenderTexture".into();
params.window.size = (568, 320).into();
crayon::application::setup(params, Window::build).unwrap();
});