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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
use std::collections::HashMap;
use glow::HasContext;
use crate::{context, mesh};
const COMMON_VERT: &'static str = include_str!("assets/shaders/common/vert.glsl");
const COMMON_FRAG: &'static str = include_str!("assets/shaders/common/frag.glsl");
#[derive(Clone)]
pub struct Shader {
pub program: glow::Program,
pub uniforms: std::rc::Rc<HashMap<String, glow::UniformLocation>>
}
impl Shader {
pub fn new_nolib(ctx: &context::Context, vsrc: &str, fsrc: &str) -> Self {
unsafe {
let program = ctx.gl.create_program()
.expect("cannot create shader program");
let vert = ctx.gl.create_shader(glow::VERTEX_SHADER)
.expect("cannot create shader");
ctx.gl.shader_source(vert, &vsrc);
ctx.gl.compile_shader(vert);
if !ctx.gl.get_shader_compile_status(vert) {
panic!(
"failed to compile vertex shader:\n{}",
ctx.gl.get_shader_info_log(vert)
);
}
ctx.gl.attach_shader(program, vert);
let frag = ctx.gl.create_shader(glow::FRAGMENT_SHADER)
.expect("cannot create shader");
ctx.gl.shader_source(frag, &fsrc);
ctx.gl.compile_shader(frag);
if !ctx.gl.get_shader_compile_status(frag) {
panic!(
"failed to compile fragment shader:\n{}",
ctx.gl.get_shader_info_log(frag)
);
}
ctx.gl.attach_shader(program, frag);
ctx.gl.bind_attrib_location(program, mesh::ATTRIB_VERTEX, "vertex");
ctx.gl.bind_attrib_location(program, mesh::ATTRIB_NORMAL, "normal");
ctx.gl.bind_attrib_location(program, mesh::ATTRIB_TEXCOORD, "texcoord");
ctx.gl.link_program(program);
if !ctx.gl.get_program_link_status(program) {
panic!(
"failed to link shader program:\n{}",
ctx.gl.get_program_info_log(program),
);
}
ctx.gl.detach_shader(program, vert);
ctx.gl.delete_shader(vert);
ctx.gl.detach_shader(program, frag);
ctx.gl.delete_shader(frag);
ctx.gl.use_program(Some(program));
let mut uniforms = HashMap::new();
for index in 0..ctx.gl.get_active_uniforms(program) {
if let Some(active) = ctx.gl.get_active_uniform(program, index) {
if let Some(loc) = ctx.gl.get_uniform_location(program, &active.name) {
uniforms.insert(active.name, loc);
} else {
log::warn!("failed to get location for uniform: {}", active.name);
}
} else {
log::warn!("failed to get active uniform for index: {}", index);
}
}
Self {
program,
uniforms: std::rc::Rc::new(uniforms),
}
}
}
pub fn new(ctx: &context::Context, vsrcstr: &str, fsrcstr: &str) -> Self {
let vsrc = format!("{}\n{}\n", COMMON_VERT, vsrcstr);
let fsrc = format!("{}\n{}\n", COMMON_FRAG, fsrcstr);
Self::new_nolib(ctx, &vsrc, &fsrc)
}
pub fn set_i32(&self, ctx: &context::Context, name: &str, val: i32) {
if let Some(loc) = self.uniforms.get(name) {
unsafe { ctx.gl.uniform_1_i32(Some(loc), val) }
}
}
pub fn set_i32_array(&self, ctx: &context::Context, name: &str, val: &[i32]) {
if let Some(loc) = self.uniforms.get(name) {
unsafe {
ctx.gl.uniform_1_i32_slice(Some(loc), val)
}
}
}
pub fn set_f32(&self, ctx: &context::Context, name: &str, val: f32) {
if let Some(loc) = self.uniforms.get(name) {
unsafe { ctx.gl.uniform_1_f32(Some(loc), val) }
}
}
pub fn set_vec2(&self, ctx: &context::Context, name: &str, val: &glam::Vec2) {
if let Some(loc) = self.uniforms.get(name) {
unsafe {
ctx.gl.uniform_2_f32(
Some(loc),
val.x,
val.y,
);
}
}
}
pub fn set_vec2_array(&self, ctx: &context::Context, name: &str, val: &[glam::Vec2]) {
if let Some(loc) = self.uniforms.get(name) {
let vs: Vec<f32> = val.iter().flat_map(|v| [v.x, v.y]).collect();
unsafe {
ctx.gl.uniform_2_f32_slice(
Some(loc),
&vs,
);
}
}
}
pub fn set_vec3(&self, ctx: &context::Context, name: &str, val: &glam::Vec3) {
if let Some(loc) = self.uniforms.get(name) {
unsafe {
ctx.gl.uniform_3_f32(
Some(loc),
val.x,
val.y,
val.z,
);
}
}
}
pub fn set_vec3_array(&self, ctx: &context::Context, name: &str, val: &[glam::Vec3]) {
if let Some(loc) = self.uniforms.get(name) {
let vs: Vec<f32> = val.iter().flat_map(|v| [v.x, v.y, v.z]).collect();
unsafe {
ctx.gl.uniform_3_f32_slice(
Some(loc),
&vs,
);
}
}
}
pub fn set_vec4(&self, ctx: &context::Context, name: &str, val: &glam::Vec4) {
if let Some(loc) = self.uniforms.get(name) {
unsafe {
ctx.gl.uniform_4_f32(
Some(loc),
val.x,
val.y,
val.z,
val.w,
);
}
}
}
pub fn set_mat4(&self, ctx: &context::Context, name: &str, val: &glam::Mat4) {
if let Some(loc) = self.uniforms.get(name) {
unsafe {
ctx.gl.uniform_matrix_4_f32_slice(
Some(loc),
false,
&val.to_cols_array(),
);
}
}
}
pub fn set_mat4_array(&self, ctx: &context::Context, name: &str, val: &[glam::Mat4]) {
if let Some(loc) = self.uniforms.get(name) {
let vs: Vec<f32> = val.iter().flat_map(|m| m.to_cols_array()).collect();
unsafe {
ctx.gl.uniform_matrix_4_f32_slice(
Some(loc),
false,
&vs,
);
}
}
}
pub fn set_position_3d(&self, ctx: &context::Context, position: &glam::Mat4) {
self.set_mat4(&ctx, "position", &position);
self.set_mat4(&ctx, "normal_matrix", &position.inverse().transpose());
}
pub fn set_position_2d_helper(&self, ctx: &context::Context, pos: &glam::Vec2, dims: &glam::Vec2, rot: &glam::Quat) {
let halfwidth = dims.x / 2.0;
let halfheight = dims.y / 2.0;
self.set_mat4(
&ctx, "position",
&glam::Mat4::from_scale_rotation_translation(
glam::Vec3::new(halfwidth, halfheight, 1.0),
rot.clone(),
glam::Vec3::new(
-context::RENDER_WIDTH / 2.0 + pos.x + halfwidth,
context::RENDER_HEIGHT / 2.0 - pos.y - halfheight,
0.0,
),
)
);
}
pub fn set_position_2d(&self, ctx: &context::Context, pos: &glam::Vec2, dims: &glam::Vec2) {
self.set_position_2d_helper(ctx, pos, dims, &glam::Quat::IDENTITY)
}
pub fn set_texture_offset(&self, ctx: &context::Context, inc: i32, x: i32, y: i32) {
let count = inc as f32;
let ratio = 1.0 / count;
self.set_vec3(
ctx, "texture_offset",
&glam::Vec3::new((x % inc) as f32 * ratio, (y % inc) as f32 * ratio, count)
);
}
pub fn bind(&self, ctx: &context::Context) {
unsafe {
ctx.gl.use_program(Some(self.program));
}
}
}
|