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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
pub use glin::macros::*;
pub use rin_util::WORKSPACE_DIR;

#[macro_export]
macro_rules! program_cache {
    { $vertex:expr, $fragment:expr } => {
        thread_local!(static SHADER: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::Program>> =
                ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn get_program<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> &'static $crate::Program{

            fn new_program<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> $crate::Program{
                use rin_util::LogErr;
                gl.new_program()
                    .from_src_bindings(&[
                        ($crate::VERTEX_SHADER, shaders::VERTEX_SHADER),
                        ($crate::FRAGMENT_SHADER, shaders::FRAGMENT_SHADER)
                    ], $crate::default_attribute_bindings())
                    .log_err("Error loading program")
                    .unwrap()
            }

        	SHADER.with(|cache| {
                let cache = unsafe{ &mut *cache.get() };
                let program = cache
                    .entry(gl.id())
                    .or_insert_with(|| new_program(gl));
                unsafe{ ::std::mem::transmute(program) }
            })
        }

        mod shaders{
            pub static VERTEX_SHADER: &'static str = include_str!($vertex);
            pub static FRAGMENT_SHADER: &'static str = include_str!($fragment);
        }
    };

    { $vertex:expr, $fragment:expr, $getter:ident, $shader:ident  } => {
        thread_local!(static $shader: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::Program>> =
                ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn $getter<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> &'static $crate::Program{
            static VERTEX_SHADER: &'static str = include_str!($vertex);
            static FRAGMENT_SHADER: &'static str = include_str!($fragment);

            fn new_program<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> $crate::Program{
                use rin_util::LogErr;
                gl.new_program()
                    .from_src_bindings(&[
                        ($crate::VERTEX_SHADER, VERTEX_SHADER),
                        ($crate::FRAGMENT_SHADER, FRAGMENT_SHADER)
                    ], $crate::default_attribute_bindings())
                    .log_err("Error loading program")
                    .unwrap()
            }

        	$shader.with(|cache| {
                let cache = unsafe{ &mut *cache.get() };
                let program = cache
                    .entry(gl.id())
                    .or_insert_with(|| new_program(gl));
                unsafe{ ::std::mem::transmute(program) }
            })
        }
    };


    { $settings:expr, $getter:ident, $shader:ident  } => {
        thread_local!(static $shader: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::Program>> =
                ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn $getter<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> &'static $crate::Program{
            fn new_program<'c, R: $crate::RenderSurface>(gl: &$crate::Renderer<'c, R>) -> $crate::Program{
                use rin_util::LogErr;
                gl.new_program()
                    .from_settings($settings)
                    .log_err("Error loading program")
                    .unwrap()
            }

        	$shader.with(|cache| {
                let cache = unsafe{ &mut *cache.get() };
                let program = cache
                    .entry(gl.id())
                    .or_insert_with(|| new_program(gl));
                unsafe{ ::std::mem::transmute(program) }
            })
        }
    }
}

#[macro_export]
macro_rules! geometry_cache{
    {$ty: ty, $mesh: expr} => {
        thread_local!(static GEOMETRY: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::SimpleVao<$ty>>> =
            ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn get_geometry<'r, 'c, R: $crate::RenderSurface>(gl: &'r $crate::Renderer<'c, R>) -> &'r mut $crate::SimpleVao<$ty> {
            GEOMETRY.with(|cache|{
                let cache = unsafe{ &mut *cache.get() };
                let vao = cache.entry(gl.id())
                    .or_insert_with(|| {
                        gl.new_simple_vao().from_data_bindings(
                            &$mesh,
                            $crate::default_attribute_bindings(),
                            $crate::gl::STATIC_DRAW).unwrap()
                    });
                unsafe{ ::std::mem::transmute(vao) }
            })
        }
    }
}

#[macro_export]
macro_rules! geometry_cache_per_vertex_type {
    {} => {
        thread_local!(static GEOMETRY: ::std::cell::UnsafeCell<::std::collections::HashMap<(usize, ::std::any::TypeId), Box<::std::any::Any>>> =
            ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn get_geometry<'r, 'c, T: VertexFormat, R: $crate::offscreen_buffer::RenderSurface>(gl: &'r $crate::Renderer<'c, R>, mesh: ::graphics::Mesh<T>) -> &'r mut ::glin::SimpleVao<T> {
            GEOMETRY.with(|cache|{
                let cache = unsafe{ &mut *cache.get() };
                let vao = cache.entry((gl.id(), ::std::any::TypeId::of::<T>()))
                    .or_insert_with(|| {
                        gl.new_simple_vao().from_data_bindings(
                            &$mesh,
                            &$crate::default_attribute_bindings(),
                            $crate::STATIC_DRAW).unwrap()
                    });
                unsafe{ ::std::mem::transmute(vao) }
            })
        }
    }
}


