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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use super::vertex::AttributeType;
use crate::{GraphicsError, ShaderKey};

#[derive(Clone, Debug)]
pub struct Attribute {
    pub name: String,
    pub size: i32,
    pub atype: AttributeType,
    pub location: u32,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UniformLocation(pub(crate) super::GLUniformLocation);

#[derive(Clone, Debug)]
pub struct Uniform {
    pub name: String,
    pub size: i32,
    pub utype: u32,
    pub location: UniformLocation,
    pub initial_data: RawUniformValue,
}

#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub enum RawUniformValue {
    SignedInt(i32),
    //    UnsignedInt(u32),
    Float(f32),
    Mat2(mint::ColumnMatrix2<f32>),
    Mat3(mint::ColumnMatrix3<f32>),
    Mat4(mint::ColumnMatrix4<f32>),
    Vec2(mint::Vector2<f32>),
    Vec3(mint::Vector3<f32>),
    Vec4(mint::Vector4<f32>),
    IntVec2(mint::Vector2<i32>),
    IntVec3(mint::Vector3<i32>),
    IntVec4(mint::Vector4<i32>),
    //    UnsignedIntVec2([u32; 2]),
    //    UnsignedIntVec3([u32; 3]),
    //    UnsignedIntVec4([u32; 4]),
}

macro_rules! raw_uniform_conv {
    ($from:ty, $to:ident) => {
        impl From<$from> for RawUniformValue {
            fn from(v: $from) -> Self {
                RawUniformValue::$to(v)
            }
        }

        impl std::convert::TryInto<$from> for RawUniformValue {
            type Error = &'static str;

            fn try_into(self) -> Result<$from, Self::Error> {
                match self {
                    RawUniformValue::$to(v) => Ok(v),
                    _ => Err("RawUniformValue::$to cannot be converted to $from."),
                }
            }
        }
    };
}

raw_uniform_conv!(i32, SignedInt);
raw_uniform_conv!(f32, Float);
raw_uniform_conv!(mint::ColumnMatrix2<f32>, Mat2);
raw_uniform_conv!(mint::ColumnMatrix3<f32>, Mat3);
raw_uniform_conv!(mint::ColumnMatrix4<f32>, Mat4);
raw_uniform_conv!(mint::Vector2<f32>, Vec2);
raw_uniform_conv!(mint::Vector3<f32>, Vec3);
raw_uniform_conv!(mint::Vector4<f32>, Vec4);
raw_uniform_conv!(mint::Vector2<i32>, IntVec2);
raw_uniform_conv!(mint::Vector3<i32>, IntVec3);
raw_uniform_conv!(mint::Vector4<i32>, IntVec4);

#[derive(Debug)]
pub enum ShaderError {
    VertexCompileError(String),
    FragmentCompileError(String),
    LinkError(String),
    ResourceCreationError,
}

impl std::fmt::Display for ShaderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for ShaderError {}

#[derive(Clone, Debug)]
pub struct DynamicShader {
    inner: super::ShaderKey,
    attributes: Vec<Attribute>,
    uniforms: Vec<Uniform>,
}

impl std::cmp::PartialEq for DynamicShader {
    fn eq(&self, other: &Self) -> bool {
        self.inner.eq(&other.inner)
    }
}

impl DynamicShader {
    pub fn new(
        gl: &mut super::Context,
        vertex_source: &str,
        fragment_source: &str,
    ) -> Result<Self, GraphicsError> {
        let inner = gl
            .new_shader(vertex_source, fragment_source)
            .map_err(GraphicsError::ShaderError)?;
        let attributes = gl.get_shader_attributes(inner);
        let uniforms = gl.get_shader_uniforms(inner);

        Ok(Self {
            inner,
            attributes,
            uniforms,
        })
    }

    pub fn handle(&self) -> super::ShaderKey {
        self.inner
    }

    pub fn get_attribute_by_name(&self, name: &str) -> Option<&Attribute> {
        self.attributes
            .iter()
            .find(|attribute| attribute.name == name)
    }

