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
use crate::prelude::Texture2D;
use crate::quad_gl::GlPipeline;
use crate::{get_context, get_quad_context};
use miniquad::{PipelineParams, ShaderError, UniformType};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Material {
pipeline: GlPipeline,
}
impl Material {
pub fn set_uniform<T>(&self, name: &str, uniform: T) {
get_context().gl.set_uniform(self.pipeline, name, uniform);
}
pub fn set_texture(&self, name: &str, texture: Texture2D) {
get_context().gl.set_texture(self.pipeline, name, texture);
}
pub fn delete(&mut self) {
get_context().gl.delete_pipeline(self.pipeline);
}
}
pub struct MaterialParams {
pub pipeline_params: PipelineParams,
pub uniforms: Vec<(String, UniformType)>,
pub textures: Vec<String>,
}
impl Default for MaterialParams {
fn default() -> Self {
MaterialParams {
pipeline_params: Default::default(),
uniforms: vec![],
textures: vec![],
}
}
}
pub fn load_material(
vertex_shader: &str,
fragment_shader: &str,
params: MaterialParams,
) -> Result<Material, ShaderError> {
let context = &mut get_context();
let pipeline = context.gl.make_pipeline(
get_quad_context(),
vertex_shader,
fragment_shader,
params.pipeline_params,
params.uniforms,
params.textures,
)?;
Ok(Material { pipeline })
}
pub fn gl_use_material(material: Material) {
get_context().gl.pipeline(Some(material.pipeline));
}
pub fn gl_use_default_material() {
get_context().gl.pipeline(None);
}
#[doc(hidden)]
pub mod shaders {
type IncludeFilename = String;
type IncludeContent = String;
#[derive(Debug, Clone)]
pub struct PreprocessorConfig {
pub includes: Vec<(IncludeFilename, IncludeContent)>,
}
impl Default for PreprocessorConfig {
fn default() -> PreprocessorConfig {
PreprocessorConfig { includes: vec![] }
}
}
impl PreprocessorConfig {}
pub fn preprocess_shader(source: &str, config: &PreprocessorConfig) -> String {
let mut res = source.chars().collect::<Vec<_>>();
fn find(data: &[char], n: &mut usize, target: &str) -> bool {
if *n >= data.len() {
return false;
}
let target = target.chars().collect::<Vec<_>>();
'outer: for i in *n..data.len() {
for j in 0..target.len() {
if data[i + j] != target[j] {
*n += 1;
continue 'outer;
}
}
return true;
}
false
}
fn skip_character(data: &[char], n: &mut usize, target: char) {
while *n < data.len() && data[*n] == target {
*n += 1;
}
}
let mut i = 0;
while find(&res, &mut i, "#include") {
let directive_start_ix = i;
i += "#include".len();
skip_character(&res, &mut i, ' ');
assert!(res[i] == '\"');
i += 1;
let filename_start_ix = i;
find(&res, &mut i, "\"");
let filename_end_ix = i;
let filename = res[filename_start_ix..filename_end_ix]
.iter()
.cloned()
.collect::<String>();
let include_content = config
.includes
.iter()
.find(|(name, _)| name == &filename)
.expect(&format!(
"Include file {} in not on \"includes\" list",
filename
));
let _ = res
.splice(
directive_start_ix..filename_end_ix + 1,
include_content.1.chars(),
)
.collect::<Vec<_>>();
}
res.into_iter().collect()
}
#[test]
fn preprocessor_test() {
let shader_string = r#"
#version blah blah
asd
asd
#include "hello.glsl"
qwe
"#;
let preprocessed = r#"
#version blah blah
asd
asd
iii
jjj
qwe
"#;
let result = preprocess_shader(
shader_string,
&PreprocessorConfig {
includes: vec![("hello.glsl".to_string(), "iii\njjj".to_string())],
..Default::default()
},
);
assert_eq!(result, preprocessed);
}
}