#[macro_export]
macro_rules! image_cache{
    {$getter:ident, $cache_name: ident, $image: expr} => {
        thread_local!(static $cache_name: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::Texture>> =
            ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn $getter<'r, 'c, R: $crate::RenderSurface>(gl: &'r $crate::Renderer<'c, R>) -> &'r mut $crate::Texture {
            $cache_name.with(|cache|{
                use rin_util::LogErr;
                let cache = unsafe{ &mut *cache.get() };
                let tex = cache.entry(gl.id())
                    .or_insert_with(|| {
                        gl.new_texture()
                            .from_image(&$image, $crate::TEXTURE_2D)
                            .log_err("Error loading image into texture")
                            .unwrap()
                    });
                unsafe{ ::std::mem::transmute(tex) }
            })
        }
    };


    {$getter:ident, $cache_name: ident, $image: expr, $tex_init_fn: expr} => {
        thread_local!(static $cache_name: ::std::cell::UnsafeCell<::std::collections::HashMap<usize, $crate::Texture>> =
            ::std::cell::UnsafeCell::new(::std::collections::HashMap::new()));

        pub fn $getter<'r, 'c, R: $crate::RenderSurface>(gl: &'r $crate::Renderer<'c, R>) -> &'r mut $crate::Texture {
            $cache_name.with(|cache|{
                let cache = unsafe{ &mut *cache.get() };
                let tex = cache.entry(gl.id())
                    .or_insert_with(|| {
                        let mut tex = gl.new_texture().from_image(&$image, $crate::TEXTURE_2D).unwrap();
                        $tex_init_fn(&mut tex);
                        tex
                    });
                unsafe{ ::std::mem::transmute(tex) }
            })
        }
    };
}

#[macro_export]
#[cfg(feature="glsl_debug")]
macro_rules! shader {
    ($ty: expr, $path: expr, $replacements: expr) => {{
        let path = ::std::path::Path::new($path);
        let base_path = if path.is_absolute(){
            path.parent().map(|p| p.to_owned())
        }else{
            let src_path = ::std::path::Path::new(file!());
            if src_path.is_absolute(){
                src_path.parent().map(|p| p.to_owned())
            }else{
                $crate::macros::WORKSPACE_DIR
                    .join(std::path::Path::new(file!()))
                    .parent()
                    .map(|p| p.to_owned())
            }
        };
        $crate::Shader::Path{
            path: path.to_owned(),
            replacements: $replacements,
            base_path,
            includes: Vec::new(),
            ty: $ty,
        }
    }};

    ($ty: expr, $path: expr) => {{
        $crate::shader!($ty, $path, vec![])
    }};
}

#[macro_export]
#[cfg(not(feature="glsl_debug"))]
macro_rules! shader {
    ($ty: expr, $path: expr, $replacements: expr) => {{
        let path = ::std::path::Path::new($path);
        let base_path;
        let absolute_path;
        if path.is_absolute(){
            base_path = path.parent().map(|p| p.to_owned());
            absolute_path = path.to_owned();
        }else{
            base_path = $crate::macros::WORKSPACE_DIR
                .join(std::path::Path::new(file!()))
                .parent()
                .map(|p| p.to_owned());
            absolute_path = base_path
                .as_ref()
                .and_then(|b| b.join(path).canonicalize().ok())
                .unwrap_or(path.to_owned());
        };
        $crate::Shader::Source{
            include_name: absolute_path.to_str().unwrap().to_owned(),
            source: include_str!($path).to_owned(),
            replacements: $replacements,
            base_path,
            includes: Vec::new(),
            ty: $ty,
        }
     }};

    ($ty: expr, $path: expr) => {{
        $crate::shader!($ty, $path, vec![])
    }};
}

#[macro_export]
macro_rules! vertex_shader {
    ($path: expr, $replacements: expr) => {
        $crate::shader!($crate::VERTEX_SHADER, $path, $replacements)
    };
    ($path: expr) => {
        $crate::shader!($crate::VERTEX_SHADER, $path)
    };
}

#[macro_export]
macro_rules! fragment_shader {
    ($path: expr, $replacements: expr) => {
        $crate::shader!($crate::FRAGMENT_SHADER, $path, $replacements)
    };
    ($path: expr) => {
        $crate::shader!($crate::FRAGMENT_SHADER, $path)
    };
}

#[macro_export]
macro_rules! shader_source {
    ($ty: expr, $source: expr) => {{
        let include_name = match $ty{
            gl::VERTEX_SHADER => format!("vertex shader inlined in {}:{}:{}", file!(), line!(), column!()),
            gl::FRAGMENT_SHADER => format!("fragment_shader inlined in {}:{}:{}", file!(), line!(), column!()),
            _ => panic!("Shader type not supported yet"),
        };
        $crate::renderer::Shader::Source{
            include_name,
            source: $source.to_owned(),
            replacements: vec![],
            base_path: None,
            includes: Vec::new(),
            ty: $ty,
        }
    }};
}

#[macro_export]
macro_rules! vertex_shader_source {
    ($source: expr) => {
        $crate::shader_source!(crate::VERTEX_SHADER, $source)
    };
}

#[macro_export]
macro_rules! fragment_shader_source {
    ($source: expr) => {
        $crate::shader_source!(crate::FRAGMENT_SHADER, $source)
    };
}