    pub fn get_uniform_by_name(&self, name: &str) -> Option<&Uniform> {
        self.uniforms.iter().find(|uniform| uniform.name == name)
    }

    pub fn create_source(vertex: &str, fragment: &str) -> (String, String) {
        let vertex = format!(
            "{}\n{}\n{}\n{}\n{}\n{}",
            GLSL_VERSION, SYNTAX, VERTEX_HEADER, FUNCTIONS, LINE_PRAGMA, vertex
        );
        let fragment = format!(
            "{}\n{}\n{}\n{}\n{}\n{}",
            GLSL_VERSION, SYNTAX, FRAG_HEADER, FUNCTIONS, LINE_PRAGMA, fragment
        );
        (vertex, fragment)
    }
}

impl Shader for DynamicShader {
    fn handle(&self) -> ShaderKey {
        self.inner
    }

    fn attributes(&self) -> &[Attribute] {
        &self.attributes
    }

    fn uniforms(&self) -> &[Uniform] {
        &self.uniforms
    }
}

#[cfg(target_arch = "wasm32")]
const GLSL_VERSION: &str = "#version 100";

#[cfg(not(target_arch = "wasm32"))]
const GLSL_VERSION: &str = "#version 330 core";

#[cfg(target_arch = "wasm32")]
const LINE_PRAGMA: &str = "#line 1";

#[cfg(not(target_arch = "wasm32"))]
const LINE_PRAGMA: &str = "#line 1";

const SYNTAX: &str = r#"
#if !defined(GL_ES) && __VERSION__ < 140
    #define lowp
    #define mediump
    #define highp
#endif

#if defined(VERTEX) || __VERSION__ > 100 || defined(GL_FRAGMENT_PRECISION_HIGH)
	#define SOLSTICE_HIGHP_OR_MEDIUMP highp
#else
	#define SOLSTICE_HIGHP_OR_MEDIUMP mediump
#endif

#define extern uniform
#ifdef GL_EXT_texture_array
    #extension GL_EXT_texture_array : enable
#endif
#ifdef GL_OES_texture_3D
    #extension GL_OES_texture_3D : enable
#endif
#ifdef GL_OES_standard_derivatives
    #extension GL_OES_standard_derivatives : enable
#endif"#;

const FUNCTIONS: &str = r#"
#ifdef GL_ES
	#if __VERSION__ >= 300 || defined(GL_EXT_texture_array)
		precision lowp sampler2DArray;
	#endif
	#if __VERSION__ >= 300 || defined(GL_OES_texture_3D)
		precision lowp sampler3D;
	#endif
	#if __VERSION__ >= 300
		precision lowp sampler2DShadow;
		precision lowp samplerCubeShadow;
		precision lowp sampler2DArrayShadow;
	#endif
#endif

#if __VERSION__ >= 130
    #define texture2D Texel
    #define texture3D Texel
    #define textureCube Texel
    #define texture2DArray Texel
    #define solstice_texture2D texture
    #define solstice_texture3D texture
    #define solstice_textureCube texture
    #define solstice_texture2DArray texture
#else
    #define solstice_texture2D texture2D
    #define solstice_texture3D texture3D
    #define solstice_textureCube textureCube
    #define solstice_texture2DArray texture2DArray
#endif
vec4 Texel(sampler2D s, vec2 c) { return solstice_texture2D(s, c); }
vec4 Texel(samplerCube s, vec3 c) { return solstice_textureCube(s, c); }
#if __VERSION__ > 100 || defined(GL_OES_texture_3D)
    vec4 Texel(sampler3D s, vec3 c) { return solstice_texture3D(s, c); }
#endif
#if __VERSION__ >= 130 || defined(GL_EXT_texture_array)
    vec4 Texel(sampler2DArray s, vec3 c) { return solstice_texture2DArray(s, c); }
#endif
#ifdef PIXEL
    vec4 Texel(sampler2D s, vec2 c, float b) { return solstice_texture2D(s, c, b); }
    vec4 Texel(samplerCube s, vec3 c, float b) { return solstice_textureCube(s, c, b); }
    #if __VERSION__ > 100 || defined(GL_OES_texture_3D)
        vec4 Texel(sampler3D s, vec3 c, float b) { return solstice_texture3D(s, c, b); }
    #endif
    #if __VERSION__ >= 130 || defined(GL_EXT_texture_array)
        vec4 Texel(sampler2DArray s, vec3 c, float b) { return solstice_texture2DArray(s, c, b); }
    #endif
#endif
#define texture solstice_texture"#;

const VERTEX_HEADER: &str = r#"
#define VERTEX

#if __VERSION__ >= 130
	#define attribute in
	#define varying out
#endif"#;

const FRAG_HEADER: &str = r#"
#define FRAGMENT

#ifdef GL_ES
    precision mediump float;
#endif

#if __VERSION__ >= 130
    #define varying in
    layout(location = 0) out vec4 fragColor;
#else
    #define fragColor gl_FragColor
#endif"#;

pub trait UniformTrait {
    type Value;
    const NAME: &'static str;

    fn get_location(&self) -> Option<&UniformLocation>;
    fn update(&self, ctx: &mut super::Context, v: Self::Value)
    where
        Self::Value: std::convert::TryInto<RawUniformValue>,
    {
        use std::convert::TryInto;
        if let Some(location) = self.get_location() {
            if let Ok(value) = v.try_into() {
                ctx.set_uniform_by_location(location, &value)
            }
        }
    }
}

pub trait CachedUniformTrait: UniformTrait {
    fn get_cache(&mut self) -> &mut Self::Value;
}

pub trait Shader {
    fn handle(&self) -> super::ShaderKey;
    fn attributes(&self) -> &[Attribute];
    fn uniforms(&self) -> &[Uniform];
}

pub trait UniformGetter<U: UniformTrait> {
    fn get_uniform(&self) -> &U;
}

pub trait UniformGetterMut<U: UniformTrait>: UniformGetter<U> {
    fn get_uniform_mut(&mut self) -> &mut U;
}

pub trait BasicUniformSetter {
    fn set_uniform<U>(&mut self, gl: &mut super::Context, value: <U as UniformTrait>::Value)
    where
        Self: UniformGetter<U>,
        U: UniformTrait,
        <U as UniformTrait>::Value: Into<RawUniformValue>,
    {
        let uniform = self.get_uniform();
        if let Some(location) = uniform.get_location() {
            gl.set_uniform_by_location(location, &value.into());
        }
    }

    fn bind_texture<U, T>(
        &mut self,
        gl: &mut super::Context,
        texture: T,
        texture_unit: <U as UniformTrait>::Value,
    ) where
        Self: UniformGetter<U>,
        U: UniformTrait,
        <U as UniformTrait>::Value: Copy + Into<super::TextureUnit> + Into<RawUniformValue>,
        T: super::texture::Texture,
    {
        let uniform = self.get_uniform();
        if uniform.get_location().is_some() {
            gl.bind_texture_to_unit(
                texture.get_texture_type(),
                texture.get_texture_key(),
                texture_unit.into(),
            );
            self.set_uniform(gl, texture_unit);
        }
    }
}

pub trait CachedUniformSetter: BasicUniformSetter {
    fn set_uniform_cached<U>(&mut self, gl: &mut super::Context, value: <U as UniformTrait>::Value)
    where
        Self: UniformGetterMut<U>,
        U: CachedUniformTrait,
        <U as UniformTrait>::Value: Into<RawUniformValue> + PartialEq + Copy,
    {
        if value.ne(self.get_uniform_mut().get_cache()) {
            let uniform = self.get_uniform();
            if let Some(location) = uniform.get_location() {
                gl.set_uniform_by_location(location, &value.into());
            }
            *self.get_uniform_mut().get_cache() = value;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::TryInto;

    #[test]
    fn uniform_conv() {
        let a = mint::Vector2 { x: 1., y: 2. };
        let b: RawUniformValue = a.into();
        let c: mint::Vector2<f32> = b.try_into().unwrap();
        assert_eq!(a, c);
    }
}